2 * Copyright (c) 2009-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3 * Copyright (c) 2008 Dan Walsh <dwalsh@redhat.com>
5 * Borrowed heavily from newrole source code
9 * Steve Grubb <sgrubb@redhat.com>
10 * Darrel Goeddel <DGoeddel@trustedcs.com>
11 * Michael Thompson <mcthomps@us.ibm.com>
12 * Dan Walsh <dwalsh@redhat.com>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <sys/types.h>
40 #include <selinux/flask.h> /* for SECCLASS_CHR_FILE */
41 #include <selinux/selinux.h> /* for is_selinux_enabled() */
42 #include <selinux/context.h> /* for context-mangling functions */
43 #include <selinux/get_default_type.h>
44 #include <selinux/get_context_list.h>
46 #ifdef HAVE_LINUX_AUDIT
47 # include <libaudit.h>
52 static struct selinux_state {
53 security_context_t old_context;
54 security_context_t new_context;
55 security_context_t tty_context;
56 security_context_t new_tty_context;
62 #ifdef HAVE_LINUX_AUDIT
64 audit_role_change(const security_context_t old_context,
65 const security_context_t new_context, const char *ttyn)
69 debug_decl(audit_role_change, SUDO_DEBUG_SELINUX)
73 /* Kernel may not have audit support. */
74 if (errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT
76 error(1, _("unable to open audit system"));
78 /* audit role change using the same format as newrole(1) */
79 easprintf(&message, "newrole: old-context=%s new-context=%s",
80 old_context, new_context);
81 rc = audit_log_user_message(au_fd, AUDIT_USER_ROLE_CHANGE,
82 message, NULL, NULL, ttyn, 1);
84 warning(_("unable to send audit message"));
94 * This function attempts to revert the relabeling done to the tty.
95 * fd - referencing the opened ttyn
96 * ttyn - name of tty to restore
98 * Returns zero on success, non-zero otherwise
101 selinux_restore_tty(void)
104 security_context_t chk_tty_context = NULL;
105 debug_decl(selinux_restore_tty, SUDO_DEBUG_SELINUX)
107 if (se_state.ttyfd == -1 || se_state.new_tty_context == NULL)
110 /* Verify that the tty still has the context set by sudo. */
111 if ((retval = fgetfilecon(se_state.ttyfd, &chk_tty_context)) < 0) {
112 warning(_("unable to fgetfilecon %s"), se_state.ttyn);
116 if ((retval = strcmp(chk_tty_context, se_state.new_tty_context))) {
117 warningx(_("%s changed labels"), se_state.ttyn);
121 if ((retval = fsetfilecon(se_state.ttyfd, se_state.tty_context)) < 0)
122 warning(_("unable to restore context for %s"), se_state.ttyn);
125 if (se_state.ttyfd != -1) {
126 close(se_state.ttyfd);
129 if (chk_tty_context != NULL) {
130 freecon(chk_tty_context);
131 chk_tty_context = NULL;
133 debug_return_int(retval);
137 * This function attempts to relabel the tty. If this function fails, then
138 * the contexts are free'd and -1 is returned. On success, 0 is returned
139 * and tty_context and new_tty_context are set.
141 * This function will not fail if it can not relabel the tty when selinux is
142 * in permissive mode.
145 relabel_tty(const char *ttyn, int ptyfd)
147 security_context_t tty_con = NULL;
148 security_context_t new_tty_con = NULL;
150 debug_decl(relabel_tty, SUDO_DEBUG_SELINUX)
152 se_state.ttyfd = ptyfd;
154 /* It is perfectly legal to have no tty. */
155 if (ptyfd == -1 && ttyn == NULL)
158 /* If sudo is not allocating a pty for the command, open current tty. */
160 se_state.ttyfd = open(ttyn, O_RDWR|O_NONBLOCK);
161 if (se_state.ttyfd == -1) {
162 warning(_("unable to open %s, not relabeling tty"), ttyn);
163 if (se_state.enforcing)
166 (void)fcntl(se_state.ttyfd, F_SETFL,
167 fcntl(se_state.ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
170 if (fgetfilecon(se_state.ttyfd, &tty_con) < 0) {
171 warning(_("unable to get current tty context, not relabeling tty"));
172 if (se_state.enforcing)
176 if (tty_con && (security_compute_relabel(se_state.new_context, tty_con,
177 SECCLASS_CHR_FILE, &new_tty_con) < 0)) {
178 warning(_("unable to get new tty context, not relabeling tty"));
179 if (se_state.enforcing)
183 if (new_tty_con != NULL) {
184 if (fsetfilecon(se_state.ttyfd, new_tty_con) < 0) {
185 warning(_("unable to set new tty context"));
186 if (se_state.enforcing)
192 /* Reopen pty that was relabeled, std{in,out,err} are reset later. */
193 se_state.ttyfd = open(ttyn, O_RDWR|O_NOCTTY, 0);
194 if (se_state.ttyfd == -1) {
195 warning(_("unable to open %s"), ttyn);
196 if (se_state.enforcing)
199 if (dup2(se_state.ttyfd, ptyfd) == -1) {
204 /* Re-open tty to get new label and reset std{in,out,err} */
205 close(se_state.ttyfd);
206 se_state.ttyfd = open(ttyn, O_RDWR|O_NONBLOCK);
207 if (se_state.ttyfd == -1) {
208 warning(_("unable to open %s"), ttyn);
211 (void)fcntl(se_state.ttyfd, F_SETFL,
212 fcntl(se_state.ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
213 for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
214 if (isatty(fd) && dup2(se_state.ttyfd, fd) == -1) {
220 /* Retain se_state.ttyfd so we can restore label when command finishes. */
221 (void)fcntl(se_state.ttyfd, F_SETFD, FD_CLOEXEC);
223 se_state.ttyn = ttyn;
224 se_state.tty_context = tty_con;
225 se_state.new_tty_context = new_tty_con;
229 if (se_state.ttyfd != -1 && se_state.ttyfd != ptyfd) {
230 close(se_state.ttyfd);
234 debug_return_int(-1);
238 * Returns a new security context based on the old context and the
239 * specified role and type.
242 get_exec_context(security_context_t old_context, const char *role, const char *type)
244 security_context_t new_context = NULL;
245 context_t context = NULL;
246 char *typebuf = NULL;
247 debug_decl(get_exec_context, SUDO_DEBUG_SELINUX)
249 /* We must have a role, the type is optional (we can use the default). */
251 warningx(_("you must specify a role for type %s"), type);
256 if (get_default_type(role, &typebuf)) {
257 warningx(_("unable to get default type for role %s"), role);
265 * Expand old_context into a context_t so that we extract and modify
266 * its components easily.
268 context = context_new(old_context);
271 * Replace the role and type in "context" with the role and
272 * type we will be running the command as.
274 if (context_role_set(context, role)) {
275 warning(_("failed to set new role %s"), role);
278 if (context_type_set(context, type)) {
279 warning(_("failed to set new type %s"), type);
284 * Convert "context" back into a string and verify it.
286 new_context = estrdup(context_str(context));
287 if (security_check_context(new_context) < 0) {
288 warningx(_("%s is not a valid context"), new_context);
294 warningx("Your new context is %s", new_context);
297 context_free(context);
298 debug_return_ptr(new_context);
302 context_free(context);
303 freecon(new_context);
304 debug_return_ptr(NULL);
308 * Set the exec and tty contexts in preparation for fork/exec.
309 * Must run as root, before the uid change.
310 * If ptyfd is not -1, it indicates we are running
311 * in a pty and do not need to reset std{in,out,err}.
312 * Returns 0 on success and -1 on failure.
315 selinux_setup(const char *role, const char *type, const char *ttyn,
319 debug_decl(selinux_setup, SUDO_DEBUG_SELINUX)
321 /* Store the caller's SID in old_context. */
322 if (getprevcon(&se_state.old_context)) {
323 warning(_("failed to get old_context"));
327 se_state.enforcing = security_getenforce();
328 if (se_state.enforcing < 0) {
329 warning(_("unable to determine enforcing mode."));
334 warningx("your old context was %s", se_state.old_context);
336 se_state.new_context = get_exec_context(se_state.old_context, role, type);
337 if (!se_state.new_context)
340 if (relabel_tty(ttyn, ptyfd) < 0) {
341 warning(_("unable to setup tty context for %s"), se_state.new_context);
346 if (se_state.ttyfd != -1) {
347 warningx("your old tty context is %s", se_state.tty_context);
348 warningx("your new tty context is %s", se_state.new_tty_context);
352 #ifdef HAVE_LINUX_AUDIT
353 audit_role_change(se_state.old_context, se_state.new_context,
360 debug_return_int(rval);
364 selinux_execve(const char *path, char *const argv[], char *const envp[],
369 debug_decl(selinux_execve, SUDO_DEBUG_SELINUX)
371 if (setexeccon(se_state.new_context)) {
372 warning(_("unable to set exec context to %s"), se_state.new_context);
373 if (se_state.enforcing)
377 #ifdef HAVE_SETKEYCREATECON
378 if (setkeycreatecon(se_state.new_context)) {
379 warning(_("unable to set key creation context to %s"), se_state.new_context);
380 if (se_state.enforcing)
383 #endif /* HAVE_SETKEYCREATECON */
386 * Build new argv with sesh as argv[0].
387 * If argv[0] ends in -noexec, sesh will disable execute
388 * for the command it runs.
390 for (argc = 0; argv[argc] != NULL; argc++)
392 nargv = emalloc2(argc + 2, sizeof(char *));
394 nargv[0] = *argv[0] == '-' ? "-sesh-noexec" : "sesh-noexec";
396 nargv[0] = *argv[0] == '-' ? "-sesh" : "sesh";
397 nargv[1] = (char *)path;
398 memcpy(&nargv[2], &argv[1], argc * sizeof(char *)); /* copies NULL */
400 /* sesh will handle noexec for us. */
401 sudo_execve(_PATH_SUDO_SESH, nargv, envp, 0);