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

Contents of /trunk/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (hide annotations) (download) (as text)
Wed May 11 15:52:08 2005 UTC (9 years, 7 months ago) by azarah
File MIME type: text/x-csrc
File size: 15932 byte(s)
Move print_sandbox_log() up to make things neater.

1 ferringb 2 /*
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 azarah 30 /* 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 ferringb 2 {
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 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
56 ferringb 2 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 azarah 30 break; /* No more PIDs */
72 ferringb 2
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 azarah 30 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
81 ferringb 2 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 azarah 30 void cleanup()
97 ferringb 2 {
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 ferringb 34 fprintf(stderr, ">>> pids file is not a regular file\n");
120 ferringb 2 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 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
147 ferringb 2 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 azarah 88 int print_sandbox_log(char *sandbox_log)
176     {
177     int sandbox_log_file = -1;
178     char *beep_count_env = NULL;
179     int i, color, beep_count = 0;
180     long len = 0;
181     char *buffer = NULL;
182    
183     sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
184     if (-1 == sandbox_log_file)
185     return 0;
186    
187     len = file_length(sandbox_log_file);
188     buffer = (char *)malloc((len + 1) * sizeof(char));
189     memset(buffer, 0, len + 1);
190     read(sandbox_log_file, buffer, len);
191     file_close(sandbox_log_file);
192    
193     color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
194    
195     if (color)
196     printf("\e[31;01m");
197     printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
198     if (color)
199     printf("\033[0m");
200     if (color)
201     printf("\e[31;01m");
202     printf("\nLOG FILE = \"%s\"", sandbox_log);
203     if (color)
204     printf("\033[0m");
205     printf("\n\n");
206     printf("%s", buffer);
207     if (buffer)
208     free(buffer);
209     buffer = NULL;
210     printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
211    
212     beep_count_env = getenv(ENV_SANDBOX_BEEP);
213     if (beep_count_env)
214     beep_count = atoi(beep_count_env);
215     else
216     beep_count = DEFAULT_BEEP_COUNT;
217    
218     for (i = 0; i < beep_count; i++) {
219     fputc('\a', stderr);
220     if (i < beep_count - 1)
221     sleep(1);
222     }
223     return 1;
224     }
225    
226 azarah 30 void stop(int signum)
227 ferringb 2 {
228     if (stop_called == 0) {
229     stop_called = 1;
230     printf("Caught signal %d in pid %d\r\n", signum, getpid());
231 azarah 30 cleanup();
232 ferringb 2 } else {
233     fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
234     }
235     }
236    
237 azarah 30 void setenv_sandbox_write(char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
238 ferringb 2 {
239     char buf[1024];
240 azarah 30
241 ferringb 2 /* bzero out entire buffer then append trailing 0 */
242     memset(buf, 0, sizeof(buf));
243    
244     if (!getenv(ENV_SANDBOX_WRITE)) {
245     /* these could go into make.globals later on */
246     snprintf(buf, sizeof(buf),
247 azarah 30 "%s:%s/.gconfd/lock:%s/.bash_history:",
248     "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
249 azarah 65 "/dev/vc/:/dev/pty:/dev/tty:/tmp/:"
250 azarah 30 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
251     "/usr/tmp/conftest:/usr/lib/conftest:"
252     "/usr/lib32/conftest:/usr/lib64/conftest:"
253     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
254     home_dir, home_dir);
255 ferringb 2
256     if (NULL == portage_tmp_dir) {
257     strncat(buf, tmp_dir, sizeof(buf));
258     strncat(buf, ":", sizeof(buf));
259     strncat(buf, var_tmp_dir, sizeof(buf));
260     strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf));
261     } else {
262     strncat(buf, portage_tmp_dir, sizeof(buf));
263     strncat(buf, ":", sizeof(buf));
264     strncat(buf, tmp_dir, sizeof(buf));
265     strncat(buf, ":", sizeof(buf));
266     strncat(buf, var_tmp_dir, sizeof(buf));
267     strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf));
268     }
269     buf[sizeof(buf) - 1] = '\0';
270     setenv(ENV_SANDBOX_WRITE, buf, 1);
271     }
272     }
273    
274 azarah 30 void setenv_sandbox_predict(char *home_dir)
275 ferringb 2 {
276     char buf[1024];
277    
278     memset(buf, 0, sizeof(buf));
279    
280     if (!getenv(ENV_SANDBOX_PREDICT)) {
281     /* these should go into make.globals later on */
282 azarah 30 snprintf(buf, sizeof(buf), "%s/.:"
283     "/usr/lib/python2.0/:"
284     "/usr/lib/python2.1/:"
285     "/usr/lib/python2.2/:"
286     "/usr/lib/python2.3/:"
287     "/usr/lib/python2.4/:"
288     "/usr/lib/python2.5/:"
289     "/usr/lib/python3.0/:",
290     home_dir);
291 ferringb 2
292     buf[sizeof(buf) - 1] = '\0';
293     setenv(ENV_SANDBOX_PREDICT, buf, 1);
294     }
295     }
296    
297 azarah 30 int spawn_shell(char *argv_bash[])
298 ferringb 2 {
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 azarah 86
316 ferringb 2 return 1;
317     }
318    
319 azarah 30 int main(int argc, char **argv)
320 ferringb 2 {
321 azarah 35 int ret = 0, i = 0, success = 1;
322 ferringb 2 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 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
353 ferringb 2
354     /* check if a sandbox is already running */
355     if (NULL != getenv(ENV_SANDBOX_ON)) {
356 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
357     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
358 ferringb 2 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 azarah 30 fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
397 ferringb 2 return -1;
398 azarah 30 }
399 ferringb 2 #endif
400     if (file_exist(sandbox_rc, 0) <= 0) {
401 azarah 30 fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
402 ferringb 2 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 azarah 35 sprintf(pid_string, "%d", getpid());
419 ferringb 2 snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
420 azarah 30 DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
421 ferringb 2 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 azarah 35 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 ferringb 2 }
435 azarah 35 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 ferringb 2
444     setenv(ENV_SANDBOX_DIR, sandbox_dir, 1);
445     setenv(ENV_SANDBOX_LIB, sandbox_lib, 1);
446 azarah 70 setenv(ENV_SANDBOX_BASHRC, sandbox_rc, 1);
447 azarah 78 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 azarah 71 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 azarah 77 strncpy(tmp_string, sandbox_lib, strlen(sandbox_lib));
459 azarah 71 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 ferringb 2
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 azarah 30 argv_bash = (char **)malloc(6 * sizeof(char *));
484 ferringb 2 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 azarah 30 argv_bash[4] = NULL; /* strdup(run_arg); */
494 ferringb 2 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 azarah 30 argv_bash[4] = (char *)realloc(argv_bash[4], (len + strlen(argv[i]) + 2) * sizeof(char));
504 ferringb 2
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 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
555 ferringb 2 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 azarah 30 printf("--------------------------------------------------------------------------------\n");
580 ferringb 2 }
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 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
609 ferringb 2 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 azarah 30 printf("--------------------------------------------------------------------------------\n");
626 ferringb 2 }
627    
628 azarah 35 free(sandbox_pids_file);
629    
630 ferringb 2 if ((sandbox_log_presence) || (!success))
631     return 1;
632     else
633     return 0;
634     }
635     }
636    
637 azarah 30 // 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