From 0ab7b3dd45e0e2edb5abc4bba41af7df6e9a6095 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 20 Nov 2026 15:07:56 -0600 Subject: [PATCH] Make sure toybuf is zeroed on re-entry throuh toy_init(). --- main.c | 13 +++++++------ toys.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index a573568b..c0139677 100644 --- a/main.c +++ b/main.c @@ -188,7 +188,6 @@ void check_help(char **arg) // Lie to autoconf when it asks stupid questions, so configure regexes // that look for "GNU sed version %f" greater than some old buggy number // don't fail us for not matching their narrow expectations. - *toybuf = 0; if (flags&TOYFLAG_AUTOCONF) sprintf(toybuf, " (is not GNU %s 9.0)", toys.which->name); xprintf("toybox %s%s\n", toybox_version, toybuf); @@ -236,12 +235,17 @@ void toy_init(struct toy_list *which, char *argv[]) { void *oldwhich = toys.which; + if (oldwhich) { + memset(toybuf, 0, sizeof(toybuf)); + memset(&toys, 0, offsetof(struct toy_context, rebound)); + memset(&this, 0, sizeof(this)); + } + // Drop permissions for non-suid commands. if (CFG_TOYBOX_SUID) { - if (!toys.which) toys.which = toy_list; - uid_t uid = getuid(), euid = geteuid(); + if (!oldwhich) toys.which = toy_list; // for error messages if (!(which->flags & TOYFLAG_STAYROOT)) { if (uid != euid) { if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root @@ -258,9 +262,6 @@ void toy_init(struct toy_list *which, char *argv[]) } } - memset(&toys, 0, offsetof(struct toy_context, rebound)); - if (oldwhich) memset(&this, 0, sizeof(this)); - // Continue to portion of init needed by standalone commands toy_singleinit(which, argv); } diff --git a/toys.h b/toys.h index b951c314..dcefde07 100644 --- a/toys.h +++ b/toys.h @@ -122,7 +122,7 @@ extern struct toy_context { struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit() void *stacktop; // nested toy_exec() call count, or 0 if vforked int envc; // Count of original environ entries - int old_umask; // Old umask preserved by TOYFLAG_UMASK + short old_umask; // Old umask preserved by TOYFLAG_UMASK short signal; // generic_signal() records what signal it saw here int signalfd; // and writes signal to this fd, if set } toys; -- 2.39.5