1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include <selinux/context.h>
54#include <selinux/flask.h>
55
56#include "libbb.h"
57
58static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
59 char *command, int compute_trans)
60{
61 context_t con;
62 security_context_t cur_context;
63
64 if (getcon(&cur_context))
65 bb_error_msg_and_die("can't get current context");
66
67 if (compute_trans) {
68 security_context_t file_context, new_context;
69
70 if (getfilecon(command, &file_context) < 0)
71 bb_error_msg_and_die("can't retrieve attributes of '%s'",
72 command);
73 if (security_compute_create(cur_context, file_context,
74 SECCLASS_PROCESS, &new_context))
75 bb_error_msg_and_die("unable to compute a new context");
76 cur_context = new_context;
77 }
78
79 con = context_new(cur_context);
80 if (!con)
81 bb_error_msg_and_die("'%s' is not a valid context", cur_context);
82 if (user && context_user_set(con, user))
83 bb_error_msg_and_die("can't set new user '%s'", user);
84 if (type && context_type_set(con, type))
85 bb_error_msg_and_die("can't set new type '%s'", type);
86 if (range && context_range_set(con, range))
87 bb_error_msg_and_die("can't set new range '%s'", range);
88 if (role && context_role_set(con, role))
89 bb_error_msg_and_die("can't set new role '%s'", role);
90
91 return con;
92}
93
94#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
95static const char runcon_longopts[] ALIGN1 =
96 "user\0" Required_argument "u"
97 "role\0" Required_argument "r"
98 "type\0" Required_argument "t"
99 "range\0" Required_argument "l"
100 "compute\0" No_argument "c"
101 "help\0" No_argument "h"
102 ;
103#endif
104
105#define OPTS_ROLE (1<<0)
106#define OPTS_TYPE (1<<1)
107#define OPTS_USER (1<<2)
108#define OPTS_RANGE (1<<3)
109#define OPTS_COMPUTE (1<<4)
110#define OPTS_HELP (1<<5)
111#define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
112
113int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
114int runcon_main(int argc UNUSED_PARAM, char **argv)
115{
116 char *role = NULL;
117 char *range = NULL;
118 char *user = NULL;
119 char *type = NULL;
120 char *context = NULL;
121 unsigned opts;
122 context_t con;
123
124 selinux_or_die();
125
126#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
127 applet_long_options = runcon_longopts;
128#endif
129 opt_complementary = "-1";
130 opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
131 argv += optind;
132
133 if (!(opts & OPTS_CONTEXT_COMPONENT)) {
134 context = *argv++;
135 if (!argv[0])
136 bb_error_msg_and_die("no command given");
137 }
138
139 if (context) {
140 con = context_new(context);
141 if (!con)
142 bb_error_msg_and_die("'%s' is not a valid context", context);
143 } else {
144 con = runcon_compute_new_context(user, role, type, range,
145 argv[0], opts & OPTS_COMPUTE);
146 }
147
148 if (security_check_context(context_str(con)))
149 bb_error_msg_and_die("'%s' is not a valid context",
150 context_str(con));
151
152 if (setexeccon(context_str(con)))
153 bb_error_msg_and_die("can't set up security context '%s'",
154 context_str(con));
155
156 BB_EXECVP_or_die(argv);
157}
158