summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbeck <>2014-04-16 20:36:35 +0000
committerbeck <>2014-04-16 20:36:35 +0000
commit750d86a4fc04f53024575d65269281ea6c4e450c (patch)
tree4a8d2bd6f2dd786d658a75ea2db858806f2ec5f4
parentbe77aa550ef0450b00eb62880d4d98112ba86e50 (diff)
downloadopenbsd-750d86a4fc04f53024575d65269281ea6c4e450c.tar.gz
openbsd-750d86a4fc04f53024575d65269281ea6c4e450c.tar.bz2
openbsd-750d86a4fc04f53024575d65269281ea6c4e450c.zip
Clean up dangerous strncpy use. This included a use where the resulting
string was potentially not nul terminated and a place where malloc return was unchecked. while we're at it remove dummytest.c ok miod@
-rw-r--r--src/lib/libcrypto/bio/bss_log.c4
-rw-r--r--src/lib/libcrypto/err/err.c3
-rw-r--r--src/lib/libcrypto/evp/evp_key.c3
-rw-r--r--src/lib/libcrypto/ts/ts_rsp_verify.c14
-rw-r--r--src/lib/libcrypto/x509/by_dir.c4
-rw-r--r--src/lib/libcrypto/x509/x509_obj.c3
-rw-r--r--src/lib/libcrypto/x509v3/v3_alt.c10
-rw-r--r--src/lib/libcrypto/x509v3/v3_info.c3
-rw-r--r--src/lib/libssl/src/crypto/bio/bss_log.c4
-rw-r--r--src/lib/libssl/src/crypto/err/err.c3
-rw-r--r--src/lib/libssl/src/crypto/evp/evp_key.c3
-rw-r--r--src/lib/libssl/src/crypto/ts/ts_rsp_verify.c14
-rw-r--r--src/lib/libssl/src/crypto/x509/by_dir.c4
-rw-r--r--src/lib/libssl/src/crypto/x509/x509_obj.c3
-rw-r--r--src/lib/libssl/src/crypto/x509v3/v3_alt.c10
-rw-r--r--src/lib/libssl/src/crypto/x509v3/v3_info.c3
-rw-r--r--src/lib/libssl/src/test/dummytest.c48
-rw-r--r--src/lib/libssl/test/dummytest.c48
18 files changed, 34 insertions, 150 deletions
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c
index 5a79f72673..2d38837f9e 100644
--- a/src/lib/libcrypto/bio/bss_log.c
+++ b/src/lib/libcrypto/bio/bss_log.c
@@ -160,9 +160,7 @@ slg_write(BIO *b, const char *in, int inl)
160 if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) { 160 if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) {
161 return (0); 161 return (0);
162 } 162 }
163 strncpy(buf, in, inl); 163 strlcpy(buf, in, inl + 1);
164 buf[inl] = '\0';
165
166 i = 0; 164 i = 0;
167 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0) 165 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
168 i++; 166 i++;
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c
index ae9a209ad7..f6f9d2c080 100644
--- a/src/lib/libcrypto/err/err.c
+++ b/src/lib/libcrypto/err/err.c
@@ -603,8 +603,7 @@ static void build_SYS_str_reasons(void)
603 char *src = strerror(i); 603 char *src = strerror(i);
604 if (src != NULL) 604 if (src != NULL)
605 { 605 {
606 strncpy(*dest, src, sizeof *dest); 606 strlcpy(*dest, src, sizeof *dest);
607 (*dest)[sizeof *dest - 1] = '\0';
608 str->string = *dest; 607 str->string = *dest;
609 } 608 }
610 } 609 }
diff --git a/src/lib/libcrypto/evp/evp_key.c b/src/lib/libcrypto/evp/evp_key.c
index 7961fbebf2..b3cb0638fa 100644
--- a/src/lib/libcrypto/evp/evp_key.c
+++ b/src/lib/libcrypto/evp/evp_key.c
@@ -72,8 +72,7 @@ void EVP_set_pw_prompt(const char *prompt)
72 prompt_string[0]='\0'; 72 prompt_string[0]='\0';
73 else 73 else
74 { 74 {
75 strncpy(prompt_string,prompt,79); 75 strlcpy(prompt_string,prompt,sizeof(prompt_string));
76 prompt_string[79]='\0';
77 } 76 }
78 } 77 }
79 78
diff --git a/src/lib/libcrypto/ts/ts_rsp_verify.c b/src/lib/libcrypto/ts/ts_rsp_verify.c
index a003207428..f241230ef4 100644
--- a/src/lib/libcrypto/ts/ts_rsp_verify.c
+++ b/src/lib/libcrypto/ts/ts_rsp_verify.c
@@ -538,7 +538,6 @@ static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
538 int i; 538 int i;
539 unsigned int length = 0; 539 unsigned int length = 0;
540 char *result = NULL; 540 char *result = NULL;
541 char *p;
542 541
543 /* Determine length first. */ 542 /* Determine length first. */
544 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) 543 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i)
@@ -554,17 +553,14 @@ static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
554 return NULL; 553 return NULL;
555 } 554 }
556 /* Concatenate the descriptions. */ 555 /* Concatenate the descriptions. */
557 for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) 556 result[0] = '\0';
557 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i)
558 { 558 {
559 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i); 559 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
560 length = ASN1_STRING_length(current); 560 if (i > 0)
561 if (i > 0) *p++ = '/'; 561 strlcat(result, "/", length);
562 strncpy(p, (const char *)ASN1_STRING_data(current), length); 562 strlcat(result, ASN1_STRING_data(current), length);
563 p += length;
564 } 563 }
565 /* We do have space for this, too. */
566 *p = '\0';
567
568 return result; 564 return result;
569 } 565 }
570 566
diff --git a/src/lib/libcrypto/x509/by_dir.c b/src/lib/libcrypto/x509/by_dir.c
index b5512895a1..ccf2f6e0bf 100644
--- a/src/lib/libcrypto/x509/by_dir.c
+++ b/src/lib/libcrypto/x509/by_dir.c
@@ -246,13 +246,11 @@ add_cert_dir(BY_DIR *ctx, const char *dir, int type)
246 return 0; 246 return 0;
247 ent->dir_type = type; 247 ent->dir_type = type;
248 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); 248 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
249 ent->dir = OPENSSL_malloc((unsigned int)len + 1); 249 ent->dir = strdup(ss);
250 if (!ent->dir || !ent->hashes) { 250 if (!ent->dir || !ent->hashes) {
251 by_dir_entry_free(ent); 251 by_dir_entry_free(ent);
252 return 0; 252 return 0;
253 } 253 }
254 strncpy(ent->dir, ss,(unsigned int)len);
255 ent->dir[len] = '\0';
256 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { 254 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
257 by_dir_entry_free(ent); 255 by_dir_entry_free(ent);
258 return 0; 256 return 0;
diff --git a/src/lib/libcrypto/x509/x509_obj.c b/src/lib/libcrypto/x509/x509_obj.c
index bcc1e7429e..1d3cf547d7 100644
--- a/src/lib/libcrypto/x509/x509_obj.c
+++ b/src/lib/libcrypto/x509/x509_obj.c
@@ -90,8 +90,7 @@ int i;
90 buf=b->data; 90 buf=b->data;
91 OPENSSL_free(b); 91 OPENSSL_free(b);
92 } 92 }
93 strncpy(buf,"NO X509_NAME",len); 93 strlcpy(buf,"NO X509_NAME",len);
94 buf[len-1]='\0';
95 return buf; 94 return buf;
96 } 95 }
97 96
diff --git a/src/lib/libcrypto/x509v3/v3_alt.c b/src/lib/libcrypto/x509v3/v3_alt.c
index 66ea96db51..8de5dd041b 100644
--- a/src/lib/libcrypto/x509v3/v3_alt.c
+++ b/src/lib/libcrypto/x509v3/v3_alt.c
@@ -579,10 +579,12 @@ static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
579 return 0; 579 return 0;
580 objlen = p - value; 580 objlen = p - value;
581 objtmp = OPENSSL_malloc(objlen + 1); 581 objtmp = OPENSSL_malloc(objlen + 1);
582 strncpy(objtmp, value, objlen); 582 if (objtmp) {
583 objtmp[objlen] = 0; 583 strlcpy(objtmp, value, objlen + 1);
584 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); 584 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
585 OPENSSL_free(objtmp); 585 OPENSSL_free(objtmp);
586 } else
587 gen->d.otherName->type_id = NULL;
586 if (!gen->d.otherName->type_id) 588 if (!gen->d.otherName->type_id)
587 return 0; 589 return 0;
588 return 1; 590 return 1;
diff --git a/src/lib/libcrypto/x509v3/v3_info.c b/src/lib/libcrypto/x509v3/v3_info.c
index e1b8699f92..44bc3e1105 100644
--- a/src/lib/libcrypto/x509v3/v3_info.c
+++ b/src/lib/libcrypto/x509v3/v3_info.c
@@ -165,8 +165,7 @@ static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *metho
165 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,ERR_R_MALLOC_FAILURE); 165 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,ERR_R_MALLOC_FAILURE);
166 goto err; 166 goto err;
167 } 167 }
168 strncpy(objtmp, cnf->name, objlen); 168 strlcpy(objtmp, cnf->name, objlen + 1);
169 objtmp[objlen] = 0;
170 acc->method = OBJ_txt2obj(objtmp, 0); 169 acc->method = OBJ_txt2obj(objtmp, 0);
171 if(!acc->method) { 170 if(!acc->method) {
172 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,X509V3_R_BAD_OBJECT); 171 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,X509V3_R_BAD_OBJECT);
diff --git a/src/lib/libssl/src/crypto/bio/bss_log.c b/src/lib/libssl/src/crypto/bio/bss_log.c
index 5a79f72673..2d38837f9e 100644
--- a/src/lib/libssl/src/crypto/bio/bss_log.c
+++ b/src/lib/libssl/src/crypto/bio/bss_log.c
@@ -160,9 +160,7 @@ slg_write(BIO *b, const char *in, int inl)
160 if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) { 160 if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) {
161 return (0); 161 return (0);
162 } 162 }
163 strncpy(buf, in, inl); 163 strlcpy(buf, in, inl + 1);
164 buf[inl] = '\0';
165
166 i = 0; 164 i = 0;
167 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0) 165 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
168 i++; 166 i++;
diff --git a/src/lib/libssl/src/crypto/err/err.c b/src/lib/libssl/src/crypto/err/err.c
index ae9a209ad7..f6f9d2c080 100644
--- a/src/lib/libssl/src/crypto/err/err.c
+++ b/src/lib/libssl/src/crypto/err/err.c
@@ -603,8 +603,7 @@ static void build_SYS_str_reasons(void)
603 char *src = strerror(i); 603 char *src = strerror(i);
604 if (src != NULL) 604 if (src != NULL)
605 { 605 {
606 strncpy(*dest, src, sizeof *dest); 606 strlcpy(*dest, src, sizeof *dest);
607 (*dest)[sizeof *dest - 1] = '\0';
608 str->string = *dest; 607 str->string = *dest;
609 } 608 }
610 } 609 }
diff --git a/src/lib/libssl/src/crypto/evp/evp_key.c b/src/lib/libssl/src/crypto/evp/evp_key.c
index 7961fbebf2..b3cb0638fa 100644
--- a/src/lib/libssl/src/crypto/evp/evp_key.c
+++ b/src/lib/libssl/src/crypto/evp/evp_key.c
@@ -72,8 +72,7 @@ void EVP_set_pw_prompt(const char *prompt)
72 prompt_string[0]='\0'; 72 prompt_string[0]='\0';
73 else 73 else
74 { 74 {
75 strncpy(prompt_string,prompt,79); 75 strlcpy(prompt_string,prompt,sizeof(prompt_string));
76 prompt_string[79]='\0';
77 } 76 }
78 } 77 }
79 78
diff --git a/src/lib/libssl/src/crypto/ts/ts_rsp_verify.c b/src/lib/libssl/src/crypto/ts/ts_rsp_verify.c
index a003207428..f241230ef4 100644
--- a/src/lib/libssl/src/crypto/ts/ts_rsp_verify.c
+++ b/src/lib/libssl/src/crypto/ts/ts_rsp_verify.c
@@ -538,7 +538,6 @@ static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
538 int i; 538 int i;
539 unsigned int length = 0; 539 unsigned int length = 0;
540 char *result = NULL; 540 char *result = NULL;
541 char *p;
542 541
543 /* Determine length first. */ 542 /* Determine length first. */
544 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) 543 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i)
@@ -554,17 +553,14 @@ static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
554 return NULL; 553 return NULL;
555 } 554 }
556 /* Concatenate the descriptions. */ 555 /* Concatenate the descriptions. */
557 for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) 556 result[0] = '\0';
557 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i)
558 { 558 {
559 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i); 559 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
560 length = ASN1_STRING_length(current); 560 if (i > 0)
561 if (i > 0) *p++ = '/'; 561 strlcat(result, "/", length);
562 strncpy(p, (const char *)ASN1_STRING_data(current), length); 562 strlcat(result, ASN1_STRING_data(current), length);
563 p += length;
564 } 563 }
565 /* We do have space for this, too. */
566 *p = '\0';
567
568 return result; 564 return result;
569 } 565 }
570 566
diff --git a/src/lib/libssl/src/crypto/x509/by_dir.c b/src/lib/libssl/src/crypto/x509/by_dir.c
index b5512895a1..ccf2f6e0bf 100644
--- a/src/lib/libssl/src/crypto/x509/by_dir.c
+++ b/src/lib/libssl/src/crypto/x509/by_dir.c
@@ -246,13 +246,11 @@ add_cert_dir(BY_DIR *ctx, const char *dir, int type)
246 return 0; 246 return 0;
247 ent->dir_type = type; 247 ent->dir_type = type;
248 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); 248 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
249 ent->dir = OPENSSL_malloc((unsigned int)len + 1); 249 ent->dir = strdup(ss);
250 if (!ent->dir || !ent->hashes) { 250 if (!ent->dir || !ent->hashes) {
251 by_dir_entry_free(ent); 251 by_dir_entry_free(ent);
252 return 0; 252 return 0;
253 } 253 }
254 strncpy(ent->dir, ss,(unsigned int)len);
255 ent->dir[len] = '\0';
256 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { 254 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
257 by_dir_entry_free(ent); 255 by_dir_entry_free(ent);
258 return 0; 256 return 0;
diff --git a/src/lib/libssl/src/crypto/x509/x509_obj.c b/src/lib/libssl/src/crypto/x509/x509_obj.c
index bcc1e7429e..1d3cf547d7 100644
--- a/src/lib/libssl/src/crypto/x509/x509_obj.c
+++ b/src/lib/libssl/src/crypto/x509/x509_obj.c
@@ -90,8 +90,7 @@ int i;
90 buf=b->data; 90 buf=b->data;
91 OPENSSL_free(b); 91 OPENSSL_free(b);
92 } 92 }
93 strncpy(buf,"NO X509_NAME",len); 93 strlcpy(buf,"NO X509_NAME",len);
94 buf[len-1]='\0';
95 return buf; 94 return buf;
96 } 95 }
97 96
diff --git a/src/lib/libssl/src/crypto/x509v3/v3_alt.c b/src/lib/libssl/src/crypto/x509v3/v3_alt.c
index 66ea96db51..8de5dd041b 100644
--- a/src/lib/libssl/src/crypto/x509v3/v3_alt.c
+++ b/src/lib/libssl/src/crypto/x509v3/v3_alt.c
@@ -579,10 +579,12 @@ static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
579 return 0; 579 return 0;
580 objlen = p - value; 580 objlen = p - value;
581 objtmp = OPENSSL_malloc(objlen + 1); 581 objtmp = OPENSSL_malloc(objlen + 1);
582 strncpy(objtmp, value, objlen); 582 if (objtmp) {
583 objtmp[objlen] = 0; 583 strlcpy(objtmp, value, objlen + 1);
584 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); 584 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
585 OPENSSL_free(objtmp); 585 OPENSSL_free(objtmp);
586 } else
587 gen->d.otherName->type_id = NULL;
586 if (!gen->d.otherName->type_id) 588 if (!gen->d.otherName->type_id)
587 return 0; 589 return 0;
588 return 1; 590 return 1;
diff --git a/src/lib/libssl/src/crypto/x509v3/v3_info.c b/src/lib/libssl/src/crypto/x509v3/v3_info.c
index e1b8699f92..44bc3e1105 100644
--- a/src/lib/libssl/src/crypto/x509v3/v3_info.c
+++ b/src/lib/libssl/src/crypto/x509v3/v3_info.c
@@ -165,8 +165,7 @@ static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *metho
165 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,ERR_R_MALLOC_FAILURE); 165 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,ERR_R_MALLOC_FAILURE);
166 goto err; 166 goto err;
167 } 167 }
168 strncpy(objtmp, cnf->name, objlen); 168 strlcpy(objtmp, cnf->name, objlen + 1);
169 objtmp[objlen] = 0;
170 acc->method = OBJ_txt2obj(objtmp, 0); 169 acc->method = OBJ_txt2obj(objtmp, 0);
171 if(!acc->method) { 170 if(!acc->method) {
172 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,X509V3_R_BAD_OBJECT); 171 X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,X509V3_R_BAD_OBJECT);
diff --git a/src/lib/libssl/src/test/dummytest.c b/src/lib/libssl/src/test/dummytest.c
deleted file mode 100644
index 5b4467e042..0000000000
--- a/src/lib/libssl/src/test/dummytest.c
+++ /dev/null
@@ -1,48 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <openssl/e_os2.h>
6#include <openssl/buffer.h>
7#include <openssl/crypto.h>
8
9int main(int argc, char *argv[])
10 {
11 char *p, *q = 0, *program;
12
13 p = strrchr(argv[0], '/');
14 if (!p) p = strrchr(argv[0], '\\');
15#ifdef OPENSSL_SYS_VMS
16 if (!p) p = strrchr(argv[0], ']');
17 if (p) q = strrchr(p, '>');
18 if (q) p = q;
19 if (!p) p = strrchr(argv[0], ':');
20 q = 0;
21#endif
22 if (p) p++;
23 if (!p) p = argv[0];
24 if (p) q = strchr(p, '.');
25 if (p && !q) q = p + strlen(p);
26
27 if (!p)
28 program = BUF_strdup("(unknown)");
29 else
30 {
31 program = OPENSSL_malloc((q - p) + 1);
32 strncpy(program, p, q - p);
33 program[q - p] = '\0';
34 }
35
36 for(p = program; *p; p++)
37 if (islower((unsigned char)(*p)))
38 *p = toupper((unsigned char)(*p));
39
40 q = strstr(program, "TEST");
41 if (q > p && q[-1] == '_') q--;
42 *q = '\0';
43
44 printf("No %s support\n", program);
45
46 OPENSSL_free(program);
47 return(0);
48 }
diff --git a/src/lib/libssl/test/dummytest.c b/src/lib/libssl/test/dummytest.c
deleted file mode 100644
index 5b4467e042..0000000000
--- a/src/lib/libssl/test/dummytest.c
+++ /dev/null
@@ -1,48 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <openssl/e_os2.h>
6#include <openssl/buffer.h>
7#include <openssl/crypto.h>
8
9int main(int argc, char *argv[])
10 {
11 char *p, *q = 0, *program;
12
13 p = strrchr(argv[0], '/');
14 if (!p) p = strrchr(argv[0], '\\');
15#ifdef OPENSSL_SYS_VMS
16 if (!p) p = strrchr(argv[0], ']');
17 if (p) q = strrchr(p, '>');
18 if (q) p = q;
19 if (!p) p = strrchr(argv[0], ':');
20 q = 0;
21#endif
22 if (p) p++;
23 if (!p) p = argv[0];
24 if (p) q = strchr(p, '.');
25 if (p && !q) q = p + strlen(p);
26
27 if (!p)
28 program = BUF_strdup("(unknown)");
29 else
30 {
31 program = OPENSSL_malloc((q - p) + 1);
32 strncpy(program, p, q - p);
33 program[q - p] = '\0';
34 }
35
36 for(p = program; *p; p++)
37 if (islower((unsigned char)(*p)))
38 *p = toupper((unsigned char)(*p));
39
40 q = strstr(program, "TEST");
41 if (q > p && q[-1] == '_') q--;
42 *q = '\0';
43
44 printf("No %s support\n", program);
45
46 OPENSSL_free(program);
47 return(0);
48 }