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