]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Properly compute the physical memory size
authorMark Andrews <marka@isc.org>
Tue, 9 Jul 2024 02:37:13 +0000 (12:37 +1000)
committerMark Andrews <marka@isc.org>
Wed, 31 Jul 2024 07:30:39 +0000 (07:30 +0000)
On a 32 bit machine casting to size_t can still lead to an overflow.
Cast to uint64_t.  Also detect all possible negative values for
pages and pagesize to silence warning about possible negative value.

    39#if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
     1. tainted_data_return: Called function sysconf(_SC_PHYS_PAGES),
           and a possible return value may be less than zero.
     2. assign: Assigning: pages = sysconf(_SC_PHYS_PAGES).
    40        long pages = sysconf(_SC_PHYS_PAGES);
    41        long pagesize = sysconf(_SC_PAGESIZE);
    42
     3. Condition pages == -1, taking false branch.
     4. Condition pagesize == -1, taking false branch.
    43        if (pages == -1 || pagesize == -1) {
    44                return (0);
    45        }
    46
     5. overflow: The expression (size_t)pages * pagesize might be negative,
           but is used in a context that treats it as unsigned.

    CID 498034: (#1 of 1): Overflowed return value (INTEGER_OVERFLOW)
    6. return_overflow: (size_t)pages * pagesize, which might have underflowed,
       is returned from the function.
    47        return ((size_t)pages * pagesize);
    48#endif /* if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) */

(cherry picked from commit e8dbc5db929c40174780b95540e33cc228bb2391)

lib/isc/meminfo.c

index 612cceaed31a02c2094f04bf594c28d87ae4f457..6d68afec6ef92474e1813356e985591821871e40 100644 (file)
@@ -40,11 +40,11 @@ isc_meminfo_totalphys(void) {
        long pages = sysconf(_SC_PHYS_PAGES);
        long pagesize = sysconf(_SC_PAGESIZE);
 
-       if (pages == -1 || pagesize == -1) {
+       if (pages < 0 || pagesize < 0) {
                return (0);
        }
 
-       return ((size_t)pages * pagesize);
+       return ((uint64_t)pages * pagesize);
 #endif /* if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) */
        return (0);
 }