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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 157 - (hide annotations) (download) (as text)
Fri Aug 5 13:55:08 2005 UTC (8 years, 11 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15401 byte(s)
If we are called from the command line, do not care about PORTAGE_TMPDIR,
and make the current directory the work directory.  Also rename the variable
portage_tmp_dir to work_dir.

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