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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 266 - (show annotations) (download) (as text)
Wed Jul 5 10:55:54 2006 UTC (8 years ago) by azarah
File MIME type: text/x-csrc
File size: 16295 byte(s)
Add /dev/full to write allowed paths, bug #137240.

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 <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <signal.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "sandbox.h"
31
32 struct sandbox_info_t {
33 char sandbox_log[SB_PATH_MAX];
34 char sandbox_debug_log[SB_PATH_MAX];
35 char sandbox_lib[SB_PATH_MAX];
36 char sandbox_rc[SB_PATH_MAX];
37 char work_dir[SB_PATH_MAX];
38 char var_tmp_dir[SB_PATH_MAX];
39 char tmp_dir[SB_PATH_MAX];
40 char *home_dir;
41 } sandbox_info_t;
42
43 static int print_debug = 0;
44 static int stop_called = 0;
45
46 volatile static pid_t child_pid = 0;
47
48 extern char **environ;
49
50 int sandbox_setup(struct sandbox_info_t *sandbox_info)
51 {
52 if (NULL != getenv(ENV_PORTAGE_TMPDIR)) {
53 /* Portage handle setting SANDBOX_WRITE itself. */
54 sandbox_info->work_dir[0] = '\0';
55 } else {
56 if (NULL == getcwd(sandbox_info->work_dir, SB_PATH_MAX)) {
57 perror("sandbox: Failed to get current directory");
58 return -1;
59 }
60 }
61
62 /* Do not resolve symlinks, etc .. libsandbox will handle that. */
63 if (1 != is_dir(VAR_TMPDIR, 1)) {
64 perror("sandbox: Failed to get var_tmp_dir");
65 return -1;
66 }
67 snprintf(sandbox_info->var_tmp_dir, SB_PATH_MAX, "%s", VAR_TMPDIR);
68
69 if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
70 perror("sandbox: Failed to get tmp_dir");
71 return -1;
72 }
73 setenv(ENV_TMPDIR, sandbox_info->tmp_dir, 1);
74
75 sandbox_info->home_dir = getenv("HOME");
76 if (!sandbox_info->home_dir) {
77 sandbox_info->home_dir = sandbox_info->tmp_dir;
78 setenv("HOME", sandbox_info->home_dir, 1);
79 }
80
81 /* Generate sandbox lib path */
82 get_sandbox_lib(sandbox_info->sandbox_lib);
83
84 /* Generate sandbox bashrc path */
85 get_sandbox_rc(sandbox_info->sandbox_rc);
86
87 /* Generate sandbox log full path */
88 get_sandbox_log(sandbox_info->sandbox_log);
89 if (1 == exists(sandbox_info->sandbox_log)) {
90 if (-1 == unlink(sandbox_info->sandbox_log)) {
91 perror("sandbox: Could not unlink old log file");
92 return -1;
93 }
94 }
95
96 /* Generate sandbox debug log full path */
97 get_sandbox_debug_log(sandbox_info->sandbox_debug_log);
98 if (1 == exists(sandbox_info->sandbox_debug_log)) {
99 if (-1 == unlink(sandbox_info->sandbox_debug_log)) {
100 perror("sandbox: Could not unlink old debug log file");
101 return -1;
102 }
103 }
104
105 return 0;
106 }
107
108 int print_sandbox_log(char *sandbox_log)
109 {
110 int sandbox_log_file = -1;
111 char *beep_count_env = NULL;
112 int i, color, beep_count = 0;
113 long len = 0;
114 char *buffer = NULL;
115
116 if (1 != is_file(sandbox_log)) {
117 perror("sandbox: Log file is not a regular file");
118 return 0;
119 }
120
121 sandbox_log_file = open(sandbox_log, O_RDONLY);
122 if (-1 == sandbox_log_file) {
123 perror("sandbox: Could not open Log file");
124 return 0;
125 }
126
127 len = file_length(sandbox_log_file);
128 buffer = (char *)malloc((len + 1) * sizeof(char));
129 memset(buffer, 0, len + 1);
130 read(sandbox_log_file, buffer, len);
131 close(sandbox_log_file);
132
133 color = ((is_env_on(ENV_NOCOLOR)) ? 0 : 1);
134
135 EERROR(color,
136 "--------------------------- ACCESS VIOLATION SUMMARY ---------------------------",
137 "\n");
138 EERROR(color, "LOG FILE = \"%s\"", "\n\n", sandbox_log);
139 fprintf(stderr, "%s", buffer);
140 if (NULL != buffer)
141 free(buffer);
142 EERROR(color,
143 "--------------------------------------------------------------------------------",
144 "\n");
145
146 beep_count_env = getenv(ENV_SANDBOX_BEEP);
147 if (beep_count_env)
148 beep_count = atoi(beep_count_env);
149 else
150 beep_count = DEFAULT_BEEP_COUNT;
151
152 for (i = 0; i < beep_count; i++) {
153 fputc('\a', stderr);
154 if (i < beep_count - 1)
155 sleep(1);
156 }
157
158 return 1;
159 }
160
161 void stop(int signum)
162 {
163 if (stop_called == 0) {
164 stop_called = 1;
165 printf("sandbox: Caught signal %d in pid %d\n",
166 signum, getpid());
167
168 if ((SIGUSR1 == signum) && (0 != child_pid))
169 kill(child_pid, SIGKILL);
170 } else {
171 fprintf(stderr,
172 "sandbox: Signal already caught and busy still cleaning up!\n");
173 }
174 }
175
176 int get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
177 {
178 int retval = 0;
179
180 /* bzero out entire buffer then append trailing 0 */
181 memset(buf, 0, SB_BUF_LEN);
182
183 /* these could go into make.globals later on */
184 retval = snprintf(buf, SB_BUF_LEN,
185 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
186 "/dev/zero:/dev/null:/dev/full:/dev/fd:/proc/self/fd:/dev/pts/:"
187 "/dev/vc/:/dev/pty:/dev/tty:/dev/tts:/dev/console:"
188 "/dev/shm:/dev/shm/ngpt:/var/log/scrollkeeper.log:"
189 "/usr/tmp/conftest:/usr/lib/conftest:"
190 "/usr/lib32/conftest:/usr/lib64/conftest:"
191 "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
192 sandbox_info->home_dir, sandbox_info->home_dir,
193 sandbox_info->work_dir[0] != '\0' ? sandbox_info->work_dir
194 : "",
195 sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
196 "/tmp/:/var/tmp/");
197 if (SB_BUF_LEN <= retval) {
198 errno = EMSGSIZE;
199 perror("sandbox: Failed to generate SANDBOX_WRITE");
200 return -1;
201 }
202
203 return 0;
204 }
205
206 int get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
207 {
208 int retval = 0;
209 /* bzero out entire buffer then append trailing 0 */
210 memset(buf, 0, SB_BUF_LEN);
211
212 /* these should go into make.globals later on */
213 retval = snprintf(buf, SB_BUF_LEN, "%s/.:"
214 "/usr/lib/python2.0/:"
215 "/usr/lib/python2.1/:"
216 "/usr/lib/python2.2/:"
217 "/usr/lib/python2.3/:"
218 "/usr/lib/python2.4/:"
219 "/usr/lib/python2.5/:"
220 "/usr/lib/python3.0/:"
221 "/var/db/aliases.db:"
222 "/var/db/netgroup.db:"
223 "/var/db/netmasks.db:"
224 "/var/db/ethers.db:"
225 "/var/db/rpc.db:"
226 "/var/db/protocols.db:"
227 "/var/db/services.db:"
228 "/var/db/networks.db:"
229 "/var/db/hosts.db:"
230 "/var/db/group.db:"
231 "/var/db/passwd.db",
232 sandbox_info->home_dir);
233 if (SB_BUF_LEN <= retval) {
234 errno = EMSGSIZE;
235 perror("sandbox: Failed to generate SANDBOX_PREDICT");
236 return -1;
237 }
238
239 return 0;
240 }
241
242 int sandbox_setenv(char **env, const char *name, const char *val) {
243 char **tmp_env = env;
244 char *tmp_string = NULL;
245 int retval = 0;
246
247 /* XXX: We add the new variable to the end (no replacing). If this
248 * is changed, we need to fix sandbox_setup_environ() below */
249 while (NULL != *tmp_env)
250 tmp_env++;
251
252 /* strlen(name) + strlen(val) + '=' + '\0' */
253 /* FIXME: Should probably free this at some stage - more neatness than
254 * a real leak that will cause issues. */
255 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
256 if (NULL == tmp_string) {
257 perror("sandbox: Out of memory (sandbox_setenv)");
258 exit(EXIT_FAILURE);
259 }
260
261 retval = snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
262 name, val);
263 *tmp_env = tmp_string;
264
265 return 0;
266 }
267
268 /* We setup the environment child side only to prevent issues with
269 * setting LD_PRELOAD parent side */
270 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info, bool interactive)
271 {
272 int env_size = 0;
273 int have_ld_preload = 0;
274
275 char **new_environ;
276 char **env_ptr = environ;
277 char sandbox_write_envvar[SB_BUF_LEN];
278 char sandbox_predict_envvar[SB_BUF_LEN];
279 char *ld_preload_envvar = NULL;
280 char *orig_ld_preload_envvar = NULL;
281 char sb_pid[64];
282
283 /* Unset these, as its easier than replacing when setting up our
284 * new environment below */
285 unsetenv(ENV_SANDBOX_ON);
286 unsetenv(ENV_SANDBOX_PID);
287 unsetenv(ENV_SANDBOX_LIB);
288 unsetenv(ENV_SANDBOX_BASHRC);
289 unsetenv(ENV_SANDBOX_LOG);
290 unsetenv(ENV_SANDBOX_DEBUG_LOG);
291 unsetenv(ENV_SANDBOX_ACTIVE);
292
293 if (NULL != getenv(ENV_LD_PRELOAD)) {
294 have_ld_preload = 1;
295 orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
296
297 /* FIXME: Should probably free this at some stage - more neatness
298 * than a real leak that will cause issues. */
299 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
300 strlen(sandbox_info->sandbox_lib) + 2,
301 sizeof(char *));
302 if (NULL == ld_preload_envvar)
303 return NULL;
304 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
305 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
306 sandbox_info->sandbox_lib, orig_ld_preload_envvar);
307 } else {
308 /* FIXME: Should probably free this at some stage - more neatness
309 * than a real leak that will cause issues. */
310 ld_preload_envvar = gstrndup(sandbox_info->sandbox_lib,
311 strlen(sandbox_info->sandbox_lib));
312 if (NULL == ld_preload_envvar)
313 return NULL;
314 }
315 /* Do not unset this, as strange things might happen */
316 /* unsetenv(ENV_LD_PRELOAD); */
317
318 while (NULL != *env_ptr) {
319 env_size++;
320 env_ptr++;
321 }
322
323 /* FIXME: Should probably free this at some stage - more neatness than
324 * a real leak that will cause issues. */
325 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
326 if (NULL == new_environ)
327 return NULL;
328
329 snprintf(sb_pid, sizeof(sb_pid), "%i", getpid());
330
331 /* First add our new variables to the beginning - this is due to some
332 * weirdness that I cannot remember */
333 sandbox_setenv(new_environ, ENV_SANDBOX_ON, "1");
334 sandbox_setenv(new_environ, ENV_SANDBOX_PID, sb_pid);
335 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
336 sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
337 sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
338 sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
339 sandbox_info->sandbox_debug_log);
340 /* Is this an interactive session? */
341 if (interactive)
342 sandbox_setenv(new_environ, ENV_SANDBOX_INTRACTV, "1");
343 /* Just set the these if not already set so that is_env_on() work */
344 if (!getenv(ENV_SANDBOX_VERBOSE))
345 sandbox_setenv(new_environ, ENV_SANDBOX_VERBOSE, "1");
346 if (!getenv(ENV_SANDBOX_DEBUG))
347 sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG, "0");
348 if (!getenv(ENV_NOCOLOR))
349 sandbox_setenv(new_environ, ENV_NOCOLOR, "0");
350 /* If LD_PRELOAD was not set, set it here, else do it below */
351 if (1 != have_ld_preload)
352 sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
353
354 if (!getenv(ENV_SANDBOX_DENY))
355 sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
356
357 if (!getenv(ENV_SANDBOX_READ))
358 sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
359
360 if (-1 == get_sandbox_write_envvar(sandbox_write_envvar, sandbox_info))
361 return NULL;
362 if (!getenv(ENV_SANDBOX_WRITE))
363 sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
364
365 if (-1 == get_sandbox_predict_envvar(sandbox_predict_envvar, sandbox_info))
366 return NULL;
367 if (!getenv(ENV_SANDBOX_PREDICT))
368 sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
369
370 /* Make sure our bashrc gets preference */
371 sandbox_setenv(new_environ, ENV_BASH_ENV, sandbox_info->sandbox_rc);
372
373 /* This one should NEVER be set in ebuilds, as it is the one
374 * private thing libsandbox.so use to test if the sandbox
375 * should be active for this pid, or not.
376 *
377 * azarah (3 Aug 2002)
378 */
379
380 sandbox_setenv(new_environ, ENV_SANDBOX_ACTIVE, SANDBOX_ACTIVE);
381
382 env_size = 0;
383 while (NULL != new_environ[env_size])
384 env_size++;
385
386 /* Now add the rest */
387 env_ptr = environ;
388 while (NULL != *env_ptr) {
389 if ((1 == have_ld_preload) &&
390 (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
391 /* If LD_PRELOAD was set, and this is it in the original
392 * environment, replace it with our new copy */
393 /* XXX: The following works as it just add whatever as
394 * the last variable to nev_environ */
395 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
396 ld_preload_envvar);
397 else
398 new_environ[env_size + (env_ptr - environ)] = *env_ptr;
399 env_ptr++;
400 }
401
402 return new_environ;
403 }
404
405 int spawn_shell(char *argv_bash[], char *env[], int debug)
406 {
407 int status = 0;
408 int ret = 0;
409
410 child_pid = fork();
411
412 /* Child's process */
413 if (0 == child_pid) {
414 execve(argv_bash[0], argv_bash, env);
415 return 0;
416 } else if (child_pid < 0) {
417 if (debug)
418 fprintf(stderr, "Process failed to spawn!\n");
419 return 0;
420 }
421 ret = waitpid(child_pid, &status, 0);
422 if ((-1 == ret) || (status > 0)) {
423 if (debug)
424 fprintf(stderr, "Process returned with failed exit status!\n");
425 return 0;
426 }
427
428 return 1;
429 }
430
431 int main(int argc, char **argv)
432 {
433 int i = 0, success = 1;
434 int sandbox_log_presence = 0;
435 long len;
436
437 struct sandbox_info_t sandbox_info;
438
439 char **sandbox_environ;
440 char **argv_bash = NULL;
441
442 char *run_str = "-c";
443
444 /* Only print info if called with no arguments .... */
445 if (argc < 2)
446 print_debug = 1;
447
448 if (print_debug)
449 printf("========================== Gentoo linux path sandbox ===========================\n");
450
451 /* check if a sandbox is already running */
452 if (NULL != getenv(ENV_SANDBOX_ACTIVE)) {
453 fprintf(stderr, "Not launching a new sandbox instance\n");
454 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
455 exit(EXIT_FAILURE);
456 }
457
458 /* determine the location of all the sandbox support files */
459 if (print_debug)
460 printf("Detection of the support files.\n");
461
462 if (-1 == sandbox_setup(&sandbox_info)) {
463 fprintf(stderr, "sandbox: Failed to setup sandbox.");
464 exit(EXIT_FAILURE);
465 }
466
467 /* verify the existance of required files */
468 if (print_debug)
469 printf("Verification of the required files.\n");
470
471 #ifndef SB_HAVE_MULTILIB
472 if (0 >= exists(sandbox_info.sandbox_lib)) {
473 perror("sandbox: Could not open the sandbox library");
474 exit(EXIT_FAILURE);
475 }
476 #endif
477 if (0 >= exists(sandbox_info.sandbox_rc)) {
478 perror("sandbox: Could not open the sandbox rc file");
479 exit(EXIT_FAILURE);
480 }
481
482 /* set up the required environment variables */
483 if (print_debug)
484 printf("Setting up the required environment variables.\n");
485
486 /* Setup the child environment stuff */
487 sandbox_environ = sandbox_setup_environ(&sandbox_info, print_debug);
488 if (NULL == sandbox_environ) {
489 perror("sandbox: Out of memory (environ)");
490 exit(EXIT_FAILURE);
491 }
492
493 /* If not in portage, cd into it work directory */
494 if ('\0' != sandbox_info.work_dir[0])
495 chdir(sandbox_info.work_dir);
496
497 argv_bash = (char **)malloc(6 * sizeof(char *));
498 argv_bash[0] = strdup("/bin/bash");
499 argv_bash[1] = strdup("-rcfile");
500 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
501
502 if (argc < 2)
503 argv_bash[3] = NULL;
504 else
505 argv_bash[3] = strdup(run_str); /* "-c" */
506
507 argv_bash[4] = NULL; /* strdup(run_arg); */
508 argv_bash[5] = NULL;
509
510 if (argc >= 2) {
511 for (i = 1; i < argc; i++) {
512 if (NULL == argv_bash[4])
513 len = 0;
514 else
515 len = strlen(argv_bash[4]);
516
517 argv_bash[4] = (char *)realloc(argv_bash[4],
518 (len + strlen(argv[i]) + 2) * sizeof(char));
519
520 if (0 == len)
521 argv_bash[4][0] = 0;
522 if (1 != i)
523 strcat(argv_bash[4], " ");
524
525 strcat(argv_bash[4], argv[i]);
526 }
527 }
528
529 /* set up the required signal handlers */
530 signal(SIGHUP, &stop);
531 signal(SIGINT, &stop);
532 signal(SIGQUIT, &stop);
533 signal(SIGTERM, &stop);
534 signal(SIGUSR1, &stop);
535
536 /* STARTING PROTECTED ENVIRONMENT */
537 if (print_debug) {
538 printf("The protected environment has been started.\n");
539 printf("--------------------------------------------------------------------------------\n");
540 }
541
542 if (print_debug)
543 printf("Process being started in forked instance.\n");
544
545 /* Start Bash */
546 if (!spawn_shell(argv_bash, sandbox_environ, print_debug))
547 success = 0;
548
549 /* Free bash stuff */
550 for (i = 0; i < 6; i++) {
551 if (argv_bash[i])
552 free(argv_bash[i]);
553 argv_bash[i] = NULL;
554 }
555 if (argv_bash)
556 free(argv_bash);
557 argv_bash = NULL;
558
559 if (print_debug)
560 printf("Cleaning up sandbox process\n");
561
562 if (print_debug) {
563 printf("========================== Gentoo linux path sandbox ===========================\n");
564 printf("The protected environment has been shut down.\n");
565 }
566
567 if (1 == exists(sandbox_info.sandbox_log)) {
568 sandbox_log_presence = 1;
569 print_sandbox_log(sandbox_info.sandbox_log);
570 } else if (print_debug) {
571 printf("--------------------------------------------------------------------------------\n");
572 }
573
574 if ((sandbox_log_presence) || (!success))
575 return 1;
576 else
577 return 0;
578 }
579
580 // 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