| … | |
… | |
| 31 | #include <sys/wait.h> |
31 | #include <sys/wait.h> |
| 32 | #include <unistd.h> |
32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
33 | #include <fcntl.h> |
| 34 | #include "sandbox.h" |
34 | #include "sandbox.h" |
| 35 | |
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 *sandbox_pids_file; |
|
|
43 | char portage_tmp_dir[SB_PATH_MAX]; |
|
|
44 | char var_tmp_dir[SB_PATH_MAX]; |
| 36 | static char tmp_dir[SB_PATH_MAX]; |
45 | char tmp_dir[SB_PATH_MAX]; |
|
|
46 | char *home_dir; |
|
|
47 | } sandbox_info_t; |
|
|
48 | |
|
|
49 | static char *tmp_dir; |
| 37 | |
50 | |
| 38 | static int cleaned_up = 0; |
51 | static int cleaned_up = 0; |
| 39 | static int print_debug = 0; |
52 | static int print_debug = 0; |
| 40 | static int stop_called = 0; |
53 | static int stop_called = 0; |
| 41 | |
54 | |
| … | |
… | |
| 92 | data = NULL; |
105 | data = NULL; |
| 93 | |
106 | |
| 94 | return num_pids; |
107 | return num_pids; |
| 95 | } |
108 | } |
| 96 | |
109 | |
|
|
110 | int write_pids_file(struct sandbox_info_t *sandbox_info) |
|
|
111 | { |
|
|
112 | int pids_file = -1; |
|
|
113 | int *pids_array = NULL; |
|
|
114 | int num_of_pids = 0; |
|
|
115 | int i = 0, success = 1; |
|
|
116 | |
|
|
117 | char pid_string[SB_BUF_LEN]; |
|
|
118 | |
|
|
119 | if (file_exist(sandbox_info->sandbox_pids_file, 1) < 0) { |
|
|
120 | fprintf(stderr, ">>> %s is not a regular file\n", |
|
|
121 | sandbox_info->sandbox_pids_file); |
|
|
122 | return -1; |
|
|
123 | } else { |
|
|
124 | pids_file = file_open(sandbox_info->sandbox_pids_file, |
|
|
125 | "r+", 1, 0664, "portage"); |
|
|
126 | if (-1 == pids_file) |
|
|
127 | return -1; |
|
|
128 | } |
|
|
129 | |
|
|
130 | /* Grab still active pids */ |
|
|
131 | num_of_pids = load_active_pids(pids_file, &pids_array); |
|
|
132 | |
|
|
133 | /* Zero out file */ |
|
|
134 | file_truncate(pids_file); |
|
|
135 | |
|
|
136 | /* Output active pids, and append our pid */ |
|
|
137 | for (i = 0; i < num_of_pids + 1; i++) { |
|
|
138 | /* Time for our entry */ |
|
|
139 | if (i == num_of_pids) |
|
|
140 | sprintf(pid_string, "%d\n", getpid()); |
|
|
141 | else |
|
|
142 | sprintf(pid_string, "%d\n", pids_array[i]); |
|
|
143 | |
|
|
144 | if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) { |
|
|
145 | success = 0; |
|
|
146 | break; |
|
|
147 | } |
|
|
148 | } |
|
|
149 | |
|
|
150 | /* Clean pids_array */ |
|
|
151 | if (pids_array) |
|
|
152 | free(pids_array); |
|
|
153 | |
|
|
154 | /* We're done with the pids file */ |
|
|
155 | file_close(pids_file); |
|
|
156 | |
|
|
157 | if (!success) |
|
|
158 | return -1; |
|
|
159 | |
|
|
160 | return 0; |
|
|
161 | } |
|
|
162 | |
|
|
163 | int sandbox_setup(char *argv[], struct sandbox_info_t *sandbox_info) |
|
|
164 | { |
|
|
165 | if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR) |
|
|
166 | : PORTAGE_TMPDIR, |
|
|
167 | sandbox_info->portage_tmp_dir)) { |
|
|
168 | perror(">>> get portage_tmp_dir"); |
|
|
169 | return -1; |
|
|
170 | } |
|
|
171 | |
|
|
172 | if (NULL == realpath(VAR_TMPDIR, sandbox_info->var_tmp_dir)) { |
|
|
173 | perror(">>> get var_tmp_dir"); |
|
|
174 | return -1; |
|
|
175 | } |
|
|
176 | |
|
|
177 | if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) { |
|
|
178 | perror(">>> get tmp_dir"); |
|
|
179 | return -1; |
|
|
180 | } |
|
|
181 | tmp_dir = sandbox_info->tmp_dir; |
|
|
182 | |
|
|
183 | sandbox_info->home_dir = getenv("HOME"); |
|
|
184 | if (!sandbox_info->home_dir) { |
|
|
185 | sandbox_info->home_dir = tmp_dir; |
|
|
186 | setenv("HOME", sandbox_info->home_dir, 1); |
|
|
187 | } |
|
|
188 | |
|
|
189 | /* Generate base sandbox path */ |
|
|
190 | snprintf(sandbox_info->sandbox_dir, SB_PATH_MAX, "%s/", |
|
|
191 | get_sandbox_path(argv[0])); |
|
|
192 | |
|
|
193 | /* Generate sandbox lib path */ |
|
|
194 | snprintf(sandbox_info->sandbox_lib, SB_PATH_MAX, "%s", |
|
|
195 | get_sandbox_lib(sandbox_info->sandbox_dir)); |
|
|
196 | |
|
|
197 | /* Generate sandbox pids-file path */ |
|
|
198 | sandbox_info->sandbox_pids_file = get_sandbox_pids_file(tmp_dir); |
|
|
199 | |
|
|
200 | /* Generate sandbox bashrc path */ |
|
|
201 | snprintf(sandbox_info->sandbox_rc, SB_PATH_MAX, "%s", |
|
|
202 | get_sandbox_rc(sandbox_info->sandbox_dir)); |
|
|
203 | |
|
|
204 | /* Generate sandbox log full path */ |
|
|
205 | snprintf(sandbox_info->sandbox_log, SB_PATH_MAX, "%s", |
|
|
206 | get_sandbox_log(tmp_dir)); |
|
|
207 | |
|
|
208 | /* Generate sandbox debug log full path */ |
|
|
209 | snprintf(sandbox_info->sandbox_debug_log, SB_PATH_MAX, "%s", |
|
|
210 | get_sandbox_debug_log(tmp_dir)); |
|
|
211 | |
|
|
212 | return 0; |
|
|
213 | } |
|
|
214 | |
| 97 | void cleanup() |
215 | void sandbox_cleanup() |
| 98 | { |
216 | { |
| 99 | int i = 0; |
217 | int i = 0; |
| 100 | int success = 1; |
218 | int success = 1; |
| 101 | int pids_file = -1, num_of_pids = 0; |
219 | int pids_file = -1, num_of_pids = 0; |
| 102 | int *pids_array = NULL; |
220 | int *pids_array = NULL; |
| … | |
… | |
| 167 | free(pids_array); |
285 | free(pids_array); |
| 168 | pids_array = NULL; |
286 | pids_array = NULL; |
| 169 | } |
287 | } |
| 170 | |
288 | |
| 171 | free(sandbox_pids_file); |
289 | free(sandbox_pids_file); |
| 172 | if (0 == success) |
|
|
| 173 | return; |
|
|
| 174 | } |
290 | } |
| 175 | |
291 | |
| 176 | int print_sandbox_log(char *sandbox_log) |
292 | int print_sandbox_log(char *sandbox_log) |
| 177 | { |
293 | { |
| 178 | int sandbox_log_file = -1; |
294 | int sandbox_log_file = -1; |
| … | |
… | |
| 227 | void stop(int signum) |
343 | void stop(int signum) |
| 228 | { |
344 | { |
| 229 | if (stop_called == 0) { |
345 | if (stop_called == 0) { |
| 230 | stop_called = 1; |
346 | stop_called = 1; |
| 231 | printf("Caught signal %d in pid %d\r\n", signum, getpid()); |
347 | printf("Caught signal %d in pid %d\r\n", signum, getpid()); |
| 232 | cleanup(); |
348 | sandbox_cleanup(); |
| 233 | } else { |
349 | } else { |
| 234 | fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid()); |
350 | fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid()); |
| 235 | } |
351 | } |
| 236 | } |
352 | } |
| 237 | |
353 | |
| 238 | void get_sandbox_write_envvar(char *buf, char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir) |
354 | void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info) |
| 239 | { |
355 | { |
| 240 | /* bzero out entire buffer then append trailing 0 */ |
356 | /* bzero out entire buffer then append trailing 0 */ |
| 241 | memset(buf, 0, SB_BUF_LEN); |
357 | memset(buf, 0, SB_BUF_LEN); |
| 242 | |
358 | |
| 243 | /* these could go into make.globals later on */ |
359 | /* these could go into make.globals later on */ |
| … | |
… | |
| 247 | "/dev/vc/:/dev/pty:/dev/tty:" |
363 | "/dev/vc/:/dev/pty:/dev/tty:" |
| 248 | "/dev/shm/ngpt:/var/log/scrollkeeper.log:" |
364 | "/dev/shm/ngpt:/var/log/scrollkeeper.log:" |
| 249 | "/usr/tmp/conftest:/usr/lib/conftest:" |
365 | "/usr/tmp/conftest:/usr/lib/conftest:" |
| 250 | "/usr/lib32/conftest:/usr/lib64/conftest:" |
366 | "/usr/lib32/conftest:/usr/lib64/conftest:" |
| 251 | "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf", |
367 | "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf", |
| 252 | home_dir, home_dir, |
368 | sandbox_info->home_dir, sandbox_info->home_dir, |
| 253 | (NULL != portage_tmp_dir) ? portage_tmp_dir : tmp_dir, |
369 | (NULL != sandbox_info->portage_tmp_dir) ? sandbox_info->portage_tmp_dir : tmp_dir, |
| 254 | tmp_dir, var_tmp_dir, "/tmp/:/var/tmp/"); |
370 | sandbox_info->tmp_dir, sandbox_info->var_tmp_dir, |
|
|
371 | "/tmp/:/var/tmp/"); |
| 255 | } |
372 | } |
| 256 | |
373 | |
| 257 | void get_sandbox_predict_envvar(char *buf, char *home_dir) |
374 | void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info) |
| 258 | { |
375 | { |
| 259 | /* bzero out entire buffer then append trailing 0 */ |
376 | /* bzero out entire buffer then append trailing 0 */ |
| 260 | memset(buf, 0, SB_BUF_LEN); |
377 | memset(buf, 0, SB_BUF_LEN); |
| 261 | |
378 | |
| 262 | /* these should go into make.globals later on */ |
379 | /* these should go into make.globals later on */ |
| … | |
… | |
| 266 | "/usr/lib/python2.2/:" |
383 | "/usr/lib/python2.2/:" |
| 267 | "/usr/lib/python2.3/:" |
384 | "/usr/lib/python2.3/:" |
| 268 | "/usr/lib/python2.4/:" |
385 | "/usr/lib/python2.4/:" |
| 269 | "/usr/lib/python2.5/:" |
386 | "/usr/lib/python2.5/:" |
| 270 | "/usr/lib/python3.0/:", |
387 | "/usr/lib/python3.0/:", |
| 271 | home_dir); |
388 | sandbox_info->home_dir); |
| 272 | } |
389 | } |
| 273 | |
390 | |
| 274 | int sandbox_setenv(char **env, char *name, char *val) { |
391 | int sandbox_setenv(char **env, char *name, char *val) { |
| 275 | char **tmp_env = env; |
392 | char **tmp_env = env; |
| 276 | char *tmp_string = NULL; |
393 | char *tmp_string = NULL; |
| … | |
… | |
| 296 | return 0; |
413 | return 0; |
| 297 | } |
414 | } |
| 298 | |
415 | |
| 299 | /* We setup the environment child side only to prevent issues with |
416 | /* We setup the environment child side only to prevent issues with |
| 300 | * setting LD_PRELOAD parent side */ |
417 | * setting LD_PRELOAD parent side */ |
| 301 | char **sandbox_setup_environ(char *sandbox_dir, char *sandbox_lib, char *sandbox_rc, char *sandbox_log, |
418 | char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info, |
| 302 | char *sandbox_debug_log, char *sandbox_write_envvar, char *sandbox_predict_envvar) |
419 | char *sandbox_write_envvar, char *sandbox_predict_envvar) |
| 303 | { |
420 | { |
| 304 | int env_size = 0; |
421 | int env_size = 0; |
| 305 | int have_ld_preload = 0; |
422 | int have_ld_preload = 0; |
| 306 | |
423 | |
| 307 | char **new_environ; |
424 | char **new_environ; |
| … | |
… | |
| 322 | orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD); |
439 | orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD); |
| 323 | |
440 | |
| 324 | /* FIXME: Should probably free this at some stage - more neatness |
441 | /* FIXME: Should probably free this at some stage - more neatness |
| 325 | * than a real leak that will cause issues. */ |
442 | * than a real leak that will cause issues. */ |
| 326 | ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) + |
443 | ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) + |
| 327 | strlen(sandbox_lib) + 2, sizeof(char *)); |
444 | strlen(sandbox_info->sandbox_lib) + 2, |
|
|
445 | sizeof(char *)); |
| 328 | if (NULL == ld_preload_envvar) |
446 | if (NULL == ld_preload_envvar) |
| 329 | return NULL; |
447 | return NULL; |
| 330 | snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) + |
448 | snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) + |
| 331 | strlen(sandbox_lib) + 2, "%s %s", |
449 | strlen(sandbox_info->sandbox_lib) + 2, "%s %s", |
| 332 | sandbox_lib, orig_ld_preload_envvar); |
450 | sandbox_info->sandbox_lib, orig_ld_preload_envvar); |
| 333 | } else { |
451 | } else { |
| 334 | /* FIXME: Should probably free this at some stage - more neatness |
452 | /* FIXME: Should probably free this at some stage - more neatness |
| 335 | * than a real leak that will cause issues. */ |
453 | * than a real leak that will cause issues. */ |
| 336 | ld_preload_envvar = strndup(sandbox_lib, strlen(sandbox_lib)); |
454 | ld_preload_envvar = strndup(sandbox_info->sandbox_lib, |
|
|
455 | strlen(sandbox_info->sandbox_lib)); |
| 337 | if (NULL == ld_preload_envvar) |
456 | if (NULL == ld_preload_envvar) |
| 338 | return NULL; |
457 | return NULL; |
| 339 | } |
458 | } |
| 340 | /* Do not unset this, as strange things might happen */ |
459 | /* Do not unset this, as strange things might happen */ |
| 341 | /* unsetenv(ENV_LD_PRELOAD); */ |
460 | /* unsetenv(ENV_LD_PRELOAD); */ |
| … | |
… | |
| 351 | if (NULL == new_environ) |
470 | if (NULL == new_environ) |
| 352 | return NULL; |
471 | return NULL; |
| 353 | |
472 | |
| 354 | /* First add our new variables to the beginning - this is due to some |
473 | /* First add our new variables to the beginning - this is due to some |
| 355 | * weirdness that I cannot remember */ |
474 | * weirdness that I cannot remember */ |
| 356 | sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_dir); |
475 | sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_info->sandbox_dir); |
| 357 | sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_lib); |
476 | sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib); |
| 358 | sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_rc); |
477 | sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc); |
| 359 | sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_log); |
478 | sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log); |
| 360 | sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log); |
479 | sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG, |
|
|
480 | sandbox_info->sandbox_debug_log); |
| 361 | /* If LD_PRELOAD was not set, set it here, else do it below */ |
481 | /* If LD_PRELOAD was not set, set it here, else do it below */ |
| 362 | if (1 != have_ld_preload) |
482 | if (1 != have_ld_preload) |
| 363 | sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar); |
483 | sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar); |
| 364 | |
484 | |
| 365 | if (!getenv(ENV_SANDBOX_DENY)) |
485 | if (!getenv(ENV_SANDBOX_DENY)) |
| … | |
… | |
| 392 | while (NULL != *env_ptr) { |
512 | while (NULL != *env_ptr) { |
| 393 | if ((1 == have_ld_preload) && |
513 | if ((1 == have_ld_preload) && |
| 394 | (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr)) |
514 | (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr)) |
| 395 | /* If LD_PRELOAD was set, and this is it in the original |
515 | /* If LD_PRELOAD was set, and this is it in the original |
| 396 | * environment, replace it with our new copy */ |
516 | * environment, replace it with our new copy */ |
| 397 | /* XXX: The following works as it just add whatever as |
517 | /* XXX: The following works as it just add whatever as |
| 398 | * the last variable to nev_environ */ |
518 | * the last variable to nev_environ */ |
| 399 | sandbox_setenv(new_environ, ENV_LD_PRELOAD, |
519 | sandbox_setenv(new_environ, ENV_LD_PRELOAD, |
| 400 | ld_preload_envvar); |
520 | ld_preload_envvar); |
| 401 | else |
521 | else |
| 402 | new_environ[env_size + (env_ptr - environ)] = *env_ptr; |
522 | new_environ[env_size + (env_ptr - environ)] = *env_ptr; |
| 403 | env_ptr++; |
523 | env_ptr++; |
| … | |
… | |
| 428 | return 1; |
548 | return 1; |
| 429 | } |
549 | } |
| 430 | |
550 | |
| 431 | int main(int argc, char **argv) |
551 | int main(int argc, char **argv) |
| 432 | { |
552 | { |
| 433 | int ret = 0, i = 0, success = 1; |
553 | int i = 0, success = 1; |
| 434 | int sandbox_log_presence = 0; |
554 | int sandbox_log_presence = 0; |
| 435 | int pids_file = -1; |
|
|
| 436 | long len; |
555 | long len; |
| 437 | |
556 | |
| 438 | int *pids_array = NULL; |
557 | struct sandbox_info_t sandbox_info; |
| 439 | int num_of_pids = 0; |
|
|
| 440 | |
558 | |
| 441 | // char run_arg[255]; |
|
|
| 442 | char **sandbox_environ; |
559 | char **sandbox_environ; |
| 443 | char sandbox_log[SB_PATH_MAX]; |
|
|
| 444 | char sandbox_debug_log[SB_PATH_MAX]; |
|
|
| 445 | char sandbox_dir[SB_PATH_MAX]; |
|
|
| 446 | char sandbox_lib[SB_PATH_MAX]; |
|
|
| 447 | char sandbox_rc[SB_PATH_MAX]; |
|
|
| 448 | char *sandbox_pids_file; |
|
|
| 449 | char portage_tmp_dir[SB_PATH_MAX]; |
|
|
| 450 | char var_tmp_dir[SB_PATH_MAX]; |
|
|
| 451 | char sandbox_write_envvar[SB_BUF_LEN]; |
560 | char sandbox_write_envvar[SB_BUF_LEN]; |
| 452 | char sandbox_predict_envvar[SB_BUF_LEN]; |
561 | char sandbox_predict_envvar[SB_BUF_LEN]; |
| 453 | char pid_string[SB_BUF_LEN]; |
|
|
| 454 | char **argv_bash = NULL; |
562 | char **argv_bash = NULL; |
| 455 | |
563 | |
| 456 | char *run_str = "-c"; |
564 | char *run_str = "-c"; |
| 457 | char *home_dir = NULL; |
565 | char *home_dir = NULL; |
| 458 | char *tmp_string = NULL; |
566 | char *tmp_string = NULL; |
| … | |
… | |
| 473 | |
581 | |
| 474 | /* determine the location of all the sandbox support files */ |
582 | /* determine the location of all the sandbox support files */ |
| 475 | if (print_debug) |
583 | if (print_debug) |
| 476 | printf("Detection of the support files.\n"); |
584 | printf("Detection of the support files.\n"); |
| 477 | |
585 | |
| 478 | if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR) |
586 | if (-1 == sandbox_setup(argv, &sandbox_info)) { |
| 479 | : PORTAGE_TMPDIR, |
587 | perror(">>> setup"); |
| 480 | portage_tmp_dir)) { |
|
|
| 481 | perror(">>> get portage_tmp_dir"); |
|
|
| 482 | exit(1); |
588 | exit(1); |
| 483 | } |
589 | } |
| 484 | if (NULL == realpath(VAR_TMPDIR, var_tmp_dir)) { |
|
|
| 485 | perror(">>> get var_tmp_dir"); |
|
|
| 486 | exit(1); |
|
|
| 487 | } |
590 | |
| 488 | if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR) |
|
|
| 489 | : TMPDIR, |
|
|
| 490 | tmp_dir)) { |
|
|
| 491 | perror(">>> get tmp_dir"); |
|
|
| 492 | exit(1); |
|
|
| 493 | } |
|
|
| 494 | |
|
|
| 495 | /* Generate base sandbox path */ |
|
|
| 496 | snprintf(sandbox_dir, SB_PATH_MAX, "%s/", |
|
|
| 497 | get_sandbox_path(argv[0])); |
|
|
| 498 | |
|
|
| 499 | /* Generate sandbox lib path */ |
|
|
| 500 | snprintf(sandbox_lib, SB_PATH_MAX, "%s", |
|
|
| 501 | get_sandbox_lib(sandbox_dir)); |
|
|
| 502 | |
|
|
| 503 | /* Generate sandbox pids-file path */ |
|
|
| 504 | sandbox_pids_file = get_sandbox_pids_file(tmp_dir); |
|
|
| 505 | |
|
|
| 506 | /* Generate sandbox bashrc path */ |
|
|
| 507 | snprintf(sandbox_rc, SB_PATH_MAX, "%s", |
|
|
| 508 | get_sandbox_rc(sandbox_dir)); |
|
|
| 509 | |
|
|
| 510 | /* verify the existance of required files */ |
591 | /* verify the existance of required files */ |
| 511 | if (print_debug) |
592 | if (print_debug) |
| 512 | printf("Verification of the required files.\n"); |
593 | printf("Verification of the required files.\n"); |
| 513 | |
594 | |
| 514 | #ifndef SB_HAVE_64BIT_ARCH |
595 | #ifndef SB_HAVE_64BIT_ARCH |
| 515 | if (file_exist(sandbox_lib, 0) <= 0) { |
596 | if (file_exist(sandbox_info.sandbox_lib, 0) <= 0) { |
| 516 | fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib); |
597 | fprintf(stderr, "Could not open the sandbox library at '%s'.\n", |
|
|
598 | sandbox_info.sandbox_lib); |
| 517 | return -1; |
599 | return -1; |
| 518 | } |
600 | } |
| 519 | #endif |
601 | #endif |
| 520 | if (file_exist(sandbox_rc, 0) <= 0) { |
602 | if (file_exist(sandbox_info.sandbox_rc, 0) <= 0) { |
| 521 | fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc); |
603 | fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", |
|
|
604 | sandbox_info.sandbox_rc); |
| 522 | return -1; |
605 | return -1; |
| 523 | } |
606 | } |
| 524 | |
607 | |
| 525 | /* set up the required environment variables */ |
608 | /* set up the required environment variables */ |
| 526 | if (print_debug) |
609 | if (print_debug) |
| 527 | printf("Setting up the required environment variables.\n"); |
610 | printf("Setting up the required environment variables.\n"); |
| 528 | |
|
|
| 529 | home_dir = getenv("HOME"); |
|
|
| 530 | if (!home_dir) { |
|
|
| 531 | home_dir = "/tmp"; |
|
|
| 532 | setenv("HOME", home_dir, 1); |
|
|
| 533 | } |
|
|
| 534 | |
|
|
| 535 | /* Generate sandbox log full path */ |
|
|
| 536 | snprintf(sandbox_log, SB_PATH_MAX, "%s", |
|
|
| 537 | get_sandbox_log(tmp_dir)); |
|
|
| 538 | |
|
|
| 539 | /* Generate sandbox debug log full path */ |
|
|
| 540 | snprintf(sandbox_debug_log, SB_PATH_MAX, "%s", |
|
|
| 541 | get_sandbox_debug_log(tmp_dir)); |
|
|
| 542 | |
611 | |
| 543 | /* This one should not be child only, as we check above to see |
612 | /* This one should not be child only, as we check above to see |
| 544 | * if we are already running (check sandbox_setup_environ). |
613 | * if we are already running (check sandbox_setup_environ). |
| 545 | * This needs to be set before calling sandbox_setup_environ(), |
614 | * This needs to be set before calling sandbox_setup_environ(), |
| 546 | * else its not set for the child */ |
615 | * else its not set for the child */ |
| 547 | setenv(ENV_SANDBOX_ON, "1", 0); |
616 | setenv(ENV_SANDBOX_ON, "1", 0); |
| 548 | |
617 | |
| 549 | /* Setup the child environment stuff */ |
618 | /* Setup the child environment stuff */ |
| 550 | get_sandbox_write_envvar(sandbox_write_envvar, home_dir, |
619 | get_sandbox_write_envvar(sandbox_write_envvar, &sandbox_info); |
| 551 | portage_tmp_dir, var_tmp_dir, tmp_dir); |
|
|
| 552 | get_sandbox_predict_envvar(sandbox_predict_envvar, home_dir); |
620 | get_sandbox_predict_envvar(sandbox_predict_envvar, &sandbox_info); |
| 553 | sandbox_environ = sandbox_setup_environ(sandbox_dir, sandbox_lib, |
621 | sandbox_environ = sandbox_setup_environ(&sandbox_info, |
| 554 | sandbox_rc, sandbox_log, sandbox_debug_log, |
|
|
| 555 | sandbox_write_envvar, sandbox_predict_envvar); |
622 | sandbox_write_envvar, sandbox_predict_envvar); |
| 556 | if (NULL == sandbox_environ) { |
623 | if (NULL == sandbox_environ) { |
| 557 | perror(">>> out of memory (environ)"); |
624 | perror(">>> out of memory (environ)"); |
| 558 | exit(1); |
625 | exit(1); |
| 559 | } |
626 | } |
| 560 | |
627 | |
| 561 | /* if the portage temp dir was present, cd into it */ |
628 | /* if the portage temp dir was present, cd into it */ |
| 562 | if (NULL != portage_tmp_dir) |
629 | if (NULL != sandbox_info.portage_tmp_dir) |
| 563 | chdir(portage_tmp_dir); |
630 | chdir(sandbox_info.portage_tmp_dir); |
| 564 | |
631 | |
| 565 | argv_bash = (char **)malloc(6 * sizeof(char *)); |
632 | argv_bash = (char **)malloc(6 * sizeof(char *)); |
| 566 | argv_bash[0] = strdup("/bin/bash"); |
633 | argv_bash[0] = strdup("/bin/bash"); |
| 567 | argv_bash[1] = strdup("-rcfile"); |
634 | argv_bash[1] = strdup("-rcfile"); |
| 568 | argv_bash[2] = strdup(sandbox_rc); |
635 | argv_bash[2] = strdup(sandbox_info.sandbox_rc); |
| 569 | |
636 | |
| 570 | if (argc < 2) |
637 | if (argc < 2) |
| 571 | argv_bash[3] = NULL; |
638 | argv_bash[3] = NULL; |
| 572 | else |
639 | else |
| 573 | argv_bash[3] = strdup(run_str); /* "-c" */ |
640 | argv_bash[3] = strdup(run_str); /* "-c" */ |
| … | |
… | |
| 599 | signal(SIGINT, &stop); |
666 | signal(SIGINT, &stop); |
| 600 | signal(SIGQUIT, &stop); |
667 | signal(SIGQUIT, &stop); |
| 601 | signal(SIGTERM, &stop); |
668 | signal(SIGTERM, &stop); |
| 602 | |
669 | |
| 603 | /* Load our PID into PIDs file */ |
670 | /* Load our PID into PIDs file */ |
| 604 | success = 1; |
671 | if (-1 == write_pids_file(&sandbox_info)) { |
| 605 | if (file_exist(sandbox_pids_file, 1) < 0) { |
|
|
| 606 | success = 0; |
|
|
| 607 | fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file); |
|
|
| 608 | } else { |
|
|
| 609 | pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage"); |
|
|
| 610 | if (-1 == pids_file) |
|
|
| 611 | success = 0; |
|
|
| 612 | } |
|
|
| 613 | if (1 == success) { |
|
|
| 614 | /* Grab still active pids */ |
|
|
| 615 | num_of_pids = load_active_pids(pids_file, &pids_array); |
|
|
| 616 | |
|
|
| 617 | /* Zero out file */ |
|
|
| 618 | file_truncate(pids_file); |
|
|
| 619 | |
|
|
| 620 | /* Output active pids, and append our pid */ |
|
|
| 621 | for (i = 0; i < num_of_pids + 1; i++) { |
|
|
| 622 | /* Time for our entry */ |
|
|
| 623 | if (i == num_of_pids) |
|
|
| 624 | sprintf(pid_string, "%d\n", getpid()); |
|
|
| 625 | else |
|
|
| 626 | sprintf(pid_string, "%d\n", pids_array[i]); |
|
|
| 627 | |
|
|
| 628 | if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) { |
|
|
| 629 | perror(">>> pids file write"); |
|
|
| 630 | success = 0; |
|
|
| 631 | break; |
|
|
| 632 | } |
|
|
| 633 | } |
|
|
| 634 | /* Clean pids_array */ |
|
|
| 635 | if (pids_array) |
|
|
| 636 | free(pids_array); |
|
|
| 637 | pids_array = NULL; |
|
|
| 638 | num_of_pids = 0; |
|
|
| 639 | |
|
|
| 640 | /* We're done with the pids file */ |
|
|
| 641 | file_close(pids_file); |
|
|
| 642 | } |
|
|
| 643 | |
|
|
| 644 | /* Something went wrong, bail out */ |
|
|
| 645 | if (0 == success) { |
|
|
| 646 | perror(">>> pids file write"); |
672 | perror(">>> pids file write"); |
| 647 | exit(1); |
673 | exit(1); |
| 648 | } |
674 | } |
| 649 | |
675 | |
| 650 | /* STARTING PROTECTED ENVIRONMENT */ |
676 | /* STARTING PROTECTED ENVIRONMENT */ |
| … | |
… | |
| 674 | argv_bash = NULL; |
700 | argv_bash = NULL; |
| 675 | |
701 | |
| 676 | if (print_debug) |
702 | if (print_debug) |
| 677 | printf("Cleaning up sandbox process\n"); |
703 | printf("Cleaning up sandbox process\n"); |
| 678 | |
704 | |
| 679 | cleanup(); |
705 | sandbox_cleanup(); |
| 680 | |
706 | |
| 681 | if (print_debug) { |
707 | if (print_debug) { |
| 682 | printf("========================== Gentoo linux path sandbox ===========================\n"); |
708 | printf("========================== Gentoo linux path sandbox ===========================\n"); |
| 683 | printf("The protected environment has been shut down.\n"); |
709 | printf("The protected environment has been shut down.\n"); |
| 684 | } |
710 | } |
| 685 | |
711 | |
| 686 | if (file_exist(sandbox_log, 0)) { |
712 | if (file_exist(sandbox_info.sandbox_log, 0)) { |
| 687 | sandbox_log_presence = 1; |
713 | sandbox_log_presence = 1; |
| 688 | success = 1; |
714 | success = 1; |
| 689 | if (!print_sandbox_log(sandbox_log)) |
715 | if (!print_sandbox_log(sandbox_info.sandbox_log)) |
| 690 | success = 0; |
716 | success = 0; |
| 691 | |
|
|
| 692 | #if 0 |
|
|
| 693 | if (!success) |
|
|
| 694 | exit(1); |
|
|
| 695 | #endif |
|
|
| 696 | |
|
|
| 697 | } else if (print_debug) { |
717 | } else if (print_debug) { |
| 698 | printf("--------------------------------------------------------------------------------\n"); |
718 | printf("--------------------------------------------------------------------------------\n"); |
| 699 | } |
719 | } |
| 700 | |
720 | |
| 701 | free(sandbox_pids_file); |
721 | free(sandbox_info.sandbox_pids_file); |
| 702 | |
722 | |
| 703 | if ((sandbox_log_presence) || (!success)) |
723 | if ((sandbox_log_presence) || (!success)) |
| 704 | return 1; |
724 | return 1; |
| 705 | else |
725 | else |
| 706 | return 0; |
726 | return 0; |