From e1c5b6a00e98c7a995b16113f3d0c045d8d56799 Mon Sep 17 00:00:00 2001 From: fQwQf Date: Fri, 20 Nov 2026 19:19:15 +0800 Subject: [PATCH] Fix getusershell() fallback for /etc/shells and array growth The getusershell() compatibility implementation read /etc/shell instead of /etc/shells. It also expanded the shell array with reallocarray(NULL, ...), discarding the entries read so far once the list grew beyond the initial allocation. Fix both and skip blank or comment-only lines so they do not consume array entries. The path fix must not be applied without the array fix: once the fallback reads /etc/shells, a list with 16 or more entries would otherwise hit the uninitialized slots. --- lib/util/getusershell.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/util/getusershell.c b/lib/util/getusershell.c index 0185357d7e..9e7d78702e 100644 --- a/lib/util/getusershell.c +++ b/lib/util/getusershell.c @@ -30,7 +30,7 @@ # include #endif -static const char *shellfile = "/etc/shell"; +static const char *shellfile = "/etc/shells"; static char **allowed_shells, * const *current_shell; static const char *default_shells[] = { "/bin/sh", @@ -62,10 +62,15 @@ read_shells(void) goto bad; while (sudo_parseln(&line, &linesize, NULL, fp, PARSELN_CONT_IGN) != -1) { + /* Skip blank lines and comments. */ + if (line[0] == '\0') + continue; if (nshells + 1 >= maxshells) { char **new_shells; - new_shells = reallocarray(NULL, maxshells + 16, sizeof(char *)); + /* Preserve the entries already read when growing the array. */ + new_shells = reallocarray(allowed_shells, maxshells + 16, + sizeof(char *)); if (new_shells == NULL) goto bad; allowed_shells = new_shells;