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

Contents of /trunk/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 158 - (show annotations) (download) (as text)
Fri Aug 5 13:58:53 2005 UTC (8 years, 8 months ago) by azarah
File MIME type: text/x-csrc
File size: 15327 byte(s)
Remove the tmp_dir variable - we do not need it.
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 void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
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 snprintf(buf, SB_BUF_LEN,
185 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
186 "/dev/zero:/dev/null:/dev/fd:/proc/self/fd:/dev/pts/:"
187 "/dev/vc/:/dev/pty:/dev/tty:/dev/tts:/dev/console:"
188 "/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,
194 sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
195 "/tmp/:/var/tmp/");
196 }
197
198 void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
199 {
200 /* bzero out entire buffer then append trailing 0 */
201 memset(buf, 0, SB_BUF_LEN);
202
203 /* these should go into make.globals later on */
204 snprintf(buf, SB_BUF_LEN, "%s/.:"
205 "/usr/lib/python2.0/:"
206 "/usr/lib/python2.1/:"
207 "/usr/lib/python2.2/:"
208 "/usr/lib/python2.3/:"
209 "/usr/lib/python2.4/:"
210 "/usr/lib/python2.5/:"
211 "/usr/lib/python3.0/:"
212 "/var/db/aliases.db:"
213 "/var/db/netgroup.db:"
214 "/var/db/netmasks.db:"
215 "/var/db/ethers.db:"
216 "/var/db/rpc.db:"
217 "/var/db/protocols.db:"
218 "/var/db/services.db:"
219 "/var/db/networks.db:"
220 "/var/db/hosts.db:"
221 "/var/db/group.db:"
222 "/var/db/passwd.db",
223 sandbox_info->home_dir);
224 }
225
226 int sandbox_setenv(char **env, const char *name, const char *val) {
227 char **tmp_env = env;
228 char *tmp_string = NULL;
229
230 /* XXX: We add the new variable to the end (no replacing). If this
231 * is changed, we need to fix sandbox_setup_environ() below */
232 while (NULL != *tmp_env)
233 tmp_env++;
234
235 /* strlen(name) + strlen(val) + '=' + '\0' */
236 /* FIXME: Should probably free this at some stage - more neatness than
237 * a real leak that will cause issues. */
238 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
239 if (NULL == tmp_string) {
240 perror("sandbox: Out of memory (sandbox_setenv)");
241 exit(EXIT_FAILURE);
242 }
243
244 snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
245 name, val);
246 *tmp_env = tmp_string;
247
248 return 0;
249 }
250
251 /* We setup the environment child side only to prevent issues with
252 * setting LD_PRELOAD parent side */
253 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info)
254 {
255 int env_size = 0;
256 int have_ld_preload = 0;
257
258 char **new_environ;
259 char **env_ptr = environ;
260 char sandbox_write_envvar[SB_BUF_LEN];
261 char sandbox_predict_envvar[SB_BUF_LEN];
262 char *ld_preload_envvar = NULL;
263 char *orig_ld_preload_envvar = NULL;
264
265 /* Unset these, as its easier than replacing when setting up our
266 * new environment below */
267 unsetenv(ENV_SANDBOX_LIB);
268 unsetenv(ENV_SANDBOX_BASHRC);
269 unsetenv(ENV_SANDBOX_LOG);
270 unsetenv(ENV_SANDBOX_DEBUG_LOG);
271
272 if (NULL != getenv(ENV_LD_PRELOAD)) {
273 have_ld_preload = 1;
274 orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
275
276 /* FIXME: Should probably free this at some stage - more neatness
277 * than a real leak that will cause issues. */
278 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
279 strlen(sandbox_info->sandbox_lib) + 2,
280 sizeof(char *));
281 if (NULL == ld_preload_envvar)
282 return NULL;
283 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
284 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
285 sandbox_info->sandbox_lib, orig_ld_preload_envvar);
286 } else {
287 /* FIXME: Should probably free this at some stage - more neatness
288 * than a real leak that will cause issues. */
289 ld_preload_envvar = strndup(sandbox_info->sandbox_lib,
290 strlen(sandbox_info->sandbox_lib));
291 if (NULL == ld_preload_envvar)
292 return NULL;
293 }
294 /* Do not unset this, as strange things might happen */
295 /* unsetenv(ENV_LD_PRELOAD); */
296
297 while (NULL != *env_ptr) {
298 env_size++;
299 env_ptr++;
300 }
301
302 /* FIXME: Should probably free this at some stage - more neatness than
303 * a real leak that will cause issues. */
304 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
305 if (NULL == new_environ)
306 return NULL;
307
308 /* First add our new variables to the beginning - this is due to some
309 * weirdness that I cannot remember */
310 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
311 sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
312 sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
313 sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
314 sandbox_info->sandbox_debug_log);
315 /* If LD_PRELOAD was not set, set it here, else do it below */
316 if (1 != have_ld_preload)
317 sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
318
319 if (!getenv(ENV_SANDBOX_DENY))
320 sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
321
322 if (!getenv(ENV_SANDBOX_READ))
323 sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
324
325 get_sandbox_write_envvar(sandbox_write_envvar, sandbox_info);
326 if (!getenv(ENV_SANDBOX_WRITE))
327 sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
328
329 get_sandbox_predict_envvar(sandbox_predict_envvar, sandbox_info);
330 if (!getenv(ENV_SANDBOX_PREDICT))
331 sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
332
333 /* This one should NEVER be set in ebuilds, as it is the one
334 * private thing libsandbox.so use to test if the sandbox
335 * should be active for this pid, or not.
336 *
337 * azarah (3 Aug 2002)
338 */
339
340 sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
341
342 env_size = 0;
343 while (NULL != new_environ[env_size])
344 env_size++;
345
346 /* Now add the rest */
347 env_ptr = environ;
348 while (NULL != *env_ptr) {
349 if ((1 == have_ld_preload) &&
350 (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
351 /* If LD_PRELOAD was set, and this is it in the original
352 * environment, replace it with our new copy */
353 /* XXX: The following works as it just add whatever as
354 * the last variable to nev_environ */
355 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
356 ld_preload_envvar);
357 else
358 new_environ[env_size + (env_ptr - environ)] = *env_ptr;
359 env_ptr++;
360 }
361
362 return new_environ;
363 }
364
365 int spawn_shell(char *argv_bash[], char *env[], int debug)
366 {
367 int pid;
368 int status = 0;
369 int ret = 0;
370
371 pid = fork();
372
373 /* Child's process */
374 if (0 == pid) {
375 execve(argv_bash[0], argv_bash, env);
376 return 0;
377 } else if (pid < 0) {
378 if (debug)
379 fprintf(stderr, "Process failed to spawn!\n");
380 return 0;
381 }
382 ret = waitpid(pid, &status, 0);
383 if ((-1 == ret) || (status > 0)) {
384 if (debug)
385 fprintf(stderr, "Process returned with failed exit status!\n");
386 return 0;
387 }
388
389 return 1;
390 }
391
392 int main(int argc, char **argv)
393 {
394 int i = 0, success = 1;
395 int sandbox_log_presence = 0;
396 long len;
397
398 struct sandbox_info_t sandbox_info;
399
400 char **sandbox_environ;
401 char **argv_bash = NULL;
402
403 char *run_str = "-c";
404
405 /* Only print info if called with no arguments .... */
406 if (argc < 2)
407 print_debug = 1;
408
409 if (print_debug)
410 printf("========================== Gentoo linux path sandbox ===========================\n");
411
412 /* check if a sandbox is already running */
413 if (NULL != getenv(ENV_SANDBOX_ON)) {
414 fprintf(stderr, "Not launching a new sandbox instance\n");
415 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
416 exit(EXIT_FAILURE);
417 }
418
419 /* determine the location of all the sandbox support files */
420 if (print_debug)
421 printf("Detection of the support files.\n");
422
423 if (-1 == sandbox_setup(&sandbox_info)) {
424 fprintf(stderr, "sandbox: Failed to setup sandbox.");
425 exit(EXIT_FAILURE);
426 }
427
428 /* verify the existance of required files */
429 if (print_debug)
430 printf("Verification of the required files.\n");
431
432 #ifndef SB_HAVE_64BIT_ARCH
433 if (0 >= exists(sandbox_info.sandbox_lib)) {
434 perror("sandbox: Could not open the sandbox library");
435 exit(EXIT_FAILURE);
436 }
437 #endif
438 if (0 >= exists(sandbox_info.sandbox_rc)) {
439 perror("sandbox: Could not open the sandbox rc file");
440 exit(EXIT_FAILURE);
441 }
442
443 /* set up the required environment variables */
444 if (print_debug)
445 printf("Setting up the required environment variables.\n");
446
447 /* This one should not be child only, as we check above to see
448 * if we are already running (check sandbox_setup_environ).
449 * This needs to be set before calling sandbox_setup_environ(),
450 * else its not set for the child */
451 setenv(ENV_SANDBOX_ON, "1", 0);
452
453 /* Setup the child environment stuff */
454 sandbox_environ = sandbox_setup_environ(&sandbox_info);
455 if (NULL == sandbox_environ) {
456 perror("sandbox: Out of memory (environ)");
457 exit(EXIT_FAILURE);
458 }
459
460 /* if the portage temp dir was present, cd into it */
461 if (NULL != sandbox_info.work_dir)
462 chdir(sandbox_info.work_dir);
463
464 argv_bash = (char **)malloc(6 * sizeof(char *));
465 argv_bash[0] = strdup("/bin/bash");
466 argv_bash[1] = strdup("-rcfile");
467 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
468
469 if (argc < 2)
470 argv_bash[3] = NULL;
471 else
472 argv_bash[3] = strdup(run_str); /* "-c" */
473
474 argv_bash[4] = NULL; /* strdup(run_arg); */
475 argv_bash[5] = NULL;
476
477 if (argc >= 2) {
478 for (i = 1; i < argc; i++) {
479 if (NULL == argv_bash[4])
480 len = 0;
481 else
482 len = strlen(argv_bash[4]);
483
484 argv_bash[4] = (char *)realloc(argv_bash[4],
485 (len + strlen(argv[i]) + 2) * sizeof(char));
486
487 if (0 == len)
488 argv_bash[4][0] = 0;
489 if (1 != i)
490 strcat(argv_bash[4], " ");
491
492 strcat(argv_bash[4], argv[i]);
493 }
494 }
495
496 /* set up the required signal handlers */
497 signal(SIGHUP, &stop);
498 signal(SIGINT, &stop);
499 signal(SIGQUIT, &stop);
500 signal(SIGTERM, &stop);
501
502 /* STARTING PROTECTED ENVIRONMENT */
503 if (print_debug) {
504 printf("The protected environment has been started.\n");
505 printf("--------------------------------------------------------------------------------\n");
506 }
507
508 if (print_debug)
509 printf("Process being started in forked instance.\n");
510
511 /* Start Bash */
512 if (!spawn_shell(argv_bash, sandbox_environ, print_debug))
513 success = 0;
514
515 /* Free bash stuff */
516 for (i = 0; i < 6; i++) {
517 if (argv_bash[i])
518 free(argv_bash[i]);
519 argv_bash[i] = NULL;
520 }
521 if (argv_bash)
522 free(argv_bash);
523 argv_bash = NULL;
524
525 if (print_debug)
526 printf("Cleaning up sandbox process\n");
527
528 if (print_debug) {
529 printf("========================== Gentoo linux path sandbox ===========================\n");
530 printf("The protected environment has been shut down.\n");
531 }
532
533 if (1 == exists(sandbox_info.sandbox_log)) {
534 sandbox_log_presence = 1;
535 print_sandbox_log(sandbox_info.sandbox_log);
536 } else if (print_debug) {
537 printf("--------------------------------------------------------------------------------\n");
538 }
539
540 if ((sandbox_log_presence) || (!success))
541 return 1;
542 else
543 return 0;
544 }
545
546 // 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