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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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