From: Joe Simmons-Talbott <josimmon@redhat.com>
To: libc-alpha@sourceware.org
Cc: Joe Simmons-Talbott <josimmon@redhat.com>
Subject: [PATCH v3] vfprintf-internal: Get rid of alloca.
Date: Mon, 22 May 2023 13:06:27 -0400 [thread overview]
Message-ID: <20230522170627.239536-1-josimmon@redhat.com> (raw)
Avoid potential stack overflow from unbounded alloca. Use the existing
scratch_buffer instead.
---
Changes to v2:
* Don't assume the first scratch buffer resize adds enough space.
stdio-common/vfprintf-internal.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c
index c76c06e49b..20fd3c3043 100644
--- a/stdio-common/vfprintf-internal.c
+++ b/stdio-common/vfprintf-internal.c
@@ -1066,6 +1066,8 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
union printf_arg *args_value;
int *args_size;
int *args_type;
+ int *args_pa_user;
+ size_t args_pa_user_offset;
{
/* Calculate total size needed to represent a single argument
across all three argument-related arrays. */
@@ -1082,6 +1084,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));
}
@@ -1171,7 +1174,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] >
+ (int *) &argsbuf + argsbuf.length)
+ {
+ args_pa_user_offset = args_pa_user - &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 = &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);
}
--
2.39.2
next reply other threads:[~2023-05-22 17:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-22 17:06 Joe Simmons-Talbott [this message]
2023-06-01 13:34 ` Joe Simmons-Talbott
2023-06-09 14:59 ` Joe Simmons-Talbott
2023-06-12 20:15 ` Adhemerval Zanella Netto
2023-06-14 18:45 ` Joe Simmons-Talbott
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230522170627.239536-1-josimmon@redhat.com \
--to=josimmon@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).