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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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