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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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