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

Contents of /trunk/libsandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (show annotations) (download) (as text)
Wed Jul 6 00:12:49 2005 UTC (9 years, 2 months ago) by azarah
File MIME type: text/x-csrc
File size: 39915 byte(s)
Remove unused variables.
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 <libgen.h>
57 #include <stdarg.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/file.h>
62 #include <sys/stat.h>
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <unistd.h>
66 #include <utime.h>
67 #include "config.h"
68 #include "localdecls.h"
69 #include "symbols.h"
70
71 #ifdef SB_MEM_DEBUG
72 # include <mcheck.h>
73 #endif
74
75 #ifdef WRAP_MKNOD
76 # undef __xmknod
77 #endif
78
79 #undef open
80 #undef open64
81
82 //#include "localdecls.h"
83 #include "sandbox.h"
84
85 /* Macros to check if a function should be executed */
86 #define FUNCTION_SANDBOX_SAFE(func, path) \
87 ((0 == is_sandbox_on()) || (1 == before_syscall(func, path)))
88
89 #define FUNCTION_SANDBOX_SAFE_ACCESS(func, path, flags) \
90 ((0 == is_sandbox_on()) || (1 == before_syscall_access(func, path, flags)))
91
92 #define FUNCTION_SANDBOX_SAFE_OPEN_INT(func, path, flags) \
93 ((0 == is_sandbox_on()) || (1 == before_syscall_open_int(func, path, flags)))
94
95 #define FUNCTION_SANDBOX_SAFE_OPEN_CHAR(func, path, mode) \
96 ((0 == is_sandbox_on()) || (1 == before_syscall_open_char(func, path, mode)))
97
98 /* Macro to check if a wrapper is defined, if not
99 * then try to resolve it again. */
100 #define check_dlsym(name) \
101 { \
102 int old_errno=errno; \
103 if (!true_ ## name) \
104 true_ ## name = get_dlsym(symname_ ## name, symver_ ## name); \
105 errno=old_errno; \
106 }
107
108 static char sandbox_lib[SB_PATH_MAX];
109 //static char sandbox_pids_file[255];
110 static char *sandbox_pids_file;
111
112 typedef struct {
113 int show_access_violation;
114 char **deny_prefixes;
115 int num_deny_prefixes;
116 char **read_prefixes;
117 int num_read_prefixes;
118 char **write_prefixes;
119 int num_write_prefixes;
120 char **predict_prefixes;
121 int num_predict_prefixes;
122 char **write_denied_prefixes;
123 int num_write_denied_prefixes;
124 } sbcontext_t;
125
126 static sbcontext_t sbcontext;
127 static char **cached_env_vars;
128 static int sb_init = 0;
129 static int sb_path_size_warning = 0;
130
131 void __attribute__ ((constructor)) libsb_init(void);
132 void __attribute__ ((destructor)) libsb_fini(void);
133
134 /* glibc modified realpath() functions */
135 static char *erealpath(const char *, char *);
136 /* glibc modified getcwd() functions */
137 static char *egetcwd(char *, size_t);
138
139 static void init_wrappers(void);
140 static void *get_dlsym(const char *, const char *);
141 static int canonicalize(const char *, char *);
142 static char *filter_path(const char *, int);
143 static int check_access(sbcontext_t *, const char *, const char *, const char *);
144 static int check_syscall(sbcontext_t *, const char *, const char *);
145 static int before_syscall(const char *, const char *);
146 static int before_syscall_access(const char *, const char *, int);
147 static int before_syscall_open_int(const char *, const char *, int);
148 static int before_syscall_open_char(const char *, const char *, const char *);
149 static void clean_env_entries(char ***, int *);
150 static void init_context(sbcontext_t *);
151 static void init_env_entries(char ***, int *, const char *, const char *, int);
152 static int is_sandbox_on();
153 static int is_sandbox_pid();
154
155 /* Wrapped functions */
156
157 extern int chmod(const char *, mode_t);
158 static int (*true_chmod) (const char *, mode_t) = NULL;
159 extern int chown(const char *, uid_t, gid_t);
160 static int (*true_chown) (const char *, uid_t, gid_t) = NULL;
161 extern int creat(const char *, mode_t);
162 static int (*true_creat) (const char *, mode_t) = NULL;
163 extern FILE *fopen(const char *, const char *);
164 static FILE *(*true_fopen) (const char *, const char *) = NULL;
165 extern int lchown(const char *, uid_t, gid_t);
166 static int (*true_lchown) (const char *, uid_t, gid_t) = NULL;
167 extern int link(const char *, const char *);
168 static int (*true_link) (const char *, const char *) = NULL;
169 extern int mkdir(const char *, mode_t);
170 static int (*true_mkdir) (const char *, mode_t) = NULL;
171 extern DIR *opendir(const char *);
172 static DIR *(*true_opendir) (const char *) = NULL;
173 #ifdef WRAP_MKNOD
174 extern int __xmknod(const char *, mode_t, dev_t);
175 static int (*true___xmknod) (const char *, mode_t, dev_t) = NULL;
176 #endif
177 extern int access(const char *, int);
178 static int (*true_access) (const char *, int) = NULL;
179 extern int open(const char *, int, ...);
180 static int (*true_open) (const char *, int, ...) = NULL;
181 extern int rename(const char *, const char *);
182 static int (*true_rename) (const char *, const char *) = NULL;
183 extern int rmdir(const char *);
184 static int (*true_rmdir) (const char *) = NULL;
185 extern int symlink(const char *, const char *);
186 static int (*true_symlink) (const char *, const char *) = NULL;
187 extern int truncate(const char *, TRUNCATE_T);
188 static int (*true_truncate) (const char *, TRUNCATE_T) = NULL;
189 extern int unlink(const char *);
190 static int (*true_unlink) (const char *) = NULL;
191
192 #if (GLIBC_MINOR >= 1)
193
194 extern int creat64(const char *, __mode_t);
195 static int (*true_creat64) (const char *, __mode_t) = NULL;
196 extern FILE *fopen64(const char *, const char *);
197 static FILE *(*true_fopen64) (const char *, const char *) = NULL;
198 extern int open64(const char *, int, ...);
199 static int (*true_open64) (const char *, int, ...) = NULL;
200 extern int truncate64(const char *, __off64_t);
201 static int (*true_truncate64) (const char *, __off64_t) = NULL;
202
203 #endif
204
205 extern int execve(const char *filename, char *const argv[], char *const envp[]);
206 static int (*true_execve) (const char *, char *const[], char *const[]);
207
208 /*
209 * Initialize the shabang
210 */
211
212 static void init_wrappers(void)
213 {
214 check_dlsym(chmod);
215 check_dlsym(chown);
216 check_dlsym(creat);
217 check_dlsym(fopen);
218 check_dlsym(lchown);
219 check_dlsym(link);
220 check_dlsym(mkdir);
221 check_dlsym(opendir);
222 #ifdef WRAP_MKNOD
223 check_dlsym(__xmknod);
224 #endif
225 check_dlsym(access);
226 check_dlsym(open);
227 check_dlsym(rename);
228 check_dlsym(rmdir);
229 check_dlsym(symlink);
230 check_dlsym(truncate);
231 check_dlsym(unlink);
232
233 #if (GLIBC_MINOR >= 1)
234 check_dlsym(creat64);
235 check_dlsym(fopen64);
236 check_dlsym(open64);
237 check_dlsym(truncate64);
238 #endif
239
240 check_dlsym(execve);
241 }
242
243 static void *libc_handle = NULL;
244
245 static void *get_dlsym(const char *symname, const char *symver)
246 {
247 void *symaddr = NULL;
248
249 if (NULL == libc_handle) {
250 #ifdef BROKEN_RTLD_NEXT
251 libc_handle = dlopen(LIBC_VERSION, RTLD_LAZY);
252 if (!libc_handle) {
253 printf("libsandbox.so: Can't dlopen libc: %s\n", dlerror());
254 abort();
255 }
256 #else
257 libc_handle = RTLD_NEXT;
258 #endif
259 }
260
261 if (NULL == symver)
262 symaddr = dlsym(libc_handle, symname);
263 else
264 symaddr = dlvsym(libc_handle, symname, symver);
265 if (!symaddr) {
266 printf("libsandbox.so: Can't resolve %s: %s\n", symname, dlerror());
267 abort();
268 }
269
270 return symaddr;
271 }
272
273
274 void __attribute__ ((destructor)) libsb_fini(void)
275 {
276 int x;
277 if(NULL != cached_env_vars) {
278 for(x=0; x < 4; x++) {
279 if(NULL != cached_env_vars[x])
280 free(cached_env_vars[x]);
281 }
282 free(cached_env_vars);
283 }
284 clean_env_entries(&(sbcontext.deny_prefixes),
285 &(sbcontext.num_deny_prefixes));
286 clean_env_entries(&(sbcontext.read_prefixes),
287 &(sbcontext.num_read_prefixes));
288 clean_env_entries(&(sbcontext.write_prefixes),
289 &(sbcontext.num_write_prefixes));
290 clean_env_entries(&(sbcontext.predict_prefixes),
291 &(sbcontext.num_predict_prefixes));
292
293 free(sandbox_pids_file);
294 }
295
296 void __attribute__ ((constructor)) libsb_init(void)
297 {
298 int old_errno = errno;
299 char tmp_dir[SB_PATH_MAX];
300
301 #ifdef SB_MEM_DEBUG
302 mtrace();
303 #endif
304
305 init_wrappers();
306
307 /* Get the path and name to this library */
308 snprintf(sandbox_lib, SB_PATH_MAX, "%s", get_sandbox_lib("/"));
309
310 if (-1 == get_tmp_dir(tmp_dir))
311 snprintf(tmp_dir, SB_PATH_MAX, "%s", TMPDIR);
312
313 /* Generate sandbox pids-file path */
314 sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
315
316 // sb_init = 1;
317
318 errno = old_errno;
319 }
320
321 static int canonicalize(const char *path, char *resolved_path)
322 {
323 int old_errno = errno;
324 char *retval;
325
326 *resolved_path = '\0';
327
328 /* If path == NULL, return or we get a segfault */
329 if (NULL == path) {
330 errno = EINVAL;
331 return -1;
332 }
333
334 /* Do not try to resolve an empty path */
335 if ('\0' == path[0]) {
336 errno = old_errno;
337 return 0;
338 }
339
340 retval = erealpath(path, resolved_path);
341
342 if ((NULL == retval) && (path[0] != '/')) {
343 /* The path could not be canonicalized, append it
344 * to the current working directory if it was not
345 * an absolute path
346 */
347
348 if (ENAMETOOLONG == errno)
349 return -1;
350
351 if (NULL == egetcwd(resolved_path, SB_PATH_MAX - 2))
352 return -1;
353 snprintf((char *)(resolved_path + strlen(resolved_path)),
354 SB_PATH_MAX - strlen(resolved_path), "/%s", path);
355
356 if (NULL == erealpath(resolved_path, resolved_path)) {
357 if (errno == ENAMETOOLONG) {
358 /* The resolved path is too long for the buffer to hold */
359 return -1;
360 } else {
361 /* Whatever it resolved, is not a valid path */
362 errno = ENOENT;
363 return -1;
364 }
365 }
366
367 } else if ((NULL == retval) && (path[0] == '/')) {
368 /* Whatever it resolved, is not a valid path */
369 errno = ENOENT;
370 return -1;
371 }
372
373 errno = old_errno;
374 return 0;
375 }
376
377 static char *filter_path(const char *path, int follow_link)
378 {
379 int old_errno = errno;
380 char tmp_str1[SB_PATH_MAX], tmp_str2[SB_PATH_MAX];
381 char *dname, *bname;
382 char *filtered_path;
383
384 if (NULL == path)
385 return NULL;
386
387 filtered_path = malloc(SB_PATH_MAX * sizeof(char));
388 if (NULL == filtered_path)
389 return NULL;
390
391 if (0 == follow_link) {
392 if (-1 == canonicalize(path, filtered_path))
393 return NULL;
394 } else {
395 /* Basically we get the realpath which should resolve symlinks,
396 * etc. If that fails (might not exist), we try to get the
397 * realpath of the parent directory, as that should hopefully
398 * exist. If all else fails, just go with canonicalize */
399 if (NULL == realpath(path, filtered_path)) {
400 snprintf(tmp_str1, SB_PATH_MAX, "%s", path);
401
402 dname = dirname(tmp_str1);
403
404 /* If not, then check if we can resolve the
405 * parent directory */
406 if (NULL == realpath(dname, filtered_path)) {
407 /* Fall back to canonicalize */
408 if (-1 == canonicalize(path, filtered_path))
409 return NULL;
410 } else {
411 /* OK, now add the basename to keep our access
412 * checking happy (don't want '/usr/lib' if we
413 * tried to do something with non-existing
414 * file '/usr/lib/cf*' ...) */
415 snprintf(tmp_str2, SB_PATH_MAX, "%s", path);
416
417 bname = basename(tmp_str2);
418 snprintf((char *)(filtered_path + strlen(filtered_path)),
419 SB_PATH_MAX - strlen(filtered_path), "%s%s",
420 (filtered_path[strlen(filtered_path) - 1] != '/') ? "/" : "",
421 bname);
422 }
423 }
424 }
425
426 errno = old_errno;
427
428 return filtered_path;
429 }
430
431 /*
432 * Wrapper Functions
433 */
434
435 int chmod(const char *path, mode_t mode)
436 {
437 int result = -1;
438
439 if FUNCTION_SANDBOX_SAFE("chmod", path) {
440 check_dlsym(chmod);
441 result = true_chmod(path, mode);
442 }
443
444 return result;
445 }
446
447 int chown(const char *path, uid_t owner, gid_t group)
448 {
449 int result = -1;
450
451 if FUNCTION_SANDBOX_SAFE("chown", path) {
452 check_dlsym(chown);
453 result = true_chown(path, owner, group);
454 }
455
456 return result;
457 }
458
459 int creat(const char *pathname, mode_t mode)
460 {
461 /* Is it a system call? */
462 int result = -1;
463
464 if FUNCTION_SANDBOX_SAFE("creat", pathname) {
465 check_dlsym(open);
466 result = true_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
467 }
468
469 return result;
470 }
471
472 FILE *fopen(const char *pathname, const char *mode)
473 {
474 FILE *result = NULL;
475
476 if FUNCTION_SANDBOX_SAFE_OPEN_CHAR("fopen", pathname, mode) {
477 check_dlsym(fopen);
478 result = true_fopen(pathname, mode);
479 }
480
481 return result;
482 }
483
484 int lchown(const char *path, uid_t owner, gid_t group)
485 {
486 int result = -1;
487
488 if FUNCTION_SANDBOX_SAFE("lchown", path) {
489 check_dlsym(lchown);
490 result = true_lchown(path, owner, group);
491 }
492
493 return result;
494 }
495
496 int link(const char *oldpath, const char *newpath)
497 {
498 int result = -1;
499
500 if FUNCTION_SANDBOX_SAFE("link", newpath) {
501 check_dlsym(link);
502 result = true_link(oldpath, newpath);
503 }
504
505 return result;
506 }
507
508 int mkdir(const char *pathname, mode_t mode)
509 {
510 struct stat st;
511 int result = -1, my_errno = errno;
512 char canonic[SB_PATH_MAX];
513
514 if (-1 == canonicalize(pathname, canonic))
515 /* Path is too long to canonicalize, do not fail, but just let
516 * the real function handle it (see bug #94630 and #21766). */
517 if (ENAMETOOLONG != errno)
518 return -1;
519
520 /* XXX: Hack to prevent errors if the directory exist,
521 * and are not writable - we rather return EEXIST rather
522 * than failing */
523 if (0 == lstat(canonic, &st)) {
524 errno = EEXIST;
525 return -1;
526 }
527 errno = my_errno;
528
529 if FUNCTION_SANDBOX_SAFE("mkdir", pathname) {
530 check_dlsym(mkdir);
531 result = true_mkdir(pathname, mode);
532 }
533
534 return result;
535 }
536
537 DIR *opendir(const char *name)
538 {
539 DIR *result = NULL;
540
541 if FUNCTION_SANDBOX_SAFE("opendir", name) {
542 check_dlsym(opendir);
543 result = true_opendir(name);
544 }
545
546 return result;
547 }
548
549 #ifdef WRAP_MKNOD
550
551 int __xmknod(const char *pathname, mode_t mode, dev_t dev)
552 {
553 int result = -1;
554
555 if FUNCTION_SANDBOX_SAFE("__xmknod", pathname) {
556 check_dlsym(__xmknod);
557 result = true___xmknod(pathname, mode, dev);
558 }
559
560 return result;
561 }
562
563 #endif
564
565 int access(const char *pathname, int mode)
566 {
567 int result = -1;
568
569 if FUNCTION_SANDBOX_SAFE_ACCESS("access", pathname, mode) {
570 check_dlsym(access);
571 result = true_access(pathname, mode);
572 }
573
574 return result;
575 }
576
577 /* Eventually, there is a third parameter: it's mode_t mode */
578 int open(const char *pathname, int flags, ...)
579 {
580 va_list ap;
581 mode_t mode = 0;
582 int result = -1;
583
584 if (flags & O_CREAT) {
585 va_start(ap, flags);
586 mode = va_arg(ap, mode_t);
587 va_end(ap);
588 }
589
590 if FUNCTION_SANDBOX_SAFE_OPEN_INT("open", pathname, flags) {
591 check_dlsym(open);
592 result = true_open(pathname, flags, mode);
593 }
594
595 return result;
596 }
597
598 int rename(const char *oldpath, const char *newpath)
599 {
600 int result = -1;
601
602 if (FUNCTION_SANDBOX_SAFE("rename", oldpath) &&
603 FUNCTION_SANDBOX_SAFE("rename", newpath)) {
604 check_dlsym(rename);
605 result = true_rename(oldpath, newpath);
606 }
607
608 return result;
609 }
610
611 int rmdir(const char *pathname)
612 {
613 int result = -1;
614
615 if FUNCTION_SANDBOX_SAFE("rmdir", pathname) {
616 check_dlsym(rmdir);
617 result = true_rmdir(pathname);
618 }
619
620 return result;
621 }
622
623 int symlink(const char *oldpath, const char *newpath)
624 {
625 int result = -1;
626
627 if FUNCTION_SANDBOX_SAFE("symlink", newpath) {
628 check_dlsym(symlink);
629 result = true_symlink(oldpath, newpath);
630 }
631
632 return result;
633 }
634
635 int truncate(const char *path, TRUNCATE_T length)
636 {
637 int result = -1;
638
639 if FUNCTION_SANDBOX_SAFE("truncate", path) {
640 check_dlsym(truncate);
641 result = true_truncate(path, length);
642 }
643
644 return result;
645 }
646
647 int unlink(const char *pathname)
648 {
649 int result = -1;
650 char canonic[SB_PATH_MAX];
651
652 if (-1 == canonicalize(pathname, canonic))
653 /* Path is too long to canonicalize, do not fail, but just let
654 * the real function handle it (see bug #94630 and #21766). */
655 if (ENAMETOOLONG != errno)
656 return -1;
657
658 /* XXX: Hack to make sure sandboxed process cannot remove
659 * a device node, bug #79836. */
660 if ((0 == strncmp(canonic, "/dev/null", 9)) ||
661 (0 == strncmp(canonic, "/dev/zero", 9))) {
662 errno = EACCES;
663 return result;
664 }
665
666 if FUNCTION_SANDBOX_SAFE("unlink", pathname) {
667 check_dlsym(unlink);
668 result = true_unlink(pathname);
669 }
670
671 return result;
672 }
673
674 #if (GLIBC_MINOR >= 1)
675
676 int creat64(const char *pathname, __mode_t mode)
677 {
678 /* Is it a system call? */
679 int result = -1;
680
681 if FUNCTION_SANDBOX_SAFE("creat64", pathname) {
682 check_dlsym(open64);
683 result = true_open64(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
684 }
685
686 return result;
687 }
688
689 FILE *fopen64(const char *pathname, const char *mode)
690 {
691 FILE *result = NULL;
692
693 if FUNCTION_SANDBOX_SAFE_OPEN_CHAR("fopen64", pathname, mode) {
694 check_dlsym(fopen64);
695 result = true_fopen64(pathname, mode);
696 }
697
698 return result;
699 }
700
701 /* Eventually, there is a third parameter: it's mode_t mode */
702 int open64(const char *pathname, int flags, ...)
703 {
704 va_list ap;
705 mode_t mode = 0;
706 int result = -1;
707
708 if (flags & O_CREAT) {
709 va_start(ap, flags);
710 mode = va_arg(ap, mode_t);
711 va_end(ap);
712 }
713
714 if FUNCTION_SANDBOX_SAFE_OPEN_INT("open64", pathname, flags) {
715 check_dlsym(open64);
716 result = true_open64(pathname, flags, mode);
717 }
718
719 return result;
720 }
721
722 int truncate64(const char *path, __off64_t length)
723 {
724 int result = -1;
725
726 if FUNCTION_SANDBOX_SAFE("truncate64", path) {
727 check_dlsym(truncate64);
728 result = true_truncate64(path, length);
729 }
730
731 return result;
732 }
733
734 #endif /* GLIBC_MINOR >= 1 */
735
736 /*
737 * Exec Wrappers
738 */
739
740 int execve(const char *filename, char *const argv[], char *const envp[])
741 {
742 int old_errno = errno;
743 int result = -1;
744 int count = 0;
745 int env_len = 0;
746 char **my_env = NULL;
747 int kill_env = 1;
748 /* We limit the size LD_PRELOAD can be here, but it should be enough */
749 char tmp_str[SB_BUF_LEN];
750
751 if FUNCTION_SANDBOX_SAFE("execve", filename) {
752 while (envp[count] != NULL) {
753 /* Check if we do not have to do anything */
754 if (strstr(envp[count], LD_PRELOAD_EQ) == envp[count]) {
755 if (NULL != strstr(envp[count], sandbox_lib)) {
756 my_env = (char **)envp;
757 kill_env = 0;
758 goto end_loop;
759 }
760 }
761
762 /* If LD_PRELOAD is set and sandbox_lib not in it */
763 if (((strstr(envp[count], LD_PRELOAD_EQ) == envp[count]) &&
764 (NULL == strstr(envp[count], sandbox_lib))) ||
765 /* Or LD_PRELOAD is not set, and this is the last loop */
766 ((strstr(envp[count], LD_PRELOAD_EQ) != envp[count]) &&
767 (NULL == envp[count + 1]))) {
768 int i = 0;
769 int add_ldpreload = 0;
770 const int max_envp_len = strlen(envp[count]) + strlen(sandbox_lib) + 1;
771
772 /* Fail safe ... */
773 if (max_envp_len > SB_BUF_LEN) {
774 fprintf(stderr, "sandbox: max_envp_len too big!\n");
775 errno = ENOMEM;
776 return result;
777 }
778
779 /* Calculate envp size */
780 my_env = (char **)envp;
781 do
782 env_len++;
783 while (NULL != *my_env++);
784
785 /* Should we add LD_PRELOAD ? */
786 if (strstr(envp[count], LD_PRELOAD_EQ) != envp[count])
787 add_ldpreload = 1;
788
789 my_env = (char **)calloc(env_len + add_ldpreload, sizeof(char *));
790 if (NULL == my_env) {
791 errno = ENOMEM;
792 return result;
793 }
794 /* Copy envp to my_env */
795 do
796 /* Leave a space for LD_PRELOAD if needed */
797 my_env[i + add_ldpreload] = envp[i];
798 while (NULL != envp[i++]);
799
800 /* Add 'LD_PRELOAD=' to the beginning of our new string */
801 snprintf(tmp_str, max_envp_len, "%s%s", LD_PRELOAD_EQ, sandbox_lib);
802
803 /* LD_PRELOAD already have variables other than sandbox_lib,
804 * thus we have to add sandbox_lib seperated via a whitespace. */
805 if (0 == add_ldpreload) {
806 snprintf((char *)(tmp_str + strlen(tmp_str)),
807 max_envp_len - strlen(tmp_str) + 1, " %s",
808 (char *)(envp[count] + strlen(LD_PRELOAD_EQ)));
809 }
810
811 /* Valid string? */
812 tmp_str[max_envp_len] = '\0';
813
814 /* Ok, replace my_env[count] with our version that contains
815 * sandbox_lib ... */
816 if (1 == add_ldpreload)
817 /* We reserved a space for LD_PRELOAD above */
818 my_env[0] = tmp_str;
819 else
820 my_env[count] = tmp_str;
821
822 goto end_loop;
823 }
824 count++;
825 }
826
827 end_loop:
828 errno = old_errno;
829 check_dlsym(execve);
830 result = true_execve(filename, argv, my_env);
831 old_errno = errno;
832
833 if (my_env && kill_env)
834 free(my_env);
835 }
836
837 errno = old_errno;
838
839 return result;
840 }
841
842 /*
843 * Internal Functions
844 */
845
846 #if (GLIBC_MINOR == 1)
847
848 /* This hack is needed for glibc 2.1.1 (and others?)
849 * (not really needed, but good example) */
850 extern int fclose(FILE *);
851 static int (*true_fclose) (FILE *) = NULL;
852 int fclose(FILE * file)
853 {
854 int result = -1;
855
856 check_dlsym(fclose);
857 result = true_fclose(file);
858
859 return result;
860 }
861
862 #endif /* GLIBC_MINOR == 1 */
863
864 static void init_context(sbcontext_t * context)
865 {
866 context->show_access_violation = 1;
867 context->deny_prefixes = NULL;
868 context->num_deny_prefixes = 0;
869 context->read_prefixes = NULL;
870 context->num_read_prefixes = 0;
871 context->write_prefixes = NULL;
872 context->num_write_prefixes = 0;
873 context->predict_prefixes = NULL;
874 context->num_predict_prefixes = 0;
875 context->write_denied_prefixes = NULL;
876 context->num_write_denied_prefixes = 0;
877 }
878
879 static int is_sandbox_pid()
880 {
881 int old_errno = errno;
882 int result = 0;
883 FILE *pids_stream = NULL;
884 int pids_file = -1;
885 int current_pid = 0;
886 int tmp_pid = 0;
887
888 init_wrappers();
889
890 check_dlsym(fopen);
891 pids_stream = true_fopen(sandbox_pids_file, "r");
892
893 if (NULL == pids_stream) {
894 perror(">>> pids file fopen");
895 } else {
896 pids_file = fileno(pids_stream);
897
898 if (pids_file < 0) {
899 perror(">>> pids file fileno");
900 } else {
901 current_pid = getpid();
902
903 while (EOF != fscanf(pids_stream, "%d\n", &tmp_pid)) {
904 if (tmp_pid == current_pid) {
905 result = 1;
906 break;
907 }
908 }
909 }
910 if (EOF == fclose(pids_stream)) {
911 perror(">>> pids file fclose");
912 }
913 pids_stream = NULL;
914 pids_file = -1;
915 }
916
917 errno = old_errno;
918
919 return result;
920 }
921
922 static void clean_env_entries(char ***prefixes_array, int *prefixes_num)
923 {
924 int old_errno = errno;
925 int i = 0;
926
927 if (NULL != *prefixes_array) {
928 for (i = 0; i < *prefixes_num; i++) {
929 if (NULL != (*prefixes_array)[i]) {
930 free((*prefixes_array)[i]);
931 (*prefixes_array)[i] = NULL;
932 }
933 }
934 if (NULL != *prefixes_array)
935 free(*prefixes_array);
936 *prefixes_array = NULL;
937 *prefixes_num = 0;
938 }
939
940 errno = old_errno;
941 }
942
943 #define pfx_num (*prefixes_num)
944 #define pfx_array (*prefixes_array)
945 #define pfx_item ((*prefixes_array)[(*prefixes_num)])
946
947 static void init_env_entries(char ***prefixes_array, int *prefixes_num, const char *env, const char *prefixes_env, int warn)
948 {
949 int old_errno = errno;
950 // char *prefixes_env = getenv(env);
951
952 if (NULL == prefixes_env) {
953 /* Do not warn if this is in init stage, as we might get
954 * issues due to LD_PRELOAD already set (bug #91431). */
955 if (1 == sb_init)
956 fprintf(stderr, "Sandbox error : the %s environmental variable should be defined.\n", env);
957 if(pfx_array) {
958 int x;
959 for(x=0; x < pfx_num; x++)
960 free(pfx_item);
961 free(pfx_array);
962 }
963 pfx_num = 0;
964 } else {
965 char *buffer = NULL;
966 char *buffer_ptr = NULL;
967 int prefixes_env_length = strlen(prefixes_env);
968 int i = 0;
969 int num_delimiters = 0;
970 char *token = NULL;
971 char *rpath = NULL;
972
973 for (i = 0; i < prefixes_env_length; i++) {
974 if (':' == prefixes_env[i]) {
975 num_delimiters++;
976 }
977 }
978
979 if (num_delimiters > 0) {
980 pfx_array = malloc(((num_delimiters * 2) + 1) * sizeof(char *));
981 if (NULL == pfx_array)
982 return;
983 buffer = strndup(prefixes_env, prefixes_env_length);
984 if (NULL == buffer)
985 return;
986 buffer_ptr = buffer;
987
988 #ifdef REENTRANT_STRTOK
989 token = strtok_r(buffer_ptr, ":", &buffer_ptr);
990 #else
991 token = strtok(buffer_ptr, ":");
992 #endif
993
994 while ((NULL != token) && (strlen(token) > 0)) {
995 pfx_item = filter_path(token, 0);
996 if (NULL != pfx_item) {
997 pfx_num++;
998
999 /* Now add the realpath if it exists and
1000 * are not a duplicate */
1001 rpath = malloc(SB_PATH_MAX * sizeof(char));
1002 if (NULL != rpath) {
1003 pfx_item = realpath(*(&(pfx_item) - 1), rpath);
1004 if ((NULL != pfx_item) &&
1005 (0 != strcmp(*(&(pfx_item) - 1), pfx_item))) {
1006 pfx_num++;
1007 } else {
1008 free(rpath);
1009 pfx_item = NULL;
1010 }
1011 }
1012 }
1013
1014 #ifdef REENTRANT_STRTOK
1015 token = strtok_r(NULL, ":", &buffer_ptr);
1016 #else
1017 token = strtok(NULL, ":");
1018 #endif
1019 }
1020
1021 free(buffer);
1022 } else if (prefixes_env_length > 0) {
1023 pfx_array = malloc(2 * sizeof(char *));
1024 if (NULL == pfx_array)
1025 return;
1026
1027 pfx_item = filter_path(prefixes_env, 0);
1028 if (NULL != pfx_item) {
1029 pfx_num++;
1030
1031 /* Now add the realpath if it exists and
1032 * are not a duplicate */
1033 rpath = malloc(SB_PATH_MAX * sizeof(char));
1034 if (NULL != rpath) {
1035 pfx_item = realpath(*(&(pfx_item) - 1), rpath);
1036 if ((NULL != pfx_item) &&
1037 (0 != strcmp(*(&(pfx_item) - 1), pfx_item))) {
1038 pfx_num++;
1039 } else {
1040 free(rpath);
1041 pfx_item = NULL;
1042 }
1043 }
1044 }
1045 }
1046 }
1047
1048 errno = old_errno;
1049 }
1050
1051 static int check_access(sbcontext_t * sbcontext, const char *func, const char *path, const char *fpath)
1052 {
1053 int old_errno = errno;
1054 int result = -1;
1055 int i = 0;
1056
1057 if ((-1 == result) && (NULL != sbcontext->deny_prefixes)) {
1058 for (i = 0; i < sbcontext->num_deny_prefixes; i++) {
1059 if (NULL != sbcontext->deny_prefixes[i]) {
1060 if (0 == strncmp(fpath,
1061 sbcontext->deny_prefixes[i],
1062 strlen(sbcontext->deny_prefixes[i]))) {
1063 result = 0;
1064 break;
1065 }
1066 }
1067 }
1068 }
1069
1070 if (-1 == result) {
1071 if ((NULL != sbcontext->read_prefixes) &&
1072 ((0 == strncmp(func, "access_rd", 9)) ||
1073 (0 == strncmp(func, "open_rd", 7)) ||
1074 (0 == strncmp(func, "popen", 5)) ||
1075 (0 == strncmp(func, "opendir", 7)) ||
1076 (0 == strncmp(func, "system", 6)) ||
1077 (0 == strncmp(func, "execl", 5)) ||
1078 (0 == strncmp(func, "execlp", 6)) ||
1079 (0 == strncmp(func, "execle", 6)) ||
1080 (0 == strncmp(func, "execv", 5)) ||
1081 (0 == strncmp(func, "execvp", 6)) ||
1082 (0 == strncmp(func, "execve", 6)))) {
1083 for (i = 0; i < sbcontext->num_read_prefixes; i++) {
1084 if (NULL != sbcontext->read_prefixes[i]) {
1085 if (0 == strncmp(fpath,
1086 sbcontext->read_prefixes[i],
1087 strlen(sbcontext->read_prefixes[i]))) {
1088 result = 1;
1089 break;
1090 }
1091 }
1092 }
1093 }
1094 if ((NULL != sbcontext->write_prefixes) &&
1095 ((0 == strncmp(func, "access_wr", 7)) ||
1096 (0 == strncmp(func, "open_wr", 7)) ||
1097 (0 == strncmp(func, "creat", 5)) ||
1098 (0 == strncmp(func, "creat64", 7)) ||
1099 (0 == strncmp(func, "mkdir", 5)) ||
1100 (0 == strncmp(func, "mknod", 5)) ||
1101 (0 == strncmp(func, "mkfifo", 6)) ||
1102 (0 == strncmp(func, "link", 4)) ||
1103 (0 == strncmp(func, "symlink", 7)) ||
1104 (0 == strncmp(func, "rename", 6)) ||
1105 (0 == strncmp(func, "utime", 5)) ||
1106 (0 == strncmp(func, "utimes", 6)) ||
1107 (0 == strncmp(func, "unlink", 6)) ||
1108 (0 == strncmp(func, "rmdir", 5)) ||
1109 (0 == strncmp(func, "chown", 5)) ||
1110 (0 == strncmp(func, "lchown", 6)) ||
1111 (0 == strncmp(func, "chmod", 5)) ||
1112 (0 == strncmp(func, "truncate", 8)) ||
1113 (0 == strncmp(func, "ftruncate", 9)) ||
1114 (0 == strncmp(func, "truncate64", 10)) ||
1115 (0 == strncmp(func, "ftruncate64", 11)))) {
1116 struct stat st;
1117
1118 /* No need to check here, as we do it above
1119 if (NULL != sbcontext->write_prefixes) { */
1120
1121 if (NULL != sbcontext->write_denied_prefixes) {
1122 for (i = 0; i < sbcontext->num_write_denied_prefixes; i++) {
1123 if (NULL != sbcontext->write_denied_prefixes[i]) {
1124 if (0 == strncmp(fpath,
1125 sbcontext->write_denied_prefixes[i],
1126 strlen(sbcontext->write_denied_prefixes[i]))) {
1127 /* Special paths in writable context that should
1128 * be denied - not implemented yet */
1129 result = 0;
1130 break;
1131 }
1132 }
1133 }
1134 }
1135
1136 if ((-1 == result) && (NULL != sbcontext->write_prefixes)) {
1137 for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1138 if (NULL != sbcontext->write_prefixes[i]) {
1139 if (0 == strncmp(fpath,
1140 sbcontext->write_prefixes[i],
1141 strlen(sbcontext->write_prefixes[i]))) {
1142 /* Falls in a writable path */
1143 result = 1;
1144 break;
1145 }
1146 }
1147 }
1148 }
1149
1150 /* XXX: Hack to enable us to remove symlinks pointing
1151 * to protected stuff. First we make sure that the
1152 * passed path is writable, and if so, check if its a
1153 * symlink, and give access only if the resolved path
1154 * of the symlink's parent also have write access. */
1155 if ((-1 == result) &&
1156 ((0 == strncmp(func, "unlink", 6)) ||
1157 (0 == strncmp(func, "lchown", 6)) ||
1158 (0 == strncmp(func, "rename", 6))) &&
1159 ((-1 != lstat(path, &st)) && (S_ISLNK(st.st_mode)))) {
1160 int hresult = -1;
1161
1162 /* Check if the symlink unresolved path have access */
1163 for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1164 if (NULL != sbcontext->write_prefixes[i]) {
1165 if (0 == strncmp(path,
1166 sbcontext->write_prefixes[i],
1167 strlen(sbcontext->write_prefixes[i]))) {
1168 /* Does have write access on path */
1169 hresult = 1;
1170 break;
1171 }
1172 }
1173 }
1174 if (1 == hresult) {
1175 char tmp_buf[SB_PATH_MAX];
1176 char *dname, *rpath;
1177
1178 snprintf(tmp_buf, SB_PATH_MAX, "%s", path);
1179
1180 dname = dirname(tmp_buf);
1181 /* Get symlink resolved path */
1182 rpath = filter_path(dname, 1);
1183 if (NULL == rpath)
1184 /* Don't really worry here about
1185 * memory issues */
1186 goto unlink_hack_end;
1187
1188 /* Now check if the symlink resolved path have access */
1189 for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1190 if (NULL != sbcontext->write_prefixes[i]) {
1191 if (0 == strncmp(rpath,
1192 sbcontext->write_prefixes[i],
1193 strlen(sbcontext->write_prefixes[i]))) {
1194 /* Does have write access on path */
1195 hresult = 2;
1196 break;
1197 }
1198 }
1199 }
1200 free(rpath);
1201
1202 if (2 == hresult) {
1203 /* Ok, enable the hack as it is a symlink */
1204 result = 1;
1205 }
1206 }
1207 }
1208 unlink_hack_end:
1209
1210 /* XXX: Hack to allow writing to '/proc/self/fd' (bug #91516)
1211 * It needs to be here, as for each process '/proc/self'
1212 * will differ ... */
1213 if (-1 == result) {
1214 char proc_self_fd[SB_PATH_MAX];
1215
1216 if (NULL != realpath("/proc/self/fd", proc_self_fd)) {
1217 if (0 == strncmp(fpath, proc_self_fd, strlen(proc_self_fd)))
1218 result = 1;
1219 }
1220 }
1221
1222 if ((-1 == result) && (NULL != sbcontext->predict_prefixes)) {
1223 for (i = 0; i < sbcontext->num_predict_prefixes; i++) {
1224 if (NULL != sbcontext->predict_prefixes[i]) {
1225 if (0 == strncmp(fpath,
1226 sbcontext->predict_prefixes[i],
1227 strlen(sbcontext->predict_prefixes[i]))) {
1228 /* Is a known access violation, so deny access,
1229 * and do not log it */
1230 sbcontext->show_access_violation = 0;
1231 result = 0;
1232 break;
1233 }
1234 }
1235 }
1236 }
1237 }
1238 }
1239
1240 if (-1 == result) {
1241 result = 0;
1242 }
1243
1244 errno = old_errno;
1245
1246 return result;
1247 }
1248
1249 static int check_syscall(sbcontext_t * sbcontext, const char *func, const char *file)
1250 {
1251 int old_errno = errno;
1252 int result = 1;
1253 struct stat log_stat;
1254 char *log_path = NULL;
1255 char *absolute_path = NULL;
1256 char *resolved_path = NULL;
1257 int log_file = 0;
1258 struct stat debug_log_stat;
1259 char *debug_log_env = NULL;
1260 char *debug_log_path = NULL;
1261 int debug_log_file = 0;
1262 char buffer[512];
1263 char *dpath = NULL;
1264
1265 init_wrappers();
1266
1267 absolute_path = filter_path(file, 0);
1268 if (NULL == absolute_path)
1269 goto fp_error;
1270 resolved_path = filter_path(file, 1);
1271 if (NULL == resolved_path)
1272 goto fp_error;
1273
1274 log_path = getenv("SANDBOX_LOG");
1275 debug_log_env = getenv("SANDBOX_DEBUG");
1276 debug_log_path = getenv("SANDBOX_DEBUG_LOG");
1277
1278 if (((NULL == log_path) ||
1279 (0 != strncmp(absolute_path, log_path, strlen(log_path)))) &&
1280 ((NULL == debug_log_env) ||
1281 (NULL == debug_log_path) ||
1282 (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path)))) &&
1283 (0 == check_access(sbcontext, func, absolute_path, resolved_path))) {
1284 if (1 == sbcontext->show_access_violation) {
1285 fprintf(stderr, "\e[31;01mACCESS DENIED\033[0m %s:%*s%s\n",
1286 func, (int)(10 - strlen(func)), "", absolute_path);
1287
1288 if (NULL != log_path) {
1289 if (0 != strncmp(absolute_path, resolved_path, strlen(absolute_path))) {
1290 sprintf(buffer, "%s:%*s%s (symlink to %s)\n", func, (int)(10-strlen(func)), "",
1291 absolute_path, resolved_path);
1292 } else {
1293 sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1294 }
1295 // log_path somehow gets corrupted. figuring out why would be good.
1296 dpath = strdup(log_path);
1297 if ((0 == lstat(log_path, &log_stat)) &&
1298 (0 == S_ISREG(log_stat.st_mode))) {
1299 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m %s already exists and is not a regular file.\n", dpath);
1300 } else if (0 == check_access(sbcontext, "open_wr", dpath, filter_path(dpath, 1))) {
1301 unsetenv("SANDBOX_LOG");
1302 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m SANDBOX_LOG %s isn't allowed via SANDBOX_WRITE\n", dpath);
1303 } else {
1304 check_dlsym(open);
1305 log_file = true_open(dpath, O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1306 if (log_file >= 0) {
1307 write(log_file, buffer, strlen(buffer));
1308 close(log_file);
1309 }
1310 }
1311 free(dpath);
1312 }
1313 }
1314
1315 result = 0;
1316 } else if (NULL != debug_log_env) {
1317 if (NULL != debug_log_path) {
1318 if (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path))) {
1319 sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1320 //debug_log_path somehow gets corupted, same thing as log_path above.
1321 dpath = strdup(debug_log_path);
1322 if ((0 == lstat(debug_log_path, &debug_log_stat))
1323 && (0 == S_ISREG(debug_log_stat.st_mode))) {
1324 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m %s already exists and is not a regular file.\n",
1325 debug_log_path);
1326 } else if (0 == check_access(sbcontext, "open_wr", dpath, filter_path(dpath, 1))) {
1327 unsetenv("SANDBOX_DEBUG");
1328 unsetenv("SANDBOX_DEBUG_LOG");
1329 fprintf(stderr, "\e[31;01mSECURITY BREACH\033[0m SANDBOX_DEBUG_LOG %s isn't allowed by SANDBOX_WRITE.\n",
1330 dpath);
1331 } else {
1332 check_dlsym(open);
1333 debug_log_file = true_open(dpath, O_APPEND | O_WRONLY | O_CREAT,
1334 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1335 if (debug_log_file >= 0) {
1336 write(debug_log_file, buffer, strlen(buffer));
1337 close(debug_log_file);
1338 }
1339 }
1340 free(dpath);
1341 }
1342 } else {
1343 fprintf(stderr, "\e[32;01mACCESS ALLOWED\033[0m %s:%*s%s\n",
1344 func, (int)(10 - strlen(func)), "", absolute_path);
1345 }
1346 }
1347
1348 if (NULL != absolute_path)
1349 free(absolute_path);
1350 if (NULL != resolved_path)
1351 free(resolved_path);
1352
1353 errno = old_errno;
1354
1355 return result;
1356
1357 fp_error:
1358 if (NULL != absolute_path)
1359 free(absolute_path);
1360 if (NULL != resolved_path)
1361 free(resolved_path);
1362
1363 /* The path is too long to be canonicalized, so just warn and let the
1364 * function handle it (see bug #94630 and #21766 for more info) */
1365 if (ENAMETOOLONG == errno) {
1366 if (0 == sb_path_size_warning) {
1367 fprintf(stderr, "\e[31;01mPATH LENGTH\033[0m %s:%*s%s\n",
1368 func, (int)(10 - strlen(func)), "", file);
1369 sb_path_size_warning = 1;
1370 }
1371
1372 return 1;
1373 }
1374
1375 return 0;
1376 }
1377
1378 static int is_sandbox_on()
1379 {
1380 int old_errno = errno;
1381
1382 /* $SANDBOX_ACTIVE is an env variable that should ONLY
1383 * be used internal by sandbox.c and libsanbox.c. External
1384 * sources should NEVER set it, else the sandbox is enabled
1385 * in some cases when run in parallel with another sandbox,
1386 * but not even in the sandbox shell.
1387 *
1388 * Azarah (3 Aug 2002)
1389 */
1390 if ((NULL != getenv("SANDBOX_ON")) &&
1391 (0 == strncmp(getenv("SANDBOX_ON"), "1", 1)) &&
1392 (NULL != getenv("SANDBOX_ACTIVE")) &&
1393 (0 == strncmp(getenv("SANDBOX_ACTIVE"), "armedandready", 13))) {
1394 errno = old_errno;
1395 return 1;
1396 } else {
1397 errno = old_errno;
1398 return 0;
1399 }
1400 }
1401
1402 static int before_syscall(const char *func, const char *file)
1403 {
1404 int old_errno = errno;
1405 int result = 1;
1406 // static sbcontext_t sbcontext;
1407 char *deny = getenv("SANDBOX_DENY");
1408 char *read = getenv("SANDBOX_READ");
1409 char *write = getenv("SANDBOX_WRITE");
1410 char *predict = getenv("SANDBOX_PREDICT");
1411
1412 if (NULL == file || 0 == strlen(file)) {
1413 /* The file/directory does not exist */
1414 errno = ENOENT;
1415 return 0;
1416 }
1417
1418 if(sb_init == 0) {
1419 init_context(&sbcontext);
1420 cached_env_vars = malloc(sizeof(char *)*4);
1421 cached_env_vars[0] = cached_env_vars[1] = cached_env_vars[2] = cached_env_vars[3] = NULL;
1422 sb_init=1;
1423 }
1424
1425 if((deny == NULL && cached_env_vars[0] != deny) || cached_env_vars[0] == NULL ||
1426 strcmp(cached_env_vars[0], deny) != 0) {
1427
1428 clean_env_entries(&(sbcontext.deny_prefixes),
1429 &(sbcontext.num_deny_prefixes));
1430
1431 if(NULL != cached_env_vars[0])
1432 free(cached_env_vars[0]);
1433
1434 if(NULL != deny) {
1435 init_env_entries(&(sbcontext.deny_prefixes),
1436 &(sbcontext.num_deny_prefixes), "SANDBOX_DENY", deny, 1);
1437 cached_env_vars[0] = strdup(deny);
1438 } else {
1439 cached_env_vars[0] = NULL;
1440 }
1441 }
1442
1443 if((read == NULL && cached_env_vars[1] != read) || cached_env_vars[1] == NULL ||
1444 strcmp(cached_env_vars[1], read) != 0) {
1445
1446 clean_env_entries(&(sbcontext.read_prefixes),
1447 &(sbcontext.num_read_prefixes));
1448
1449 if(NULL != cached_env_vars[1])
1450 free(cached_env_vars[1]);
1451
1452 if(NULL != read) {
1453 init_env_entries(&(sbcontext.read_prefixes),
1454 &(sbcontext.num_read_prefixes), "SANDBOX_READ", read, 1);
1455 cached_env_vars[1] = strdup(read);
1456 } else {
1457 cached_env_vars[1] = NULL;
1458 }
1459 }
1460
1461 if((write == NULL && cached_env_vars[2] != write) || cached_env_vars[2] == NULL ||
1462 strcmp(cached_env_vars[2], write) != 0) {
1463
1464 clean_env_entries(&(sbcontext.write_prefixes),
1465 &(sbcontext.num_write_prefixes));
1466
1467 if(NULL != cached_env_vars[2])
1468 free(cached_env_vars[2]);
1469
1470 if(NULL != write) {
1471 init_env_entries(&(sbcontext.write_prefixes),
1472 &(sbcontext.num_write_prefixes), "SANDBOX_WRITE", write, 1);
1473 cached_env_vars[2] = strdup(write);
1474 } else {
1475 cached_env_vars[2] = NULL;
1476 }
1477 }
1478
1479 if((predict == NULL && cached_env_vars[3] != predict) || cached_env_vars[3] == NULL ||
1480 strcmp(cached_env_vars[3], predict) != 0) {
1481
1482 clean_env_entries(&(sbcontext.predict_prefixes),
1483 &(sbcontext.num_predict_prefixes));
1484
1485 if(NULL != cached_env_vars[3])
1486 free(cached_env_vars[3]);
1487
1488 if(NULL != predict) {
1489 init_env_entries(&(sbcontext.predict_prefixes),
1490 &(sbcontext.num_predict_prefixes), "SANDBOX_PREDICT", predict, 1);
1491 cached_env_vars[3] = strdup(predict);
1492 } else {
1493 cached_env_vars[3] = NULL;
1494 }
1495
1496 }
1497
1498 result = check_syscall(&sbcontext, func, file);
1499
1500 errno = old_errno;
1501
1502 if (0 == result) {
1503 errno = EACCES;
1504 }
1505
1506 return result;
1507 }
1508
1509 static int before_syscall_access(const char *func, const char *file, int flags)
1510 {
1511 if (flags & W_OK) {
1512 return before_syscall("access_wr", file);
1513 } else {
1514 return before_syscall("access_rd", file);
1515 }
1516 }
1517
1518 static int before_syscall_open_int(const char *func, const char *file, int flags)
1519 {
1520 if ((flags & O_WRONLY) || (flags & O_RDWR)) {
1521 return before_syscall("open_wr", file);
1522 } else {
1523 return before_syscall("open_rd", file);
1524 }
1525 }
1526
1527 static int before_syscall_open_char(const char *func, const char *file, const char *mode)
1528 {
1529 if (*mode == 'r' && ((strcmp(mode, "r") == 0) ||
1530 /* The strspn accept args are known non-writable modifiers */
1531 (strlen(++mode) == strspn(mode, "xbtmc")))) {
1532 return before_syscall("open_rd", file);
1533 } else {
1534 return before_syscall("open_wr", file);
1535 }
1536 }
1537
1538 #include "getcwd.c"
1539 #include "canonicalize.c"
1540 #include "sandbox_futils.c"
1541
1542 // 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