1 /* runcon -- run command with specified security context
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 * | ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
20 * command [arg1 [arg2 ...] ]
22 * attempt to run the specified command with the specified context.
24 * -r role : use the current context with the specified role
25 * -t type : use the current context with the specified type
26 * -u user : use the current context with the specified user
27 * -l level : use the current context with the specified level range
28 * -c : compute process transition context before modifying
30 * Contexts are interpreted as follows:
39 * 4 Y user:role:type:range
46 #include <selinux/selinux.h>
47 #include <selinux/context.h>
48 #include <sys/types.h>
54 /* The official name of this program (e.g., no 'g' prefix). */
55 #define PROGRAM_NAME "runcon"
57 #define AUTHORS proper_name ("Russell Coker")
59 static struct option
const long_options
[] =
61 {"role", required_argument
, NULL
, 'r'},
62 {"type", required_argument
, NULL
, 't'},
63 {"user", required_argument
, NULL
, 'u'},
64 {"range", required_argument
, NULL
, 'l'},
65 {"compute", no_argument
, NULL
, 'c'},
66 {GETOPT_HELP_OPTION_DECL
},
67 {GETOPT_VERSION_OPTION_DECL
},
74 if (status
!= EXIT_SUCCESS
)
79 Usage: %s CONTEXT COMMAND [args]\n\
80 or: %s [ -c ] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] COMMAND [args]\n\
81 "), program_name
, program_name
);
83 Run a program in a different SELinux security context.\n\
84 With neither CONTEXT nor COMMAND, print the current security context.\n\
87 emit_mandatory_arg_note ();
90 CONTEXT Complete security context\n\
91 -c, --compute compute process transition context before modifying\n\
92 -t, --type=TYPE type (for same role as parent)\n\
93 -u, --user=USER user identity\n\
94 -r, --role=ROLE role\n\
95 -l, --range=RANGE levelrange\n\
98 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
99 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
100 emit_ancillary_info (PROGRAM_NAME
);
106 main (int argc
, char **argv
)
112 char *context
= NULL
;
113 char *cur_context
= NULL
;
114 char *file_context
= NULL
;
115 char *new_context
= NULL
;
116 bool compute_trans
= false;
120 initialize_main (&argc
, &argv
);
121 set_program_name (argv
[0]);
122 setlocale (LC_ALL
, "");
123 bindtextdomain (PACKAGE
, LOCALEDIR
);
124 textdomain (PACKAGE
);
126 atexit (close_stdout
);
130 int option_index
= 0;
131 int c
= getopt_long (argc
, argv
, "+r:t:u:l:c", long_options
,
139 die (EXIT_FAILURE
, 0, _("multiple roles"));
144 die (EXIT_FAILURE
, 0, _("multiple types"));
149 die (EXIT_FAILURE
, 0, _("multiple users"));
154 die (EXIT_FAILURE
, 0, _("multiple levelranges"));
158 compute_trans
= true;
161 case_GETOPT_HELP_CHAR
;
162 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
164 usage (EXIT_FAILURE
);
169 if (argc
- optind
== 0)
171 if (getcon (&cur_context
) < 0)
172 die (EXIT_FAILURE
, errno
, _("failed to get current context"));
173 fputs (cur_context
, stdout
);
174 fputc ('\n', stdout
);
178 if (!(user
|| role
|| type
|| range
|| compute_trans
))
182 error (0, 0, _("you must specify -c, -t, -u, -l, -r, or context"));
183 usage (EXIT_FAILURE
);
185 context
= argv
[optind
++];
190 error (0, 0, _("no command specified"));
191 usage (EXIT_FAILURE
);
194 if (is_selinux_enabled () != 1)
195 die (EXIT_FAILURE
, 0, _("%s may be used only on a SELinux kernel"),
200 con
= context_new (context
);
202 die (EXIT_FAILURE
, errno
, _("failed to create security context: %s"),
207 if (getcon (&cur_context
) < 0)
208 die (EXIT_FAILURE
, errno
, _("failed to get current context"));
210 /* We will generate context based on process transition */
213 /* Get context of file to be executed */
214 if (getfilecon (argv
[optind
], &file_context
) == -1)
215 die (EXIT_FAILURE
, errno
,
216 _("failed to get security context of %s"),
217 quoteaf (argv
[optind
]));
218 /* compute result of process transition */
219 if (security_compute_create (cur_context
, file_context
,
220 string_to_security_class ("process"),
222 die (EXIT_FAILURE
, errno
, _("failed to compute a new context"));
224 freecon (file_context
);
225 freecon (cur_context
);
227 /* set cur_context equal to new_context */
228 cur_context
= new_context
;
231 con
= context_new (cur_context
);
233 die (EXIT_FAILURE
, errno
, _("failed to create security context: %s"),
234 quote (cur_context
));
235 if (user
&& context_user_set (con
, user
))
236 die (EXIT_FAILURE
, errno
, _("failed to set new user: %s"),
238 if (type
&& context_type_set (con
, type
))
239 die (EXIT_FAILURE
, errno
, _("failed to set new type: %s"),
241 if (range
&& context_range_set (con
, range
))
242 die (EXIT_FAILURE
, errno
, _("failed to set new range: %s"),
244 if (role
&& context_role_set (con
, role
))
245 die (EXIT_FAILURE
, errno
, _("failed to set new role: %s"),
249 if (security_check_context (context_str (con
)) < 0)
250 die (EXIT_FAILURE
, errno
, _("invalid context: %s"),
251 quote (context_str (con
)));
253 if (setexeccon (context_str (con
)) != 0)
254 die (EXIT_FAILURE
, errno
, _("unable to set security context %s"),
255 quote (context_str (con
)));
256 if (cur_context
!= NULL
)
257 freecon (cur_context
);
259 execvp (argv
[optind
], argv
+ optind
);
261 int exit_status
= errno
== ENOENT
? EXIT_ENOENT
: EXIT_CANNOT_INVOKE
;
262 error (0, errno
, "%s", quote (argv
[optind
]));