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

Contents of /trunk/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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