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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 144 - (show annotations) (download) (as text)
Thu Jul 7 22:58:16 2005 UTC (8 years, 9 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 14558 byte(s)
Cleanup access/log printing.  Make access printing honour NOCOLOR.  Fix log
printing's last line not honouring NOCOLOR.

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