Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 28 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 29 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 30 | */ |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 31 | //config:config RUNCON |
| 32 | //config: bool "runcon" |
| 33 | //config: default n |
| 34 | //config: depends on SELINUX |
| 35 | //config: help |
| 36 | //config: Enable support to run command in specified security context. |
| 37 | //config: |
| 38 | //config:config FEATURE_RUNCON_LONG_OPTIONS |
| 39 | //config: bool "Enable long options" |
| 40 | //config: default y |
| 41 | //config: depends on RUNCON && LONG_OPTS |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 42 | |
| 43 | //applet:IF_RUNCON(APPLET(runcon, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 44 | |
| 45 | //kbuild:lib-$(CONFIG_RUNCON) += runcon.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 46 | |
| 47 | //usage:#define runcon_trivial_usage |
| 48 | //usage: "[-c] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] PROG ARGS\n" |
| 49 | //usage: "runcon CONTEXT PROG ARGS" |
| 50 | //usage:#define runcon_full_usage "\n\n" |
| 51 | //usage: "Run PROG in a different security context\n" |
| 52 | //usage: "\n CONTEXT Complete security context\n" |
| 53 | //usage: IF_FEATURE_RUNCON_LONG_OPTIONS( |
| 54 | //usage: "\n -c,--compute Compute process transition context before modifying" |
Denys Vlasenko | bbc7bee | 2017-01-21 02:49:58 +0100 | [diff] [blame] | 55 | //usage: "\n -t,--type TYPE Type (for same role as parent)" |
| 56 | //usage: "\n -u,--user USER User identity" |
| 57 | //usage: "\n -r,--role ROLE Role" |
| 58 | //usage: "\n -l,--range RNG Levelrange" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 59 | //usage: ) |
| 60 | //usage: IF_NOT_FEATURE_RUNCON_LONG_OPTIONS( |
| 61 | //usage: "\n -c Compute process transition context before modifying" |
| 62 | //usage: "\n -t TYPE Type (for same role as parent)" |
| 63 | //usage: "\n -u USER User identity" |
| 64 | //usage: "\n -r ROLE Role" |
| 65 | //usage: "\n -l RNG Levelrange" |
| 66 | //usage: ) |
| 67 | |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 68 | #include <selinux/context.h> |
Denys Vlasenko | 4ea0ebd | 2016-12-23 13:52:13 +0100 | [diff] [blame] | 69 | /* from deprecated <selinux/flask.h>: */ |
| 70 | #undef SECCLASS_PROCESS |
| 71 | #define SECCLASS_PROCESS 2 |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 73 | #include "libbb.h" |
| 74 | |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 75 | static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 76 | char *command, int compute_trans) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 77 | { |
| 78 | context_t con; |
| 79 | security_context_t cur_context; |
| 80 | |
| 81 | if (getcon(&cur_context)) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 82 | bb_error_msg_and_die("can't get current context"); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 83 | |
| 84 | if (compute_trans) { |
| 85 | security_context_t file_context, new_context; |
| 86 | |
| 87 | if (getfilecon(command, &file_context) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 88 | bb_error_msg_and_die("can't retrieve attributes of '%s'", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 89 | command); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 90 | if (security_compute_create(cur_context, file_context, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 91 | SECCLASS_PROCESS, &new_context)) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 92 | bb_error_msg_and_die("unable to compute a new context"); |
| 93 | cur_context = new_context; |
| 94 | } |
| 95 | |
| 96 | con = context_new(cur_context); |
| 97 | if (!con) |
| 98 | bb_error_msg_and_die("'%s' is not a valid context", cur_context); |
| 99 | if (user && context_user_set(con, user)) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 100 | bb_error_msg_and_die("can't set new user '%s'", user); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 101 | if (type && context_type_set(con, type)) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 102 | bb_error_msg_and_die("can't set new type '%s'", type); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 103 | if (range && context_range_set(con, range)) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 104 | bb_error_msg_and_die("can't set new range '%s'", range); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 105 | if (role && context_role_set(con, role)) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 106 | bb_error_msg_and_die("can't set new role '%s'", role); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 107 | |
| 108 | return con; |
| 109 | } |
| 110 | |
| 111 | #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 112 | static const char runcon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 113 | "user\0" Required_argument "u" |
| 114 | "role\0" Required_argument "r" |
| 115 | "type\0" Required_argument "t" |
| 116 | "range\0" Required_argument "l" |
| 117 | "compute\0" No_argument "c" |
Denis Vlasenko | 319f8eb | 2007-08-13 11:09:30 +0000 | [diff] [blame] | 118 | "help\0" No_argument "h" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 119 | ; |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | #define OPTS_ROLE (1<<0) /* r */ |
| 123 | #define OPTS_TYPE (1<<1) /* t */ |
| 124 | #define OPTS_USER (1<<2) /* u */ |
| 125 | #define OPTS_RANGE (1<<3) /* l */ |
| 126 | #define OPTS_COMPUTE (1<<4) /* c */ |
| 127 | #define OPTS_HELP (1<<5) /* h */ |
| 128 | #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE) |
| 129 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 130 | int runcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 131 | int runcon_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 132 | { |
| 133 | char *role = NULL; |
| 134 | char *range = NULL; |
| 135 | char *user = NULL; |
| 136 | char *type = NULL; |
| 137 | char *context = NULL; |
| 138 | unsigned opts; |
| 139 | context_t con; |
| 140 | |
| 141 | selinux_or_die(); |
| 142 | |
| 143 | #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 144 | applet_long_options = runcon_longopts; |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 145 | #endif |
| 146 | opt_complementary = "-1"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 147 | opts = getopt32(argv, "r:t:u:l:ch", &role, &type, &user, &range); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 148 | argv += optind; |
| 149 | |
| 150 | if (!(opts & OPTS_CONTEXT_COMPONENT)) { |
| 151 | context = *argv++; |
| 152 | if (!argv[0]) |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 153 | bb_error_msg_and_die("no command given"); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (context) { |
| 157 | con = context_new(context); |
| 158 | if (!con) |
| 159 | bb_error_msg_and_die("'%s' is not a valid context", context); |
| 160 | } else { |
| 161 | con = runcon_compute_new_context(user, role, type, range, |
| 162 | argv[0], opts & OPTS_COMPUTE); |
| 163 | } |
| 164 | |
| 165 | if (security_check_context(context_str(con))) |
| 166 | bb_error_msg_and_die("'%s' is not a valid context", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 167 | context_str(con)); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 168 | |
| 169 | if (setexeccon(context_str(con))) |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 170 | bb_error_msg_and_die("can't set up security context '%s'", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 171 | context_str(con)); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 172 | |
Denys Vlasenko | 1c31e9e | 2010-11-28 04:34:09 +0100 | [diff] [blame] | 173 | BB_EXECVP_or_die(argv); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 174 | } |