Imported Upstream version 1.8.4p4
[debian/sudo] / src / selinux.c
1 /*
2  * Copyright (c) 2009-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  * Copyright (c) 2008 Dan Walsh <dwalsh@redhat.com>
4  *
5  * Borrowed heavily from newrole source code
6  * Authors:
7  *      Anthony Colatrella
8  *      Tim Fraser
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>
13  *
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.
17  *
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.
25  */
26
27 #include <config.h>
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39
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>
45
46 #ifdef HAVE_LINUX_AUDIT
47 # include <libaudit.h>
48 #endif
49
50 #include "sudo.h"
51
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;
57     const char *ttyn;
58     int ttyfd;
59     int enforcing;
60 } se_state;
61
62 #ifdef HAVE_LINUX_AUDIT
63 static int
64 audit_role_change(const security_context_t old_context,
65     const security_context_t new_context, const char *ttyn)
66 {
67     int au_fd, rc = -1;
68     char *message;
69     debug_decl(audit_role_change, SUDO_DEBUG_SELINUX)
70
71     au_fd = audit_open();
72     if (au_fd == -1) {
73         /* Kernel may not have audit support. */
74         if (errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT
75 )
76             error(1, _("unable to open audit system"));
77     } else {
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);
83         if (rc <= 0)
84             warning(_("unable to send audit message"));
85         efree(message);
86         close(au_fd);
87     }
88
89     debug_return_int(rc);
90 }
91 #endif
92
93 /*
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
97  *
98  * Returns zero on success, non-zero otherwise
99  */
100 int
101 selinux_restore_tty(void)
102 {
103     int retval = 0;
104     security_context_t chk_tty_context = NULL;
105     debug_decl(selinux_restore_tty, SUDO_DEBUG_SELINUX)
106
107     if (se_state.ttyfd == -1 || se_state.new_tty_context == NULL)
108         goto skip_relabel;
109
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);
113         goto skip_relabel;
114     }
115
116     if ((retval = strcmp(chk_tty_context, se_state.new_tty_context))) {
117         warningx(_("%s changed labels"), se_state.ttyn);
118         goto skip_relabel;
119     }
120
121     if ((retval = fsetfilecon(se_state.ttyfd, se_state.tty_context)) < 0)
122         warning(_("unable to restore context for %s"), se_state.ttyn);
123
124 skip_relabel:
125     if (se_state.ttyfd != -1) {
126         close(se_state.ttyfd);
127         se_state.ttyfd = -1;
128     }
129     if (chk_tty_context != NULL) {
130         freecon(chk_tty_context);
131         chk_tty_context = NULL;
132     }
133     debug_return_int(retval);
134 }
135
136 /*
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.
140  *
141  * This function will not fail if it can not relabel the tty when selinux is
142  * in permissive mode.
143  */
144 static int
145 relabel_tty(const char *ttyn, int ptyfd)
146 {
147     security_context_t tty_con = NULL;
148     security_context_t new_tty_con = NULL;
149     int fd;
150     debug_decl(relabel_tty, SUDO_DEBUG_SELINUX)
151
152     se_state.ttyfd = ptyfd;
153
154     /* It is perfectly legal to have no tty. */
155     if (ptyfd == -1 && ttyn == NULL)
156         debug_return_int(0);
157
158     /* If sudo is not allocating a pty for the command, open current tty. */
159     if (ptyfd == -1) {
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)
164                 goto bad;
165         }
166         (void)fcntl(se_state.ttyfd, F_SETFL,
167             fcntl(se_state.ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
168     }
169
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)
173             goto bad;
174     }
175
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)
180             goto bad;
181     }
182
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)
187                 goto bad;
188         }
189     }
190
191     if (ptyfd != -1) {
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)
197                 goto bad;
198         }
199         if (dup2(se_state.ttyfd, ptyfd) == -1) {
200             warning("dup2");
201             goto bad;
202         }
203     } else {
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);
209             goto bad;
210         }
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) {
215                 warning("dup2");
216                 goto bad;
217             }
218         }
219     }
220     /* Retain se_state.ttyfd so we can restore label when command finishes. */
221     (void)fcntl(se_state.ttyfd, F_SETFD, FD_CLOEXEC);
222
223     se_state.ttyn = ttyn;
224     se_state.tty_context = tty_con;
225     se_state.new_tty_context = new_tty_con;
226     debug_return_int(0);
227
228 bad:
229     if (se_state.ttyfd != -1 && se_state.ttyfd != ptyfd) {
230         close(se_state.ttyfd);
231         se_state.ttyfd = -1;
232     }
233     freecon(tty_con);
234     debug_return_int(-1);
235 }
236
237 /*
238  * Returns a new security context based on the old context and the
239  * specified role and type.
240  */
241 security_context_t
242 get_exec_context(security_context_t old_context, const char *role, const char *type)
243 {
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)
248     
249     /* We must have a role, the type is optional (we can use the default). */
250     if (!role) {
251         warningx(_("you must specify a role for type %s"), type);
252         errno = EINVAL;
253         goto bad;
254     }
255     if (!type) {
256         if (get_default_type(role, &typebuf)) {
257             warningx(_("unable to get default type for role %s"), role);
258             errno = EINVAL;
259             goto bad;
260         }
261         type = typebuf;
262     }
263     
264     /* 
265      * Expand old_context into a context_t so that we extract and modify 
266      * its components easily. 
267      */
268     context = context_new(old_context);
269     
270     /*
271      * Replace the role and type in "context" with the role and
272      * type we will be running the command as.
273      */
274     if (context_role_set(context, role)) {
275         warning(_("failed to set new role %s"), role);
276         goto bad;
277     }
278     if (context_type_set(context, type)) {
279         warning(_("failed to set new type %s"), type);
280         goto bad;
281     }
282       
283     /*
284      * Convert "context" back into a string and verify it.
285      */
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);
289         errno = EINVAL;
290         goto bad;
291     }
292
293 #ifdef DEBUG
294     warningx("Your new context is %s", new_context);
295 #endif
296
297     context_free(context);
298     debug_return_ptr(new_context);
299
300 bad:
301     efree(typebuf);
302     context_free(context);
303     freecon(new_context);
304     debug_return_ptr(NULL);
305 }
306
307 /* 
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.
313  */
314 int
315 selinux_setup(const char *role, const char *type, const char *ttyn,
316     int ptyfd)
317 {
318     int rval = -1;
319     debug_decl(selinux_setup, SUDO_DEBUG_SELINUX)
320
321     /* Store the caller's SID in old_context. */
322     if (getprevcon(&se_state.old_context)) {
323         warning(_("failed to get old_context"));
324         goto done;
325     }
326
327     se_state.enforcing = security_getenforce();
328     if (se_state.enforcing < 0) {
329         warning(_("unable to determine enforcing mode."));
330         goto done;
331     }
332
333 #ifdef DEBUG
334     warningx("your old context was %s", se_state.old_context);
335 #endif
336     se_state.new_context = get_exec_context(se_state.old_context, role, type);
337     if (!se_state.new_context)
338         goto done;
339     
340     if (relabel_tty(ttyn, ptyfd) < 0) {
341         warning(_("unable to setup tty context for %s"), se_state.new_context);
342         goto done;
343     }
344
345 #ifdef DEBUG
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);
349     }
350 #endif
351
352 #ifdef HAVE_LINUX_AUDIT
353     audit_role_change(se_state.old_context, se_state.new_context,
354         se_state.ttyn);
355 #endif
356
357     rval = 0;
358
359 done:
360     debug_return_int(rval);
361 }
362
363 void
364 selinux_execve(const char *path, char *const argv[], char *const envp[],
365     int noexec)
366 {
367     char **nargv;
368     int argc, serrno;
369     debug_decl(selinux_execve, SUDO_DEBUG_SELINUX)
370
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)
374             debug_return;
375     }
376
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)
381             debug_return;
382     }
383 #endif /* HAVE_SETKEYCREATECON */
384
385     /*
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.
389      */
390     for (argc = 0; argv[argc] != NULL; argc++)
391         continue;
392     nargv = emalloc2(argc + 2, sizeof(char *));
393     if (noexec)
394         nargv[0] = *argv[0] == '-' ? "-sesh-noexec" : "sesh-noexec";
395     else
396         nargv[0] = *argv[0] == '-' ? "-sesh" : "sesh";
397     nargv[1] = (char *)path;
398     memcpy(&nargv[2], &argv[1], argc * sizeof(char *)); /* copies NULL */
399
400     /* sesh will handle noexec for us. */
401     sudo_execve(_PATH_SUDO_SESH, nargv, envp, 0);
402     serrno = errno;
403     free(nargv);
404     errno = serrno;
405     debug_return;
406 }