a2cf7d
commit 16554464bcd9d77b07c6ff419dc54f00e394fa50
a2cf7d
Author: DJ Delorie <dj@redhat.com>
a2cf7d
Date:   Tue Dec 3 17:44:36 2019 -0500
a2cf7d
a2cf7d
    Correct range checking in mallopt/mxfast/tcache [BZ #25194]
a2cf7d
    
a2cf7d
    do_set_tcache_max, do_set_mxfast:
a2cf7d
    Fix two instances of comparing "size_t < 0"
a2cf7d
    Both cases have upper limit, so the "negative value" case
a2cf7d
    is already handled via overflow semantics.
a2cf7d
    
a2cf7d
    do_set_tcache_max, do_set_tcache_count:
a2cf7d
    Fix return value on error.  Note: currently not used.
a2cf7d
    
a2cf7d
    mallopt:
a2cf7d
    pass return value of helper functions to user.  Behavior should
a2cf7d
    only be actually changed for mxfast, where we restore the old
a2cf7d
    (pre-tunables) behavior.
a2cf7d
    
a2cf7d
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
a2cf7d
a2cf7d
diff --git a/malloc/malloc.c b/malloc/malloc.c
a2cf7d
index 90825b2aaed53761..00a37f218c0ab3b2 100644
a2cf7d
--- a/malloc/malloc.c
a2cf7d
+++ b/malloc/malloc.c
a2cf7d
@@ -5111,13 +5111,14 @@ static inline int
a2cf7d
 __always_inline
a2cf7d
 do_set_tcache_max (size_t value)
a2cf7d
 {
a2cf7d
-  if (value >= 0 && value <= MAX_TCACHE_SIZE)
a2cf7d
+  if (value <= MAX_TCACHE_SIZE)
a2cf7d
     {
a2cf7d
       LIBC_PROBE (memory_tunable_tcache_max_bytes, 2, value, mp_.tcache_max_bytes);
a2cf7d
       mp_.tcache_max_bytes = value;
a2cf7d
       mp_.tcache_bins = csize2tidx (request2size(value)) + 1;
a2cf7d
+      return 1;
a2cf7d
     }
a2cf7d
-  return 1;
a2cf7d
+  return 0;
a2cf7d
 }
a2cf7d
 
a2cf7d
 static inline int
a2cf7d
@@ -5128,8 +5129,9 @@ do_set_tcache_count (size_t value)
a2cf7d
     {
a2cf7d
       LIBC_PROBE (memory_tunable_tcache_count, 2, value, mp_.tcache_count);
a2cf7d
       mp_.tcache_count = value;
a2cf7d
+      return 1;
a2cf7d
     }
a2cf7d
-  return 1;
a2cf7d
+  return 0;
a2cf7d
 }
a2cf7d
 
a2cf7d
 static inline int
a2cf7d
@@ -5146,7 +5148,7 @@ static inline int
a2cf7d
 __always_inline
a2cf7d
 do_set_mxfast (size_t value)
a2cf7d
 {
a2cf7d
-  if (value >= 0 && value <= MAX_FAST_SIZE)
a2cf7d
+  if (value <= MAX_FAST_SIZE)
a2cf7d
     {
a2cf7d
       LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ());
a2cf7d
       set_max_fast (value);
a2cf7d
@@ -5171,18 +5173,24 @@ __libc_mallopt (int param_number, int value)
a2cf7d
      (see definition of set_max_fast).  */
a2cf7d
   malloc_consolidate (av);
a2cf7d
 
a2cf7d
+  /* Many of these helper functions take a size_t.  We do not worry
a2cf7d
+     about overflow here, because negative int values will wrap to
a2cf7d
+     very large size_t values and the helpers have sufficient range
a2cf7d
+     checking for such conversions.  Many of these helpers are also
a2cf7d
+     used by the tunables macros in arena.c.  */
a2cf7d
+
a2cf7d
   switch (param_number)
a2cf7d
     {
a2cf7d
     case M_MXFAST:
a2cf7d
-      do_set_mxfast (value);
a2cf7d
+      res = do_set_mxfast (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_TRIM_THRESHOLD:
a2cf7d
-      do_set_trim_threshold (value);
a2cf7d
+      res = do_set_trim_threshold (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_TOP_PAD:
a2cf7d
-      do_set_top_pad (value);
a2cf7d
+      res = do_set_top_pad (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_MMAP_THRESHOLD:
a2cf7d
@@ -5190,25 +5198,25 @@ __libc_mallopt (int param_number, int value)
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_MMAP_MAX:
a2cf7d
-      do_set_mmaps_max (value);
a2cf7d
+      res = do_set_mmaps_max (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_CHECK_ACTION:
a2cf7d
-      do_set_mallopt_check (value);
a2cf7d
+      res = do_set_mallopt_check (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_PERTURB:
a2cf7d
-      do_set_perturb_byte (value);
a2cf7d
+      res = do_set_perturb_byte (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_ARENA_TEST:
a2cf7d
       if (value > 0)
a2cf7d
-	do_set_arena_test (value);
a2cf7d
+	res = do_set_arena_test (value);
a2cf7d
       break;
a2cf7d
 
a2cf7d
     case M_ARENA_MAX:
a2cf7d
       if (value > 0)
a2cf7d
-	do_set_arena_max (value);
a2cf7d
+	res = do_set_arena_max (value);
a2cf7d
       break;
a2cf7d
     }
a2cf7d
   __libc_lock_unlock (av->mutex);