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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 216 - (show annotations) (download) (as text)
Mon Dec 5 09:28:34 2005 UTC (8 years, 10 months ago) by azarah
File MIME type: text/x-csrc
File size: 15456 byte(s)
Rename sandbox_futils.c to sandbox_utils.c.  Add gstrndup() and gbasename()
to sandbox_utils.c.  Add check for glibc, and fixup things to not need glibc
only extensions if not needed for versioned symbols.

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 ** $Header$
15 */
16
17 /* #define _GNU_SOURCE */
18
19 #include <errno.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "sandbox.h"
30
31 struct sandbox_info_t {
32 char sandbox_log[SB_PATH_MAX];
33 char sandbox_debug_log[SB_PATH_MAX];
34 char sandbox_lib[SB_PATH_MAX];
35 char sandbox_rc[SB_PATH_MAX];
36 char work_dir[SB_PATH_MAX];
37 char var_tmp_dir[SB_PATH_MAX];
38 char tmp_dir[SB_PATH_MAX];
39 char *home_dir;
40 } sandbox_info_t;
41
42 static int print_debug = 0;
43 static int stop_called = 0;
44
45 extern char **environ;
46
47 int sandbox_setup(struct sandbox_info_t *sandbox_info)
48 {
49 if (NULL != getenv(ENV_PORTAGE_TMPDIR)) {
50 /* Portage handle setting SANDBOX_WRITE itself. */
51 sandbox_info->work_dir[0] = '\0';
52 } else {
53 if (NULL == egetcwd(sandbox_info->work_dir, SB_PATH_MAX)) {
54 perror("sandbox: Failed to get current directory");
55 return -1;
56 }
57 }
58
59 /* Do not resolve symlinks, etc .. libsandbox will handle that. */
60 if (1 != is_dir(VAR_TMPDIR, 1)) {
61 perror("sandbox: Failed to get var_tmp_dir");
62 return -1;
63 }
64 snprintf(sandbox_info->var_tmp_dir, SB_PATH_MAX, "%s", VAR_TMPDIR);
65
66 if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
67 perror("sandbox: Failed to get tmp_dir");
68 return -1;
69 }
70 setenv(ENV_TMPDIR, sandbox_info->tmp_dir, 1);
71
72 sandbox_info->home_dir = getenv("HOME");
73 if (!sandbox_info->home_dir) {
74 sandbox_info->home_dir = sandbox_info->tmp_dir;
75 setenv("HOME", sandbox_info->home_dir, 1);
76 }
77
78 /* Generate sandbox lib path */
79 get_sandbox_lib(sandbox_info->sandbox_lib);
80
81 /* Generate sandbox bashrc path */
82 get_sandbox_rc(sandbox_info->sandbox_rc);
83
84 /* Generate sandbox log full path */
85 get_sandbox_log(sandbox_info->sandbox_log);
86 if (1 == exists(sandbox_info->sandbox_log)) {
87 if (-1 == unlink(sandbox_info->sandbox_log)) {
88 perror("sandbox: Could not unlink old log file");
89 return -1;
90 }
91 }
92
93 /* Generate sandbox debug log full path */
94 get_sandbox_debug_log(sandbox_info->sandbox_debug_log);
95 if (1 == exists(sandbox_info->sandbox_debug_log)) {
96 if (-1 == unlink(sandbox_info->sandbox_debug_log)) {
97 perror("sandbox: Could not unlink old debug log file");
98 return -1;
99 }
100 }
101
102 return 0;
103 }
104
105 int print_sandbox_log(char *sandbox_log)
106 {
107 int sandbox_log_file = -1;
108 char *beep_count_env = NULL;
109 int i, color, beep_count = 0;
110 long len = 0;
111 char *buffer = NULL;
112
113 if (1 != is_file(sandbox_log)) {
114 perror("sandbox: Log file is not a regular file");
115 return 0;
116 }
117
118 sandbox_log_file = open(sandbox_log, O_RDONLY);
119 if (-1 == sandbox_log_file) {
120 perror("sandbox: Could not open Log file");
121 return 0;
122 }
123
124 len = file_length(sandbox_log_file);
125 buffer = (char *)malloc((len + 1) * sizeof(char));
126 memset(buffer, 0, len + 1);
127 read(sandbox_log_file, buffer, len);
128 close(sandbox_log_file);
129
130 color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
131
132 EERROR(color,
133 "--------------------------- ACCESS VIOLATION SUMMARY ---------------------------",
134 "\n");
135 EERROR(color, "LOG FILE = \"%s\"", "\n\n", sandbox_log);
136 fprintf(stderr, "%s", buffer);
137 if (NULL != buffer)
138 free(buffer);
139 EERROR(color,
140 "--------------------------------------------------------------------------------",
141 "\n");
142
143 beep_count_env = getenv(ENV_SANDBOX_BEEP);
144 if (beep_count_env)
145 beep_count = atoi(beep_count_env);
146 else
147 beep_count = DEFAULT_BEEP_COUNT;
148
149 for (i = 0; i < beep_count; i++) {
150 fputc('\a', stderr);
151 if (i < beep_count - 1)
152 sleep(1);
153 }
154
155 return 1;
156 }
157
158 void stop(int signum)
159 {
160 if (stop_called == 0) {
161 stop_called = 1;
162 printf("sandbox: Caught signal %d in pid %d\n",
163 signum, getpid());
164 } else {
165 fprintf(stderr,
166 "sandbox: Signal already caught and busy still cleaning up!\n");
167 }
168 }
169
170 int get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
171 {
172 int retval = 0;
173
174 /* bzero out entire buffer then append trailing 0 */
175 memset(buf, 0, SB_BUF_LEN);
176
177 /* these could go into make.globals later on */
178 retval = snprintf(buf, SB_BUF_LEN,
179 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
180 "/dev/zero:/dev/null:/dev/fd:/proc/self/fd:/dev/pts/:"
181 "/dev/vc/:/dev/pty:/dev/tty:/dev/tts:/dev/console:"
182 "/dev/shm:/dev/shm/ngpt:/var/log/scrollkeeper.log:"
183 "/usr/tmp/conftest:/usr/lib/conftest:"
184 "/usr/lib32/conftest:/usr/lib64/conftest:"
185 "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
186 sandbox_info->home_dir, sandbox_info->home_dir,
187 sandbox_info->work_dir[0] != '\0' ? sandbox_info->work_dir
188 : "",
189 sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
190 "/tmp/:/var/tmp/");
191 if (SB_BUF_LEN <= retval) {
192 errno = EMSGSIZE;
193 perror("sandbox: Failed to generate SANDBOX_WRITE");
194 return -1;
195 }
196
197 return 0;
198 }
199
200 int get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
201 {
202 int retval = 0;
203 /* bzero out entire buffer then append trailing 0 */
204 memset(buf, 0, SB_BUF_LEN);
205
206 /* these should go into make.globals later on */
207 retval = snprintf(buf, SB_BUF_LEN, "%s/.:"
208 "/usr/lib/python2.0/:"
209 "/usr/lib/python2.1/:"
210 "/usr/lib/python2.2/:"
211 "/usr/lib/python2.3/:"
212 "/usr/lib/python2.4/:"
213 "/usr/lib/python2.5/:"
214 "/usr/lib/python3.0/:"
215 "/var/db/aliases.db:"
216 "/var/db/netgroup.db:"
217 "/var/db/netmasks.db:"
218 "/var/db/ethers.db:"
219 "/var/db/rpc.db:"
220 "/var/db/protocols.db:"
221 "/var/db/services.db:"
222 "/var/db/networks.db:"
223 "/var/db/hosts.db:"
224 "/var/db/group.db:"
225 "/var/db/passwd.db",
226 sandbox_info->home_dir);
227 if (SB_BUF_LEN <= retval) {
228 errno = EMSGSIZE;
229 perror("sandbox: Failed to generate SANDBOX_PREDICT");
230 return -1;
231 }
232
233 return 0;
234 }
235
236 int sandbox_setenv(char **env, const char *name, const char *val) {
237 char **tmp_env = env;
238 char *tmp_string = NULL;
239 int retval = 0;
240
241 /* XXX: We add the new variable to the end (no replacing). If this
242 * is changed, we need to fix sandbox_setup_environ() below */
243 while (NULL != *tmp_env)
244 tmp_env++;
245
246 /* strlen(name) + strlen(val) + '=' + '\0' */
247 /* FIXME: Should probably free this at some stage - more neatness than
248 * a real leak that will cause issues. */
249 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
250 if (NULL == tmp_string) {
251 perror("sandbox: Out of memory (sandbox_setenv)");
252 exit(EXIT_FAILURE);
253 }
254
255 retval = snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
256 name, val);
257 *tmp_env = tmp_string;
258
259 return 0;
260 }
261
262 /* We setup the environment child side only to prevent issues with
263 * setting LD_PRELOAD parent side */
264 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info)
265 {
266 int env_size = 0;
267 int have_ld_preload = 0;
268
269 char **new_environ;
270 char **env_ptr = environ;
271 char sandbox_write_envvar[SB_BUF_LEN];
272 char sandbox_predict_envvar[SB_BUF_LEN];
273 char *ld_preload_envvar = NULL;
274 char *orig_ld_preload_envvar = NULL;
275
276 /* Unset these, as its easier than replacing when setting up our
277 * new environment below */
278 unsetenv(ENV_SANDBOX_ON);
279 unsetenv(ENV_SANDBOX_LIB);
280 unsetenv(ENV_SANDBOX_BASHRC);
281 unsetenv(ENV_SANDBOX_LOG);
282 unsetenv(ENV_SANDBOX_DEBUG_LOG);
283
284 if (NULL != getenv(ENV_LD_PRELOAD)) {
285 have_ld_preload = 1;
286 orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
287
288 /* FIXME: Should probably free this at some stage - more neatness
289 * than a real leak that will cause issues. */
290 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
291 strlen(sandbox_info->sandbox_lib) + 2,
292 sizeof(char *));
293 if (NULL == ld_preload_envvar)
294 return NULL;
295 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
296 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
297 sandbox_info->sandbox_lib, orig_ld_preload_envvar);
298 } else {
299 /* FIXME: Should probably free this at some stage - more neatness
300 * than a real leak that will cause issues. */
301 ld_preload_envvar = gstrndup(sandbox_info->sandbox_lib,
302 strlen(sandbox_info->sandbox_lib));
303 if (NULL == ld_preload_envvar)
304 return NULL;
305 }
306 /* Do not unset this, as strange things might happen */
307 /* unsetenv(ENV_LD_PRELOAD); */
308
309 while (NULL != *env_ptr) {
310 env_size++;
311 env_ptr++;
312 }
313
314 /* FIXME: Should probably free this at some stage - more neatness than
315 * a real leak that will cause issues. */
316 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
317 if (NULL == new_environ)
318 return NULL;
319
320 /* First add our new variables to the beginning - this is due to some
321 * weirdness that I cannot remember */
322 sandbox_setenv(new_environ, ENV_SANDBOX_ON, "1");
323 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
324 sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
325 sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
326 sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
327 sandbox_info->sandbox_debug_log);
328 /* If LD_PRELOAD was not set, set it here, else do it below */
329 if (1 != have_ld_preload)
330 sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
331
332 if (!getenv(ENV_SANDBOX_DENY))
333 sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
334
335 if (!getenv(ENV_SANDBOX_READ))
336 sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
337
338 if (-1 == get_sandbox_write_envvar(sandbox_write_envvar, sandbox_info))
339 return NULL;
340 if (!getenv(ENV_SANDBOX_WRITE))
341 sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
342
343 if (-1 == get_sandbox_predict_envvar(sandbox_predict_envvar, sandbox_info))
344 return NULL;
345 if (!getenv(ENV_SANDBOX_PREDICT))
346 sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
347
348 /* Make sure our bashrc gets preference */
349 sandbox_setenv(new_environ, ENV_BASH_ENV, sandbox_info->sandbox_rc);
350
351 /* This one should NEVER be set in ebuilds, as it is the one
352 * private thing libsandbox.so use to test if the sandbox
353 * should be active for this pid, or not.
354 *
355 * azarah (3 Aug 2002)
356 */
357
358 sandbox_setenv(new_environ, ENV_SANDBOX_ACTIVE, SANDBOX_ACTIVE);
359
360 env_size = 0;
361 while (NULL != new_environ[env_size])
362 env_size++;
363
364 /* Now add the rest */
365 env_ptr = environ;
366 while (NULL != *env_ptr) {
367 if ((1 == have_ld_preload) &&
368 (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
369 /* If LD_PRELOAD was set, and this is it in the original
370 * environment, replace it with our new copy */
371 /* XXX: The following works as it just add whatever as
372 * the last variable to nev_environ */
373 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
374 ld_preload_envvar);
375 else
376 new_environ[env_size + (env_ptr - environ)] = *env_ptr;
377 env_ptr++;
378 }
379
380 return new_environ;
381 }
382
383 int spawn_shell(char *argv_bash[], char *env[], int debug)
384 {
385 int pid;
386 int status = 0;
387 int ret = 0;
388
389 pid = fork();
390
391 /* Child's process */
392 if (0 == pid) {
393 execve(argv_bash[0], argv_bash, env);
394 return 0;
395 } else if (pid < 0) {
396 if (debug)
397 fprintf(stderr, "Process failed to spawn!\n");
398 return 0;
399 }
400 ret = waitpid(pid, &status, 0);
401 if ((-1 == ret) || (status > 0)) {
402 if (debug)
403 fprintf(stderr, "Process returned with failed exit status!\n");
404 return 0;
405 }
406
407 return 1;
408 }
409
410 int main(int argc, char **argv)
411 {
412 int i = 0, success = 1;
413 int sandbox_log_presence = 0;
414 long len;
415
416 struct sandbox_info_t sandbox_info;
417
418 char **sandbox_environ;
419 char **argv_bash = NULL;
420
421 char *run_str = "-c";
422
423 /* Only print info if called with no arguments .... */
424 if (argc < 2)
425 print_debug = 1;
426
427 if (print_debug)
428 printf("========================== Gentoo linux path sandbox ===========================\n");
429
430 /* check if a sandbox is already running */
431 if (NULL != getenv(ENV_SANDBOX_ACTIVE)) {
432 fprintf(stderr, "Not launching a new sandbox instance\n");
433 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
434 exit(EXIT_FAILURE);
435 }
436
437 /* determine the location of all the sandbox support files */
438 if (print_debug)
439 printf("Detection of the support files.\n");
440
441 if (-1 == sandbox_setup(&sandbox_info)) {
442 fprintf(stderr, "sandbox: Failed to setup sandbox.");
443 exit(EXIT_FAILURE);
444 }
445
446 /* verify the existance of required files */
447 if (print_debug)
448 printf("Verification of the required files.\n");
449
450 #ifndef SB_HAVE_MULTILIB
451 if (0 >= exists(sandbox_info.sandbox_lib)) {
452 perror("sandbox: Could not open the sandbox library");
453 exit(EXIT_FAILURE);
454 }
455 #endif
456 if (0 >= exists(sandbox_info.sandbox_rc)) {
457 perror("sandbox: Could not open the sandbox rc file");
458 exit(EXIT_FAILURE);
459 }
460
461 /* set up the required environment variables */
462 if (print_debug)
463 printf("Setting up the required environment variables.\n");
464
465 /* Setup the child environment stuff */
466 sandbox_environ = sandbox_setup_environ(&sandbox_info);
467 if (NULL == sandbox_environ) {
468 perror("sandbox: Out of memory (environ)");
469 exit(EXIT_FAILURE);
470 }
471
472 /* If not in portage, cd into it work directory */
473 if ('\0' != sandbox_info.work_dir[0])
474 chdir(sandbox_info.work_dir);
475
476 argv_bash = (char **)malloc(6 * sizeof(char *));
477 argv_bash[0] = strdup("/bin/bash");
478 argv_bash[1] = strdup("-rcfile");
479 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
480
481 if (argc < 2)
482 argv_bash[3] = NULL;
483 else
484 argv_bash[3] = strdup(run_str); /* "-c" */
485
486 argv_bash[4] = NULL; /* strdup(run_arg); */
487 argv_bash[5] = NULL;
488
489 if (argc >= 2) {
490 for (i = 1; i < argc; i++) {
491 if (NULL == argv_bash[4])
492 len = 0;
493 else
494 len = strlen(argv_bash[4]);
495
496 argv_bash[4] = (char *)realloc(argv_bash[4],
497 (len + strlen(argv[i]) + 2) * sizeof(char));
498
499 if (0 == len)
500 argv_bash[4][0] = 0;
501 if (1 != i)
502 strcat(argv_bash[4], " ");
503
504 strcat(argv_bash[4], argv[i]);
505 }
506 }
507
508 /* set up the required signal handlers */
509 signal(SIGHUP, &stop);
510 signal(SIGINT, &stop);
511 signal(SIGQUIT, &stop);
512 signal(SIGTERM, &stop);
513
514 /* STARTING PROTECTED ENVIRONMENT */
515 if (print_debug) {
516 printf("The protected environment has been started.\n");
517 printf("--------------------------------------------------------------------------------\n");
518 }
519
520 if (print_debug)
521 printf("Process being started in forked instance.\n");
522
523 /* Start Bash */
524 if (!spawn_shell(argv_bash, sandbox_environ, print_debug))
525 success = 0;
526
527 /* Free bash stuff */
528 for (i = 0; i < 6; i++) {
529 if (argv_bash[i])
530 free(argv_bash[i]);
531 argv_bash[i] = NULL;
532 }
533 if (argv_bash)
534 free(argv_bash);
535 argv_bash = NULL;
536
537 if (print_debug)
538 printf("Cleaning up sandbox process\n");
539
540 if (print_debug) {
541 printf("========================== Gentoo linux path sandbox ===========================\n");
542 printf("The protected environment has been shut down.\n");
543 }
544
545 if (1 == exists(sandbox_info.sandbox_log)) {
546 sandbox_log_presence = 1;
547 print_sandbox_log(sandbox_info.sandbox_log);
548 } else if (print_debug) {
549 printf("--------------------------------------------------------------------------------\n");
550 }
551
552 if ((sandbox_log_presence) || (!success))
553 return 1;
554 else
555 return 0;
556 }
557
558 // 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