5e60a9
From 49c3c37651e2d2ec4ff8ce21252bbbc08a9d6639 Mon Sep 17 00:00:00 2001
5e60a9
From: Eyal Itkin <eyalit@checkpoint.com>
5e60a9
Date: Tue, 31 Mar 2020 02:00:14 -0400
5e60a9
Subject: Fix alignment bug in Safe-Linking
5e60a9
5e60a9
Alignment checks should be performed on the user's buffer and NOT
5e60a9
on the mchunkptr as was done before. This caused bugs in 32 bit
5e60a9
versions, because: 2*sizeof(t) != MALLOC_ALIGNMENT.
5e60a9
5e60a9
As the tcache works on users' buffers it uses the aligned_OK()
5e60a9
check, and the rest work on mchunkptr and therefore check using
5e60a9
misaligned_chunk().
5e60a9
5e60a9
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
5e60a9
5e60a9
diff --git a/malloc/malloc.c b/malloc/malloc.c
5e60a9
index 0e4acb22f6..6acb5ad43a 100644
5e60a9
--- a/malloc/malloc.c
5e60a9
+++ b/malloc/malloc.c
5e60a9
@@ -2169,7 +2169,7 @@ do_check_malloc_state (mstate av)
5e60a9
 
5e60a9
       while (p != 0)
5e60a9
         {
5e60a9
-	  if (__glibc_unlikely (!aligned_OK (p)))
5e60a9
+	  if (__glibc_unlikely (misaligned_chunk (p)))
5e60a9
 	    malloc_printerr ("do_check_malloc_state(): "
5e60a9
 			     "unaligned fastbin chunk detected");
5e60a9
           /* each chunk claims to be inuse */
5e60a9
@@ -2949,11 +2949,11 @@ static __always_inline void *
5e60a9
 tcache_get (size_t tc_idx)
5e60a9
 {
5e60a9
   tcache_entry *e = tcache->entries[tc_idx];
5e60a9
+  if (__glibc_unlikely (!aligned_OK (e)))
5e60a9
+    malloc_printerr ("malloc(): unaligned tcache chunk detected");
5e60a9
   tcache->entries[tc_idx] = REVEAL_PTR (e->next);
5e60a9
   --(tcache->counts[tc_idx]);
5e60a9
   e->key = NULL;
5e60a9
-  if (__glibc_unlikely (!aligned_OK (e)))
5e60a9
-    malloc_printerr ("malloc(): unaligned tcache chunk detected");
5e60a9
   return (void *) e;
5e60a9
 }
5e60a9
 
5e60a9
@@ -3591,7 +3591,7 @@ _int_malloc (mstate av, size_t bytes)
5e60a9
       if (victim == NULL)				\
5e60a9
 	break;						\
5e60a9
       pp = REVEAL_PTR (victim->fd);                                     \
5e60a9
-      if (__glibc_unlikely (!aligned_OK (pp)))                          \
5e60a9
+      if (__glibc_unlikely (pp != NULL && misaligned_chunk (pp)))       \
5e60a9
 	malloc_printerr ("malloc(): unaligned fastbin chunk detected"); \
5e60a9
     }							\
5e60a9
   while ((pp = catomic_compare_and_exchange_val_acq (fb, pp, victim)) \
5e60a9
@@ -3606,8 +3606,8 @@ _int_malloc (mstate av, size_t bytes)
5e60a9
 
5e60a9
       if (victim != NULL)
5e60a9
 	{
5e60a9
-	  if (__glibc_unlikely (!aligned_OK (victim)))
5e60a9
-	    malloc_printerr ("malloc(): unaligned fastbin chunk detected");
5e60a9
+	  if (__glibc_unlikely (misaligned_chunk (victim)))
5e60a9
+	    malloc_printerr ("malloc(): unaligned fastbin chunk detected 2");
5e60a9
 
5e60a9
 	  if (SINGLE_THREAD_P)
5e60a9
 	    *fb = REVEAL_PTR (victim->fd);
5e60a9
@@ -3631,8 +3631,8 @@ _int_malloc (mstate av, size_t bytes)
5e60a9
 		  while (tcache->counts[tc_idx] < mp_.tcache_count
5e60a9
 			 && (tc_victim = *fb) != NULL)
5e60a9
 		    {
5e60a9
-		      if (__glibc_unlikely (!aligned_OK (tc_victim)))
5e60a9
-			malloc_printerr ("malloc(): unaligned fastbin chunk detected");
5e60a9
+		      if (__glibc_unlikely (misaligned_chunk (tc_victim)))
5e60a9
+			malloc_printerr ("malloc(): unaligned fastbin chunk detected 3");
5e60a9
 		      if (SINGLE_THREAD_P)
5e60a9
 			*fb = REVEAL_PTR (tc_victim->fd);
5e60a9
 		      else
5e60a9
@@ -4505,7 +4505,7 @@ static void malloc_consolidate(mstate av)
5e60a9
     if (p != 0) {
5e60a9
       do {
5e60a9
 	{
5e60a9
-	  if (__glibc_unlikely (!aligned_OK (p)))
5e60a9
+	  if (__glibc_unlikely (misaligned_chunk (p)))
5e60a9
 	    malloc_printerr ("malloc_consolidate(): "
5e60a9
 			     "unaligned fastbin chunk detected");
5e60a9
 
5e60a9
@@ -4937,7 +4937,7 @@ int_mallinfo (mstate av, struct mallinfo *m)
5e60a9
 	   p != 0;
5e60a9
 	   p = REVEAL_PTR (p->fd))
5e60a9
         {
5e60a9
-	  if (__glibc_unlikely (!aligned_OK (p)))
5e60a9
+	  if (__glibc_unlikely (misaligned_chunk (p)))
5e60a9
 	    malloc_printerr ("int_mallinfo(): "
5e60a9
 			     "unaligned fastbin chunk detected");
5e60a9
           ++nfastblocks;
5e60a9
@@ -5479,7 +5479,7 @@ __malloc_info (int options, FILE *fp)
5e60a9
 
5e60a9
 	      while (p != NULL)
5e60a9
 		{
5e60a9
-		  if (__glibc_unlikely (!aligned_OK (p)))
5e60a9
+		  if (__glibc_unlikely (misaligned_chunk (p)))
5e60a9
 		    malloc_printerr ("__malloc_info(): "
5e60a9
 				     "unaligned fastbin chunk detected");
5e60a9
 		  ++nthissize;