ce426f
Short description: CVE-2015-5220: calloc() returns non-zeroed memory.
ce426f
Author(s): Ondrej Bilka
ce426f
Origin: git://sourceware.org/git/glibc.git
ce426f
Bug-RHEL: #1296453 (rhel-7.2.z),  #1293976 (rhel-7.3), #1256285 (SRT)
ce426f
Bug-Fedora: NA
ce426f
Bug-Upstream: NA
ce426f
Upstream status: committed
ce426f
#
ce426f
# commit e8349efd466cfedc0aa98be61d88ca8795c9e565
ce426f
# Author: Ondřej Bílka <neleai@seznam.cz>
ce426f
# Date:   Mon Dec 9 17:25:19 2013 +0100
ce426f
#
ce426f
#    Simplify perturb_byte logic.
ce426f
#
ce426f
diff --git a/malloc/malloc.c b/malloc/malloc.c
ce426f
index 4821deb..ac8c3f6 100644
ce426f
--- a/malloc/malloc.c
ce426f
+++ b/malloc/malloc.c
ce426f
@@ -1870,8 +1870,20 @@ static int check_action = DEFAULT_CHECK_ACTION;
ce426f
 
ce426f
 static int perturb_byte;
ce426f
 
ce426f
-#define alloc_perturb(p, n) memset (p, (perturb_byte ^ 0xff) & 0xff, n)
ce426f
-#define free_perturb(p, n) memset (p, perturb_byte & 0xff, n)
ce426f
+static inline void
ce426f
+alloc_perturb (char *p, size_t n)
ce426f
+{
ce426f
+  if (__glibc_unlikely (perturb_byte))
ce426f
+    memset (p, perturb_byte ^ 0xff, n);
ce426f
+}
ce426f
+
ce426f
+static inline void
ce426f
+free_perturb (char *p, size_t n)
ce426f
+{
ce426f
+  if (__glibc_unlikely (perturb_byte))
ce426f
+    memset (p, perturb_byte, n);
ce426f
+}
ce426f
+
ce426f
 
ce426f
 
ce426f
 #include <stap-probe.h>
ce426f
@@ -3287,8 +3299,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 	}
ce426f
       check_remalloced_chunk(av, victim, nb);
ce426f
       void *p = chunk2mem(victim);
ce426f
-      if (__builtin_expect (perturb_byte, 0))
ce426f
-	alloc_perturb (p, bytes);
ce426f
+      alloc_perturb (p, bytes);
ce426f
       return p;
ce426f
     }
ce426f
   }
ce426f
@@ -3323,8 +3334,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 	  victim->size |= NON_MAIN_ARENA;
ce426f
 	check_malloced_chunk(av, victim, nb);
ce426f
 	void *p = chunk2mem(victim);
ce426f
-	if (__builtin_expect (perturb_byte, 0))
ce426f
-	  alloc_perturb (p, bytes);
ce426f
+	alloc_perturb (p, bytes);
ce426f
 	return p;
ce426f
       }
ce426f
     }
ce426f
@@ -3403,8 +3413,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 
ce426f
 	check_malloced_chunk(av, victim, nb);
ce426f
 	void *p = chunk2mem(victim);
ce426f
-	if (__builtin_expect (perturb_byte, 0))
ce426f
-	  alloc_perturb (p, bytes);
ce426f
+	alloc_perturb (p, bytes);
ce426f
 	return p;
ce426f
       }
ce426f
 
ce426f
@@ -3420,8 +3429,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 	  victim->size |= NON_MAIN_ARENA;
ce426f
 	check_malloced_chunk(av, victim, nb);
ce426f
 	void *p = chunk2mem(victim);
ce426f
-	if (__builtin_expect (perturb_byte, 0))
ce426f
-	  alloc_perturb (p, bytes);
ce426f
+	alloc_perturb (p, bytes);
ce426f
 	return p;
ce426f
       }
ce426f
 
ce426f
@@ -3545,8 +3553,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 	}
ce426f
 	check_malloced_chunk(av, victim, nb);
ce426f
 	void *p = chunk2mem(victim);
ce426f
-	if (__builtin_expect (perturb_byte, 0))
ce426f
-	  alloc_perturb (p, bytes);
ce426f
+	alloc_perturb (p, bytes);
ce426f
 	return p;
ce426f
       }
ce426f
     }
ce426f
@@ -3649,8 +3656,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 	}
ce426f
 	check_malloced_chunk(av, victim, nb);
ce426f
 	void *p = chunk2mem(victim);
ce426f
-	if (__builtin_expect (perturb_byte, 0))
ce426f
-	  alloc_perturb (p, bytes);
ce426f
+	alloc_perturb (p, bytes);
ce426f
 	return p;
ce426f
       }
ce426f
     }
ce426f
@@ -3684,8 +3690,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
 
ce426f
       check_malloced_chunk(av, victim, nb);
ce426f
       void *p = chunk2mem(victim);
ce426f
-      if (__builtin_expect (perturb_byte, 0))
ce426f
-	alloc_perturb (p, bytes);
ce426f
+      alloc_perturb (p, bytes);
ce426f
       return p;
ce426f
     }
ce426f
 
ce426f
@@ -3705,7 +3710,7 @@ _int_malloc(mstate av, size_t bytes)
ce426f
     */
ce426f
     else {
ce426f
       void *p = sysmalloc(nb, av);
ce426f
-      if (p != NULL && __builtin_expect (perturb_byte, 0))
ce426f
+      if (p != NULL)
ce426f
 	alloc_perturb (p, bytes);
ce426f
       return p;
ce426f
     }
ce426f
@@ -3798,8 +3803,7 @@ _int_free(mstate av, mchunkptr p, int have_lock)
ce426f
 	  }
ce426f
       }
ce426f
 
ce426f
-    if (__builtin_expect (perturb_byte, 0))
ce426f
-      free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
ce426f
+    free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
ce426f
 
ce426f
     set_fastchunks(av);
ce426f
     unsigned int idx = fastbin_index(size);
ce426f
@@ -3881,8 +3885,7 @@ _int_free(mstate av, mchunkptr p, int have_lock)
ce426f
 	goto errout;
ce426f
       }
ce426f
 
ce426f
-    if (__builtin_expect (perturb_byte, 0))
ce426f
-      free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
ce426f
+    free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
ce426f
 
ce426f
     /* consolidate backward */
ce426f
     if (!prev_inuse(p)) {