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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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