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