]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Sec 3385] NTP-01-010 NTP: ereallocarray()/eallocarray() underused. HStenn
authorHarlan Stenn <stenn@ntp.org>
Tue, 28 Feb 2017 11:13:45 +0000 (06:13 -0500)
committerHarlan Stenn <stenn@ntp.org>
Tue, 28 Feb 2017 11:13:45 +0000 (06:13 -0500)
bk: 58b55b69DpojVa9LLiLLQfXjCCJWZA

ChangeLog
include/ntp_stdlib.h
libntp/authkeys.c
libntp/emalloc.c
libntp/recvbuff.c
ntpd/ntp_loopfilter.c
ntpd/ntp_peer.c
ntpd/ntp_restrict.c
sntp/kod_management.c

index fea59795fdd7782eef1c5c7f277764ee3748e9b8..53ebd1835be75a1bbbcb232d179a2d58862a109b 100644 (file)
--- 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 <ntp.org@eroen.eu>. <perlinger@ntp.org>
 * [Bug 3356] Bugfix 3072 breaks multicastclient <perlinger@ntp.org>
index 5a20a83fa618da5943db893e6f347670a8a7cd5d..a1ac8e3a9ed17fb8dae1d7f008b22fef8867c525 100644 (file)
@@ -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__)
index 51337d5c4f6f12f84636ed93b687527050125ee4..e1456a4ca1921cf849f2bfb84b881a824ee008fe 100644 (file)
@@ -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
index 8b7ef990c6b69ef7f187a5f9d4047ca1486596d8..8d786c4112c4f2428de54c97de33459466efd29e 100644 (file)
@@ -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
 }
 
index f8889503c071a80076b4508293ad896cc3b0da54..a3c41a04dc2e04c38156336572bd49fd15fc90f4 100644 (file)
@@ -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++) {
index c8320c480196f0363229e85ec0c3514c8ebb53fa..8d44fb17db4e06ce8311613ee8d1098224340c48 100644 (file)
@@ -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;
index 5afc1507c5b4bf584cc6b4b446f358f08b0c32ad..a296ea7dd32fcf15aae020169e206f536c27f73d 100644 (file)
@@ -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);
index 0b5fa2e3fa0e60f3f9a283d80fdbee060b540ca9..ad6c82a32a41198823064eee0dcbfaadf7d36b54 100644 (file)
@@ -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--) {
index c8df3bdca60e1e9deca23225f2297c74e44c9a5d..b378bf294d7710f94b08d2b026559c6646fc8cfc 100644 (file)
@@ -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)) {