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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 144 - (hide annotations) (download) (as text)
Thu Jul 7 22:58:16 2005 UTC (9 years ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 14558 byte(s)
Cleanup access/log printing.  Make access printing honour NOCOLOR.  Fix log
printing's last line not honouring NOCOLOR.

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