1 /*
2 * Copyright (c) 2021 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 #include <config.h>
18
19 #include <sys/socket.h>
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <pwd.h>
27 #include <unistd.h>
28 #if defined(HAVE_STDINT_H)
29 # include <stdint.h>
30 #elif defined(HAVE_INTTYPES_H)
31 # include <inttypes.h>
32 #endif
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #ifdef NEED_RESOLV_H
36 # include <arpa/nameser.h>
37 # include <resolv.h>
38 #endif /* NEED_RESOLV_H */
39 #include <netdb.h>
40
41 #include "sudoers.h"
42 #include "interfaces.h"
43
44 static int fuzz_conversation(int num_msgs, const struct sudo_conv_message msgs[], struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);
45
46 /* Required to link with parser. */
47 struct sudo_user sudo_user;
48 struct passwd *list_pw;
49 sudo_conv_t sudo_conv = fuzz_conversation;
50 bool sudoers_recovery = true;
51 int sudo_mode;
52
53 FILE *
open_sudoers(const char * file,bool doedit,bool * keepopen)54 open_sudoers(const char *file, bool doedit, bool *keepopen)
55 {
56 /*
57 * If we allow the fuzzer to choose include paths it will
58 * include random files in the file system.
59 * This leads to bug reports that cannot be reproduced.
60 */
61 return NULL;
62 }
63
64 static int
fuzz_conversation(int num_msgs,const struct sudo_conv_message msgs[],struct sudo_conv_reply replies[],struct sudo_conv_callback * callback)65 fuzz_conversation(int num_msgs, const struct sudo_conv_message msgs[],
66 struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
67 {
68 int n;
69
70 for (n = 0; n < num_msgs; n++) {
71 const struct sudo_conv_message *msg = &msgs[n];
72 FILE *fp = stdout;
73
74 switch (msg->msg_type & 0xff) {
75 case SUDO_CONV_PROMPT_ECHO_ON:
76 case SUDO_CONV_PROMPT_MASK:
77 case SUDO_CONV_PROMPT_ECHO_OFF:
78 /* input not supported */
79 return -1;
80 case SUDO_CONV_ERROR_MSG:
81 fp = stderr;
82 FALLTHROUGH;
83 case SUDO_CONV_INFO_MSG:
84 if (msg->msg != NULL) {
85 size_t len = strlen(msg->msg);
86
87 if (len == 0)
88 break;
89
90 if (fwrite(msg->msg, 1, len, fp) == 0 || fputc('\n', fp) == EOF)
91 return -1;
92 }
93 break;
94 default:
95 return -1;
96 }
97 }
98 return 0;
99 }
100
101 bool
init_envtables(void)102 init_envtables(void)
103 {
104 return true;
105 }
106
107 int
set_cmnd_path(const char * runchroot)108 set_cmnd_path(const char *runchroot)
109 {
110 /* Cannot return FOUND without also setting user_cmnd to a new value. */
111 return NOT_FOUND;
112 }
113
114 bool
log_warningx(int flags,const char * fmt,...)115 log_warningx(int flags, const char *fmt, ...)
116 {
117 va_list ap;
118
119 /* Just display on stderr. */
120 va_start(ap, fmt);
121 sudo_vwarnx_nodebug(fmt, ap);
122 va_end(ap);
123
124 return true;
125 }
126
127 static int
sudo_fuzz_query(struct sudo_nss * nss,struct passwd * pw)128 sudo_fuzz_query(struct sudo_nss *nss, struct passwd *pw)
129 {
130 return 0;
131 }
132
133 static int
cb_unused(struct sudoers_parse_tree * parse_tree,struct alias * a,void * v)134 cb_unused(struct sudoers_parse_tree *parse_tree, struct alias *a, void *v)
135 {
136 return 0;
137 }
138
139 static FILE *
open_data(const uint8_t * data,size_t size)140 open_data(const uint8_t *data, size_t size)
141 {
142 #ifdef HAVE_FMEMOPEN
143 /* Operate in-memory. */
144 return fmemopen((void *)data, size, "r");
145 #else
146 char tempfile[] = "/tmp/sudoers.XXXXXX";
147 size_t nwritten;
148 int fd;
149
150 /* Use (unlinked) temporary file. */
151 fd = mkstemp(tempfile);
152 if (fd == -1)
153 return NULL;
154 unlink(tempfile);
155 nwritten = write(fd, data, size);
156 if (nwritten != size) {
157 close(fd);
158 return NULL;
159 }
160 lseek(fd, 0, SEEK_SET);
161 return fdopen(fd, "r");
162 #endif
163 }
164
165 static struct user_data {
166 char *user;
167 char *runuser;
168 char *rungroup;
169 } user_data[] = {
170 { "root", NULL, NULL },
171 { "millert", "operator", NULL },
172 { "millert", NULL, "wheel" },
173 { "operator", NULL, NULL },
174 { NULL }
175 };
176
177 int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)178 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
179 {
180 struct user_data *ud;
181 struct sudo_nss sudo_nss_fuzz;
182 struct sudo_nss_list snl = TAILQ_HEAD_INITIALIZER(snl);
183 struct sudoers_parse_tree parse_tree;
184 struct interface_list *interfaces;
185 struct passwd *pw;
186 struct group *gr;
187 char *gids[10];
188 FILE *fp;
189
190 /* Don't waste time fuzzing tiny inputs. */
191 if (size < 5)
192 return 0;
193
194 fp = open_data(data, size);
195 if (fp == NULL)
196 return 0;
197
198 setprogname("fuzz_sudoers");
199 sudoers_debug_register(getprogname(), NULL);
200
201 /* Sudoers locale setup. */
202 sudoers_initlocale(setlocale(LC_ALL, ""), "C");
203 sudo_warn_set_locale_func(sudoers_warn_setlocale);
204 bindtextdomain("sudoers", LOCALEDIR);
205 textdomain("sudoers");
206
207 /* Use the sudoers locale for everything. */
208 sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, NULL);
209
210 /* Prime the group cache */
211 gr = sudo_mkgrent("wheel", 0, "millert", "root", (char *)NULL);
212 if (gr == NULL)
213 goto done;
214 sudo_gr_delref(gr);
215
216 gr = sudo_mkgrent("operator", 5, "operator", "root", "millert", (char *)NULL);
217 if (gr == NULL)
218 goto done;
219 sudo_gr_delref(gr);
220
221 gr = sudo_mkgrent("staff", 20, "root", "millert", (char *)NULL);
222 if (gr == NULL)
223 goto done;
224 sudo_gr_delref(gr);
225
226 gr = sudo_mkgrent("sudo", 100, "root", "millert", (char *)NULL);
227 if (gr == NULL)
228 goto done;
229 sudo_gr_delref(gr);
230
231 /* Prime the passwd cache */
232 pw = sudo_mkpwent("root", 0, 0, "/", "/bin/sh");
233 if (pw == NULL)
234 goto done;
235 gids[0] = "0";
236 gids[1] = "20";
237 gids[2] = "5";
238 gids[3] = NULL;
239 if (sudo_set_gidlist(pw, gids, ENTRY_TYPE_FRONTEND) == -1)
240 goto done;
241 sudo_pw_delref(pw);
242
243 pw = sudo_mkpwent("operator", 2, 5, "/operator", "/sbin/nologin");
244 if (pw == NULL)
245 goto done;
246 gids[0] = "5";
247 gids[1] = NULL;
248 if (sudo_set_gidlist(pw, gids, ENTRY_TYPE_FRONTEND) == -1)
249 goto done;
250 sudo_pw_delref(pw);
251
252 pw = sudo_mkpwent("millert", 8036, 20, "/home/millert", "/bin/tcsh");
253 if (pw == NULL)
254 goto done;
255 gids[0] = "0";
256 gids[1] = "20";
257 gids[2] = "5";
258 gids[3] = "100";
259 gids[4] = NULL;
260 if (sudo_set_gidlist(pw, gids, ENTRY_TYPE_FRONTEND) == -1)
261 goto done;
262 sudo_pw_delref(pw);
263
264 /* The minimum needed to perform matching (user_cmnd must be dynamic). */
265 user_host = user_shost = user_runhost = user_srunhost = "localhost";
266 user_cmnd = strdup("/usr/bin/id");
267 if (user_cmnd == NULL)
268 goto done;
269 user_args = "-u";
270 user_base = "id";
271
272 /* Add a fake network interfaces. */
273 interfaces = get_interfaces();
274 if (SLIST_EMPTY(interfaces)) {
275 static struct interface interface;
276
277 interface.family = AF_INET;
278 inet_pton(AF_INET, "128.138.243.151", &interface.addr.ip4);
279 inet_pton(AF_INET, "255.255.255.0", &interface.netmask.ip4);
280 SLIST_INSERT_HEAD(interfaces, &interface, entries);
281 }
282
283 /* Only one sudoers source, the sudoers file itself. */
284 init_parse_tree(&parse_tree, user_host, user_shost);
285 memset(&sudo_nss_fuzz, 0, sizeof(sudo_nss_fuzz));
286 sudo_nss_fuzz.parse_tree = &parse_tree;
287 sudo_nss_fuzz.query = sudo_fuzz_query;
288 TAILQ_INSERT_TAIL(&snl, &sudo_nss_fuzz, entries);
289
290 /* Initialize defaults and parse sudoers. */
291 init_defaults();
292 init_parser("sudoers", false, true);
293 sudoersrestart(fp);
294 sudoersparse();
295 reparent_parse_tree(&parse_tree);
296
297 if (!parse_error) {
298 /* Match user/host/command against parsed policy. */
299 for (ud = user_data; ud->user != NULL; ud++) {
300 int cmnd_status;
301
302 /* Invoking user. */
303 user_name = ud->user;
304 if (sudo_user.pw != NULL)
305 sudo_pw_delref(sudo_user.pw);
306 sudo_user.pw = sudo_getpwnam(user_name);
307 if (sudo_user.pw == NULL) {
308 fprintf(stderr, "unknown user %s\n", user_name);
309 continue;
310 }
311
312 /* Run user. */
313 if (runas_pw != NULL)
314 sudo_pw_delref(runas_pw);
315 if (ud->runuser != NULL) {
316 sudo_user.runas_user = ud->runuser;
317 SET(sudo_user.flags, RUNAS_USER_SPECIFIED);
318 runas_pw = sudo_getpwnam(sudo_user.runas_user);
319 } else {
320 sudo_user.runas_user = NULL;
321 CLR(sudo_user.flags, RUNAS_USER_SPECIFIED);
322 runas_pw = sudo_getpwnam("root");
323 }
324 if (runas_pw == NULL) {
325 fprintf(stderr, "unknown run user %s\n",
326 sudo_user.runas_user);
327 continue;
328 }
329
330 /* Run group. */
331 if (runas_gr != NULL)
332 sudo_gr_delref(runas_gr);
333 if (ud->rungroup != NULL) {
334 sudo_user.runas_group = ud->rungroup;
335 SET(sudo_user.flags, RUNAS_GROUP_SPECIFIED);
336 runas_gr = sudo_getgrnam(sudo_user.runas_group);
337 if (runas_gr == NULL) {
338 fprintf(stderr, "unknown run group %s\n",
339 sudo_user.runas_group);
340 continue;
341 }
342 } else {
343 sudo_user.runas_group = NULL;
344 CLR(sudo_user.flags, RUNAS_GROUP_SPECIFIED);
345 runas_gr = NULL;
346 }
347
348 update_defaults(&parse_tree, NULL, SETDEF_ALL, false);
349
350 sudoers_lookup(&snl, sudo_user.pw, &cmnd_status, false);
351
352 /* Match again as a pseudo-command (list, validate, etc). */
353 sudoers_lookup(&snl, sudo_user.pw, &cmnd_status, true);
354
355 /* Display privileges. */
356 display_privs(&snl, sudo_user.pw, false);
357 display_privs(&snl, sudo_user.pw, true);
358 }
359
360 /* Expand tildes in runcwd and runchroot. */
361 if (def_runcwd != NULL && strcmp(def_runcwd, "*") != 0) {
362 expand_tilde(&def_runcwd, runas_pw->pw_name);
363 }
364 if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
365 expand_tilde(&def_runchroot, runas_pw->pw_name);
366 }
367
368 /* Check Defaults and aliases. */
369 check_defaults(&parse_tree, false);
370 check_aliases(&parse_tree, true, false, cb_unused);
371 }
372
373 done:
374 /* Cleanup. */
375 fclose(fp);
376 free_parse_tree(&parse_tree);
377 init_parser(NULL, false, true);
378 if (sudo_user.pw != NULL)
379 sudo_pw_delref(sudo_user.pw);
380 if (runas_pw != NULL)
381 sudo_pw_delref(runas_pw);
382 if (runas_gr != NULL)
383 sudo_gr_delref(runas_gr);
384 sudo_freepwcache();
385 sudo_freegrcache();
386 free(user_cmnd);
387 free(safe_cmnd);
388 memset(&sudo_user, 0, sizeof(sudo_user));
389 sudoers_setlocale(SUDOERS_LOCALE_USER, NULL);
390 sudoers_debug_deregister();
391 fflush(stdout);
392
393 return 0;
394 }
395