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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download) (as text)
Wed Jul 5 16:51:12 2006 UTC (8 years, 5 months ago) by azarah
File MIME type: text/x-csrc
File size: 17109 byte(s)
Use librcutil.

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