/[path-sandbox]/trunk/libsandbox.c
Gentoo

Contents of /trunk/libsandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download) (as text)
Wed Mar 2 17:30:16 2005 UTC (9 years, 6 months ago) by azarah
File MIME type: text/x-csrc
File size: 32684 byte(s)
Hack to make sure sandboxed process cannot remove a device node, bug #79836.

1 /*
2 * Path sandbox for the gentoo linux portage package system, initially
3 * based on the ROCK Linux Wrapper for getting a list of created files
4 *
5 * to integrate with bash, bash should have been built like this
6 *
7 * ./configure --prefix=<prefix> --host=<host> --without-gnu-malloc
8 *
9 * it's very important that the --enable-static-link option is NOT specified
10 *
11 * Copyright (C) 2001 Geert Bevin, Uwyn, http://www.uwyn.com
12 * Distributed under the terms of the GNU General Public License, v2 or later
13 * Author : Geert Bevin <gbevin@uwyn.com>
14 *
15 * Post Bevin leaving Gentoo ranks:
16 * --------------------------------
17 * Ripped out all the wrappers, and implemented those of InstallWatch.
18 * Losts of cleanups and bugfixes. Implement a execve that forces $LIBSANDBOX
19 * in $LD_PRELOAD. Reformat the whole thing to look somewhat like the reworked
20 * sandbox.c from Brad House <brad@mainstreetsoftworks.com>.
21 *
22 * Martin Schlemmer <azarah@gentoo.org> (18 Aug 2002)
23 *
24 * Partly Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>,
25 * as some of the InstallWatch code was used.
26 *
27 *
28 * $Header$
29 *
30 */
31
32 /* Uncomment below to enable wrapping of mknod().
33 * This is broken currently. */
34 /* #define WRAP_MKNOD 1 */
35
36 /* Uncomment below to enable the use of strtok_r(). */
37 #define REENTRANT_STRTOK 1
38
39 /* Uncomment below to enable memory debugging. */
40 /* #define SB_MEM_DEBUG 1 */
41
42 #define open xxx_open
43 #define open64 xxx_open64
44
45 /* Wrapping mknod, do not have any effect, and
46 * wrapping __xmknod causes calls to it to segfault
47 */
48 #ifdef WRAP_MKNOD
49 # define __xmknod xxx___xmknod
50 #endif
51
52 #include <dirent.h>
53 #include <dlfcn.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sys/file.h>
61 #include <sys/stat.h>
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <unistd.h>
65 #include <utime.h>
66 #include "config.h"
67 #include "localdecls.h"
68 #include "symbols.h"
69
70 #ifdef SB_MEM_DEBUG
71 # include <mcheck.h>
72 #endif
73
74 #ifdef WRAP_MKNOD
75 # undef __xmknod
76 #endif
77
78 #undef open
79 #undef open64
80
81 //#include "localdecls.h"
82 #include "sandbox.h"
83
84 /* Macros to check if a function should be executed */
85 #define FUNCTION_SANDBOX_SAFE(func, path) \
86 ((0 == is_sandbox_on()) || (1 == before_syscall(func, path)))
87
88 #define FUNCTION_SANDBOX_SAFE_INT(func, path, flags) \
89 ((0 == is_sandbox_on()) || (1 == before_syscall_open_int(func, path, flags)))
90
91 #define FUNCTION_SANDBOX_SAFE_CHAR(func, path, mode) \
92 ((0 == is_sandbox_on()) || (1 == before_syscall_open_char(func, path, mode)))
93
94 /* Macro to check if a wrapper is defined, if not
95 * then try to resolve it again. */
96 #define check_dlsym(name) \
97 { \
98 int old_errno=errno; \
99 if (!true_ ## name) \
100 true_ ## name = get_dlsym(symname_ ## name, symver_ ## name); \
101 errno=old_errno; \
102 }
103
104 /* Macro to check if we could canonicalize a path. It returns an integer on
105 * failure. */
106 #define canonicalize_int(path, resolved_path) \
107 { \
108 if (0 != canonicalize(path, resolved_path)) \
109 return -1; \
110 }
111
112 /* Macro to check if we could canonicalize a path. It returns a NULL pointer on
113 * failure. */
114 #define canonicalize_ptr(path, resolved_path) \
115 { \
116 if (0 != canonicalize(path, resolved_path)) \
117 return NULL; \
118 }
119
120 static char sandbox_lib[255];
121 //static char sandbox_pids_file[255];
122 static char *sandbox_pids_file;
123
124 typedef struct {
125 int show_access_violation;
126 char **deny_prefixes;
127 int num_deny_prefixes;
128 char **read_prefixes;
129 int num_read_prefixes;
130 char **write_prefixes;
131 int num_write_prefixes;
132 char **predict_prefixes;
133 int num_predict_prefixes;
134 char **write_denied_prefixes;
135 int num_write_denied_prefixes;
136 } sbcontext_t;
137
138 void __attribute__ ((constructor)) my_init(void);
139 void __attribute__ ((destructor)) my_fini(void);
140
141 /* glibc modified realpath() functions */
142 static char *erealpath(const char *name, char *resolved);
143 /* glibc modified getcwd() functions */
144 static char *egetcwd(char *, size_t);
145
146 static void init_wrappers(void);
147 static void *get_dlsym(const char *, const char *);
148 static int canonicalize(const char *, char *);
149 static int check_access(sbcontext_t *, const char *, const char *);
150 static int check_syscall(sbcontext_t *, const char *, const char *);
151 static int before_syscall(const char *, const char *);
152 static int before_syscall_open_int(const char *, const char *, int);
153 static int before_syscall_open_char(const char *, const char *, const char *);
154 static void clean_env_entries(char ***, int *);
155 static void init_context(sbcontext_t *);
156 static void init_env_entries(char ***, int *, char *, int);
157 static char *filter_path(const char *);
158 static int is_sandbox_on();
159 static int is_sandbox_pid();
160
161 /* Wrapped functions */
162
163 extern int chmod(const char *, mode_t);
164 static int (*true_chmod) (const char *, mode_t);
165 extern int chown(const char *, uid_t, gid_t);
166 static int (*true_chown) (const char *, uid_t, gid_t);
167 extern int creat(const char *, mode_t);
168 static int (*true_creat) (const char *, mode_t);
169 extern FILE *fopen(const char *, const char *);
170 static FILE *(*true_fopen) (const char *, const char *);
171 extern int lchown(const char *, uid_t, gid_t);
172 static int (*true_lchown) (const char *, uid_t, gid_t);
173 extern int link(const char *, const char *);
174 static int (*true_link) (const char *, const char *);
175 extern int mkdir(const char *, mode_t);
176 static int (*true_mkdir) (const char *, mode_t);
177 extern DIR *opendir(const char *);
178 static DIR *(*true_opendir) (const char *);
179 #ifdef WRAP_MKNOD
180 extern int __xmknod(const char *, mode_t, dev_t);
181 static int (*true___xmknod) (const char *, mode_t, dev_t);
182 #endif
183 extern int open(const char *, int, ...);
184 static int (*true_open) (const char *, int, ...);
185 extern int rename(const char *, const char *);
186 static int (*true_rename) (const char *, const char *);
187 extern int rmdir(const char *);
188 static int (*true_rmdir) (const char *);
189 extern int symlink(const char *, const char *);
190 static int (*true_symlink) (const char *, const char *);
191 extern int truncate(const char *, TRUNCATE_T);
192 static int (*true_truncate) (const char *, TRUNCATE_T);
193 extern int unlink(const char *);
194 static int (*true_unlink) (const char *);
195
196 #if (GLIBC_MINOR >= 1)
197
198 extern int creat64(const char *, __mode_t);
199 static int (*true_creat64) (const char *, __mode_t);
200 extern FILE *fopen64(const char *, const char *);
201 static FILE *(*true_fopen64) (const char *, const char *);
202 extern int open64(const char *, int, ...);
203 static int (*true_open64) (const char *, int, ...);
204 extern int truncate64(const char *, __off64_t);
205 static int (*true_truncate64) (const char *, __off64_t);
206
207 #endif
208
209 extern int execve(const char *filename, char *const argv[], char *const envp[]);
210 static int (*true_execve) (const char *, char *const[], char *const[]);
211
212 /*
213 * Initialize the shabang
214 */
215
216 static void init_wrappers(void)
217 {
218 check_dlsym(chmod);
219 check_dlsym(chown);
220 check_dlsym(creat);
221 check_dlsym(fopen);
222 check_dlsym(lchown);
223 check_dlsym(link);
224 check_dlsym(mkdir);
225 check_dlsym(opendir);
226 #ifdef WRAP_MKNOD
227 check_dlsym(__xmknod);
228 #endif
229 check_dlsym(open);
230 check_dlsym(rename);
231 check_dlsym(rmdir);
232 check_dlsym(symlink);
233 check_dlsym(truncate);
234 check_dlsym(unlink);
235
236 #if (GLIBC_MINOR >= 1)
237 check_dlsym(creat64);
238 check_dlsym(fopen64);
239 check_dlsym(open64);
240 check_dlsym(truncate64);
241 #endif
242
243 check_dlsym(execve);
244 }
245
246 static void *libc_handle = NULL;
247
248 static void *get_dlsym(const char *symname, const char *symver)
249 {
250 void *symaddr = NULL;
251
252 if (NULL == libc_handle) {
253 #ifdef BROKEN_RTLD_NEXT
254 libc_handle = dlopen(LIBC_VERSION, RTLD_LAZY);
255 if (!libc_handle) {
256 printf("libsandbox.so: Can't dlopen libc: %s\n", dlerror());
257 abort();
258 }
259 #else
260 libc_handle = RTLD_NEXT;
261 #endif
262 }
263
264 if (NULL == symver)
265 symaddr = dlsym(libc_handle, symname);
266 else
267 symaddr = dlvsym(libc_handle, symname, symver);
268 if (!symaddr) {
269 printf("libsandbox.so: Can't resolve %s: %s\n", symname, dlerror());
270 abort();
271 }
272
273 return symaddr;
274 }
275
276 void __attribute__ ((destructor)) my_fini(void)
277 {
278 free(sandbox_pids_file);
279 }
280
281 void __attribute__ ((constructor)) my_init(void)
282 {
283 int old_errno = errno;
284 char *tmp_string = NULL;
285
286 #ifdef SB_MEM_DEBUG
287 mtrace();
288 #endif
289
290 init_wrappers();
291
292 /* Get the path and name to this library */
293 tmp_string = get_sandbox_lib("/");
294 strncpy(sandbox_lib, tmp_string, sizeof(sandbox_lib) - 1);
295 if (tmp_string)
296 free(tmp_string);
297 tmp_string = NULL;
298
299 /* Generate sandbox pids-file path */
300 sandbox_pids_file = get_sandbox_pids_file();
301
302 errno = old_errno;
303 }
304
305 static int canonicalize(const char *path, char *resolved_path)
306 {
307 int old_errno = errno;
308 char *retval;
309
310 *resolved_path = '\0';
311
312 /* If path == NULL, return or we get a segfault */
313 if (NULL == path) {
314 errno = EINVAL;
315 return -1;
316 }
317
318 /* Do not try to resolve an empty path */
319 if ('\0' == path[0]) {
320 errno = old_errno;
321 return 0;
322 }
323
324 retval = erealpath(path, resolved_path);
325
326 if ((!retval) && (path[0] != '/')) {
327 /* The path could not be canonicalized, append it
328 * to the current working directory if it was not
329 * an absolute path
330 */
331 if (errno == ENAMETOOLONG)
332 return -1;
333
334 egetcwd(resolved_path, SB_PATH_MAX - 2);
335 strcat(resolved_path, "/");
336 strncat(resolved_path, path, SB_PATH_MAX - 1);
337
338 if (!erealpath(resolved_path, resolved_path)) {
339 if (errno == ENAMETOOLONG) {
340 /* The resolved path is too long for the buffer to hold */
341 return -1;
342 } else {
343 /* Whatever it resolved, is not a valid path */
344 errno = ENOENT;
345 return -1;
346 }
347 }
348
349 } else if ((!retval) && (path[0] == '/')) {
350 /* Whatever it resolved, is not a valid path */
351 errno = ENOENT;
352 return -1;
353 }
354
355 errno = old_errno;
356 return 0;
357 }
358
359 /*
360 * Wrapper Functions
361 */
362
363 int chmod(const char *path, mode_t mode)
364 {
365 int result = -1;
366 char canonic[SB_PATH_MAX];
367
368 canonicalize_int(path, canonic);
369
370 if FUNCTION_SANDBOX_SAFE
371 ("chmod", canonic) {
372 check_dlsym(chmod);
373 result = true_chmod(path, mode);
374 }
375
376 return result;
377 }
378
379 int chown(const char *path, uid_t owner, gid_t group)
380 {
381 int result = -1;
382 char canonic[SB_PATH_MAX];
383
384 canonicalize_int(path, canonic);
385
386 if FUNCTION_SANDBOX_SAFE
387 ("chown", canonic) {
388 check_dlsym(chown);
389 result = true_chown(path, owner, group);
390 }
391
392 return result;
393 }
394
395 int creat(const char *pathname, mode_t mode)
396 {
397 /* Is it a system call? */
398 int result = -1;
399 char canonic[SB_PATH_MAX];
400
401 canonicalize_int(pathname, canonic);
402
403 if FUNCTION_SANDBOX_SAFE
404 ("creat", canonic) {
405 check_dlsym(open);
406 result = true_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
407 }
408
409 return result;
410 }
411
412 FILE *fopen(const char *pathname, const char *mode)
413 {
414 FILE *result = NULL;
415 char canonic[SB_PATH_MAX];
416
417 canonicalize_ptr(pathname, canonic);
418
419 if FUNCTION_SANDBOX_SAFE_CHAR
420 ("fopen", canonic, mode) {
421 check_dlsym(fopen);
422 result = true_fopen(pathname, mode);
423 }
424
425 return result;
426 }
427
428 int lchown(const char *path, uid_t owner, gid_t group)
429 {
430 int result = -1;
431 char canonic[SB_PATH_MAX];
432
433 canonicalize_int(path, canonic);
434
435 if FUNCTION_SANDBOX_SAFE
436 ("lchown", canonic) {
437 check_dlsym(lchown);
438 result = true_lchown(path, owner, group);
439 }
440
441 return result;
442 }
443
444 int link(const char *oldpath, const char *newpath)
445 {
446 int result = -1;
447 char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
448
449 canonicalize_int(oldpath, old_canonic);
450 canonicalize_int(newpath, new_canonic);
451
452 if FUNCTION_SANDBOX_SAFE
453 ("link", new_canonic) {
454 check_dlsym(link);
455 result = true_link(oldpath, newpath);
456 }
457
458 return result;
459 }
460
461 int mkdir(const char *pathname, mode_t mode)
462 {
463 int result = -1, my_errno = errno;
464 char canonic[SB_PATH_MAX];
465 struct stat st;
466
467 canonicalize_int(pathname, canonic);
468
469 /* Check if the directory exist, return EEXIST rather than failing */
470 if (0 == lstat(canonic, &st)) {
471 errno = EEXIST;
472 return -1;
473 }
474 errno = my_errno;
475
476 if FUNCTION_SANDBOX_SAFE
477 ("mkdir", canonic) {
478 check_dlsym(mkdir);
479 result = true_mkdir(pathname, mode);
480 }
481
482 return result;
483 }
484
485 DIR *opendir(const char *name)
486 {
487 DIR *result = NULL;
488 char canonic[SB_PATH_MAX];
489
490 canonicalize_ptr(name, canonic);
491
492 if FUNCTION_SANDBOX_SAFE
493 ("opendir", canonic) {
494 check_dlsym(opendir);
495 result = true_opendir(name);
496 }
497
498 return result;
499 }
500
501 #ifdef WRAP_MKNOD
502
503 int __xmknod(const char *pathname, mode_t mode, dev_t dev)
504 {
505 int result = -1;
506 char canonic[SB_PATH_MAX];
507
508 canonicalize_int(pathname, canonic);
509
510 if FUNCTION_SANDBOX_SAFE
511 ("__xmknod", canonic) {
512 check_dlsym(__xmknod);
513 result = true___xmknod(pathname, mode, dev);
514 }
515
516 return result;
517 }
518
519 #endif
520
521 int open(const char *pathname, int flags, ...)
522 {
523 /* Eventually, there is a third parameter: it's mode_t mode */
524 va_list ap;
525 mode_t mode = 0;
526 int result = -1;
527 char canonic[SB_PATH_MAX];
528
529 if (flags & O_CREAT) {
530 va_start(ap, flags);
531 mode = va_arg(ap, mode_t);
532 va_end(ap);
533 }
534
535 canonicalize_int(pathname, canonic);
536
537 if FUNCTION_SANDBOX_SAFE_INT
538 ("open", canonic, flags) {
539 /* We need to resolve open() realtime in some cases,
540 * else we get a segfault when running /bin/ps, etc
541 * in a sandbox */
542 check_dlsym(open);
543 result = true_open(pathname, flags, mode);
544 }
545
546 return result;
547 }
548
549 int rename(const char *oldpath, const char *newpath)
550 {
551 int result = -1;
552 char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
553
554 canonicalize_int(oldpath, old_canonic);
555 canonicalize_int(newpath, new_canonic);
556
557 if (FUNCTION_SANDBOX_SAFE("rename", old_canonic) &&
558 FUNCTION_SANDBOX_SAFE("rename", new_canonic)) {
559 check_dlsym(rename);
560 result = true_rename(oldpath, newpath);
561 }
562
563 return result;
564 }
565
566 int rmdir(const char *pathname)
567 {
568 int result = -1;
569 char canonic[SB_PATH_MAX];
570
571 canonicalize_int(pathname, canonic);
572
573 if FUNCTION_SANDBOX_SAFE
574 ("rmdir", canonic) {
575 check_dlsym(rmdir);
576 result = true_rmdir(pathname);
577 }
578
579 return result;
580 }
581
582 int symlink(const char *oldpath, const char *newpath)
583 {
584 int result = -1;
585 char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
586
587 canonicalize_int(oldpath, old_canonic);
588 canonicalize_int(newpath, new_canonic);
589
590 if FUNCTION_SANDBOX_SAFE
591 ("symlink", new_canonic) {
592 check_dlsym(symlink);
593 result = true_symlink(oldpath, newpath);
594 }
595
596 return result;
597 }
598
599 int truncate(const char *path, TRUNCATE_T length)
600 {
601 int result = -1;
602 char canonic[SB_PATH_MAX];
603
604 canonicalize_int(path, canonic);
605
606 if FUNCTION_SANDBOX_SAFE
607 ("truncate", canonic) {
608 check_dlsym(truncate);
609 result = true_truncate(path, length);
610 }
611
612 return result;
613 }
614
615 int unlink(const char *pathname)
616 {
617 int result = -1;
618 char canonic[SB_PATH_MAX];
619
620 canonicalize_int(pathname, canonic);
621
622 /* Hack to make sure sandboxed process cannot remove a device node,
623 * bug #79836. */
624 if (0 == strncmp(canonic, "/dev/", 5)) {
625 errno = EACCES;
626 return result;
627 }
628
629 if FUNCTION_SANDBOX_SAFE
630 ("unlink", canonic) {
631 check_dlsym(unlink);
632 result = true_unlink(pathname);
633 }
634
635 return result;
636 }
637
638 #if (GLIBC_MINOR >= 1)
639
640 int creat64(const char *pathname, __mode_t mode)
641 {
642 /* Is it a system call? */
643 int result = -1;
644 char canonic[SB_PATH_MAX];
645
646 canonicalize_int(pathname, canonic);
647
648 if FUNCTION_SANDBOX_SAFE
649 ("creat64", canonic) {
650 check_dlsym(open64);
651 result = true_open64(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
652 }
653
654 return result;
655 }
656
657 FILE *fopen64(const char *pathname, const char *mode)
658 {
659 FILE *result = NULL;
660 char canonic[SB_PATH_MAX];
661
662 canonicalize_ptr(pathname, canonic);
663
664 if FUNCTION_SANDBOX_SAFE_CHAR
665 ("fopen64", canonic, mode) {
666 check_dlsym(fopen64);
667 result = true_fopen64(pathname, mode);
668 }
669
670 return result;
671 }
672
673 int open64(const char *pathname, int flags, ...)
674 {
675 /* Eventually, there is a third parameter: it's mode_t mode */
676 va_list ap;
677 mode_t mode = 0;
678 int result = -1;
679 char canonic[SB_PATH_MAX];
680
681 if (flags & O_CREAT) {
682 va_start(ap, flags);
683 mode = va_arg(ap, mode_t);
684 va_end(ap);
685 }
686
687 canonicalize_int(pathname, canonic);
688
689 if FUNCTION_SANDBOX_SAFE_INT
690 ("open64", canonic, flags) {
691 check_dlsym(open64);
692 result = true_open64(pathname, flags, mode);
693 }
694
695 return result;
696 }
697
698 int truncate64(const char *path, __off64_t length)
699 {
700 int result = -1;
701 char canonic[SB_PATH_MAX];
702
703 canonicalize_int(path, canonic);
704
705 if FUNCTION_SANDBOX_SAFE
706 ("truncate64", canonic) {
707 check_dlsym(truncate64);
708 result = true_truncate64(path, length);
709 }
710
711 return result;
712 }
713
714 #endif /* GLIBC_MINOR >= 1 */
715
716 /*
717 * Exec Wrappers
718 */
719
720 int execve(const char *filename, char *const argv[], char *const envp[])
721 {
722 int old_errno = errno;
723 int result = -1;
724 int count = 0;
725 int env_len = 0;
726 char canonic[SB_PATH_MAX];
727 char **my_env = NULL;
728 int kill_env = 1;
729 /* We limit the size LD_PRELOAD can be here, but it should be enough */
730 char tmp_str[4096];
731
732 canonicalize_int(filename, canonic);
733
734 if FUNCTION_SANDBOX_SAFE
735 ("execve", canonic) {
736 while (envp[count] != NULL) {
737 /* Check if we do not have to do anything */
738 if (strstr(envp[count], "LD_PRELOAD=") == envp[count]) {
739 if (NULL != strstr(envp[count], sandbox_lib)) {
740 my_env = (char **)envp;
741 kill_env = 0;
742 goto end_loop;
743 }
744 }
745
746 /* If LD_PRELOAD is set and sandbox_lib not in it */
747 if (((strstr(envp[count], "LD_PRELOAD=") == envp[count]) &&
748 (NULL == strstr(envp[count], sandbox_lib))) ||
749 /* Or LD_PRELOAD is not set, and this is the last loop */
750 ((strstr(envp[count], "LD_PRELOAD=") != envp[count]) &&
751 (NULL == envp[count + 1]))) {
752 int i = 0;
753 int add_ldpreload = 0;
754 const int max_envp_len = strlen(envp[count]) + strlen(sandbox_lib) + 1;
755
756 /* Fail safe ... */
757 if (max_envp_len > 4096) {
758 fprintf(stderr, "sandbox: max_envp_len too big!\n");
759 errno = ENOMEM;
760 return result;
761 }
762
763 /* Calculate envp size */
764 my_env = (char **)envp;
765 do
766 env_len++;
767 while (NULL != *my_env++);
768
769 /* Should we add LD_PRELOAD ? */
770 if (strstr(envp[count], "LD_PRELOAD=") != envp[count])
771 add_ldpreload = 1;
772
773 my_env = (char **)calloc(env_len + add_ldpreload, sizeof(char *));
774 if (NULL == my_env) {
775 errno = ENOMEM;
776 return result;
777 }
778 /* Copy envp to my_env */
779 do
780 /* Leave a space for LD_PRELOAD if needed */
781 my_env[i + add_ldpreload] = envp[i];
782 while (NULL != envp[i++]);
783
784 /* Add 'LD_PRELOAD=' to the beginning of our new string */
785 snprintf(tmp_str, max_envp_len, "LD_PRELOAD=%s", sandbox_lib);
786
787 /* LD_PRELOAD already have variables other than sandbox_lib,
788 * thus we have to add sandbox_lib seperated via a whitespace. */
789 if (0 == add_ldpreload) {
790 snprintf(&(tmp_str[strlen(tmp_str)]),
791 max_envp_len - strlen(tmp_str) + 1, " %s",
792 &(envp[count][strlen("LD_PRELOAD=")]));
793 }
794
795 /* Valid string? */
796 tmp_str[max_envp_len] = '\0';
797
798 /* Ok, replace my_env[count] with our version that contains
799 * sandbox_lib ... */
800 if (1 == add_ldpreload)
801 /* We reserved a space for LD_PRELOAD above */
802 my_env[0] = tmp_str;
803 else
804 my_env[count] = tmp_str;
805
806 goto end_loop;
807 }
808 count++;
809 }
810
811 end_loop:
812 errno = old_errno;
813 check_dlsym(execve);
814 result = true_execve(filename, argv, my_env);
815 old_errno = errno;
816
817 if (my_env && kill_env) {
818 free(tmp_str);
819 free(my_env);
820 my_env = NULL;
821 }
822 }
823
824 errno = old_errno;
825
826 return result;
827 }
828
829 /*
830 * Internal Functions
831 */
832
833 #if (GLIBC_MINOR == 1)
834
835 /* This hack is needed for glibc 2.1.1 (and others?)
836 * (not really needed, but good example) */
837 extern int fclose(FILE *);
838 static int (*true_fclose) (FILE *) = NULL;
839 int fclose(FILE * file)
840 {
841 int result = -1;
842
843 check_dlsym(fclose);
844 result = true_fclose(file);
845
846 return result;
847 }
848
849 #endif /* GLIBC_MINOR == 1 */
850
851 static void init_context(sbcontext_t * context)
852 {
853 context->show_access_violation = 1;
854 context->deny_prefixes = NULL;
855 context->num_deny_prefixes = 0;
856 context->read_prefixes = NULL;
857 context->num_read_prefixes = 0;
858 context->write_prefixes = NULL;
859 context->num_write_prefixes = 0;
860 context->predict_prefixes = NULL;
861 context->num_predict_prefixes = 0;
862 context->write_denied_prefixes = NULL;
863 context->num_write_denied_prefixes = 0;
864 }
865
866 static int is_sandbox_pid()
867 {
868 int old_errno = errno;
869 int result = 0;
870 FILE *pids_stream = NULL;
871 int pids_file = -1;
872 int current_pid = 0;
873 int tmp_pid = 0;
874
875 init_wrappers();
876
877 pids_stream = true_fopen(sandbox_pids_file, "r");
878
879 if (NULL == pids_stream) {
880 perror(">>> pids file fopen");
881 } else {
882 pids_file = fileno(pids_stream);
883
884 if (pids_file < 0) {
885 perror(">>> pids file fileno");
886 } else {
887 current_pid = getpid();
888
889 while (EOF != fscanf(pids_stream, "%d\n", &tmp_pid)) {
890 if (tmp_pid == current_pid) {
891 result = 1;
892 break;
893 }
894 }
895 }
896 if (EOF == fclose(pids_stream)) {
897 perror(">>> pids file fclose");
898 }
899 pids_stream = NULL;
900 pids_file = -1;
901 }
902
903 errno = old_errno;
904
905 return result;
906 }
907
908 static void clean_env_entries(char ***prefixes_array, int *prefixes_num)
909 {
910 int old_errno = errno;
911 int i = 0;
912
913 if (NULL != *prefixes_array) {
914 for (i = 0; i < *prefixes_num; i++) {
915 if (NULL != (*prefixes_array)[i]) {
916 free((*prefixes_array)[i]);
917 (*prefixes_array)[i] = NULL;
918 }
919 }
920 if (*prefixes_array)
921 free(*prefixes_array);
922 *prefixes_array = NULL;
923 *prefixes_num = 0;
924 }
925
926 errno = old_errno;
927 }
928
929 static void init_env_entries(char ***prefixes_array, int *prefixes_num, char *env, int warn)
930 {
931 int old_errno = errno;
932 char *prefixes_env = getenv(env);
933
934 if (NULL == prefixes_env) {
935 fprintf(stderr, "Sandbox error : the %s environmental variable should be defined.\n", env);
936 } else {
937 char *buffer = NULL;
938 int prefixes_env_length = strlen(prefixes_env);
939 int i = 0;
940 int num_delimiters = 0;
941 char *token = NULL;
942 char *prefix = NULL;
943
944 for (i = 0; i < prefixes_env_length; i++) {
945 if (':' == prefixes_env[i]) {
946 num_delimiters++;
947 }
948 }
949
950 if (num_delimiters > 0) {
951 *prefixes_array = (char **)malloc((num_delimiters + 1) * sizeof(char *));
952 buffer = strndupa(prefixes_env, prefixes_env_length);
953
954 #ifdef REENTRANT_STRTOK
955 token = strtok_r(buffer, ":", &buffer);
956 #else
957 token = strtok(buffer, ":");
958 #endif
959
960 while ((NULL != token) && (strlen(token) > 0)) {
961 prefix = strndup(token, strlen(token));
962 (*prefixes_array)[(*prefixes_num)++] = filter_path(prefix);
963
964 #ifdef REENTRANT_STRTOK
965 token = strtok_r(NULL, ":", &buffer);
966 #else
967 token = strtok(NULL, ":");
968 #endif
969
970 if (prefix)
971 free(prefix);
972 prefix = NULL;
973 }
974 } else if (prefixes_env_length > 0) {
975 (*prefixes_array) = (char **)malloc(sizeof(char *));
976
977 (*prefixes_array)[(*prefixes_num)++] = filter_path(prefixes_env);
978 }
979 }
980
981 errno = old_errno;
982 }
983
984 static char *filter_path(const char *path)
985 {
986 int old_errno = errno;
987 char *filtered_path = (char *)malloc(SB_PATH_MAX * sizeof(char));
988
989 canonicalize_ptr(path, filtered_path);
990
991 errno = old_errno;
992
993 return filtered_path;
994 }
995
996 static int check_access(sbcontext_t * sbcontext, const char *func, const char *path)
997 {
998 int old_errno = errno;
999 int result = -1;
1000 int i = 0;
1001 char *filtered_path = filter_path(path);
1002
1003 if ('/' != filtered_path[0]) {
1004 errno = old_errno;
1005
1006 if (filtered_path)
1007 free(filtered_path);
1008 filtered_path = NULL;
1009
1010 return 0;
1011 }
1012
1013 if ((0 == strncmp(filtered_path, "/etc/ld.so.preload", 18)) &&
1014 (is_sandbox_pid())) {
1015 result = 1;
1016 }
1017
1018 if (-1 == result) {
1019 if (NULL != sbcontext->deny_prefixes) {
1020 for (i = 0; i < sbcontext->num_deny_prefixes; i++) {
1021 if (NULL != sbcontext->deny_prefixes[i]) {
1022 if (0 == strncmp(filtered_path,
1023 sbcontext->deny_prefixes[i],
1024 strlen(sbcontext->deny_prefixes[i]))) {
1025 result = 0;
1026 break;
1027 }
1028 }
1029 }
1030 }
1031
1032 if (-1 == result) {
1033 if ((NULL != sbcontext->read_prefixes) &&
1034 ((0 == strncmp(func, "open_rd", 7)) ||
1035 (0 == strncmp(func, "popen", 5)) ||
1036 (0 == strncmp(func, "opendir", 7)) ||
1037 (0 == strncmp(func, "system", 6)) ||
1038 (0 == strncmp(func, "execl", 5)) ||
1039 (0 == strncmp(func, "execlp", 6)) ||
1040 (0 == strncmp(func, "execle", 6)) ||
1041 (0 == strncmp(func, "execv", 5)) ||
1042 (0 == strncmp(func, "execvp", 6)) ||
1043 (0 == strncmp(func, "execve", 6)))) {
1044 for (i = 0; i < sbcontext->num_read_prefixes; i++) {
1045 if (NULL != sbcontext->read_prefixes[i]) {
1046 if (0 == strncmp(filtered_path,
1047 sbcontext->read_prefixes[i],
1048 strlen(sbcontext->read_prefixes[i]))) {
1049 result = 1;
1050 break;
1051 }
1052 }
1053 }
1054 } else if ((NULL != sbcontext->write_prefixes) &&
1055 ((0 == strncmp(func, "open_wr", 7)) ||
1056 (0 == strncmp(func, "creat", 5)) ||
1057 (0 == strncmp(func, "creat64", 7)) ||
1058 (0 == strncmp(func, "mkdir", 5)) ||
1059 (0 == strncmp(func, "mknod", 5)) ||
1060 (0 == strncmp(func, "mkfifo", 6)) ||
1061 (0 == strncmp(func, "link", 4)) ||
1062 (0 == strncmp(func, "symlink", 7)) ||
1063 (0 == strncmp(func, "rename", 6)) ||
1064 (0 == strncmp(func, "utime", 5)) ||
1065 (0 == strncmp(func, "utimes", 6)) ||
1066 (0 == strncmp(func, "unlink", 6)) ||
1067 (0 == strncmp(func, "rmdir", 5)) ||
1068 (0 == strncmp(func, "chown", 5)) ||
1069 (0 == strncmp(func, "lchown", 6)) ||
1070 (0 == strncmp(func, "chmod", 5)) ||
1071 (0 == strncmp(func, "truncate", 8)) ||
1072 (0 == strncmp(func, "ftruncate", 9)) ||
1073 (0 == strncmp(func, "truncate64", 10)) ||
1074 (0 == strncmp(func, "ftruncate64", 11)))) {
1075
1076 for (i = 0; i < sbcontext->num_write_denied_prefixes; i++) {
1077 if (NULL != sbcontext->write_denied_prefixes[i]) {
1078 if (0 == strncmp(filtered_path,
1079 sbcontext->write_denied_prefixes[i],
1080 strlen(sbcontext->write_denied_prefixes[i]))) {
1081 result = 0;
1082 break;
1083 }
1084 }
1085 }
1086
1087 if (-1 == result) {
1088 for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1089 if (NULL != sbcontext->write_prefixes[i]) {
1090 if (0 == strncmp(filtered_path,
1091 sbcontext->write_prefixes[i],
1092 strlen(sbcontext->write_prefixes[i]))) {
1093 result = 1;
1094 break;
1095 }
1096 }
1097 }
1098
1099 if (-1 == result) {
1100 if (-1 == result) {
1101 for (i = 0; i < sbcontext->num_predict_prefixes; i++) {
1102 if (NULL != sbcontext->predict_prefixes[i]) {
1103 if (0 == strncmp(filtered_path,
1104 sbcontext->predict_prefixes[i],
1105 strlen(sbcontext->predict_prefixes[i]))) {
1106 sbcontext->show_access_violation = 0;
1107 result = 0;
1108 break;
1109 }
1110 }
1111 }
1112 }
1113 }
1114 }
1115 }
1116 }
1117 }
1118
1119 if (-1 == result) {
1120 result = 0;
1121 }
1122
1123 if (filtered_path)
1124 free(filtered_path);
1125 filtered_path = NULL;
1126
1127 errno = old_errno;
1128
1129 return result;
1130 }
1131
1132 static int check_syscall(sbcontext_t * sbcontext, const char *func, const char *file)
1133 {
1134 int old_errno = errno;
1135 int result = 1;
1136 struct stat log_stat;
1137 char *log_path = NULL;
1138 char *absolute_path = NULL;
1139 char *tmp_buffer = NULL;
1140 int log_file = 0;
1141 struct stat debug_log_stat;
1142 char *debug_log_env = NULL;
1143 char *debug_log_path = NULL;
1144 int debug_log_file = 0;
1145 char buffer[512];
1146 char *dpath = NULL;
1147
1148 init_wrappers();
1149
1150 if ('/' == file[0]) {
1151 absolute_path = (char *)malloc((strlen(file) + 1) * sizeof(char));
1152 sprintf(absolute_path, "%s", file);
1153 } else {
1154 tmp_buffer = (char *)malloc(SB_PATH_MAX * sizeof(char));
1155 egetcwd(tmp_buffer, SB_PATH_MAX - 1);
1156 absolute_path = (char *)malloc((strlen(tmp_buffer) + 1 + strlen(file) + 1) * sizeof(char));
1157 sprintf(absolute_path, "%s/%s", tmp_buffer, file);
1158 if (tmp_buffer)
1159 free(tmp_buffer);
1160 tmp_buffer = NULL;
1161 }
1162
1163 log_path = getenv("SANDBOX_LOG");
1164 debug_log_env = getenv("SANDBOX_DEBUG");
1165 debug_log_path = getenv("SANDBOX_DEBUG_LOG");
1166
1167 if (((NULL == log_path) ||
1168 (0 != strncmp(absolute_path, log_path, strlen(log_path)))) &&
1169 ((NULL == debug_log_env) ||
1170 (NULL == debug_log_path) ||
1171 (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path)))) &&
1172 (0 == check_access(sbcontext, func, absolute_path))) {
1173 if (1 == sbcontext->show_access_violation) {
1174 fprintf(stderr, "\e[31;01mACCESS DENIED\033[0m %s:%*s%s\n",
1175 func, (int)(10 - strlen(func)), "", absolute_path);
1176
1177 if (NULL != log_path) {
1178 sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1179 // log_path somehow gets corrupted. figuring out why would be good.
1180 dpath = strdup(log_path);
1181 if ((0 == lstat(log_path, &log_stat)) &&
1182 (0 == S_ISREG(log_stat.st_mode))) {
1183 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m %s already exists and is not a regular file.\n", dpath);
1184 } else if (0 == check_access(sbcontext, "open_wr", dpath)) {
1185 unsetenv("SANDBOX_LOG");
1186 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m SANDBOX_LOG %s isn't allowed via SANDBOX_WRITE\n", dpath);
1187 } else {
1188 log_file = true_open(dpath, O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1189 if (log_file >= 0) {
1190 write(log_file, buffer, strlen(buffer));
1191 close(log_file);
1192 }
1193 }
1194 free(dpath);
1195 }
1196 }
1197
1198 result = 0;
1199 } else if (NULL != debug_log_env) {
1200 if (NULL != debug_log_path) {
1201 if (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path))) {
1202 sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1203 //debug_log_path somehow gets corupted, same thing as log_path above.
1204 dpath = strdup(debug_log_path);
1205 if ((0 == lstat(debug_log_path, &debug_log_stat))
1206 && (0 == S_ISREG(debug_log_stat.st_mode))) {
1207 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m %s already exists and is not a regular file.\n",
1208 debug_log_path);
1209 } else if (0 == check_access(sbcontext, "open_wr", dpath)) {
1210 unsetenv("SANDBOX_DEBUG");
1211 unsetenv("SANDBOX_DEBUG_LOG");
1212 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m SANDBOX_DEBUG_LOG %s isn't allowed by SANDBOX_WRITE.\n",
1213 dpath);
1214 } else {
1215 debug_log_file = true_open(dpath, O_APPEND | O_WRONLY |
1216 O_CREAT, S_IRUSR | S_IWUSR |
1217 S_IRGRP | S_IROTH);
1218 if (debug_log_file >= 0) {
1219 write(debug_log_file, buffer, strlen(buffer));
1220 close(debug_log_file);
1221 }
1222 }
1223 free(dpath);
1224 }
1225 } else {
1226 fprintf(stderr, "\e[32;01mACCESS ALLOWED\033[0m %s:%*s%s\n",
1227 func, (int)(10 - strlen(func)), "", absolute_path);
1228 }
1229 }
1230
1231 if (absolute_path)
1232 free(absolute_path);
1233 absolute_path = NULL;
1234
1235 errno = old_errno;
1236
1237 return result;
1238 }
1239
1240 static int is_sandbox_on()
1241 {
1242 int old_errno = errno;
1243
1244 /* $SANDBOX_ACTIVE is an env variable that should ONLY
1245 * be used internal by sandbox.c and libsanbox.c. External
1246 * sources should NEVER set it, else the sandbox is enabled
1247 * in some cases when run in parallel with another sandbox,
1248 * but not even in the sandbox shell.
1249 *
1250 * Azarah (3 Aug 2002)
1251 */
1252 if ((NULL != getenv("SANDBOX_ON")) &&
1253 (0 == strncmp(getenv("SANDBOX_ON"), "1", 1)) &&
1254 (NULL != getenv("SANDBOX_ACTIVE")) &&
1255 (0 == strncmp(getenv("SANDBOX_ACTIVE"), "armedandready", 13))) {
1256 errno = old_errno;
1257 return 1;
1258 } else {
1259 errno = old_errno;
1260 return 0;
1261 }
1262 }
1263
1264 static int before_syscall(const char *func, const char *file)
1265 {
1266 int old_errno = errno;
1267 int result = 1;
1268 static sbcontext_t sbcontext;
1269
1270 if (!strlen(file)) {
1271 /* The file/directory does not exist */
1272 errno = ENOENT;
1273 return 0;
1274 }
1275
1276 init_context(&sbcontext);
1277
1278 init_env_entries(&(sbcontext.deny_prefixes),
1279 &(sbcontext.num_deny_prefixes), "SANDBOX_DENY", 1);
1280 init_env_entries(&(sbcontext.read_prefixes),
1281 &(sbcontext.num_read_prefixes), "SANDBOX_READ", 1);
1282 init_env_entries(&(sbcontext.write_prefixes),
1283 &(sbcontext.num_write_prefixes), "SANDBOX_WRITE", 1);
1284 init_env_entries(&(sbcontext.predict_prefixes),
1285 &(sbcontext.num_predict_prefixes), "SANDBOX_PREDICT", 1);
1286
1287 result = check_syscall(&sbcontext, func, file);
1288
1289 clean_env_entries(&(sbcontext.deny_prefixes),
1290 &(sbcontext.num_deny_prefixes));
1291 clean_env_entries(&(sbcontext.read_prefixes),
1292 &(sbcontext.num_read_prefixes));
1293 clean_env_entries(&(sbcontext.write_prefixes),
1294 &(sbcontext.num_write_prefixes));
1295 clean_env_entries(&(sbcontext.predict_prefixes),
1296 &(sbcontext.num_predict_prefixes));
1297
1298 errno = old_errno;
1299
1300 if (0 == result) {
1301 errno = EACCES;
1302 }
1303
1304 return result;
1305 }
1306
1307 static int before_syscall_open_int(const char *func, const char *file, int flags)
1308 {
1309 if ((flags & O_WRONLY) || (flags & O_RDWR)) {
1310 return before_syscall("open_wr", file);
1311 } else {
1312 return before_syscall("open_rd", file);
1313 }
1314 }
1315
1316 static int before_syscall_open_char(const char *func, const char *file, const char *mode)
1317 {
1318 if (*mode == 'r' && ((strcmp(mode, "r") == 0) ||
1319 /* The strspn accept args are known non-writable modifiers */
1320 (strlen(++mode) == strspn(mode, "xbtmc")))) {
1321 return before_syscall("open_rd", file);
1322 } else {
1323 return before_syscall("open_wr", file);
1324 }
1325 }
1326
1327 #include "getcwd.c"
1328 #include "canonicalize.c"
1329
1330 // vim:noexpandtab noai:cindent ai

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20