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
54#include <selinux/context.h>
55
56#undef SECCLASS_PROCESS
57#define SECCLASS_PROCESS 2
58
59#include "libbb.h"
60
61static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
62 char *command, int compute_trans)
63{
64 context_t con;
65 security_context_t cur_context;
66
67 if (getcon(&cur_context))
68 bb_simple_error_msg_and_die("can't get current context");
69
70 if (compute_trans) {
71 security_context_t file_context, new_context;
72
73 if (getfilecon(command, &file_context) < 0)
74 bb_error_msg_and_die("can't retrieve attributes of '%s'",
75 command);
76 if (security_compute_create(cur_context, file_context,
77 SECCLASS_PROCESS, &new_context))
78 bb_simple_error_msg_and_die("unable to compute a new context");
79 cur_context = new_context;
80 }
81
82 con = context_new(cur_context);
83 if (!con)
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);
93
94 return con;
95}
96
97#if ENABLE_LONG_OPTS
98static 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"
105 ;
106#endif
107
108#define OPTS_ROLE (1<<0)
109#define OPTS_TYPE (1<<1)
110#define OPTS_USER (1<<2)
111#define OPTS_RANGE (1<<3)
112#define OPTS_COMPUTE (1<<4)
113#define OPTS_HELP (1<<5)
114#define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
115
116int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
117int runcon_main(int argc UNUSED_PARAM, char **argv)
118{
119 char *role = NULL;
120 char *range = NULL;
121 char *user = NULL;
122 char *type = NULL;
123 char *context = NULL;
124 unsigned opts;
125 context_t con;
126
127 selinux_or_die();
128
129 opts = getopt32long(argv, "^"
130 "r:t:u:l:ch"
131 "\0" "-1",
132 runcon_longopts,
133 &role, &type, &user, &range
134 );
135 argv += optind;
136
137 if (!(opts & OPTS_CONTEXT_COMPONENT)) {
138 context = *argv++;
139 if (!argv[0])
140 bb_simple_error_msg_and_die("no command given");
141 }
142
143 if (context) {
144 con = context_new(context);
145 if (!con)
146 bb_error_msg_and_die("'%s' is not a valid context", context);
147 } else {
148 con = runcon_compute_new_context(user, role, type, range,
149 argv[0], opts & OPTS_COMPUTE);
150 }
151
152 if (security_check_context(context_str(con)))
153 bb_error_msg_and_die("'%s' is not a valid context",
154 context_str(con));
155
156 if (setexeccon(context_str(con)))
157 bb_error_msg_and_die("can't set up security context '%s'",
158 context_str(con));
159
160 BB_EXECVP_or_die(argv);
161}
162