/[path-sandbox]/tags/sandbox_1_2_13/libsandbox.c
Gentoo

Contents of /tags/sandbox_1_2_13/libsandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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