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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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