X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Frun_shell.c;h=6be09088d76d5308fbbc337b81b672e57d4db25b;hb=ab24e18c7a32ee1637be19f239e9dd9d7c7f6534;hp=d154b9852121b4b7eeb4ed6c4a3beb8ba1fa6533;hpb=cad5364599eb5062d59e0c397ed638ddd61a8d5d;p=oweals%2Fbusybox.git diff --git a/libbb/run_shell.c b/libbb/run_shell.c index d154b9852..6be09088d 100644 --- a/libbb/run_shell.c +++ b/libbb/run_shell.c @@ -36,45 +36,66 @@ #include #include #include "libbb.h" +#ifdef CONFIG_SELINUX +#include /* for setexeccon */ +#endif +#ifdef CONFIG_SELINUX +static security_context_t current_sid; + +void +renew_current_security_context(void) +{ + if (current_sid) + freecon(current_sid); /* Release old context */ + getcon(¤t_sid); /* update */ +} +void +set_current_security_context(security_context_t sid) +{ + if (current_sid) + freecon(current_sid); /* Release old context */ + current_sid = sid; +} + +#endif /* Run SHELL, or DEFAULT_SHELL if SHELL is empty. If COMMAND is nonzero, pass it to the shell with the -c option. If ADDITIONAL_ARGS is nonzero, pass it to the shell as more arguments. */ -void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args ) +void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) { const char **args; int argno = 1; int additional_args_cnt = 0; - - for ( args = additional_args; args && *args; args++ ) + + for (args = additional_args; args && *args; args++) additional_args_cnt++; - if ( additional_args ) - args = (const char **) xmalloc (sizeof (char *) * ( 4 + additional_args_cnt )); - else - args = (const char **) xmalloc (sizeof (char *) * 4 ); - - args [0] = bb_get_last_path_component ( bb_xstrdup ( shell )); - - if ( loginshell ) { - char *args0; - bb_xasprintf ( &args0, "-%s", args [0] ); - args [0] = args0; - } - - if ( command ) { - args [argno++] = "-c"; - args [argno++] = command; + args = xmalloc(sizeof(char*) * (4 + additional_args_cnt)); + + args[0] = bb_get_last_path_component(xstrdup(shell)); + + if (loginshell) + args[0] = xasprintf("-%s", args[0]); + + if (command) { + args[argno++] = "-c"; + args[argno++] = command; } - if ( additional_args ) { - for ( ; *additional_args; ++additional_args ) - args [argno++] = *additional_args; + if (additional_args) { + for (; *additional_args; ++additional_args) + args[argno++] = *additional_args; } - args [argno] = 0; - execv ( shell, (char **) args ); - bb_perror_msg_and_die ( "cannot run %s", shell ); + args[argno] = NULL; +#ifdef CONFIG_SELINUX + if (current_sid && !setexeccon(current_sid)) { + freecon(current_sid); + execve(shell, (char **) args, environ); + } else +#endif + execv(shell, (char **) args); + bb_perror_msg_and_die("cannot run %s", shell); } -