1 /* runcon -- run command with specified security context
2 Copyright (C) 2005-2013 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 #ifdef HAVE_SELINUX_FLASK_H
49 # include <selinux/flask.h>
51 # define SECCLASS_PROCESS 0
53 #include <sys/types.h>
59 /* The official name of this program (e.g., no 'g' prefix). */
60 #define PROGRAM_NAME "runcon"
62 #define AUTHORS proper_name ("Russell Coker")
64 static struct option
const long_options
[] =
66 {"role", required_argument
, NULL
, 'r'},
67 {"type", required_argument
, NULL
, 't'},
68 {"user", required_argument
, NULL
, 'u'},
69 {"range", required_argument
, NULL
, 'l'},
70 {"compute", no_argument
, NULL
, 'c'},
71 {GETOPT_HELP_OPTION_DECL
},
72 {GETOPT_VERSION_OPTION_DECL
},
79 if (status
!= EXIT_SUCCESS
)
84 Usage: %s CONTEXT COMMAND [args]\n\
85 or: %s [ -c ] [-u USER] [-r ROLE] [-t TYPE] [-l RANGE] COMMAND [args]\n\
86 "), program_name
, program_name
);
88 Run a program in a different SELinux security context.\n\
89 With neither CONTEXT nor COMMAND, print the current security context.\n\
92 emit_mandatory_arg_note ();
95 CONTEXT Complete security context\n\
96 -c, --compute compute process transition context before modifying\n\
97 -t, --type=TYPE type (for same role as parent)\n\
98 -u, --user=USER user identity\n\
99 -r, --role=ROLE role\n\
100 -l, --range=RANGE levelrange\n\
103 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
104 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
105 emit_ancillary_info ();
111 main (int argc
, char **argv
)
117 char *context
= NULL
;
118 security_context_t cur_context
= NULL
;
119 security_context_t file_context
= NULL
;
120 security_context_t new_context
= NULL
;
121 bool compute_trans
= false;
125 initialize_main (&argc
, &argv
);
126 set_program_name (argv
[0]);
127 setlocale (LC_ALL
, "");
128 bindtextdomain (PACKAGE
, LOCALEDIR
);
129 textdomain (PACKAGE
);
131 atexit (close_stdout
);
135 int option_index
= 0;
136 int c
= getopt_long (argc
, argv
, "+r:t:u:l:c", long_options
,
144 error (EXIT_FAILURE
, 0, _("multiple roles"));
149 error (EXIT_FAILURE
, 0, _("multiple types"));
154 error (EXIT_FAILURE
, 0, _("multiple users"));
159 error (EXIT_FAILURE
, 0, _("multiple levelranges"));
163 compute_trans
= true;
166 case_GETOPT_HELP_CHAR
;
167 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
169 usage (EXIT_FAILURE
);
174 if (argc
- optind
== 0)
176 if (getcon (&cur_context
) < 0)
177 error (EXIT_FAILURE
, errno
, _("failed to get current context"));
178 fputs (cur_context
, stdout
);
179 fputc ('\n', stdout
);
183 if (!(user
|| role
|| type
|| range
|| compute_trans
))
187 error (0, 0, _("you must specify -c, -t, -u, -l, -r, or context"));
188 usage (EXIT_FAILURE
);
190 context
= argv
[optind
++];
195 error (0, 0, _("no command specified"));
196 usage (EXIT_FAILURE
);
199 if (is_selinux_enabled () != 1)
200 error (EXIT_FAILURE
, 0, _("%s may be used only on a SELinux kernel"),
205 con
= context_new (context
);
207 error (EXIT_FAILURE
, errno
, _("failed to create security context: %s"),
208 quotearg_colon (context
));
212 if (getcon (&cur_context
) < 0)
213 error (EXIT_FAILURE
, errno
, _("failed to get current context"));
215 /* We will generate context based on process transition */
218 /* Get context of file to be executed */
219 if (getfilecon (argv
[optind
], &file_context
) == -1)
220 error (EXIT_FAILURE
, errno
,
221 _("failed to get security context of %s"),
222 quote (argv
[optind
]));
223 /* compute result of process transition */
224 if (security_compute_create (cur_context
, file_context
,
225 SECCLASS_PROCESS
, &new_context
) != 0)
226 error (EXIT_FAILURE
, errno
, _("failed to compute a new context"));
228 freecon (file_context
);
229 freecon (cur_context
);
231 /* set cur_context equal to new_context */
232 cur_context
= new_context
;
235 con
= context_new (cur_context
);
237 error (EXIT_FAILURE
, errno
, _("failed to create security context: %s"),
238 quotearg_colon (cur_context
));
239 if (user
&& context_user_set (con
, user
))
240 error (EXIT_FAILURE
, errno
, _("failed to set new user %s"), user
);
241 if (type
&& context_type_set (con
, type
))
242 error (EXIT_FAILURE
, errno
, _("failed to set new type %s"), type
);
243 if (range
&& context_range_set (con
, range
))
244 error (EXIT_FAILURE
, errno
, _("failed to set new range %s"), range
);
245 if (role
&& context_role_set (con
, role
))
246 error (EXIT_FAILURE
, errno
, _("failed to set new role %s"), role
);
249 if (security_check_context (context_str (con
)) < 0)
250 error (EXIT_FAILURE
, errno
, _("invalid context: %s"),
251 quotearg_colon (context_str (con
)));
253 if (setexeccon (context_str (con
)) != 0)
254 error (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
);
262 int exit_status
= (errno
== ENOENT
? EXIT_ENOENT
: EXIT_CANNOT_INVOKE
);
263 error (0, errno
, "%s", argv
[optind
]);