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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 85 - (show annotations) (download) (as text)
Wed May 11 15:40:51 2005 UTC (9 years, 4 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 17285 byte(s)
Remove USE_SYSTEM_SHELL stuff, as it is not secure, and not in use (continued).

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 }
378
379 int main(int argc, char **argv)
380 {
381 int ret = 0, i = 0, success = 1;
382 int sandbox_log_presence = 0;
383 int sandbox_log_file = -1;
384 int pids_file = -1;
385 long len;
386
387 int *pids_array = NULL;
388 int num_of_pids = 0;
389
390 // char run_arg[255];
391 char portage_tmp_dir[PATH_MAX];
392 char var_tmp_dir[PATH_MAX];
393 char tmp_dir[PATH_MAX];
394 char sandbox_log[255];
395 char sandbox_debug_log[255];
396 char sandbox_dir[255];
397 char sandbox_lib[255];
398 char *sandbox_pids_file;
399 char sandbox_rc[255];
400 char pid_string[255];
401 char **argv_bash = NULL;
402
403 char *run_str = "-c";
404 char *home_dir = NULL;
405 char *tmp_string = NULL;
406
407 /* Only print info if called with no arguments .... */
408 if (argc < 2)
409 print_debug = 1;
410
411 if (print_debug)
412 printf("========================== Gentoo linux path sandbox ===========================\n");
413
414 /* check if a sandbox is already running */
415 if (NULL != getenv(ENV_SANDBOX_ON)) {
416 fprintf(stderr, "Not launching a new sandbox instance\n");
417 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
418 exit(1);
419 } else {
420
421 /* determine the location of all the sandbox support files */
422 if (print_debug)
423 printf("Detection of the support files.\n");
424
425 /* Generate base sandbox path */
426 tmp_string = get_sandbox_path(argv[0]);
427 strncpy(sandbox_dir, tmp_string, 254);
428 if (tmp_string)
429 free(tmp_string);
430 tmp_string = NULL;
431 strcat(sandbox_dir, "/");
432
433 /* Generate sandbox lib path */
434 tmp_string = get_sandbox_lib(sandbox_dir);
435 strncpy(sandbox_lib, tmp_string, 254);
436 if (tmp_string)
437 free(tmp_string);
438 tmp_string = NULL;
439
440 /* Generate sandbox pids-file path */
441 sandbox_pids_file = get_sandbox_pids_file();
442
443 /* Generate sandbox bashrc path */
444 tmp_string = get_sandbox_rc(sandbox_dir);
445 strncpy(sandbox_rc, tmp_string, 254);
446 if (tmp_string)
447 free(tmp_string);
448 tmp_string = NULL;
449
450 /* verify the existance of required files */
451 if (print_debug)
452 printf("Verification of the required files.\n");
453
454 #ifndef SB_HAVE_64BIT_ARCH
455 if (file_exist(sandbox_lib, 0) <= 0) {
456 fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
457 return -1;
458 }
459 #endif
460 if (file_exist(sandbox_rc, 0) <= 0) {
461 fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
462 return -1;
463 }
464
465 /* set up the required environment variables */
466 if (print_debug)
467 printf("Setting up the required environment variables.\n");
468
469 /* Generate sandbox log full path */
470 tmp_string = get_sandbox_log();
471 strncpy(sandbox_log, tmp_string, 254);
472 if (tmp_string)
473 free(tmp_string);
474 tmp_string = NULL;
475
476 setenv(ENV_SANDBOX_LOG, sandbox_log, 1);
477
478 sprintf(pid_string, "%d", getpid());
479 snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
480 DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
481 setenv(ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log, 1);
482
483 home_dir = getenv("HOME");
484 if (!home_dir) {
485 home_dir = "/tmp";
486 setenv("HOME", home_dir, 1);
487 }
488
489 if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR")
490 : "/var/tmp/portage",
491 portage_tmp_dir)) {
492 perror(">>> get portage_tmp_dir");
493 exit(1);
494 }
495 if (NULL == realpath("/var/tmp", var_tmp_dir)) {
496 perror(">>> get var_tmp_dir");
497 exit(1);
498 }
499 if (NULL == realpath("/tmp", tmp_dir)) {
500 perror(">>> get tmp_dir");
501 exit(1);
502 }
503
504 setenv(ENV_SANDBOX_DIR, sandbox_dir, 1);
505 setenv(ENV_SANDBOX_LIB, sandbox_lib, 1);
506 setenv(ENV_SANDBOX_BASHRC, sandbox_rc, 1);
507 if ((NULL != getenv("LD_PRELOAD")) &&
508 /* FIXME: for now, do not use current LD_PRELOAD if
509 * it contains libtsocks, as it breaks sandbox, bug #91541.
510 */
511 (NULL == strstr(getenv("LD_PRELOAD"), "libtsocks"))) {
512 tmp_string = malloc(strlen(getenv("LD_PRELOAD")) +
513 strlen(sandbox_lib) + 2);
514 if (NULL == tmp_string) {
515 perror(">>> Out of memory (LD_PRELOAD)");
516 exit(1);
517 }
518 strncpy(tmp_string, sandbox_lib, strlen(sandbox_lib));
519 strncat(tmp_string, " ", 1);
520 strncat(tmp_string, getenv("LD_PRELOAD"), strlen(getenv("LD_PRELOAD")));
521 setenv("LD_PRELOAD", tmp_string, 1);
522 free(tmp_string);
523 } else {
524 setenv("LD_PRELOAD", sandbox_lib, 1);
525 }
526
527 if (!getenv(ENV_SANDBOX_DENY))
528 setenv(ENV_SANDBOX_DENY, LD_PRELOAD_FILE, 1);
529
530 if (!getenv(ENV_SANDBOX_READ))
531 setenv(ENV_SANDBOX_READ, "/", 1);
532
533 /* Set up Sandbox Write path */
534 setenv_sandbox_write(home_dir, portage_tmp_dir, var_tmp_dir, tmp_dir);
535 setenv_sandbox_predict(home_dir);
536
537 setenv(ENV_SANDBOX_ON, "1", 0);
538
539 /* if the portage temp dir was present, cd into it */
540 if (NULL != portage_tmp_dir)
541 chdir(portage_tmp_dir);
542
543 argv_bash = (char **)malloc(6 * sizeof(char *));
544 argv_bash[0] = strdup("/bin/bash");
545 argv_bash[1] = strdup("-rcfile");
546 argv_bash[2] = strdup(sandbox_rc);
547
548 if (argc < 2)
549 argv_bash[3] = NULL;
550 else
551 argv_bash[3] = strdup(run_str); /* "-c" */
552
553 argv_bash[4] = NULL; /* strdup(run_arg); */
554 argv_bash[5] = NULL;
555
556 if (argc >= 2) {
557 for (i = 1; i < argc; i++) {
558 if (NULL == argv_bash[4])
559 len = 0;
560 else
561 len = strlen(argv_bash[4]);
562
563 argv_bash[4] = (char *)realloc(argv_bash[4], (len + strlen(argv[i]) + 2) * sizeof(char));
564
565 if (0 == len)
566 argv_bash[4][0] = 0;
567 if (1 != i)
568 strcat(argv_bash[4], " ");
569
570 strcat(argv_bash[4], argv[i]);
571 }
572 }
573
574 /* set up the required signal handlers */
575 signal(SIGHUP, &stop);
576 signal(SIGINT, &stop);
577 signal(SIGQUIT, &stop);
578 signal(SIGTERM, &stop);
579
580 /* this one should NEVER be set in ebuilds, as it is the one
581 * private thing libsandbox.so use to test if the sandbox
582 * should be active for this pid, or not.
583 *
584 * azarah (3 Aug 2002)
585 */
586
587 setenv("SANDBOX_ACTIVE", "armedandready", 1);
588
589 /* Load our PID into PIDs file */
590 success = 1;
591 if (file_exist(sandbox_pids_file, 1) < 0) {
592 success = 0;
593 fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file);
594 } else {
595 pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
596 if (-1 == pids_file)
597 success = 0;
598 }
599 if (1 == success) {
600 /* Grab still active pids */
601 num_of_pids = load_active_pids(pids_file, &pids_array);
602
603 /* Zero out file */
604 file_truncate(pids_file);
605
606 /* Output active pids, and append our pid */
607 for (i = 0; i < num_of_pids + 1; i++) {
608 /* Time for our entry */
609 if (i == num_of_pids)
610 sprintf(pid_string, "%d\n", getpid());
611 else
612 sprintf(pid_string, "%d\n", pids_array[i]);
613
614 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
615 perror(">>> pids file write");
616 success = 0;
617 break;
618 }
619 }
620 /* Clean pids_array */
621 if (pids_array)
622 free(pids_array);
623 pids_array = NULL;
624 num_of_pids = 0;
625
626 /* We're done with the pids file */
627 file_close(pids_file);
628 }
629
630 /* Something went wrong, bail out */
631 if (0 == success) {
632 perror(">>> pids file write");
633 exit(1);
634 }
635
636 /* STARTING PROTECTED ENVIRONMENT */
637 if (print_debug) {
638 printf("The protected environment has been started.\n");
639 printf("--------------------------------------------------------------------------------\n");
640 }
641
642 if (print_debug)
643 printf("Shell being started in forked process.\n");
644
645 /* Start Bash */
646 if (!spawn_shell(argv_bash)) {
647 if (print_debug)
648 fprintf(stderr, ">>> shell process failed to spawn\n");
649 success = 0;
650 }
651
652 /* Free bash stuff */
653 for (i = 0; i < 6; i++) {
654 if (argv_bash[i])
655 free(argv_bash[i]);
656 argv_bash[i] = NULL;
657 }
658 if (argv_bash)
659 free(argv_bash);
660 argv_bash = NULL;
661
662 if (print_debug)
663 printf("Cleaning up sandbox process\n");
664
665 cleanup();
666
667 if (print_debug) {
668 printf("========================== Gentoo linux path sandbox ===========================\n");
669 printf("The protected environment has been shut down.\n");
670 }
671
672 if (file_exist(sandbox_log, 0)) {
673 sandbox_log_presence = 1;
674 success = 1;
675 if (!print_sandbox_log(sandbox_log))
676 success = 0;
677
678 #if 0
679 if (!success)
680 exit(1);
681 #endif
682
683 sandbox_log_file = -1;
684 } else if (print_debug) {
685 printf("--------------------------------------------------------------------------------\n");
686 }
687
688 free(sandbox_pids_file);
689
690 if ((sandbox_log_presence) || (!success))
691 return 1;
692 else
693 return 0;
694 }
695 }
696
697 // 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