From: Harlan Stenn Date: Tue, 28 Feb 2017 11:13:45 +0000 (-0500) Subject: [Sec 3385] NTP-01-010 NTP: ereallocarray()/eallocarray() underused. HStenn X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0883fc12a1f673aef61cf6466d6ea6f6a9320e5b;p=thirdparty%2Fntp.git [Sec 3385] NTP-01-010 NTP: ereallocarray()/eallocarray() underused. HStenn bk: 58b55b69DpojVa9LLiLLQfXjCCJWZA --- diff --git a/ChangeLog b/ChangeLog index fea59795f..53ebd1835 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ --- (4.2.8p10) +* [Sec 3385] NTP-01-010 NTP: ereallocarray()/eallocarray() underused. HStenn * [Bug 3363] Support for openssl-1.1.0 without compatibility modes - rework of patch set from . * [Bug 3356] Bugfix 3072 breaks multicastclient diff --git a/include/ntp_stdlib.h b/include/ntp_stdlib.h index 5a20a83fa..a1ac8e3a9 100644 --- a/include/ntp_stdlib.h +++ b/include/ntp_stdlib.h @@ -105,21 +105,23 @@ extern u_int32 addr2refid (sockaddr_u *); /* emalloc.c */ #ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ extern void * ereallocz (void *, size_t, size_t, int); -extern void * oreallocarray (void *optr, size_t nmemb, size_t size); +extern void * oreallocarrayx (void *optr, size_t nmemb, size_t size, size_t extra); #define erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z)) #define emalloc(n) ereallocz(NULL, (n), 0, FALSE) #define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) #define erealloc(p, c) ereallocz((p), (c), 0, FALSE) #define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE) -#define ereallocarray(p, n, s) oreallocarray((p), (n), (s)) -#define eallocarray(n, s) oreallocarray(NULL, (n), (s)) +#define ereallocarray(p, n, s) oreallocarrayx((p), (n), (s), 0) +#define eallocarray(n, s) oreallocarrayx(NULL, (n), (s), 0) +#define ereallocarrayx(p, n, s, x) oreallocarrayx((p), (n), (s), (x)) +#define eallocarrayx(n, s, x) oreallocarrayx(NULL, (n), (s), (x)) extern char * estrdup_impl(const char *); #define estrdup(s) estrdup_impl(s) #else extern void * ereallocz (void *, size_t, size_t, int, const char *, int); -extern void * oreallocarray (void *optr, size_t nmemb, size_t size, - const char *, int); +extern void * oreallocarrayx (void *optr, size_t nmemb, size_t size, + size_t extra, const char *, int); #define erealloczsite ereallocz #define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ __FILE__, __LINE__) diff --git a/libntp/authkeys.c b/libntp/authkeys.c index 51337d5c4..e1456a4ca 100644 --- a/libntp/authkeys.c +++ b/libntp/authkeys.c @@ -250,7 +250,7 @@ auth_moremem( i = (keycount > 0) ? keycount : MEMINC; - sk = emalloc_zero(i * sizeof(*sk) + MOREMEM_EXTRA_ALLOC); + sk = eallocarrayx(i, sizeof(*sk), MOREMEM_EXTRA_ALLOC); #ifdef DEBUG base = sk; #endif diff --git a/libntp/emalloc.c b/libntp/emalloc.c index 8b7ef990c..8d786c411 100644 --- a/libntp/emalloc.c +++ b/libntp/emalloc.c @@ -83,10 +83,11 @@ ereallocz( #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) void * -oreallocarray( +oreallocarrayx( void *optr, size_t nmemb, - size_t size + size_t size, + size_t extra #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ , const char * file, @@ -106,9 +107,9 @@ oreallocarray( exit(1); } #ifndef EREALLOC_CALLSITE - return ereallocz(optr, (size * nmemb), 0, FALSE); + return ereallocz(optr, extra + (size * nmemb), 0, FALSE); #else - return ereallocz(optr, (size * nmemb), 0, FALSE, file, line); + return ereallocz(optr, extra + (size * nmemb), 0, FALSE, file, line); #endif } diff --git a/libntp/recvbuff.c b/libntp/recvbuff.c index f8889503c..a3c41a04d 100644 --- a/libntp/recvbuff.c +++ b/libntp/recvbuff.c @@ -85,7 +85,7 @@ create_buffers(int nbufs) buffer_shortfall = 0; #ifndef DEBUG - bufp = emalloc_zero(abuf * sizeof(*bufp)); + bufp = eallocarray(abuf, sizeof(*bufp)); #endif for (i = 0; i < abuf; i++) { diff --git a/ntpd/ntp_loopfilter.c b/ntpd/ntp_loopfilter.c index c8320c480..8d44fb17d 100644 --- a/ntpd/ntp_loopfilter.c +++ b/ntpd/ntp_loopfilter.c @@ -1307,8 +1307,7 @@ loop_config( if (freq < HUFFPUFF) freq = HUFFPUFF; sys_hufflen = (int)(freq / HUFFPUFF); - sys_huffpuff = emalloc(sizeof(sys_huffpuff[0]) * - sys_hufflen); + sys_huffpuff = eallocarray(sys_hufflen, sizeof(sys_huffpuff[0])); for (i = 0; i < sys_hufflen; i++) sys_huffpuff[i] = 1e9; sys_mindly = 1e9; diff --git a/ntpd/ntp_peer.c b/ntpd/ntp_peer.c index 5afc1507c..a296ea7dd 100644 --- a/ntpd/ntp_peer.c +++ b/ntpd/ntp_peer.c @@ -161,7 +161,7 @@ getmorepeermem(void) int i; struct peer *peers; - peers = emalloc_zero(INC_PEER_ALLOC * sizeof(*peers)); + peers = eallocarray(INC_PEER_ALLOC, sizeof(*peers)); for (i = INC_PEER_ALLOC - 1; i >= 0; i--) LINK_SLIST(peer_free, &peers[i], p_link); diff --git a/ntpd/ntp_restrict.c b/ntpd/ntp_restrict.c index 0b5fa2e3f..ad6c82a32 100644 --- a/ntpd/ntp_restrict.c +++ b/ntpd/ntp_restrict.c @@ -166,7 +166,7 @@ alloc_res4(void) if (res != NULL) return res; - rl = emalloc_zero(count * cb); + rl = eallocarray(count, cb); /* link all but the first onto free list */ res = (void *)((char *)rl + (count - 1) * cb); for (i = count - 1; i > 0; i--) { @@ -192,7 +192,7 @@ alloc_res6(void) if (res != NULL) return res; - rl = emalloc_zero(count * cb); + rl = eallocarray(count, cb); /* link all but the first onto free list */ res = (void *)((char *)rl + (count - 1) * cb); for (i = count - 1; i > 0; i--) { diff --git a/sntp/kod_management.c b/sntp/kod_management.c index c8df3bdca..b378bf294 100644 --- a/sntp/kod_management.c +++ b/sntp/kod_management.c @@ -246,6 +246,7 @@ kod_init_kod_db( rewind(db_s); + /* Allocate the array of pointers to the struct kod_entry items */ kod_db = eallocarray(kod_db_cnt, sizeof(kod_db[0])); /* Read contents of file */ @@ -265,7 +266,8 @@ kod_init_kod_db( continue; } - kod_db[b] = emalloc(sizeof(*kod_db[b])); + /* Allocate this struct kod_entry item */ + kod_db[b] = emalloc(sizeof(*kod_db[0])); if (3 != sscanf(fbuf, "%llx %4s %254s", &ull, kod_db[b]->type, kod_db[b]->hostname)) {