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