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

Contents of /trunk/libsandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (show annotations) (download) (as text)
Mon Mar 21 08:54:02 2005 UTC (9 years, 4 months ago) by genone
File MIME type: text/x-csrc
File size: 37531 byte(s)
show resolved symlinks in log

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