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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (show annotations) (download) (as text)
Wed May 11 23:28:42 2005 UTC (9 years, 3 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 17815 byte(s)
Remove global preload_adaptable as it is no longer used.

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 <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 #define SB_BUF_LEN 2048
37
38 int cleaned_up = 0;
39 int print_debug = 0;
40 int stop_called = 0;
41
42 /* Read pids file, and load active pids into an array. Return number of pids in array */
43 int load_active_pids(int fd, int **pids)
44 {
45 char *data = NULL;
46 char *ptr = NULL, *ptr2 = NULL;
47 int my_pid;
48 int num_pids = 0;
49 long len;
50
51 pids[0] = NULL;
52
53 len = file_length(fd);
54
55 /* Allocate and zero datablock to read pids file */
56 data = (char *)malloc((len + 1) * sizeof(char));
57 memset(data, 0, len + 1);
58
59 /* Start at beginning of file */
60 lseek(fd, 0L, SEEK_SET);
61
62 /* read entire file into a buffer */
63 read(fd, data, len);
64
65 ptr = data;
66
67 /* Loop and read all pids */
68 while (1) {
69 /* Find new line */
70 ptr2 = strchr(ptr, '\n');
71 if (ptr2 == NULL)
72 break; /* No more PIDs */
73
74 /* Clear the \n. And ptr should have a null-terminated decimal string */
75 ptr2[0] = 0;
76
77 my_pid = atoi(ptr);
78
79 /* If the PID is still alive, add it to our array */
80 if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
81 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
82 pids[0][num_pids] = my_pid;
83 num_pids++;
84 }
85
86 /* Put ptr past the NULL we just wrote */
87 ptr = ptr2 + 1;
88 }
89
90 if (data)
91 free(data);
92 data = NULL;
93
94 return num_pids;
95 }
96
97 void cleanup()
98 {
99 int i = 0;
100 int success = 1;
101 int pids_file = -1, num_of_pids = 0;
102 int *pids_array = NULL;
103 char pid_string[255];
104 char *sandbox_pids_file;
105
106 /* Generate sandbox pids-file path */
107 sandbox_pids_file = get_sandbox_pids_file();
108
109 /* Remove this sandbox's bash pid from the global pids
110 * file if we have not already done so */
111 if (0 == cleaned_up) {
112 cleaned_up = 1;
113 success = 1;
114
115 if (print_debug)
116 printf("Cleaning up pids file.\n");
117
118 /* Stat the PIDs file, make sure it exists and is a regular file */
119 if (file_exist(sandbox_pids_file, 1) <= 0) {
120 fprintf(stderr, ">>> pids file is not a regular file\n");
121 success = 0;
122 /* We should really not fail if the pidsfile is missing here, but
123 * rather just exit cleanly, as there is still some cleanup to do */
124 return;
125 }
126
127 pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
128 if (-1 == pids_file) {
129 success = 0;
130 /* Nothing more to do here */
131 return;
132 }
133
134 /* Load "still active" pids into an array */
135 num_of_pids = load_active_pids(pids_file, &pids_array);
136 //printf("pids: %d\r\n", num_of_pids);
137
138
139 file_truncate(pids_file);
140
141 /* if pids are still running, write only the running pids back to the file */
142 if (num_of_pids > 1) {
143 for (i = 0; i < num_of_pids; i++) {
144 if (pids_array[i] != getpid()) {
145 sprintf(pid_string, "%d\n", pids_array[i]);
146
147 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
148 perror(">>> pids file write");
149 success = 0;
150 break;
151 }
152 }
153 }
154
155 file_close(pids_file);
156 pids_file = -1;
157 } else {
158
159 file_close(pids_file);
160 pids_file = -1;
161
162 /* remove the pidsfile, as this was the last sandbox */
163 unlink(sandbox_pids_file);
164 }
165
166 if (pids_array != NULL)
167 free(pids_array);
168 pids_array = NULL;
169 }
170
171 free(sandbox_pids_file);
172 if (0 == success)
173 return;
174 }
175
176 int print_sandbox_log(char *sandbox_log)
177 {
178 int sandbox_log_file = -1;
179 char *beep_count_env = NULL;
180 int i, color, beep_count = 0;
181 long len = 0;
182 char *buffer = NULL;
183
184 sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
185 if (-1 == sandbox_log_file)
186 return 0;
187
188 len = file_length(sandbox_log_file);
189 buffer = (char *)malloc((len + 1) * sizeof(char));
190 memset(buffer, 0, len + 1);
191 read(sandbox_log_file, buffer, len);
192 file_close(sandbox_log_file);
193
194 color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
195
196 if (color)
197 printf("\e[31;01m");
198 printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
199 if (color)
200 printf("\033[0m");
201 if (color)
202 printf("\e[31;01m");
203 printf("\nLOG FILE = \"%s\"", sandbox_log);
204 if (color)
205 printf("\033[0m");
206 printf("\n\n");
207 printf("%s", buffer);
208 if (buffer)
209 free(buffer);
210 buffer = NULL;
211 printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
212
213 beep_count_env = getenv(ENV_SANDBOX_BEEP);
214 if (beep_count_env)
215 beep_count = atoi(beep_count_env);
216 else
217 beep_count = DEFAULT_BEEP_COUNT;
218
219 for (i = 0; i < beep_count; i++) {
220 fputc('\a', stderr);
221 if (i < beep_count - 1)
222 sleep(1);
223 }
224 return 1;
225 }
226
227 void stop(int signum)
228 {
229 if (stop_called == 0) {
230 stop_called = 1;
231 printf("Caught signal %d in pid %d\r\n", signum, getpid());
232 cleanup();
233 } else {
234 fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
235 }
236 }
237
238 void get_sandbox_write_envvar(char *buf, char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
239 {
240 /* bzero out entire buffer then append trailing 0 */
241 memset(buf, 0, SB_BUF_LEN + 1);
242
243 /* these could go into make.globals later on */
244 snprintf(buf, SB_BUF_LEN,
245 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
246 "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
247 "/dev/vc/:/dev/pty:/dev/tty:"
248 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
249 "/usr/tmp/conftest:/usr/lib/conftest:"
250 "/usr/lib32/conftest:/usr/lib64/conftest:"
251 "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
252 home_dir, home_dir,
253 (NULL != portage_tmp_dir) ? portage_tmp_dir : tmp_dir,
254 tmp_dir, var_tmp_dir, "/tmp/:/var/tmp/");
255 }
256
257 void get_sandbox_predict_envvar(char *buf, char *home_dir)
258 {
259 /* bzero out entire buffer then append trailing 0 */
260 memset(buf, 0, SB_BUF_LEN + 1);
261
262 /* these should go into make.globals later on */
263 snprintf(buf, SB_BUF_LEN, "%s/.:"
264 "/usr/lib/python2.0/:"
265 "/usr/lib/python2.1/:"
266 "/usr/lib/python2.2/:"
267 "/usr/lib/python2.3/:"
268 "/usr/lib/python2.4/:"
269 "/usr/lib/python2.5/:"
270 "/usr/lib/python3.0/:",
271 home_dir);
272 }
273
274 int sandbox_setenv(char **env, char *name, char *val) {
275 char **tmp_env = env;
276 char *tmp_string = NULL;
277
278 while (NULL != *tmp_env)
279 tmp_env++;
280
281 /* strlen(name) + strlen(val) + '=' + '\0' */
282 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
283 if (NULL == tmp_string) {
284 perror(">>> out of memory (sandbox_setenv)");
285 exit(1);
286 }
287
288 snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
289 name, val);
290 *tmp_env = tmp_string;
291
292 return 0;
293 }
294
295 /* We setup the environment child side only to prevent issues with
296 * setting LD_PRELOAD parent side */
297 char **sandbox_setup_environ(char *sandbox_dir, char *sandbox_lib, char *sandbox_rc, char *sandbox_log,
298 char *sandbox_debug_log, char *sandbox_write_envvar, char *sandbox_predict_envvar)
299 {
300 int env_size = 0;
301
302 char **new_environ;
303 char **env_ptr = environ;
304 char *ld_preload_envvar = NULL;
305
306 /* Unset these, as its easier than replacing when setting up our
307 * new environment below */
308 unsetenv(ENV_SANDBOX_DIR);
309 unsetenv(ENV_SANDBOX_LIB);
310 unsetenv(ENV_SANDBOX_BASHRC);
311 unsetenv(ENV_SANDBOX_LOG);
312 unsetenv(ENV_SANDBOX_DEBUG_LOG);
313
314 if (NULL != getenv("LD_PRELOAD")) {
315 ld_preload_envvar = malloc(strlen(getenv("LD_PRELOAD")) +
316 strlen(sandbox_lib) + 2);
317 if (NULL == ld_preload_envvar)
318 return NULL;
319 strncpy(ld_preload_envvar, sandbox_lib, strlen(sandbox_lib));
320 strncat(ld_preload_envvar, " ", 1);
321 strncat(ld_preload_envvar, getenv("LD_PRELOAD"),
322 strlen(getenv("LD_PRELOAD")));
323 } else {
324 ld_preload_envvar = strndup(sandbox_lib, strlen(sandbox_lib));
325 if (NULL == ld_preload_envvar)
326 return NULL;
327 }
328 unsetenv("LD_PRELOAD");
329
330 while (NULL != *env_ptr) {
331 env_size++;
332 env_ptr++;
333 }
334
335 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
336 if (NULL == new_environ)
337 return NULL;
338
339 /* First add our new variables to the beginning - this is due to some
340 * weirdness that I cannot remember */
341 sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_dir);
342 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_lib);
343 sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_rc);
344 sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_log);
345 sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log);
346 sandbox_setenv(new_environ, "LD_PRELOAD", ld_preload_envvar);
347
348 if (!getenv(ENV_SANDBOX_DENY))
349 sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
350
351 if (!getenv(ENV_SANDBOX_READ))
352 sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
353
354 if (!getenv(ENV_SANDBOX_WRITE))
355 sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
356
357 if (!getenv(ENV_SANDBOX_PREDICT))
358 sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
359
360 /* This one should NEVER be set in ebuilds, as it is the one
361 * private thing libsandbox.so use to test if the sandbox
362 * should be active for this pid, or not.
363 *
364 * azarah (3 Aug 2002)
365 */
366
367 sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
368
369 env_size = 0;
370 while (NULL != new_environ[env_size])
371 env_size++;
372
373 /* Now add the rest */
374 env_ptr = environ;
375 while (NULL != *env_ptr) {
376 new_environ[env_size + (env_ptr - environ)] = *env_ptr;
377 env_ptr++;
378 }
379
380 return new_environ;
381 }
382
383 int spawn_shell(char *argv_bash[], char *env[])
384 {
385 int pid;
386 int status = 0;
387 int ret = 0;
388
389 pid = fork();
390
391 /* Child's process */
392 if (0 == pid) {
393 execve(argv_bash[0], argv_bash, env);
394 return 0;
395 } else if (pid < 0) {
396 return 0;
397 }
398 ret = waitpid(pid, &status, 0);
399 if ((-1 == ret) || (status > 0))
400 return 0;
401
402 return 1;
403 }
404
405 int main(int argc, char **argv)
406 {
407 int ret = 0, i = 0, success = 1;
408 int sandbox_log_presence = 0;
409 int sandbox_log_file = -1;
410 int pids_file = -1;
411 long len;
412
413 int *pids_array = NULL;
414 int num_of_pids = 0;
415
416 // char run_arg[255];
417 char portage_tmp_dir[PATH_MAX];
418 char var_tmp_dir[PATH_MAX];
419 char tmp_dir[PATH_MAX];
420 char sandbox_log[255];
421 char sandbox_debug_log[255];
422 char sandbox_dir[255];
423 char sandbox_lib[255];
424 char sandbox_write_envvar[SB_BUF_LEN + 1];
425 char sandbox_predict_envvar[SB_BUF_LEN + 1];
426 char **sandbox_environ;
427 char *sandbox_pids_file;
428 char sandbox_rc[255];
429 char pid_string[255];
430 char **argv_bash = NULL;
431
432 char *run_str = "-c";
433 char *home_dir = NULL;
434 char *tmp_string = NULL;
435
436 /* Only print info if called with no arguments .... */
437 if (argc < 2)
438 print_debug = 1;
439
440 if (print_debug)
441 printf("========================== Gentoo linux path sandbox ===========================\n");
442
443 /* check if a sandbox is already running */
444 if (NULL != getenv(ENV_SANDBOX_ON)) {
445 fprintf(stderr, "Not launching a new sandbox instance\n");
446 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
447 exit(1);
448 } else {
449
450 /* determine the location of all the sandbox support files */
451 if (print_debug)
452 printf("Detection of the support files.\n");
453
454 /* Generate base sandbox path */
455 tmp_string = get_sandbox_path(argv[0]);
456 strncpy(sandbox_dir, tmp_string, 254);
457 if (tmp_string)
458 free(tmp_string);
459 tmp_string = NULL;
460 strcat(sandbox_dir, "/");
461
462 /* Generate sandbox lib path */
463 tmp_string = get_sandbox_lib(sandbox_dir);
464 strncpy(sandbox_lib, tmp_string, 254);
465 if (tmp_string)
466 free(tmp_string);
467 tmp_string = NULL;
468
469 /* Generate sandbox pids-file path */
470 sandbox_pids_file = get_sandbox_pids_file();
471
472 /* Generate sandbox bashrc path */
473 tmp_string = get_sandbox_rc(sandbox_dir);
474 strncpy(sandbox_rc, tmp_string, 254);
475 if (tmp_string)
476 free(tmp_string);
477 tmp_string = NULL;
478
479 /* verify the existance of required files */
480 if (print_debug)
481 printf("Verification of the required files.\n");
482
483 #ifndef SB_HAVE_64BIT_ARCH
484 if (file_exist(sandbox_lib, 0) <= 0) {
485 fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
486 return -1;
487 }
488 #endif
489 if (file_exist(sandbox_rc, 0) <= 0) {
490 fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
491 return -1;
492 }
493
494 /* set up the required environment variables */
495 if (print_debug)
496 printf("Setting up the required environment variables.\n");
497
498 /* Generate sandbox log full path */
499 tmp_string = get_sandbox_log();
500 strncpy(sandbox_log, tmp_string, 254);
501 if (tmp_string)
502 free(tmp_string);
503 tmp_string = NULL;
504
505 sprintf(pid_string, "%d", getpid());
506 snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
507 DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
508
509 home_dir = getenv("HOME");
510 if (!home_dir) {
511 home_dir = "/tmp";
512 setenv("HOME", home_dir, 1);
513 }
514
515 if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR")
516 : "/var/tmp/portage",
517 portage_tmp_dir)) {
518 perror(">>> get portage_tmp_dir");
519 exit(1);
520 }
521 if (NULL == realpath("/var/tmp", var_tmp_dir)) {
522 perror(">>> get var_tmp_dir");
523 exit(1);
524 }
525 if (NULL == realpath("/tmp", tmp_dir)) {
526 perror(">>> get tmp_dir");
527 exit(1);
528 }
529
530 /* Setup the environment stuff */
531 get_sandbox_write_envvar(sandbox_write_envvar, home_dir,
532 portage_tmp_dir, var_tmp_dir, tmp_dir);
533 get_sandbox_predict_envvar(sandbox_predict_envvar, home_dir);
534 sandbox_environ = sandbox_setup_environ(sandbox_dir, sandbox_lib,
535 sandbox_rc, sandbox_log, sandbox_debug_log,
536 sandbox_write_envvar, sandbox_predict_envvar);
537 if (NULL == sandbox_environ) {
538 perror(">>> out of memory (environ)");
539 exit(1);
540 }
541
542 /* This one should not be child only, as we check above to see
543 * if we are already running (check sandbox_setup_environ) */
544 setenv(ENV_SANDBOX_ON, "1", 0);
545
546 /* if the portage temp dir was present, cd into it */
547 if (NULL != portage_tmp_dir)
548 chdir(portage_tmp_dir);
549
550 argv_bash = (char **)malloc(6 * sizeof(char *));
551 argv_bash[0] = strdup("/bin/bash");
552 argv_bash[1] = strdup("-rcfile");
553 argv_bash[2] = strdup(sandbox_rc);
554
555 if (argc < 2)
556 argv_bash[3] = NULL;
557 else
558 argv_bash[3] = strdup(run_str); /* "-c" */
559
560 argv_bash[4] = NULL; /* strdup(run_arg); */
561 argv_bash[5] = NULL;
562
563 if (argc >= 2) {
564 for (i = 1; i < argc; i++) {
565 if (NULL == argv_bash[4])
566 len = 0;
567 else
568 len = strlen(argv_bash[4]);
569
570 argv_bash[4] = (char *)realloc(argv_bash[4],
571 (len + strlen(argv[i]) + 2) * sizeof(char));
572
573 if (0 == len)
574 argv_bash[4][0] = 0;
575 if (1 != i)
576 strcat(argv_bash[4], " ");
577
578 strcat(argv_bash[4], argv[i]);
579 }
580 }
581
582 /* set up the required signal handlers */
583 signal(SIGHUP, &stop);
584 signal(SIGINT, &stop);
585 signal(SIGQUIT, &stop);
586 signal(SIGTERM, &stop);
587
588 /* Load our PID into PIDs file */
589 success = 1;
590 if (file_exist(sandbox_pids_file, 1) < 0) {
591 success = 0;
592 fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file);
593 } else {
594 pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
595 if (-1 == pids_file)
596 success = 0;
597 }
598 if (1 == success) {
599 /* Grab still active pids */
600 num_of_pids = load_active_pids(pids_file, &pids_array);
601
602 /* Zero out file */
603 file_truncate(pids_file);
604
605 /* Output active pids, and append our pid */
606 for (i = 0; i < num_of_pids + 1; i++) {
607 /* Time for our entry */
608 if (i == num_of_pids)
609 sprintf(pid_string, "%d\n", getpid());
610 else
611 sprintf(pid_string, "%d\n", pids_array[i]);
612
613 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
614 perror(">>> pids file write");
615 success = 0;
616 break;
617 }
618 }
619 /* Clean pids_array */
620 if (pids_array)
621 free(pids_array);
622 pids_array = NULL;
623 num_of_pids = 0;
624
625 /* We're done with the pids file */
626 file_close(pids_file);
627 }
628
629 /* Something went wrong, bail out */
630 if (0 == success) {
631 perror(">>> pids file write");
632 exit(1);
633 }
634
635 /* STARTING PROTECTED ENVIRONMENT */
636 if (print_debug) {
637 printf("The protected environment has been started.\n");
638 printf("--------------------------------------------------------------------------------\n");
639 }
640
641 if (print_debug)
642 printf("Shell being started in forked process.\n");
643
644 /* Start Bash */
645 if (!spawn_shell(argv_bash, sandbox_environ)) {
646 if (print_debug)
647 fprintf(stderr, ">>> shell process failed to spawn\n");
648 success = 0;
649 }
650
651 /* Free bash stuff */
652 for (i = 0; i < 6; i++) {
653 if (argv_bash[i])
654 free(argv_bash[i]);
655 argv_bash[i] = NULL;
656 }
657 if (argv_bash)
658 free(argv_bash);
659 argv_bash = NULL;
660
661 if (print_debug)
662 printf("Cleaning up sandbox process\n");
663
664 cleanup();
665
666 if (print_debug) {
667 printf("========================== Gentoo linux path sandbox ===========================\n");
668 printf("The protected environment has been shut down.\n");
669 }
670
671 if (file_exist(sandbox_log, 0)) {
672 sandbox_log_presence = 1;
673 success = 1;
674 if (!print_sandbox_log(sandbox_log))
675 success = 0;
676
677 #if 0
678 if (!success)
679 exit(1);
680 #endif
681
682 sandbox_log_file = -1;
683 } else if (print_debug) {
684 printf("--------------------------------------------------------------------------------\n");
685 }
686
687 free(sandbox_pids_file);
688
689 if ((sandbox_log_presence) || (!success))
690 return 1;
691 else
692 return 0;
693 }
694 }
695
696 // 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