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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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