add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / selinux.c
1 /*
2  * Copyright (c) 2008 Dan Walsh <dwalsh@redhat.com>
3  *
4  * Borrowed heavily from newrole source code
5  * Authors:
6  *      Anthony Colatrella
7  *      Tim Fraser
8  *      Steve Grubb <sgrubb@redhat.com>
9  *      Darrel Goeddel <DGoeddel@trustedcs.com>
10  *      Michael Thompson <mcthomps@us.ibm.com>
11  *      Dan Walsh <dwalsh@redhat.com>
12  *
13  * Permission to use, copy, modify, and distribute this software for any
14  * purpose with or without fee is hereby granted, provided that the above
15  * copyright notice and this permission notice appear in all copies.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  */
25
26 #include <config.h>
27
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #ifdef WITH_AUDIT
39 #include <libaudit.h>
40 #endif
41
42 #include <selinux/flask.h>             /* for SECCLASS_CHR_FILE */
43 #include <selinux/selinux.h>           /* for is_selinux_enabled() */
44 #include <selinux/context.h>           /* for context-mangling functions */
45 #include <selinux/get_default_type.h>
46 #include <selinux/get_context_list.h>
47
48 #include "sudo.h"
49 #include "pathnames.h"
50
51 /*
52  * This function attempts to revert the relabeling done to the tty.
53  * fd              - referencing the opened ttyn
54  * ttyn            - name of tty to restore
55  * tty_context     - original context of the tty
56  * new_tty_context - context tty was relabeled to
57  *
58  * Returns zero on success, non-zero otherwise
59  */
60 static int
61 restore_tty_label(int fd, const char *ttyn, security_context_t tty_context,
62     security_context_t new_tty_context)
63 {
64     int rc = 0;
65     security_context_t chk_tty_context = NULL;
66
67     if (!ttyn)
68             goto skip_relabel;
69
70     if (!new_tty_context)
71             goto skip_relabel;
72
73     /* Verify that the tty still has the context set by sudo. */
74     if ((rc = fgetfilecon(fd, &chk_tty_context)) < 0) {
75             warning("unable to fgetfilecon %s", ttyn);
76             goto skip_relabel;
77     }
78
79     if ((rc = strcmp(chk_tty_context, new_tty_context))) {
80             warningx("%s changed labels.", ttyn);
81             goto skip_relabel;
82     }
83
84     if ((rc = fsetfilecon(fd, tty_context)) < 0)
85         warning("unable to restore context for %s", ttyn);
86
87 skip_relabel:
88     freecon(chk_tty_context);
89     return(rc);
90 }
91
92 /*
93  * This function attempts to relabel the tty. If this function fails, then
94  * the fd is closed, the contexts are free'd and -1 is returned. On success,
95  * a valid fd is returned and tty_context and new_tty_context are set.
96  *
97  * This function will not fail if it can not relabel the tty when selinux is
98  * in permissive mode.
99  */
100 static int
101 relabel_tty(const char *ttyn, security_context_t new_context,
102     security_context_t * tty_context, security_context_t * new_tty_context,
103     int enforcing)
104 {
105     int fd;
106     security_context_t tty_con = NULL;
107     security_context_t new_tty_con = NULL;
108
109     if (!ttyn)
110         return(0);
111
112     /* Re-open TTY descriptor */
113     fd = open(ttyn, O_RDWR | O_NONBLOCK);
114     if (fd == -1) {
115         warning("unable to open %s", ttyn);
116         return(-1);
117     }
118     (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
119
120     if (fgetfilecon(fd, &tty_con) < 0) {
121         warning("unable to get current context for %s, not relabeling tty",
122             ttyn);
123         if (enforcing)
124             goto error;
125     }
126
127     if (tty_con && (security_compute_relabel(new_context, tty_con,
128         SECCLASS_CHR_FILE, &new_tty_con) < 0)) {
129         warning("unable to get new context for %s, not relabeling tty", ttyn);
130         if (enforcing)
131             goto error;
132     }
133
134     if (new_tty_con != NULL) {
135         if (fsetfilecon(fd, new_tty_con) < 0) {
136             warning("unable to set new context for %s", ttyn);
137             if (enforcing)
138                 goto error;
139         }
140     }
141
142     *tty_context = tty_con;
143     *new_tty_context = new_tty_con;
144     return(fd);
145
146 error:
147     freecon(tty_con);
148     close(fd);
149     return(-1);
150 }
151
152 /*
153  * Returns a new security context based on the old context and the
154  * specified role and type.
155  */
156 security_context_t
157 get_exec_context(security_context_t old_context, char *role, char *type)
158 {
159     security_context_t new_context = NULL;
160     context_t context = NULL;
161     char *typebuf = NULL;
162     
163     /* We must have a role, the type is optional (we can use the default). */
164     if (!role) {
165         warningx("you must specify a role.");
166         return(NULL);
167     }
168     if (!type) {
169         if (get_default_type(role, &typebuf)) {
170             warningx("unable to get default type");
171             return(NULL);
172         }
173         type = typebuf;
174     }
175     
176     /* 
177      * Expand old_context into a context_t so that we extract and modify 
178      * its components easily. 
179      */
180     context = context_new(old_context);
181     
182     /*
183      * Replace the role and type in "context" with the role and
184      * type we will be running the command as.
185      */
186     if (context_role_set(context, role)) {
187         warningx("failed to set new role %s", role);
188         goto error;
189     }
190     if (context_type_set(context, type)) {
191         warningx("failed to set new type %s", type);
192         goto error;
193     }
194       
195     /*
196      * Convert "context" back into a string and verify it.
197      */
198     new_context = estrdup(context_str(context));
199     if (security_check_context(new_context) < 0) {
200         warningx("%s is not a valid context", new_context);
201         goto error;
202     }
203
204 #ifdef DEBUG
205     warningx("Your new context is %s", new_context);
206 #endif
207
208     context_free(context);
209     return(new_context);
210
211 error:
212     free(typebuf);
213     context_free(context);
214     freecon(new_context);
215     return(NULL);
216 }
217
218 /* 
219  * If the program is being run with a different security context we
220  * need to go through an intermediary process for the transition to
221  * be allowed by the policy.  We use the "sesh" shell for this, which
222  * will simply execute the command pass to it on the command line.
223  */
224 void
225 selinux_exec(char *role, char *type, char **argv, int login_shell)
226 {
227     security_context_t old_context = NULL;
228     security_context_t new_context = NULL;
229     security_context_t tty_context = NULL;
230     security_context_t new_tty_context = NULL;
231     pid_t childPid;
232     int enforcing, ttyfd;
233
234     /* Must have a tty. */
235     if (user_ttypath == NULL || *user_ttypath == '\0')
236         error(EXIT_FAILURE, "unable to determine tty");
237
238     /* Store the caller's SID in old_context. */
239     if (getprevcon(&old_context))
240         error(EXIT_FAILURE, "failed to get old_context");
241
242     enforcing = security_getenforce();
243     if (enforcing < 0)
244         error(EXIT_FAILURE, "unable to determine enforcing mode.");
245
246     
247 #ifdef DEBUG
248     warningx("your old context was %s", old_context);
249 #endif
250     new_context = get_exec_context(old_context, role, type);
251     if (!new_context)
252         exit(EXIT_FAILURE);
253     
254     ttyfd = relabel_tty(user_ttypath, new_context, &tty_context,
255         &new_tty_context, enforcing);
256     if (ttyfd < 0)
257         error(EXIT_FAILURE, "unable to setup tty context for %s", new_context);
258
259 #ifdef DEBUG
260     warningx("your old tty context is %s", tty_context);
261     warningx("your new tty context is %s", new_tty_context);
262 #endif
263
264     childPid = fork();
265     if (childPid < 0) {
266         /* fork failed, no child to worry about */
267         warning("unable to fork");
268         if (restore_tty_label(ttyfd, user_ttypath, tty_context, new_tty_context))
269             warningx("unable to restore tty label");
270         exit(EXIT_FAILURE);
271     } else if (childPid) {
272         pid_t pid;
273         int status;
274         
275         /* Parent, wait for child to finish. */
276         do {
277                 pid = waitpid(childPid, &status, 0);
278         } while (pid == -1 && errno == EINTR);
279
280         if (pid == -1)
281             error(EXIT_FAILURE, "waitpid");
282         
283         if (restore_tty_label(ttyfd, user_ttypath, tty_context, new_tty_context))
284             errorx(EXIT_FAILURE, "unable to restore tty label");
285
286         /* Preserve child exit status. */
287         if (WIFEXITED(status))
288             exit(WEXITSTATUS(status));
289         exit(EXIT_FAILURE);
290     }
291     /* Child */
292     /* Close the tty and reopen descriptors 0 through 2 */
293     if (close(ttyfd) || close(STDIN_FILENO) || close(STDOUT_FILENO) ||
294         close(STDERR_FILENO)) {
295         warning("could not close descriptors");
296         goto error;
297     }
298     ttyfd = open(user_ttypath, O_RDONLY | O_NONBLOCK);
299     if (ttyfd != STDIN_FILENO)
300         goto error;
301     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
302     ttyfd = open(user_ttypath, O_RDWR | O_NONBLOCK);
303     if (ttyfd != STDOUT_FILENO)
304         goto error;
305     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
306     ttyfd = dup(STDOUT_FILENO);
307     if (ttyfd != STDERR_FILENO)
308         goto error;
309
310     if (setexeccon(new_context)) {
311         warning("unable to set exec context to %s", new_context);
312         if (enforcing)
313             goto error;
314     }
315
316     if (setkeycreatecon(new_context)) {
317         warning("unable to set key creation context to %s", new_context);
318         if (enforcing)
319             goto error;
320     }
321
322 #ifdef WITH_AUDIT
323     if (send_audit_message(1, old_context, new_context, user_ttypath)) 
324         goto error;
325 #endif
326
327     /* We use the "spare" slot in argv to store sesh. */
328     --argv;
329     argv[0] = login_shell ? "-sesh" : "sesh";
330     argv[1] = safe_cmnd;
331
332     execv(_PATH_SUDO_SESH, argv);
333     warning("%s", safe_cmnd);
334
335 error:
336     _exit(EXIT_FAILURE);
337 }