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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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