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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (hide annotations) (download) (as text)
Thu Jul 7 21:42:33 2005 UTC (9 years ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15253 byte(s)
Major cleanup of sandbox_futils.c.  Removed most of the functions as we now
write to /var/log/sandbox/, so in theory do not need all that.

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