]> granicus.if.org Git - sudo/blob - src/sudo_noexec.c
c103d5fe4b4d4502f4f79702d867044e80ce3c09
[sudo] / src / sudo_noexec.c
1 /*
2  * Copyright (c) 2004-2005, 2010-2018 Todd C. Miller <Todd.Miller@sudo.ws>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /*
18  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25
26 #if defined(HAVE_DECL_SECCOMP_SET_MODE_FILTER) && HAVE_DECL_SECCOMP_SET_MODE_FILTER
27 # include <sys/prctl.h>
28 # include <asm/unistd.h>
29 # include <linux/filter.h>
30 # include <linux/seccomp.h>
31 #endif
32
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #ifdef HAVE_SPAWN_H
40 #include <spawn.h>
41 #endif
42 #ifdef HAVE_STRING_H
43 # include <string.h>
44 #endif /* HAVE_STRING_H */
45 #ifdef HAVE_STRINGS_H
46 # include <strings.h>
47 #endif /* HAVE_STRINGS_H */
48 #ifdef HAVE_WORDEXP_H
49 #include <wordexp.h>
50 #endif
51 #if defined(HAVE_SHL_LOAD)
52 # include <dl.h>
53 #elif defined(HAVE_DLOPEN)
54 # include <dlfcn.h>
55 #endif
56
57 #include "sudo_compat.h"
58 #include "pathnames.h"
59
60 #ifdef HAVE___INTERPOSE
61 /*
62  * Mac OS X 10.4 and above has support for library symbol interposition.
63  * There is a good explanation of this in the Mac OS X Internals book.
64  */
65 typedef struct interpose_s {
66     void *new_func;
67     void *orig_func;
68 } interpose_t;
69
70 # define FN_NAME(fn)    dummy_ ## fn
71 # define INTERPOSE(fn) \
72     __attribute__((__used__)) static const interpose_t interpose_ ## fn \
73     __attribute__((__section__("__DATA,__interpose"))) = \
74         { (void *)dummy_ ## fn, (void *)fn };
75 #else
76 # define FN_NAME(fn)    fn
77 # define INTERPOSE(fn)
78 #endif
79
80 /*
81  * Dummy versions of the exec(3) family of syscalls.  It is not enough to
82  * just dummy out execve(2) since many C libraries do not call the public
83  * execve(2) interface.  Note that it is still possible to access the real
84  * syscalls via the syscall(2) interface, but that is rarely done.
85  */
86
87 #define DUMMY_BODY                              \
88 {                                               \
89     errno = EACCES;                             \
90     return -1;                                  \
91 }
92
93 #define DUMMY1(fn, t1)                          \
94 __dso_public int                                \
95 FN_NAME(fn)(t1 a1)                              \
96 DUMMY_BODY                                      \
97 INTERPOSE(fn)
98
99 #define DUMMY2(fn, t1, t2)                      \
100 __dso_public int                                \
101 FN_NAME(fn)(t1 a1, t2 a2)                       \
102 DUMMY_BODY                                      \
103 INTERPOSE(fn)
104
105 #define DUMMY3(fn, t1, t2, t3)                  \
106 __dso_public int                                \
107 FN_NAME(fn)(t1 a1, t2 a2, t3 a3)                \
108 DUMMY_BODY                                      \
109 INTERPOSE(fn)
110
111 #define DUMMY6(fn, t1, t2, t3, t4, t5, t6)      \
112 __dso_public int                                \
113 FN_NAME(fn)(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6)   \
114 DUMMY_BODY                                      \
115 INTERPOSE(fn)
116
117 #define DUMMY_VA(fn, t1, t2)                    \
118 __dso_public int                                \
119 FN_NAME(fn)(t1 a1, t2 a2, ...)                  \
120 DUMMY_BODY                                      \
121 INTERPOSE(fn)
122
123 /*
124  * Standard exec(3) family of functions.
125  */
126 DUMMY_VA(execl, const char *, const char *)
127 DUMMY_VA(execle, const char *, const char *)
128 DUMMY_VA(execlp, const char *, const char *)
129 DUMMY2(execv, const char *, char * const *)
130 DUMMY2(execvp, const char *, char * const *)
131 DUMMY3(execve, const char *, char * const *, char * const *)
132
133 /*
134  * Non-standard exec(3) functions and corresponding private versions.
135  */
136 #ifdef HAVE_EXECVP
137 DUMMY3(execvP, const char *, const char *, char * const *)
138 #endif
139 #ifdef HAVE_EXECVPE
140 DUMMY3(execvpe, const char *, char * const *, char * const *)
141 #endif
142 #ifdef HAVE_EXECT
143 DUMMY3(exect, const char *, char * const *, char * const *)
144 #endif
145
146 /*
147  * Not all systems support fexecve(2), posix_spawn(2) and posix_spawnp(2).
148  */
149 #ifdef HAVE_FEXECVE
150 DUMMY3(fexecve, int , char * const *, char * const *)
151 #endif
152 #ifdef HAVE_POSIX_SPAWN
153 DUMMY6(posix_spawn, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *, char * const *, char * const *)
154 #endif
155 #ifdef HAVE_POSIX_SPAWNP
156 DUMMY6(posix_spawnp, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *, char * const *, char * const *)
157 #endif
158
159 /*
160  * system(3) and popen(3).
161  * We can't use a wrapper for popen since it returns FILE *, not int.
162  */
163 DUMMY1(system, const char *)
164
165 __dso_public FILE *
166 FN_NAME(popen)(const char *c, const char *t)
167 {
168     errno = EACCES;
169     return NULL;
170 }
171 INTERPOSE(popen)
172
173 #if defined(HAVE_WORDEXP) && (defined(RTLD_NEXT) || defined(HAVE_SHL_LOAD) || defined(HAVE___INTERPOSE))
174 /*
175  * We can't use a wrapper for wordexp(3) since we still want to call
176  * the real wordexp(3) but with WRDE_NOCMD added to the flags argument.
177  */
178 typedef int (*sudo_fn_wordexp_t)(const char *, wordexp_t *, int);
179
180 __dso_public int
181 FN_NAME(wordexp)(const char *words, wordexp_t *we, int flags)
182 {
183 #if defined(HAVE___INTERPOSE)
184     return wordexp(words, we, flags | WRDE_NOCMD);
185 #else
186 # if defined(HAVE_DLOPEN)
187     void *fn = dlsym(RTLD_NEXT, "wordexp");
188 # elif defined(HAVE_SHL_LOAD)
189     const char *name, *myname = _PATH_SUDO_NOEXEC;
190     struct shl_descriptor *desc;
191     void *fn = NULL;
192     int idx = 0;
193
194     name = strrchr(myname, '/');
195     if (name != NULL)
196         myname = name + 1;
197
198     /* Search for wordexp() but skip this shared object. */
199     while (shl_get(idx++, &desc) == 0) {
200         name = strrchr(desc->filename, '/');
201         if (name == NULL)
202                 name = desc->filename;
203         else
204                 name++;
205         if (strcmp(name, myname) == 0)
206             continue;
207         if (shl_findsym(&desc->handle, "wordexp", TYPE_PROCEDURE, &fn) == 0)
208             break;
209     }
210 # else
211     void *fn = NULL;
212 # endif
213     if (fn == NULL) {
214         errno = EACCES;
215         return -1;
216     }
217     return ((sudo_fn_wordexp_t)fn)(words, we, flags | WRDE_NOCMD);
218 #endif /* HAVE___INTERPOSE */
219 }
220 INTERPOSE(wordexp)
221 #endif /* HAVE_WORDEXP && (RTLD_NEXT || HAVE_SHL_LOAD || HAVE___INTERPOSE) */
222
223 /*
224  * On Linux we can use a seccomp() filter to disable exec.
225  */
226 #if defined(HAVE_DECL_SECCOMP_SET_MODE_FILTER) && HAVE_DECL_SECCOMP_SET_MODE_FILTER
227
228 /* Older systems may not support execveat(2). */
229 #ifndef __NR_execveat
230 # define __NR_execveat -1
231 #endif
232
233 static void noexec_ctor(void) __attribute__((constructor));
234
235 static void
236 noexec_ctor(void)
237 {
238     struct sock_filter exec_filter[] = {
239         /* Load syscall number into the accumulator */
240         BPF_STMT(BPF_LD | BPF_ABS, offsetof(struct seccomp_data, nr)),
241         /* Jump to deny for execve/execveat */
242         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execve, 2, 0),
243         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execveat, 1, 0),
244         /* Allow non-matching syscalls */
245         BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
246         /* Deny execve/execveat syscall */
247         BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (EACCES & SECCOMP_RET_DATA))
248     };
249     const struct sock_fprog exec_fprog = {
250         nitems(exec_filter),
251         exec_filter
252     };
253
254     /*
255      * SECCOMP_MODE_FILTER will fail unless the process has
256      * CAP_SYS_ADMIN or the no_new_privs bit is set.
257      */
258     if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0)
259         (void)prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &exec_fprog);
260 }
261 #endif /* HAVE_DECL_SECCOMP_SET_MODE_FILTER */