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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 157 - (show annotations) (download) (as text)
Fri Aug 5 13:55:08 2005 UTC (9 years ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15401 byte(s)
If we are called from the command line, do not care about PORTAGE_TMPDIR,
and make the current directory the work directory.  Also rename the variable
portage_tmp_dir to work_dir.

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