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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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