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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (hide annotations) (download) (as text)
Wed Jun 8 13:52:33 2005 UTC (9 years, 4 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 19610 byte(s)
Do not abort if TMPDIR is not valid, but rather use '/tmp', bug #94360.  Also
make sure we re-export the new TMPDIR environment variable.

1 ferringb 2 /*
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 <fcntl.h>
21     #include <signal.h>
22     #include <stdio.h>
23     #include <stdlib.h>
24     #include <limits.h>
25     #include <string.h>
26     #include <sys/file.h>
27     #include <sys/stat.h>
28     #include <sys/time.h>
29     #include <sys/types.h>
30     #include <sys/resource.h>
31     #include <sys/wait.h>
32     #include <unistd.h>
33     #include <fcntl.h>
34     #include "sandbox.h"
35    
36 azarah 101 struct sandbox_info_t {
37     char sandbox_log[SB_PATH_MAX];
38     char sandbox_debug_log[SB_PATH_MAX];
39     char sandbox_dir[SB_PATH_MAX];
40     char sandbox_lib[SB_PATH_MAX];
41     char sandbox_rc[SB_PATH_MAX];
42     char *sandbox_pids_file;
43     char portage_tmp_dir[SB_PATH_MAX];
44     char var_tmp_dir[SB_PATH_MAX];
45     char tmp_dir[SB_PATH_MAX];
46     char *home_dir;
47     } sandbox_info_t;
48 ferringb 2
49 azarah 101 static char *tmp_dir;
50    
51 azarah 99 static int cleaned_up = 0;
52     static int print_debug = 0;
53     static int stop_called = 0;
54    
55 azarah 30 /* Read pids file, and load active pids into an array. Return number of pids in array */
56     int load_active_pids(int fd, int **pids)
57 ferringb 2 {
58     char *data = NULL;
59     char *ptr = NULL, *ptr2 = NULL;
60     int my_pid;
61     int num_pids = 0;
62     long len;
63    
64     pids[0] = NULL;
65    
66     len = file_length(fd);
67    
68     /* Allocate and zero datablock to read pids file */
69 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
70 ferringb 2 memset(data, 0, len + 1);
71    
72     /* Start at beginning of file */
73     lseek(fd, 0L, SEEK_SET);
74    
75     /* read entire file into a buffer */
76     read(fd, data, len);
77    
78     ptr = data;
79    
80     /* Loop and read all pids */
81     while (1) {
82     /* Find new line */
83     ptr2 = strchr(ptr, '\n');
84     if (ptr2 == NULL)
85 azarah 30 break; /* No more PIDs */
86 ferringb 2
87     /* Clear the \n. And ptr should have a null-terminated decimal string */
88     ptr2[0] = 0;
89    
90     my_pid = atoi(ptr);
91    
92     /* If the PID is still alive, add it to our array */
93     if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
94 azarah 30 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
95 ferringb 2 pids[0][num_pids] = my_pid;
96     num_pids++;
97     }
98    
99     /* Put ptr past the NULL we just wrote */
100     ptr = ptr2 + 1;
101     }
102    
103     if (data)
104     free(data);
105     data = NULL;
106    
107     return num_pids;
108     }
109    
110 azarah 101 int write_pids_file(struct sandbox_info_t *sandbox_info)
111 ferringb 2 {
112 azarah 101 int pids_file = -1;
113     int *pids_array = NULL;
114     int num_of_pids = 0;
115     int i = 0, success = 1;
116    
117     char pid_string[SB_BUF_LEN];
118    
119     if (file_exist(sandbox_info->sandbox_pids_file, 1) < 0) {
120     fprintf(stderr, ">>> %s is not a regular file\n",
121     sandbox_info->sandbox_pids_file);
122     return -1;
123     } else {
124     pids_file = file_open(sandbox_info->sandbox_pids_file,
125     "r+", 1, 0664, "portage");
126     if (-1 == pids_file)
127     return -1;
128     }
129    
130     /* Grab still active pids */
131     num_of_pids = load_active_pids(pids_file, &pids_array);
132    
133     /* Zero out file */
134     file_truncate(pids_file);
135    
136     /* Output active pids, and append our pid */
137     for (i = 0; i < num_of_pids + 1; i++) {
138     /* Time for our entry */
139     if (i == num_of_pids)
140     sprintf(pid_string, "%d\n", getpid());
141     else
142     sprintf(pid_string, "%d\n", pids_array[i]);
143    
144     if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
145     success = 0;
146     break;
147     }
148     }
149    
150     /* Clean pids_array */
151     if (pids_array)
152     free(pids_array);
153    
154     /* We're done with the pids file */
155     file_close(pids_file);
156    
157     if (!success)
158     return -1;
159    
160     return 0;
161     }
162    
163     int sandbox_setup(char *argv[], struct sandbox_info_t *sandbox_info)
164     {
165     if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR)
166     : PORTAGE_TMPDIR,
167     sandbox_info->portage_tmp_dir)) {
168     perror(">>> get portage_tmp_dir");
169     return -1;
170     }
171 azarah 108 setenv(ENV_PORTAGE_TMPDIR, sandbox_info->portage_tmp_dir, 1);
172 azarah 101
173     if (NULL == realpath(VAR_TMPDIR, sandbox_info->var_tmp_dir)) {
174     perror(">>> get var_tmp_dir");
175     return -1;
176     }
177    
178     if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
179     perror(">>> get tmp_dir");
180     return -1;
181     }
182     tmp_dir = sandbox_info->tmp_dir;
183 azarah 108 setenv(ENV_TMPDIR, tmp_dir, 1);
184 azarah 101
185     sandbox_info->home_dir = getenv("HOME");
186     if (!sandbox_info->home_dir) {
187     sandbox_info->home_dir = tmp_dir;
188     setenv("HOME", sandbox_info->home_dir, 1);
189     }
190    
191     /* Generate base sandbox path */
192     snprintf(sandbox_info->sandbox_dir, SB_PATH_MAX, "%s/",
193     get_sandbox_path(argv[0]));
194    
195     /* Generate sandbox lib path */
196     snprintf(sandbox_info->sandbox_lib, SB_PATH_MAX, "%s",
197     get_sandbox_lib(sandbox_info->sandbox_dir));
198    
199     /* Generate sandbox pids-file path */
200     sandbox_info->sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
201    
202     /* Generate sandbox bashrc path */
203     snprintf(sandbox_info->sandbox_rc, SB_PATH_MAX, "%s",
204     get_sandbox_rc(sandbox_info->sandbox_dir));
205    
206     /* Generate sandbox log full path */
207     snprintf(sandbox_info->sandbox_log, SB_PATH_MAX, "%s",
208     get_sandbox_log(tmp_dir));
209    
210     /* Generate sandbox debug log full path */
211     snprintf(sandbox_info->sandbox_debug_log, SB_PATH_MAX, "%s",
212     get_sandbox_debug_log(tmp_dir));
213    
214     return 0;
215     }
216    
217     void sandbox_cleanup()
218     {
219 ferringb 2 int i = 0;
220     int success = 1;
221     int pids_file = -1, num_of_pids = 0;
222     int *pids_array = NULL;
223 azarah 93 char pid_string[SB_BUF_LEN];
224 ferringb 2 char *sandbox_pids_file;
225    
226     /* Generate sandbox pids-file path */
227 azarah 99 sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
228 ferringb 2
229     /* Remove this sandbox's bash pid from the global pids
230 azarah 90 * file if we have not already done so */
231     if (0 == cleaned_up) {
232 ferringb 2 cleaned_up = 1;
233     success = 1;
234    
235     if (print_debug)
236     printf("Cleaning up pids file.\n");
237    
238     /* Stat the PIDs file, make sure it exists and is a regular file */
239     if (file_exist(sandbox_pids_file, 1) <= 0) {
240 ferringb 34 fprintf(stderr, ">>> pids file is not a regular file\n");
241 ferringb 2 success = 0;
242     /* We should really not fail if the pidsfile is missing here, but
243     * rather just exit cleanly, as there is still some cleanup to do */
244     return;
245     }
246    
247     pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
248     if (-1 == pids_file) {
249     success = 0;
250     /* Nothing more to do here */
251     return;
252     }
253    
254     /* Load "still active" pids into an array */
255     num_of_pids = load_active_pids(pids_file, &pids_array);
256     //printf("pids: %d\r\n", num_of_pids);
257    
258    
259     file_truncate(pids_file);
260    
261     /* if pids are still running, write only the running pids back to the file */
262     if (num_of_pids > 1) {
263     for (i = 0; i < num_of_pids; i++) {
264     if (pids_array[i] != getpid()) {
265     sprintf(pid_string, "%d\n", pids_array[i]);
266    
267 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
268 ferringb 2 perror(">>> pids file write");
269     success = 0;
270     break;
271     }
272     }
273     }
274    
275     file_close(pids_file);
276     pids_file = -1;
277     } else {
278    
279     file_close(pids_file);
280     pids_file = -1;
281    
282     /* remove the pidsfile, as this was the last sandbox */
283     unlink(sandbox_pids_file);
284     }
285    
286     if (pids_array != NULL)
287     free(pids_array);
288     pids_array = NULL;
289     }
290    
291     free(sandbox_pids_file);
292     }
293    
294 azarah 88 int print_sandbox_log(char *sandbox_log)
295     {
296     int sandbox_log_file = -1;
297     char *beep_count_env = NULL;
298     int i, color, beep_count = 0;
299     long len = 0;
300     char *buffer = NULL;
301    
302     sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
303     if (-1 == sandbox_log_file)
304     return 0;
305    
306     len = file_length(sandbox_log_file);
307     buffer = (char *)malloc((len + 1) * sizeof(char));
308     memset(buffer, 0, len + 1);
309     read(sandbox_log_file, buffer, len);
310     file_close(sandbox_log_file);
311    
312     color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
313    
314     if (color)
315     printf("\e[31;01m");
316     printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
317     if (color)
318     printf("\033[0m");
319     if (color)
320     printf("\e[31;01m");
321     printf("\nLOG FILE = \"%s\"", sandbox_log);
322     if (color)
323     printf("\033[0m");
324     printf("\n\n");
325     printf("%s", buffer);
326     if (buffer)
327     free(buffer);
328     buffer = NULL;
329     printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
330    
331     beep_count_env = getenv(ENV_SANDBOX_BEEP);
332     if (beep_count_env)
333     beep_count = atoi(beep_count_env);
334     else
335     beep_count = DEFAULT_BEEP_COUNT;
336    
337     for (i = 0; i < beep_count; i++) {
338     fputc('\a', stderr);
339     if (i < beep_count - 1)
340     sleep(1);
341     }
342     return 1;
343     }
344    
345 azarah 30 void stop(int signum)
346 ferringb 2 {
347     if (stop_called == 0) {
348     stop_called = 1;
349     printf("Caught signal %d in pid %d\r\n", signum, getpid());
350 azarah 101 sandbox_cleanup();
351 ferringb 2 } else {
352     fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
353     }
354     }
355    
356 azarah 101 void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
357 ferringb 2 {
358 azarah 89 /* bzero out entire buffer then append trailing 0 */
359 azarah 93 memset(buf, 0, SB_BUF_LEN);
360 azarah 30
361 azarah 89 /* these could go into make.globals later on */
362     snprintf(buf, SB_BUF_LEN,
363     "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
364     "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
365 azarah 105 "/dev/vc/:/dev/pty:/dev/tty:/dev/console:"
366 azarah 89 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
367     "/usr/tmp/conftest:/usr/lib/conftest:"
368     "/usr/lib32/conftest:/usr/lib64/conftest:"
369     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
370 azarah 101 sandbox_info->home_dir, sandbox_info->home_dir,
371     (NULL != sandbox_info->portage_tmp_dir) ? sandbox_info->portage_tmp_dir : tmp_dir,
372     sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
373     "/tmp/:/var/tmp/");
374 azarah 89 }
375    
376 azarah 101 void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
377 azarah 89 {
378 ferringb 2 /* bzero out entire buffer then append trailing 0 */
379 azarah 93 memset(buf, 0, SB_BUF_LEN);
380 ferringb 2
381 azarah 89 /* these should go into make.globals later on */
382     snprintf(buf, SB_BUF_LEN, "%s/.:"
383     "/usr/lib/python2.0/:"
384     "/usr/lib/python2.1/:"
385     "/usr/lib/python2.2/:"
386     "/usr/lib/python2.3/:"
387     "/usr/lib/python2.4/:"
388     "/usr/lib/python2.5/:"
389     "/usr/lib/python3.0/:",
390 azarah 101 sandbox_info->home_dir);
391 azarah 89 }
392 ferringb 2
393 azarah 89 int sandbox_setenv(char **env, char *name, char *val) {
394     char **tmp_env = env;
395     char *tmp_string = NULL;
396    
397 azarah 100 /* XXX: We add the new variable to the end (no replacing). If this
398     * is changed, we need to fix sandbox_setup_environ() below */
399 azarah 89 while (NULL != *tmp_env)
400     tmp_env++;
401    
402     /* strlen(name) + strlen(val) + '=' + '\0' */
403 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
404     * a real leak that will cause issues. */
405 azarah 89 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
406     if (NULL == tmp_string) {
407     perror(">>> out of memory (sandbox_setenv)");
408     exit(1);
409 ferringb 2 }
410 azarah 89
411     snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
412     name, val);
413     *tmp_env = tmp_string;
414    
415     return 0;
416 ferringb 2 }
417    
418 azarah 89 /* We setup the environment child side only to prevent issues with
419     * setting LD_PRELOAD parent side */
420 azarah 101 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info,
421     char *sandbox_write_envvar, char *sandbox_predict_envvar)
422 ferringb 2 {
423 azarah 89 int env_size = 0;
424 azarah 100 int have_ld_preload = 0;
425 azarah 89
426     char **new_environ;
427     char **env_ptr = environ;
428     char *ld_preload_envvar = NULL;
429 azarah 100 char *orig_ld_preload_envvar = NULL;
430 ferringb 2
431 azarah 89 /* Unset these, as its easier than replacing when setting up our
432     * new environment below */
433     unsetenv(ENV_SANDBOX_DIR);
434     unsetenv(ENV_SANDBOX_LIB);
435     unsetenv(ENV_SANDBOX_BASHRC);
436     unsetenv(ENV_SANDBOX_LOG);
437     unsetenv(ENV_SANDBOX_DEBUG_LOG);
438    
439 azarah 100 if (NULL != getenv(ENV_LD_PRELOAD)) {
440     have_ld_preload = 1;
441     orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
442    
443 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
444     * than a real leak that will cause issues. */
445 azarah 100 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
446 azarah 101 strlen(sandbox_info->sandbox_lib) + 2,
447     sizeof(char *));
448 azarah 89 if (NULL == ld_preload_envvar)
449     return NULL;
450 azarah 100 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
451 azarah 101 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
452     sandbox_info->sandbox_lib, orig_ld_preload_envvar);
453 azarah 89 } else {
454 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
455     * than a real leak that will cause issues. */
456 azarah 101 ld_preload_envvar = strndup(sandbox_info->sandbox_lib,
457     strlen(sandbox_info->sandbox_lib));
458 azarah 89 if (NULL == ld_preload_envvar)
459     return NULL;
460     }
461 azarah 100 /* Do not unset this, as strange things might happen */
462     /* unsetenv(ENV_LD_PRELOAD); */
463 ferringb 2
464 azarah 89 while (NULL != *env_ptr) {
465     env_size++;
466     env_ptr++;
467     }
468 ferringb 2
469 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
470     * a real leak that will cause issues. */
471 azarah 89 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
472     if (NULL == new_environ)
473     return NULL;
474    
475     /* First add our new variables to the beginning - this is due to some
476     * weirdness that I cannot remember */
477 azarah 101 sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_info->sandbox_dir);
478     sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
479     sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
480     sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
481     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
482     sandbox_info->sandbox_debug_log);
483 azarah 100 /* If LD_PRELOAD was not set, set it here, else do it below */
484     if (1 != have_ld_preload)
485     sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
486 azarah 89
487     if (!getenv(ENV_SANDBOX_DENY))
488     sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
489    
490     if (!getenv(ENV_SANDBOX_READ))
491     sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
492    
493     if (!getenv(ENV_SANDBOX_WRITE))
494     sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
495    
496     if (!getenv(ENV_SANDBOX_PREDICT))
497     sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
498    
499     /* This one should NEVER be set in ebuilds, as it is the one
500     * private thing libsandbox.so use to test if the sandbox
501     * should be active for this pid, or not.
502     *
503     * azarah (3 Aug 2002)
504     */
505    
506     sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
507    
508     env_size = 0;
509     while (NULL != new_environ[env_size])
510     env_size++;
511    
512     /* Now add the rest */
513     env_ptr = environ;
514     while (NULL != *env_ptr) {
515 azarah 100 if ((1 == have_ld_preload) &&
516     (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
517     /* If LD_PRELOAD was set, and this is it in the original
518     * environment, replace it with our new copy */
519 azarah 101 /* XXX: The following works as it just add whatever as
520     * the last variable to nev_environ */
521 azarah 100 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
522     ld_preload_envvar);
523     else
524     new_environ[env_size + (env_ptr - environ)] = *env_ptr;
525 azarah 89 env_ptr++;
526 ferringb 2 }
527 azarah 89
528     return new_environ;
529 ferringb 2 }
530    
531 azarah 89 int spawn_shell(char *argv_bash[], char *env[])
532 ferringb 2 {
533     int pid;
534     int status = 0;
535     int ret = 0;
536    
537     pid = fork();
538    
539     /* Child's process */
540     if (0 == pid) {
541 azarah 89 execve(argv_bash[0], argv_bash, env);
542 ferringb 2 return 0;
543     } else if (pid < 0) {
544     return 0;
545     }
546     ret = waitpid(pid, &status, 0);
547     if ((-1 == ret) || (status > 0))
548     return 0;
549 azarah 86
550 ferringb 2 return 1;
551     }
552    
553 azarah 30 int main(int argc, char **argv)
554 ferringb 2 {
555 azarah 101 int i = 0, success = 1;
556 ferringb 2 int sandbox_log_presence = 0;
557     long len;
558    
559 azarah 101 struct sandbox_info_t sandbox_info;
560 ferringb 2
561 azarah 89 char **sandbox_environ;
562 azarah 93 char sandbox_write_envvar[SB_BUF_LEN];
563     char sandbox_predict_envvar[SB_BUF_LEN];
564 ferringb 2 char **argv_bash = NULL;
565    
566     char *run_str = "-c";
567     char *home_dir = NULL;
568     char *tmp_string = NULL;
569    
570     /* Only print info if called with no arguments .... */
571     if (argc < 2)
572     print_debug = 1;
573    
574     if (print_debug)
575 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
576 ferringb 2
577     /* check if a sandbox is already running */
578     if (NULL != getenv(ENV_SANDBOX_ON)) {
579 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
580     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
581 ferringb 2 exit(1);
582     } else {
583    
584     /* determine the location of all the sandbox support files */
585     if (print_debug)
586     printf("Detection of the support files.\n");
587    
588 azarah 101 if (-1 == sandbox_setup(argv, &sandbox_info)) {
589     perror(">>> setup");
590 azarah 99 exit(1);
591     }
592 azarah 101
593 ferringb 2 /* verify the existance of required files */
594     if (print_debug)
595     printf("Verification of the required files.\n");
596    
597     #ifndef SB_HAVE_64BIT_ARCH
598 azarah 101 if (file_exist(sandbox_info.sandbox_lib, 0) <= 0) {
599     fprintf(stderr, "Could not open the sandbox library at '%s'.\n",
600     sandbox_info.sandbox_lib);
601 ferringb 2 return -1;
602 azarah 30 }
603 ferringb 2 #endif
604 azarah 101 if (file_exist(sandbox_info.sandbox_rc, 0) <= 0) {
605     fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n",
606     sandbox_info.sandbox_rc);
607 ferringb 2 return -1;
608     }
609    
610     /* set up the required environment variables */
611     if (print_debug)
612     printf("Setting up the required environment variables.\n");
613    
614 azarah 91 /* This one should not be child only, as we check above to see
615     * if we are already running (check sandbox_setup_environ).
616     * This needs to be set before calling sandbox_setup_environ(),
617     * else its not set for the child */
618     setenv(ENV_SANDBOX_ON, "1", 0);
619    
620     /* Setup the child environment stuff */
621 azarah 101 get_sandbox_write_envvar(sandbox_write_envvar, &sandbox_info);
622     get_sandbox_predict_envvar(sandbox_predict_envvar, &sandbox_info);
623     sandbox_environ = sandbox_setup_environ(&sandbox_info,
624 azarah 89 sandbox_write_envvar, sandbox_predict_envvar);
625     if (NULL == sandbox_environ) {
626     perror(">>> out of memory (environ)");
627     exit(1);
628 azarah 71 }
629 ferringb 2
630     /* if the portage temp dir was present, cd into it */
631 azarah 101 if (NULL != sandbox_info.portage_tmp_dir)
632     chdir(sandbox_info.portage_tmp_dir);
633 ferringb 2
634 azarah 30 argv_bash = (char **)malloc(6 * sizeof(char *));
635 ferringb 2 argv_bash[0] = strdup("/bin/bash");
636     argv_bash[1] = strdup("-rcfile");
637 azarah 101 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
638 ferringb 2
639     if (argc < 2)
640     argv_bash[3] = NULL;
641     else
642     argv_bash[3] = strdup(run_str); /* "-c" */
643    
644 azarah 30 argv_bash[4] = NULL; /* strdup(run_arg); */
645 ferringb 2 argv_bash[5] = NULL;
646    
647     if (argc >= 2) {
648     for (i = 1; i < argc; i++) {
649     if (NULL == argv_bash[4])
650     len = 0;
651     else
652     len = strlen(argv_bash[4]);
653    
654 azarah 89 argv_bash[4] = (char *)realloc(argv_bash[4],
655     (len + strlen(argv[i]) + 2) * sizeof(char));
656 ferringb 2
657     if (0 == len)
658     argv_bash[4][0] = 0;
659     if (1 != i)
660     strcat(argv_bash[4], " ");
661    
662     strcat(argv_bash[4], argv[i]);
663     }
664     }
665    
666     /* set up the required signal handlers */
667     signal(SIGHUP, &stop);
668     signal(SIGINT, &stop);
669     signal(SIGQUIT, &stop);
670     signal(SIGTERM, &stop);
671    
672     /* Load our PID into PIDs file */
673 azarah 101 if (-1 == write_pids_file(&sandbox_info)) {
674 ferringb 2 perror(">>> pids file write");
675     exit(1);
676     }
677    
678     /* STARTING PROTECTED ENVIRONMENT */
679     if (print_debug) {
680     printf("The protected environment has been started.\n");
681 azarah 30 printf("--------------------------------------------------------------------------------\n");
682 ferringb 2 }
683    
684     if (print_debug)
685     printf("Shell being started in forked process.\n");
686    
687     /* Start Bash */
688 azarah 89 if (!spawn_shell(argv_bash, sandbox_environ)) {
689 ferringb 2 if (print_debug)
690     fprintf(stderr, ">>> shell process failed to spawn\n");
691     success = 0;
692     }
693    
694     /* Free bash stuff */
695     for (i = 0; i < 6; i++) {
696     if (argv_bash[i])
697     free(argv_bash[i]);
698     argv_bash[i] = NULL;
699     }
700     if (argv_bash)
701     free(argv_bash);
702     argv_bash = NULL;
703    
704     if (print_debug)
705     printf("Cleaning up sandbox process\n");
706    
707 azarah 101 sandbox_cleanup();
708 ferringb 2
709     if (print_debug) {
710 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
711 ferringb 2 printf("The protected environment has been shut down.\n");
712     }
713    
714 azarah 101 if (file_exist(sandbox_info.sandbox_log, 0)) {
715 ferringb 2 sandbox_log_presence = 1;
716     success = 1;
717 azarah 101 if (!print_sandbox_log(sandbox_info.sandbox_log))
718 ferringb 2 success = 0;
719     } else if (print_debug) {
720 azarah 30 printf("--------------------------------------------------------------------------------\n");
721 ferringb 2 }
722    
723 azarah 101 free(sandbox_info.sandbox_pids_file);
724 azarah 35
725 ferringb 2 if ((sandbox_log_presence) || (!success))
726     return 1;
727     else
728     return 0;
729     }
730     }
731    
732 azarah 30 // 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