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

Contents of /trunk/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


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