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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (show annotations) (download) (as text)
Thu Jul 7 21:42:33 2005 UTC (9 years, 3 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 15253 byte(s)
Major cleanup of sandbox_futils.c.  Removed most of the functions as we now
write to /var/log/sandbox/, so in theory do not need all that.

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