blob: 60d41000ccf7ee45d138e3103933af6df73d41a7 [file] [log] [blame]
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00001/*
2 * runcon [ context |
3 * ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
4 * command [arg1 [arg2 ...] ]
5 *
6 * attempt to run the specified command with the specified context.
7 *
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
13 *
14 * Contexts are interpreted as follows:
15 *
16 * Number of MLS
17 * components system?
18 *
19 * 1 - type
20 * 2 - role:type
21 * 3 Y role:type:range
22 * 3 N user:role:type
23 * 4 Y user:role:type:range
24 * 4 N error
25 *
26 * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
27 * - based on coreutils-5.97 (in Fedora Core 6)
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000028 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020029 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000030 */
Pere Orga5bc8c002011-04-11 03:29:49 +020031
32//usage:#define runcon_trivial_usage
maxwen27116ba2015-08-14 21:41:28 +020033//usage: "[-c] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] PROG -- ARGS\n"
34//usage: "runcon CONTEXT PROG -- ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020035//usage:#define runcon_full_usage "\n\n"
36//usage: "Run PROG in a different security context\n"
37//usage: "\n CONTEXT Complete security context\n"
38//usage: IF_FEATURE_RUNCON_LONG_OPTIONS(
39//usage: "\n -c,--compute Compute process transition context before modifying"
40//usage: "\n -t,--type=TYPE Type (for same role as parent)"
41//usage: "\n -u,--user=USER User identity"
42//usage: "\n -r,--role=ROLE Role"
43//usage: "\n -l,--range=RNG Levelrange"
44//usage: )
45//usage: IF_NOT_FEATURE_RUNCON_LONG_OPTIONS(
46//usage: "\n -c Compute process transition context before modifying"
47//usage: "\n -t TYPE Type (for same role as parent)"
48//usage: "\n -u USER User identity"
49//usage: "\n -r ROLE Role"
50//usage: "\n -l RNG Levelrange"
51//usage: )
52
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000053#include <selinux/context.h>
maxwen27116ba2015-08-14 21:41:28 +020054#ifndef ANDROID
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000055#include <selinux/flask.h>
maxwen27116ba2015-08-14 21:41:28 +020056#endif
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000057
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000058#include "libbb.h"
59
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000060static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
Tanguy Pruvot823694d2012-11-18 13:20:29 +010061 char *command, int compute_trans)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000062{
63 context_t con;
64 security_context_t cur_context;
65
66 if (getcon(&cur_context))
Denys Vlasenko6331cf02009-11-13 09:08:27 +010067 bb_error_msg_and_die("can't get current context");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000068
69 if (compute_trans) {
70 security_context_t file_context, new_context;
71
72 if (getfilecon(command, &file_context) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010073 bb_error_msg_and_die("can't retrieve attributes of '%s'",
Tanguy Pruvot823694d2012-11-18 13:20:29 +010074 command);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000075 if (security_compute_create(cur_context, file_context,
Tanguy Pruvot823694d2012-11-18 13:20:29 +010076 SECCLASS_PROCESS, &new_context))
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000077 bb_error_msg_and_die("unable to compute a new context");
78 cur_context = new_context;
79 }
80
81 con = context_new(cur_context);
82 if (!con)
83 bb_error_msg_and_die("'%s' is not a valid context", cur_context);
84 if (user && context_user_set(con, user))
Denys Vlasenko651a2692010-03-23 16:25:17 +010085 bb_error_msg_and_die("can't set new user '%s'", user);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000086 if (type && context_type_set(con, type))
Denys Vlasenko651a2692010-03-23 16:25:17 +010087 bb_error_msg_and_die("can't set new type '%s'", type);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000088 if (range && context_range_set(con, range))
Denys Vlasenko651a2692010-03-23 16:25:17 +010089 bb_error_msg_and_die("can't set new range '%s'", range);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000090 if (role && context_role_set(con, role))
Denys Vlasenko651a2692010-03-23 16:25:17 +010091 bb_error_msg_and_die("can't set new role '%s'", role);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000092
93 return con;
94}
95
96#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000097static const char runcon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000098 "user\0" Required_argument "u"
99 "role\0" Required_argument "r"
100 "type\0" Required_argument "t"
101 "range\0" Required_argument "l"
102 "compute\0" No_argument "c"
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000103 "help\0" No_argument "h"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000104 ;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000105#endif
106
107#define OPTS_ROLE (1<<0) /* r */
108#define OPTS_TYPE (1<<1) /* t */
109#define OPTS_USER (1<<2) /* u */
110#define OPTS_RANGE (1<<3) /* l */
111#define OPTS_COMPUTE (1<<4) /* c */
112#define OPTS_HELP (1<<5) /* h */
113#define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
114
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000115int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000116int runcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000117{
118 char *role = NULL;
119 char *range = NULL;
120 char *user = NULL;
121 char *type = NULL;
122 char *context = NULL;
123 unsigned opts;
124 context_t con;
125
126 selinux_or_die();
127
128#if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000129 applet_long_options = runcon_longopts;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000130#endif
131 opt_complementary = "-1";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000132 opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000133 argv += optind;
134
135 if (!(opts & OPTS_CONTEXT_COMPONENT)) {
136 context = *argv++;
137 if (!argv[0])
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000138 bb_error_msg_and_die("no command given");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000139 }
140
141 if (context) {
142 con = context_new(context);
143 if (!con)
144 bb_error_msg_and_die("'%s' is not a valid context", context);
145 } else {
146 con = runcon_compute_new_context(user, role, type, range,
147 argv[0], opts & OPTS_COMPUTE);
148 }
149
150 if (security_check_context(context_str(con)))
151 bb_error_msg_and_die("'%s' is not a valid context",
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100152 context_str(con));
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000153
154 if (setexeccon(context_str(con)))
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000155 bb_error_msg_and_die("can't set up security context '%s'",
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100156 context_str(con));
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000157
Denys Vlasenko1c31e9e2010-11-28 04:34:09 +0100158 BB_EXECVP_or_die(argv);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000159}