Fixes so "make allnoconfig" works again.
[oweals/busybox.git] / libbb / run_shell.c
index b26eba1152397989a5191bec8f6a68ad0777a9e9..67ff2a5f82566d5ec818c14178a5fe48d93b8479 100644 (file)
 #include <syslog.h>
 #include <ctype.h>
 #include "libbb.h"
+#ifdef CONFIG_SELINUX
+#include <selinux/selinux.h>  /* for setexeccon  */
+#endif
 
+#ifdef CONFIG_SELINUX
+static security_context_t current_sid=NULL;
+
+void
+renew_current_security_context(void)
+{
+  if  (current_sid)  
+    freecon(current_sid);  /* Release old context  */
+
+  getcon(&current_sid);  /* update */
+
+  return;
+}
+void
+set_current_security_context(security_context_t sid)
+{
+  if  (current_sid)  
+    freecon(current_sid);  /* Release old context  */
+
+  current_sid=sid;
+
+  return;
+}
+
+#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++ )
                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] = get_last_path_component ( xstrdup ( shell ));
-       
+
+       args [0] = bb_get_last_path_component ( bb_xstrdup ( shell ));
+
        if ( loginshell ) {
                char *args0;
-               bb_asprintf ( &args0, "-%s", args [0] );
+               bb_xasprintf ( &args0, "-%s", args [0] );
                args [0] = args0;
        }
-    
+
        if ( command ) {
                args [argno++] = "-c";
                args [argno++] = command;
@@ -74,7 +99,12 @@ void run_shell ( const char *shell, int loginshell, const char *command, const c
                        args [argno++] = *additional_args;
        }
        args [argno] = 0;
-       execv ( shell, (char **) args );
-       perror_msg_and_die ( "cannot run %s", shell );
+#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 );
 }
-