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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (show annotations) (download) (as text)
Wed May 11 15:48:18 2005 UTC (8 years, 11 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15932 byte(s)
Remove load_preload_libs(), as its not used anymore.

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