about summary refs log tree commit diff
path: root/stdio-common/vfprintf-internal.c
diff options
context:
space:
mode:
authorJoe Simmons-Talbott <josimmon@redhat.com>2023-07-31 19:42:32 +0000
committerJoe Simmons-Talbott <josimmon@redhat.com>2023-08-01 12:32:49 +0000
commit5c37d2065286d3db7b974266a3002bb5747d2e5d (patch)
tree2390d4a6e2a70cf55861964af0ac6713482f8c86 /stdio-common/vfprintf-internal.c
parent510fc20d73de12c85823d9996faac74666e9c2e7 (diff)
downloadglibc-5c37d2065286d3db7b974266a3002bb5747d2e5d.tar.gz
glibc-5c37d2065286d3db7b974266a3002bb5747d2e5d.tar.xz
glibc-5c37d2065286d3db7b974266a3002bb5747d2e5d.zip
vfprintf-internal: Get rid of alloca.
Avoid potential stack overflow from unbounded alloca.  Use the existing
scratch_buffer instead.

Add testcases to exercise the code as suggested by Adhemerval Zanella Netto.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Diffstat (limited to 'stdio-common/vfprintf-internal.c')
-rw-r--r--stdio-common/vfprintf-internal.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c
index f30a9e9f3a..6d6d96e800 100644
--- a/stdio-common/vfprintf-internal.c
+++ b/stdio-common/vfprintf-internal.c
@@ -1130,6 +1130,8 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
   union printf_arg *args_value;
   int *args_size;
   int *args_type;
+  void *args_pa_user;
+  size_t args_pa_user_offset;
   {
     /* Calculate total size needed to represent a single argument
        across all three argument-related arrays.  */
@@ -1146,6 +1148,7 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
        now.  */
     args_size = &args_value[nargs].pa_int;
     args_type = &args_size[nargs];
+    args_pa_user = &args_type[nargs];
     memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
 	    nargs * sizeof (*args_type));
   }
@@ -1235,7 +1238,25 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
 	else if (__glibc_unlikely (__printf_va_arg_table != NULL)
 		 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
 	  {
-	    args_value[cnt].pa_user = alloca (args_size[cnt]);
+	    while (args_pa_user + args_size[cnt] >
+		argsbuf.data + argsbuf.length)
+	      {
+		args_pa_user_offset = args_pa_user - (void *) &args_type[nargs];
+	        if (!scratch_buffer_grow_preserve (&argsbuf))
+	          {
+	            Xprintf_buffer_mark_failed (buf);
+	            goto all_done;
+	          }
+                args_value = argsbuf.data;
+                /* Set up the remaining two arrays to each point past the end of
+                   the prior array, since space for all three has been allocated
+                   now.  */
+                args_size = &args_value[nargs].pa_int;
+                args_type = &args_size[nargs];
+                args_pa_user = (void *) &args_type[nargs] + args_pa_user_offset;
+	      }
+	    args_value[cnt].pa_user = args_pa_user;
+	    args_pa_user += args_size[cnt];
 	    (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
 	      (args_value[cnt].pa_user, ap_savep);
 	  }