3 * ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
4 * command [arg1 [arg2 ...] ]
6 * attempt to run the specified command with the specified context.
8 * -r role : use the current context with the specified role
9 * -t type : use the current context with the specified type
10 * -u user : use the current context with the specified user
11 * -l level : use the current context with the specified level range
12 * -c : compute process transition context before modifying
14 * Contexts are interpreted as follows:
23 * 4 Y user:role:type:range
26 * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
27 * - based on coreutils-5.97 (in Fedora Core 6)
29 * Licensed under GPLv2, see file LICENSE in this source tree.
31 //config:config RUNCON
32 //config: bool "runcon (6.6 kb)"
34 //config: depends on SELINUX
36 //config: Enable support to run command in specified security context.
38 //applet:IF_RUNCON(APPLET(runcon, BB_DIR_USR_BIN, BB_SUID_DROP))
40 //kbuild:lib-$(CONFIG_RUNCON) += runcon.o
42 //usage:#define runcon_trivial_usage
43 //usage: "[-c] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] PROG ARGS\n"
44 //usage: "runcon CONTEXT PROG ARGS"
45 //usage:#define runcon_full_usage "\n\n"
46 //usage: "Run PROG in a different security context\n"
47 //usage: "\n CONTEXT Complete security context\n"
48 //usage: "\n -c Compute process transition context before modifying"
49 //usage: "\n -t TYPE Type (for same role as parent)"
50 //usage: "\n -u USER User identity"
51 //usage: "\n -r ROLE Role"
52 //usage: "\n -l RNG Levelrange"
54 #include <selinux/context.h>
55 /* from deprecated <selinux/flask.h>: */
56 #undef SECCLASS_PROCESS
57 #define SECCLASS_PROCESS 2
61 static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
62 char *command, int compute_trans)
65 security_context_t cur_context;
67 if (getcon(&cur_context))
68 bb_error_msg_and_die("can't get current context");
71 security_context_t file_context, new_context;
73 if (getfilecon(command, &file_context) < 0)
74 bb_error_msg_and_die("can't retrieve attributes of '%s'",
76 if (security_compute_create(cur_context, file_context,
77 SECCLASS_PROCESS, &new_context))
78 bb_error_msg_and_die("unable to compute a new context");
79 cur_context = new_context;
82 con = context_new(cur_context);
84 bb_error_msg_and_die("'%s' is not a valid context", cur_context);
85 if (user && context_user_set(con, user))
86 bb_error_msg_and_die("can't set new user '%s'", user);
87 if (type && context_type_set(con, type))
88 bb_error_msg_and_die("can't set new type '%s'", type);
89 if (range && context_range_set(con, range))
90 bb_error_msg_and_die("can't set new range '%s'", range);
91 if (role && context_role_set(con, role))
92 bb_error_msg_and_die("can't set new role '%s'", role);
98 static const char runcon_longopts[] ALIGN1 =
99 "user\0" Required_argument "u"
100 "role\0" Required_argument "r"
101 "type\0" Required_argument "t"
102 "range\0" Required_argument "l"
103 "compute\0" No_argument "c"
104 "help\0" No_argument "h"
108 #define OPTS_ROLE (1<<0) /* r */
109 #define OPTS_TYPE (1<<1) /* t */
110 #define OPTS_USER (1<<2) /* u */
111 #define OPTS_RANGE (1<<3) /* l */
112 #define OPTS_COMPUTE (1<<4) /* c */
113 #define OPTS_HELP (1<<5) /* h */
114 #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
116 int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
117 int runcon_main(int argc UNUSED_PARAM, char **argv)
123 char *context = NULL;
129 opts = getopt32long(argv, "^"
133 &role, &type, &user, &range
137 if (!(opts & OPTS_CONTEXT_COMPONENT)) {
140 bb_error_msg_and_die("no command given");
144 con = context_new(context);
146 bb_error_msg_and_die("'%s' is not a valid context", context);
148 con = runcon_compute_new_context(user, role, type, range,
149 argv[0], opts & OPTS_COMPUTE);
152 if (security_check_context(context_str(con)))
153 bb_error_msg_and_die("'%s' is not a valid context",
156 if (setexeccon(context_str(con)))
157 bb_error_msg_and_die("can't set up security context '%s'",
160 BB_EXECVP_or_die(argv);