7c0489
commit d93769405996dfc11d216ddbe415946617b5a494
7c0489
Author: Andreas Schwab <schwab@suse.de>
7c0489
Date:   Mon Jan 20 17:01:50 2020 +0100
7c0489
7c0489
    Fix array overflow in backtrace on PowerPC (bug 25423)
7c0489
    
7c0489
    When unwinding through a signal frame the backtrace function on PowerPC
7c0489
    didn't check array bounds when storing the frame address.  Fixes commit
7c0489
    d400dcac5e ("PowerPC: fix backtrace to handle signal trampolines").
7c0489
7c0489
diff --git a/debug/tst-backtrace5.c b/debug/tst-backtrace5.c
7c0489
index e7ce410845..b2f46160e7 100644
7c0489
--- a/debug/tst-backtrace5.c
7c0489
+++ b/debug/tst-backtrace5.c
7c0489
@@ -89,6 +89,18 @@ handle_signal (int signum)
7c0489
       }
7c0489
   /* Symbol names are not available for static functions, so we do not
7c0489
      check do_test.  */
7c0489
+
7c0489
+  /* Check that backtrace does not return more than what fits in the array
7c0489
+     (bug 25423).  */
7c0489
+  for (int j = 0; j < NUM_FUNCTIONS; j++)
7c0489
+    {
7c0489
+      n = backtrace (addresses, j);
7c0489
+      if (n > j)
7c0489
+	{
7c0489
+	  FAIL ();
7c0489
+	  return;
7c0489
+	}
7c0489
+    }
7c0489
 }
7c0489
 
7c0489
 NO_INLINE int
7c0489
diff --git a/sysdeps/powerpc/powerpc32/backtrace.c b/sysdeps/powerpc/powerpc32/backtrace.c
7c0489
index 7c2d4726f8..d1456c8ae4 100644
7c0489
--- a/sysdeps/powerpc/powerpc32/backtrace.c
7c0489
+++ b/sysdeps/powerpc/powerpc32/backtrace.c
7c0489
@@ -114,6 +114,8 @@ __backtrace (void **array, int size)
7c0489
         }
7c0489
       if (gregset)
7c0489
 	{
7c0489
+	  if (count + 1 == size)
7c0489
+	    break;
7c0489
 	  array[++count] = (void*)((*gregset)[PT_NIP]);
7c0489
 	  current = (void*)((*gregset)[PT_R1]);
7c0489
 	}
7c0489
diff --git a/sysdeps/powerpc/powerpc64/backtrace.c b/sysdeps/powerpc/powerpc64/backtrace.c
7c0489
index 65c260ab76..8a53a1088f 100644
7c0489
--- a/sysdeps/powerpc/powerpc64/backtrace.c
7c0489
+++ b/sysdeps/powerpc/powerpc64/backtrace.c
7c0489
@@ -87,6 +87,8 @@ __backtrace (void **array, int size)
7c0489
       if (is_sigtramp_address (current->return_address))
7c0489
         {
7c0489
 	  struct signal_frame_64 *sigframe = (struct signal_frame_64*) current;
7c0489
+	  if (count + 1 == size)
7c0489
+	    break;
7c0489
           array[++count] = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_NIP];
7c0489
 	  current = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_R1];
7c0489
 	}