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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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