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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 283 - (show annotations) (download) (as text)
Thu Jul 6 08:49:13 2006 UTC (8 years, 2 months ago) by azarah
File MIME type: text/x-csrc
File size: 17153 byte(s)
Remove last libsandbox-only functions in favour for ones provided
by librcutil.  Setup log_domain properly for debugging.

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