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

Contents of /trunk/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 84 - (show annotations) (download) (as text)
Wed May 11 15:39:13 2005 UTC (9 years, 4 months ago) by azarah
File MIME type: text/x-csrc
File size: 17296 byte(s)
Remove USE_SYSTEM_SHELL stuff, as it is not secure, and not in use.

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