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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 133 - (show annotations) (download) (as text)
Wed Jul 6 00:36:46 2005 UTC (9 years, 2 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15165 byte(s)
Remove unused 'pids file' code.
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 <fcntl.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <sys/file.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/resource.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include "sandbox.h"
35
36 struct sandbox_info_t {
37 char sandbox_log[SB_PATH_MAX];
38 char sandbox_debug_log[SB_PATH_MAX];
39 char sandbox_dir[SB_PATH_MAX];
40 char sandbox_lib[SB_PATH_MAX];
41 char sandbox_rc[SB_PATH_MAX];
42 char portage_tmp_dir[SB_PATH_MAX];
43 char var_tmp_dir[SB_PATH_MAX];
44 char tmp_dir[SB_PATH_MAX];
45 char *home_dir;
46 } sandbox_info_t;
47
48 static char *tmp_dir;
49
50 static int print_debug = 0;
51 static int stop_called = 0;
52
53 int sandbox_setup(char *argv[], struct sandbox_info_t *sandbox_info)
54 {
55 if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR)
56 : PORTAGE_TMPDIR,
57 sandbox_info->portage_tmp_dir)) {
58 perror(">>> get portage_tmp_dir");
59 return -1;
60 }
61 setenv(ENV_PORTAGE_TMPDIR, sandbox_info->portage_tmp_dir, 1);
62
63 if (NULL == realpath(VAR_TMPDIR, sandbox_info->var_tmp_dir)) {
64 perror(">>> get var_tmp_dir");
65 return -1;
66 }
67
68 if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
69 perror(">>> get tmp_dir");
70 return -1;
71 }
72 tmp_dir = sandbox_info->tmp_dir;
73 setenv(ENV_TMPDIR, tmp_dir, 1);
74
75 sandbox_info->home_dir = getenv("HOME");
76 if (!sandbox_info->home_dir) {
77 sandbox_info->home_dir = tmp_dir;
78 setenv("HOME", sandbox_info->home_dir, 1);
79 }
80
81 /* Generate base sandbox path */
82 snprintf(sandbox_info->sandbox_dir, SB_PATH_MAX, "%s/",
83 get_sandbox_path(argv[0]));
84
85 /* Generate sandbox lib path */
86 snprintf(sandbox_info->sandbox_lib, SB_PATH_MAX, "%s",
87 get_sandbox_lib(sandbox_info->sandbox_dir));
88
89 /* Generate sandbox bashrc path */
90 snprintf(sandbox_info->sandbox_rc, SB_PATH_MAX, "%s",
91 get_sandbox_rc(sandbox_info->sandbox_dir));
92
93 /* Generate sandbox log full path */
94 snprintf(sandbox_info->sandbox_log, SB_PATH_MAX, "%s",
95 get_sandbox_log(tmp_dir));
96
97 /* Generate sandbox debug log full path */
98 snprintf(sandbox_info->sandbox_debug_log, SB_PATH_MAX, "%s",
99 get_sandbox_debug_log(tmp_dir));
100
101 return 0;
102 }
103
104 int print_sandbox_log(char *sandbox_log)
105 {
106 int sandbox_log_file = -1;
107 char *beep_count_env = NULL;
108 int i, color, beep_count = 0;
109 long len = 0;
110 char *buffer = NULL;
111
112 sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
113 if (-1 == sandbox_log_file)
114 return 0;
115
116 len = file_length(sandbox_log_file);
117 buffer = (char *)malloc((len + 1) * sizeof(char));
118 memset(buffer, 0, len + 1);
119 read(sandbox_log_file, buffer, len);
120 file_close(sandbox_log_file);
121
122 color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
123
124 if (color)
125 printf("\e[31;01m");
126 printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
127 if (color)
128 printf("\033[0m");
129 if (color)
130 printf("\e[31;01m");
131 printf("\nLOG FILE = \"%s\"", sandbox_log);
132 if (color)
133 printf("\033[0m");
134 printf("\n\n");
135 printf("%s", buffer);
136 if (buffer)
137 free(buffer);
138 buffer = NULL;
139 printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\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 return 1;
153 }
154
155 void stop(int signum)
156 {
157 if (stop_called == 0) {
158 stop_called = 1;
159 printf("Caught signal %d in pid %d\r\n", signum, getpid());
160 } else {
161 fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
162 }
163 }
164
165 void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
166 {
167 /* bzero out entire buffer then append trailing 0 */
168 memset(buf, 0, SB_BUF_LEN);
169
170 /* these could go into make.globals later on */
171 snprintf(buf, SB_BUF_LEN,
172 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
173 "/dev/zero:/dev/null:/dev/fd:/proc/self/fd:/dev/pts/:"
174 "/dev/vc/:/dev/pty:/dev/tty:/dev/console:"
175 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
176 "/usr/tmp/conftest:/usr/lib/conftest:"
177 "/usr/lib32/conftest:/usr/lib64/conftest:"
178 "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
179 sandbox_info->home_dir, sandbox_info->home_dir,
180 (NULL != sandbox_info->portage_tmp_dir) ? sandbox_info->portage_tmp_dir : tmp_dir,
181 sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
182 "/tmp/:/var/tmp/");
183 }
184
185 void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
186 {
187 /* bzero out entire buffer then append trailing 0 */
188 memset(buf, 0, SB_BUF_LEN);
189
190 /* these should go into make.globals later on */
191 snprintf(buf, SB_BUF_LEN, "%s/.:"
192 "/usr/lib/python2.0/:"
193 "/usr/lib/python2.1/:"
194 "/usr/lib/python2.2/:"
195 "/usr/lib/python2.3/:"
196 "/usr/lib/python2.4/:"
197 "/usr/lib/python2.5/:"
198 "/usr/lib/python3.0/:"
199 "/var/db/aliases.db:"
200 "/var/db/netgroup.db:"
201 "/var/db/netmasks.db:"
202 "/var/db/ethers.db:"
203 "/var/db/rpc.db:"
204 "/var/db/protocols.db:"
205 "/var/db/services.db:"
206 "/var/db/networks.db:"
207 "/var/db/hosts.db:"
208 "/var/db/group.db:"
209 "/var/db/passwd.db",
210 sandbox_info->home_dir);
211 }
212
213 int sandbox_setenv(char **env, const char *name, const char *val) {
214 char **tmp_env = env;
215 char *tmp_string = NULL;
216
217 /* XXX: We add the new variable to the end (no replacing). If this
218 * is changed, we need to fix sandbox_setup_environ() below */
219 while (NULL != *tmp_env)
220 tmp_env++;
221
222 /* strlen(name) + strlen(val) + '=' + '\0' */
223 /* FIXME: Should probably free this at some stage - more neatness than
224 * a real leak that will cause issues. */
225 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
226 if (NULL == tmp_string) {
227 perror(">>> out of memory (sandbox_setenv)");
228 exit(1);
229 }
230
231 snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
232 name, val);
233 *tmp_env = tmp_string;
234
235 return 0;
236 }
237
238 /* We setup the environment child side only to prevent issues with
239 * setting LD_PRELOAD parent side */
240 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info)
241 {
242 int env_size = 0;
243 int have_ld_preload = 0;
244
245 char **new_environ;
246 char **env_ptr = environ;
247 char sandbox_write_envvar[SB_BUF_LEN];
248 char sandbox_predict_envvar[SB_BUF_LEN];
249 char *ld_preload_envvar = NULL;
250 char *orig_ld_preload_envvar = NULL;
251
252 /* Unset these, as its easier than replacing when setting up our
253 * new environment below */
254 unsetenv(ENV_SANDBOX_DIR);
255 unsetenv(ENV_SANDBOX_LIB);
256 unsetenv(ENV_SANDBOX_BASHRC);
257 unsetenv(ENV_SANDBOX_LOG);
258 unsetenv(ENV_SANDBOX_DEBUG_LOG);
259
260 if (NULL != getenv(ENV_LD_PRELOAD)) {
261 have_ld_preload = 1;
262 orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
263
264 /* FIXME: Should probably free this at some stage - more neatness
265 * than a real leak that will cause issues. */
266 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
267 strlen(sandbox_info->sandbox_lib) + 2,
268 sizeof(char *));
269 if (NULL == ld_preload_envvar)
270 return NULL;
271 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
272 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
273 sandbox_info->sandbox_lib, orig_ld_preload_envvar);
274 } else {
275 /* FIXME: Should probably free this at some stage - more neatness
276 * than a real leak that will cause issues. */
277 ld_preload_envvar = strndup(sandbox_info->sandbox_lib,
278 strlen(sandbox_info->sandbox_lib));
279 if (NULL == ld_preload_envvar)
280 return NULL;
281 }
282 /* Do not unset this, as strange things might happen */
283 /* unsetenv(ENV_LD_PRELOAD); */
284
285 while (NULL != *env_ptr) {
286 env_size++;
287 env_ptr++;
288 }
289
290 /* FIXME: Should probably free this at some stage - more neatness than
291 * a real leak that will cause issues. */
292 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
293 if (NULL == new_environ)
294 return NULL;
295
296 /* First add our new variables to the beginning - this is due to some
297 * weirdness that I cannot remember */
298 sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_info->sandbox_dir);
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[])
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 return 0;
368 }
369 ret = waitpid(pid, &status, 0);
370 if ((-1 == ret) || (status > 0))
371 return 0;
372
373 return 1;
374 }
375
376 int main(int argc, char **argv)
377 {
378 int i = 0, success = 1;
379 int sandbox_log_presence = 0;
380 long len;
381
382 struct sandbox_info_t sandbox_info;
383
384 char **sandbox_environ;
385 char **argv_bash = NULL;
386
387 char *run_str = "-c";
388
389 /* Only print info if called with no arguments .... */
390 if (argc < 2)
391 print_debug = 1;
392
393 if (print_debug)
394 printf("========================== Gentoo linux path sandbox ===========================\n");
395
396 /* check if a sandbox is already running */
397 if (NULL != getenv(ENV_SANDBOX_ON)) {
398 fprintf(stderr, "Not launching a new sandbox instance\n");
399 fprintf(stderr, "Another one is already running in this process hierarchy.\n");
400 exit(1);
401 } else {
402
403 /* determine the location of all the sandbox support files */
404 if (print_debug)
405 printf("Detection of the support files.\n");
406
407 if (-1 == sandbox_setup(argv, &sandbox_info)) {
408 perror(">>> setup");
409 exit(1);
410 }
411
412 /* verify the existance of required files */
413 if (print_debug)
414 printf("Verification of the required files.\n");
415
416 #ifndef SB_HAVE_64BIT_ARCH
417 if (file_exist(sandbox_info.sandbox_lib, 0) <= 0) {
418 fprintf(stderr, "Could not open the sandbox library at '%s'.\n",
419 sandbox_info.sandbox_lib);
420 return -1;
421 }
422 #endif
423 if (file_exist(sandbox_info.sandbox_rc, 0) <= 0) {
424 fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n",
425 sandbox_info.sandbox_rc);
426 return -1;
427 }
428
429 /* set up the required environment variables */
430 if (print_debug)
431 printf("Setting up the required environment variables.\n");
432
433 /* This one should not be child only, as we check above to see
434 * if we are already running (check sandbox_setup_environ).
435 * This needs to be set before calling sandbox_setup_environ(),
436 * else its not set for the child */
437 setenv(ENV_SANDBOX_ON, "1", 0);
438
439 /* Setup the child environment stuff */
440 sandbox_environ = sandbox_setup_environ(&sandbox_info);
441 if (NULL == sandbox_environ) {
442 perror(">>> out of memory (environ)");
443 exit(1);
444 }
445
446 /* if the portage temp dir was present, cd into it */
447 if (NULL != sandbox_info.portage_tmp_dir)
448 chdir(sandbox_info.portage_tmp_dir);
449
450 argv_bash = (char **)malloc(6 * sizeof(char *));
451 argv_bash[0] = strdup("/bin/bash");
452 argv_bash[1] = strdup("-rcfile");
453 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
454
455 if (argc < 2)
456 argv_bash[3] = NULL;
457 else
458 argv_bash[3] = strdup(run_str); /* "-c" */
459
460 argv_bash[4] = NULL; /* strdup(run_arg); */
461 argv_bash[5] = NULL;
462
463 if (argc >= 2) {
464 for (i = 1; i < argc; i++) {
465 if (NULL == argv_bash[4])
466 len = 0;
467 else
468 len = strlen(argv_bash[4]);
469
470 argv_bash[4] = (char *)realloc(argv_bash[4],
471 (len + strlen(argv[i]) + 2) * sizeof(char));
472
473 if (0 == len)
474 argv_bash[4][0] = 0;
475 if (1 != i)
476 strcat(argv_bash[4], " ");
477
478 strcat(argv_bash[4], argv[i]);
479 }
480 }
481
482 /* set up the required signal handlers */
483 signal(SIGHUP, &stop);
484 signal(SIGINT, &stop);
485 signal(SIGQUIT, &stop);
486 signal(SIGTERM, &stop);
487
488 /* STARTING PROTECTED ENVIRONMENT */
489 if (print_debug) {
490 printf("The protected environment has been started.\n");
491 printf("--------------------------------------------------------------------------------\n");
492 }
493
494 if (print_debug)
495 printf("Shell being started in forked process.\n");
496
497 /* Start Bash */
498 if (!spawn_shell(argv_bash, sandbox_environ)) {
499 if (print_debug)
500 fprintf(stderr, ">>> shell process failed to spawn\n");
501 success = 0;
502 }
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 (file_exist(sandbox_info.sandbox_log, 0)) {
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
536 // 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