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