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

Contents of /trunk/libsandbox/wrappers.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 371 - (show annotations) (download) (as text)
Sun Nov 9 10:35:50 2008 UTC (5 years, 5 months ago) by vapier
File MIME type: text/x-csrc
File size: 2416 byte(s)
From: Robin H. Johnson <robbat2@gentoo.org>
Gentoo-Bug: 206678
X-Gentoo-URL: http://bugs.gentoo.org/show_bug.cgi?id=206678
Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>

Based on a previous revision by solar@gentoo.org.
It seems that on hardened systems, USE_RTLD_NEXT is not always usable, and this
trips up sandbox.

1 /*
2 * wrappers.c
3 *
4 * Function wrapping functions.
5 *
6 * Copyright 1999-2008 Gentoo Foundation
7 * Licensed under the GPL-2
8 *
9 * Partly Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>,
10 * as some of the InstallWatch code was used.
11 */
12
13 #include "headers.h"
14 #include "sbutil.h"
15 #include "libsandbox.h"
16 #include "wrappers.h"
17
18 #if !defined(BROKEN_RTLD_NEXT) && defined(HAVE_RTLD_NEXT)
19 # define USE_RTLD_NEXT
20 #endif
21
22 /* Macro to check if a wrapper is defined, if not
23 * then try to resolve it again. */
24 #define check_dlsym(_name, _symname, _symver) \
25 { \
26 int old_errno = errno; \
27 if (NULL == _name) \
28 _name = get_dlsym(_symname, _symver); \
29 errno = old_errno; \
30 }
31
32 static void *libc_handle = NULL;
33
34 extern char sandbox_lib[SB_PATH_MAX];
35 extern int sandbox_on;
36
37 /* Need to include the function wrappers here, as they are needed below */
38 #include "symbols.h"
39
40
41 void *get_dlsym(const char *symname, const char *symver)
42 {
43 void *symaddr = NULL;
44
45 #if defined(USE_RTLD_NEXT)
46 libc_handle = RTLD_NEXT;
47 #endif
48
49 /* Checking for -1UL is significent on hardened!
50 * USE_RTLD_NEXT returns it as a sign of being unusable.
51 * However using !x or NULL checks does NOT pick it up!
52 */
53 #define INVALID_LIBC_HANDLE(x) (!x || NULL == x || -1UL == x)
54 if (INVALID_LIBC_HANDLE(libc_handle)) {
55 libc_handle = dlopen(LIBC_VERSION, RTLD_LAZY);
56 if (INVALID_LIBC_HANDLE(libc_handle)) {
57 fprintf(stderr, "libsandbox: Can't dlopen libc: %s\n",
58 dlerror());
59 exit(EXIT_FAILURE);
60 }
61 }
62 #undef INVALID_LIBC_HANDLE
63
64 if (NULL == symver)
65 symaddr = dlsym(libc_handle, symname);
66 else
67 symaddr = dlvsym(libc_handle, symname, symver);
68 if (!symaddr) {
69 fprintf(stderr, "libsandbox: Can't resolve %s: %s\n",
70 symname, dlerror());
71 exit(EXIT_FAILURE);
72 }
73
74 return symaddr;
75 }
76
77 int libsb_open(const char *pathname, int flags, ...)
78 {
79 va_list ap;
80 int mode = 0;
81 int result = -1;
82
83 if (flags & O_CREAT) {
84 va_start(ap, flags);
85 mode = va_arg(ap, int);
86 va_end(ap);
87 }
88
89 check_dlsym(true_open_DEFAULT, symname_open_DEFAULT,
90 symver_open_DEFAULT);
91 if (flags & O_CREAT)
92 result = true_open_DEFAULT(pathname, flags, mode);
93 else
94 result = true_open_DEFAULT(pathname, flags);
95
96 return result;
97 }
98
99 char *libsb_getcwd(char *buf, size_t size)
100 {
101 check_dlsym(true_getcwd_DEFAULT, symname_getcwd_DEFAULT,
102 symver_getcwd_DEFAULT);
103
104 return true_getcwd_DEFAULT(buf, size);
105 }

  ViewVC Help
Powered by ViewVC 1.1.20