logo

skeud

Simple and portable utilities to deal with user accounts (su, login)
commit: ede0807dcc155cd71d80c51ddfe8978e7032007e
parent 285c64f0fdcb2f69022291aceb74b5ea9b822725
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 11 Sep 2023 08:44:35 +0200

su: Assemble shell command in an array

Diffstat:

Msu.c32++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/su.c b/su.c @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+skeud@hacktivis.me> +// SPDX-FileCopyrightText: 2022-2023 Haelwenn (lanodan) Monnier <contact+skeud@hacktivis.me> // SPDX-License-Identifier: MPL-2.0 #define _POSIX_C_SOURCE 200809L @@ -33,7 +33,7 @@ main(int argc, char *argv[]) char *username = "root"; struct passwd *pwent = NULL; char *shell = NULL; - char *command = NULL; + char *opt_cmd = NULL; if(geteuid() != 0) { @@ -46,8 +46,8 @@ main(int argc, char *argv[]) { switch(c) { - case 'c': // command - command = optarg; + case 'c': // opt_cmd + opt_cmd = optarg; break; case 'l': // login-mode opt_l = true; @@ -230,18 +230,26 @@ main(int argc, char *argv[]) setenv("SHELL", shell, 1); setenv("IFS", " \t\n", 1); - errno = 0; - int ret = 0; - if(command != NULL) + int cmd_argc = 1; + char *cmd_argv[2 + 1 + 2] = {shell, NULL}; + + if(opt_l) { - /* flawfinder: ignore CWE-78 */ - ret = execlp(shell, shell, "-l", "-c", command, NULL); + cmd_argv[cmd_argc++] = "-l"; } - else + + if(opt_cmd != NULL) { - /* flawfinder: ignore CWE-78 */ - ret = execlp(shell, shell, "-l", NULL); + cmd_argv[cmd_argc++] = "-c"; + cmd_argv[cmd_argc++] = opt_cmd; } + + /* Just to be sure it's null-terminated */ + cmd_argv[cmd_argc + 1] = NULL; + + errno = 0; + /* flawfinder: ignore CWE-78 */ + int ret = execvp(shell, cmd_argv); if(ret < 0) { if(errno == ENOENT)