| 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 |
int cleaned_up = 0;
|
| 37 |
int print_debug = 0;
|
| 38 |
int stop_called = 0;
|
| 39 |
|
| 40 |
/* Read pids file, and load active pids into an array. Return number of pids in array */
|
| 41 |
int load_active_pids(int fd, int **pids)
|
| 42 |
{
|
| 43 |
char *data = NULL;
|
| 44 |
char *ptr = NULL, *ptr2 = NULL;
|
| 45 |
int my_pid;
|
| 46 |
int num_pids = 0;
|
| 47 |
long len;
|
| 48 |
|
| 49 |
pids[0] = NULL;
|
| 50 |
|
| 51 |
len = file_length(fd);
|
| 52 |
|
| 53 |
/* Allocate and zero datablock to read pids file */
|
| 54 |
data = (char *)malloc((len + 1) * sizeof(char));
|
| 55 |
memset(data, 0, len + 1);
|
| 56 |
|
| 57 |
/* Start at beginning of file */
|
| 58 |
lseek(fd, 0L, SEEK_SET);
|
| 59 |
|
| 60 |
/* read entire file into a buffer */
|
| 61 |
read(fd, data, len);
|
| 62 |
|
| 63 |
ptr = data;
|
| 64 |
|
| 65 |
/* Loop and read all pids */
|
| 66 |
while (1) {
|
| 67 |
/* Find new line */
|
| 68 |
ptr2 = strchr(ptr, '\n');
|
| 69 |
if (ptr2 == NULL)
|
| 70 |
break; /* No more PIDs */
|
| 71 |
|
| 72 |
/* Clear the \n. And ptr should have a null-terminated decimal string */
|
| 73 |
ptr2[0] = 0;
|
| 74 |
|
| 75 |
my_pid = atoi(ptr);
|
| 76 |
|
| 77 |
/* If the PID is still alive, add it to our array */
|
| 78 |
if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
|
| 79 |
pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
|
| 80 |
pids[0][num_pids] = my_pid;
|
| 81 |
num_pids++;
|
| 82 |
}
|
| 83 |
|
| 84 |
/* Put ptr past the NULL we just wrote */
|
| 85 |
ptr = ptr2 + 1;
|
| 86 |
}
|
| 87 |
|
| 88 |
if (data)
|
| 89 |
free(data);
|
| 90 |
data = NULL;
|
| 91 |
|
| 92 |
return num_pids;
|
| 93 |
}
|
| 94 |
|
| 95 |
void cleanup()
|
| 96 |
{
|
| 97 |
int i = 0;
|
| 98 |
int success = 1;
|
| 99 |
int pids_file = -1, num_of_pids = 0;
|
| 100 |
int *pids_array = NULL;
|
| 101 |
char pid_string[SB_BUF_LEN];
|
| 102 |
char *sandbox_pids_file;
|
| 103 |
|
| 104 |
/* Generate sandbox pids-file path */
|
| 105 |
sandbox_pids_file = get_sandbox_pids_file();
|
| 106 |
|
| 107 |
/* Remove this sandbox's bash pid from the global pids
|
| 108 |
* file if we have not already done so */
|
| 109 |
if (0 == cleaned_up) {
|
| 110 |
cleaned_up = 1;
|
| 111 |
success = 1;
|
| 112 |
|
| 113 |
if (print_debug)
|
| 114 |
printf("Cleaning up pids file.\n");
|
| 115 |
|
| 116 |
/* Stat the PIDs file, make sure it exists and is a regular file */
|
| 117 |
if (file_exist(sandbox_pids_file, 1) <= 0) {
|
| 118 |
fprintf(stderr, ">>> pids file is not a regular file\n");
|
| 119 |
success = 0;
|
| 120 |
/* We should really not fail if the pidsfile is missing here, but
|
| 121 |
* rather just exit cleanly, as there is still some cleanup to do */
|
| 122 |
return;
|
| 123 |
}
|
| 124 |
|
| 125 |
pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
|
| 126 |
if (-1 == pids_file) {
|
| 127 |
success = 0;
|
| 128 |
/* Nothing more to do here */
|
| 129 |
return;
|
| 130 |
}
|
| 131 |
|
| 132 |
/* Load "still active" pids into an array */
|
| 133 |
num_of_pids = load_active_pids(pids_file, &pids_array);
|
| 134 |
//printf("pids: %d\r\n", num_of_pids);
|
| 135 |
|
| 136 |
|
| 137 |
file_truncate(pids_file);
|
| 138 |
|
| 139 |
/* if pids are still running, write only the running pids back to the file */
|
| 140 |
if (num_of_pids > 1) {
|
| 141 |
for (i = 0; i < num_of_pids; i++) {
|
| 142 |
if (pids_array[i] != getpid()) {
|
| 143 |
sprintf(pid_string, "%d\n", pids_array[i]);
|
| 144 |
|
| 145 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
|
| 146 |
perror(">>> pids file write");
|
| 147 |
success = 0;
|
| 148 |
break;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
file_close(pids_file);
|
| 154 |
pids_file = -1;
|
| 155 |
} else {
|
| 156 |
|
| 157 |
file_close(pids_file);
|
| 158 |
pids_file = -1;
|
| 159 |
|
| 160 |
/* remove the pidsfile, as this was the last sandbox */
|
| 161 |
unlink(sandbox_pids_file);
|
| 162 |
}
|
| 163 |
|
| 164 |
if (pids_array != NULL)
|
| 165 |
free(pids_array);
|
| 166 |
pids_array = NULL;
|
| 167 |
}
|
| 168 |
|
| 169 |
free(sandbox_pids_file);
|
| 170 |
if (0 == success)
|
| 171 |
return;
|
| 172 |
}
|
| 173 |
|
| 174 |
int print_sandbox_log(char *sandbox_log)
|
| 175 |
{
|
| 176 |
int sandbox_log_file = -1;
|
| 177 |
char *beep_count_env = NULL;
|
| 178 |
int i, color, beep_count = 0;
|
| 179 |
long len = 0;
|
| 180 |
char *buffer = NULL;
|
| 181 |
|
| 182 |
sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
|
| 183 |
if (-1 == sandbox_log_file)
|
| 184 |
return 0;
|
| 185 |
|
| 186 |
len = file_length(sandbox_log_file);
|
| 187 |
buffer = (char *)malloc((len + 1) * sizeof(char));
|
| 188 |
memset(buffer, 0, len + 1);
|
| 189 |
read(sandbox_log_file, buffer, len);
|
| 190 |
file_close(sandbox_log_file);
|
| 191 |
|
| 192 |
color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
|
| 193 |
|
| 194 |
if (color)
|
| 195 |
printf("\e[31;01m");
|
| 196 |
printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
|
| 197 |
if (color)
|
| 198 |
printf("\033[0m");
|
| 199 |
if (color)
|
| 200 |
printf("\e[31;01m");
|
| 201 |
printf("\nLOG FILE = \"%s\"", sandbox_log);
|
| 202 |
if (color)
|
| 203 |
printf("\033[0m");
|
| 204 |
printf("\n\n");
|
| 205 |
printf("%s", buffer);
|
| 206 |
if (buffer)
|
| 207 |
free(buffer);
|
| 208 |
buffer = NULL;
|
| 209 |
printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
|
| 210 |
|
| 211 |
beep_count_env = getenv(ENV_SANDBOX_BEEP);
|
| 212 |
if (beep_count_env)
|
| 213 |
beep_count = atoi(beep_count_env);
|
| 214 |
else
|
| 215 |
beep_count = DEFAULT_BEEP_COUNT;
|
| 216 |
|
| 217 |
for (i = 0; i < beep_count; i++) {
|
| 218 |
fputc('\a', stderr);
|
| 219 |
if (i < beep_count - 1)
|
| 220 |
sleep(1);
|
| 221 |
}
|
| 222 |
return 1;
|
| 223 |
}
|
| 224 |
|
| 225 |
void stop(int signum)
|
| 226 |
{
|
| 227 |
if (stop_called == 0) {
|
| 228 |
stop_called = 1;
|
| 229 |
printf("Caught signal %d in pid %d\r\n", signum, getpid());
|
| 230 |
cleanup();
|
| 231 |
} else {
|
| 232 |
fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
|
| 233 |
}
|
| 234 |
}
|
| 235 |
|
| 236 |
void get_sandbox_write_envvar(char *buf, char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
|
| 237 |
{
|
| 238 |
/* bzero out entire buffer then append trailing 0 */
|
| 239 |
memset(buf, 0, SB_BUF_LEN);
|
| 240 |
|
| 241 |
/* these could go into make.globals later on */
|
| 242 |
snprintf(buf, SB_BUF_LEN,
|
| 243 |
"%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
|
| 244 |
"/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
|
| 245 |
"/dev/vc/:/dev/pty:/dev/tty:"
|
| 246 |
"/dev/shm/ngpt:/var/log/scrollkeeper.log:"
|
| 247 |
"/usr/tmp/conftest:/usr/lib/conftest:"
|
| 248 |
"/usr/lib32/conftest:/usr/lib64/conftest:"
|
| 249 |
"/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
|
| 250 |
home_dir, home_dir,
|
| 251 |
(NULL != portage_tmp_dir) ? portage_tmp_dir : tmp_dir,
|
| 252 |
tmp_dir, var_tmp_dir, "/tmp/:/var/tmp/");
|
| 253 |
}
|
| 254 |
|
| 255 |
void get_sandbox_predict_envvar(char *buf, char *home_dir)
|
| 256 |
{
|
| 257 |
/* bzero out entire buffer then append trailing 0 */
|
| 258 |
memset(buf, 0, SB_BUF_LEN);
|
| 259 |
|
| 260 |
/* these should go into make.globals later on */
|
| 261 |
snprintf(buf, SB_BUF_LEN, "%s/.:"
|
| 262 |
"/usr/lib/python2.0/:"
|
| 263 |
"/usr/lib/python2.1/:"
|
| 264 |
"/usr/lib/python2.2/:"
|
| 265 |
"/usr/lib/python2.3/:"
|
| 266 |
"/usr/lib/python2.4/:"
|
| 267 |
"/usr/lib/python2.5/:"
|
| 268 |
"/usr/lib/python3.0/:",
|
| 269 |
home_dir);
|
| 270 |
}
|
| 271 |
|
| 272 |
int sandbox_setenv(char **env, char *name, char *val) {
|
| 273 |
char **tmp_env = env;
|
| 274 |
char *tmp_string = NULL;
|
| 275 |
|
| 276 |
while (NULL != *tmp_env)
|
| 277 |
tmp_env++;
|
| 278 |
|
| 279 |
/* strlen(name) + strlen(val) + '=' + '\0' */
|
| 280 |
tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
|
| 281 |
if (NULL == tmp_string) {
|
| 282 |
perror(">>> out of memory (sandbox_setenv)");
|
| 283 |
exit(1);
|
| 284 |
}
|
| 285 |
|
| 286 |
snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
|
| 287 |
name, val);
|
| 288 |
*tmp_env = tmp_string;
|
| 289 |
|
| 290 |
return 0;
|
| 291 |
}
|
| 292 |
|
| 293 |
/* We setup the environment child side only to prevent issues with
|
| 294 |
* setting LD_PRELOAD parent side */
|
| 295 |
char **sandbox_setup_environ(char *sandbox_dir, char *sandbox_lib, char *sandbox_rc, char *sandbox_log,
|
| 296 |
char *sandbox_debug_log, char *sandbox_write_envvar, char *sandbox_predict_envvar)
|
| 297 |
{
|
| 298 |
int env_size = 0;
|
| 299 |
|
| 300 |
char **new_environ;
|
| 301 |
char **env_ptr = environ;
|
| 302 |
char *ld_preload_envvar = NULL;
|
| 303 |
|
| 304 |
/* Unset these, as its easier than replacing when setting up our
|
| 305 |
* new environment below */
|
| 306 |
unsetenv(ENV_SANDBOX_DIR);
|
| 307 |
unsetenv(ENV_SANDBOX_LIB);
|
| 308 |
unsetenv(ENV_SANDBOX_BASHRC);
|
| 309 |
unsetenv(ENV_SANDBOX_LOG);
|
| 310 |
unsetenv(ENV_SANDBOX_DEBUG_LOG);
|
| 311 |
|
| 312 |
if (NULL != getenv("LD_PRELOAD")) {
|
| 313 |
ld_preload_envvar = malloc(strlen(getenv("LD_PRELOAD")) +
|
| 314 |
strlen(sandbox_lib) + 2);
|
| 315 |
if (NULL == ld_preload_envvar)
|
| 316 |
return NULL;
|
| 317 |
strncpy(ld_preload_envvar, sandbox_lib, strlen(sandbox_lib));
|
| 318 |
strncat(ld_preload_envvar, " ", 1);
|
| 319 |
strncat(ld_preload_envvar, getenv("LD_PRELOAD"),
|
| 320 |
strlen(getenv("LD_PRELOAD")));
|
| 321 |
} else {
|
| 322 |
ld_preload_envvar = strndup(sandbox_lib, strlen(sandbox_lib));
|
| 323 |
if (NULL == ld_preload_envvar)
|
| 324 |
return NULL;
|
| 325 |
}
|
| 326 |
unsetenv("LD_PRELOAD");
|
| 327 |
|
| 328 |
while (NULL != *env_ptr) {
|
| 329 |
env_size++;
|
| 330 |
env_ptr++;
|
| 331 |
}
|
| 332 |
|
| 333 |
new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
|
| 334 |
if (NULL == new_environ)
|
| 335 |
return NULL;
|
| 336 |
|
| 337 |
/* First add our new variables to the beginning - this is due to some
|
| 338 |
* weirdness that I cannot remember */
|
| 339 |
sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_dir);
|
| 340 |
sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_lib);
|
| 341 |
sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_rc);
|
| 342 |
sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_log);
|
| 343 |
sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log);
|
| 344 |
sandbox_setenv(new_environ, "LD_PRELOAD", ld_preload_envvar);
|
| 345 |
|
| 346 |
if (!getenv(ENV_SANDBOX_DENY))
|
| 347 |
sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
|
| 348 |
|
| 349 |
if (!getenv(ENV_SANDBOX_READ))
|
| 350 |
sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
|
| 351 |
|
| 352 |
if (!getenv(ENV_SANDBOX_WRITE))
|
| 353 |
sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
|
| 354 |
|
| 355 |
if (!getenv(ENV_SANDBOX_PREDICT))
|
| 356 |
sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
|
| 357 |
|
| 358 |
/* This one should NEVER be set in ebuilds, as it is the one
|
| 359 |
* private thing libsandbox.so use to test if the sandbox
|
| 360 |
* should be active for this pid, or not.
|
| 361 |
*
|
| 362 |
* azarah (3 Aug 2002)
|
| 363 |
*/
|
| 364 |
|
| 365 |
sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
|
| 366 |
|
| 367 |
env_size = 0;
|
| 368 |
while (NULL != new_environ[env_size])
|
| 369 |
env_size++;
|
| 370 |
|
| 371 |
/* Now add the rest */
|
| 372 |
env_ptr = environ;
|
| 373 |
while (NULL != *env_ptr) {
|
| 374 |
new_environ[env_size + (env_ptr - environ)] = *env_ptr;
|
| 375 |
env_ptr++;
|
| 376 |
}
|
| 377 |
|
| 378 |
return new_environ;
|
| 379 |
}
|
| 380 |
|
| 381 |
int spawn_shell(char *argv_bash[], char *env[])
|
| 382 |
{
|
| 383 |
int pid;
|
| 384 |
int status = 0;
|
| 385 |
int ret = 0;
|
| 386 |
|
| 387 |
pid = fork();
|
| 388 |
|
| 389 |
/* Child's process */
|
| 390 |
if (0 == pid) {
|
| 391 |
execve(argv_bash[0], argv_bash, env);
|
| 392 |
return 0;
|
| 393 |
} else if (pid < 0) {
|
| 394 |
return 0;
|
| 395 |
}
|
| 396 |
ret = waitpid(pid, &status, 0);
|
| 397 |
if ((-1 == ret) || (status > 0))
|
| 398 |
return 0;
|
| 399 |
|
| 400 |
return 1;
|
| 401 |
}
|
| 402 |
|
| 403 |
int main(int argc, char **argv)
|
| 404 |
{
|
| 405 |
int ret = 0, i = 0, success = 1;
|
| 406 |
int sandbox_log_presence = 0;
|
| 407 |
int sandbox_log_file = -1;
|
| 408 |
int pids_file = -1;
|
| 409 |
long len;
|
| 410 |
|
| 411 |
int *pids_array = NULL;
|
| 412 |
int num_of_pids = 0;
|
| 413 |
|
| 414 |
// char run_arg[255];
|
| 415 |
char **sandbox_environ;
|
| 416 |
char sandbox_log[SB_PATH_MAX];
|
| 417 |
char sandbox_debug_log[SB_PATH_MAX];
|
| 418 |
char sandbox_dir[SB_PATH_MAX];
|
| 419 |
char sandbox_lib[SB_PATH_MAX];
|
| 420 |
char sandbox_rc[SB_PATH_MAX];
|
| 421 |
char *sandbox_pids_file;
|
| 422 |
char portage_tmp_dir[SB_PATH_MAX];
|
| 423 |
char var_tmp_dir[SB_PATH_MAX];
|
| 424 |
char tmp_dir[SB_PATH_MAX];
|
| 425 |
char sandbox_write_envvar[SB_BUF_LEN];
|
| 426 |
char sandbox_predict_envvar[SB_BUF_LEN];
|
| 427 |
char pid_string[SB_BUF_LEN];
|
| 428 |
char **argv_bash = NULL;
|
| 429 |
|
| 430 |
char *run_str = "-c";
|
| 431 |
char *home_dir = NULL;
|
| 432 |
char *tmp_string = NULL;
|
| 433 |
|
| 434 |
/* Only print info if called with no arguments .... */
|
| 435 |
if (argc < 2)
|
| 436 |
print_debug = 1;
|
| 437 |
|
| 438 |
if (print_debug)
|
| 439 |
printf("========================== Gentoo linux path sandbox ===========================\n");
|
| 440 |
|
| 441 |
/* check if a sandbox is already running */
|
| 442 |
if (NULL != getenv(ENV_SANDBOX_ON)) {
|
| 443 |
fprintf(stderr, "Not launching a new sandbox instance\n");
|
| 444 |
fprintf(stderr, "Another one is already running in this process hierarchy.\n");
|
| 445 |
exit(1);
|
| 446 |
} else {
|
| 447 |
|
| 448 |
/* determine the location of all the sandbox support files */
|
| 449 |
if (print_debug)
|
| 450 |
printf("Detection of the support files.\n");
|
| 451 |
|
| 452 |
/* Generate base sandbox path */
|
| 453 |
tmp_string = get_sandbox_path(argv[0]);
|
| 454 |
strncpy(sandbox_dir, tmp_string, 254);
|
| 455 |
if (tmp_string)
|
| 456 |
free(tmp_string);
|
| 457 |
tmp_string = NULL;
|
| 458 |
strcat(sandbox_dir, "/");
|
| 459 |
|
| 460 |
/* Generate sandbox lib path */
|
| 461 |
tmp_string = get_sandbox_lib(sandbox_dir);
|
| 462 |
strncpy(sandbox_lib, tmp_string, 254);
|
| 463 |
if (tmp_string)
|
| 464 |
free(tmp_string);
|
| 465 |
tmp_string = NULL;
|
| 466 |
|
| 467 |
/* Generate sandbox pids-file path */
|
| 468 |
sandbox_pids_file = get_sandbox_pids_file();
|
| 469 |
|
| 470 |
/* Generate sandbox bashrc path */
|
| 471 |
tmp_string = get_sandbox_rc(sandbox_dir);
|
| 472 |
strncpy(sandbox_rc, tmp_string, 254);
|
| 473 |
if (tmp_string)
|
| 474 |
free(tmp_string);
|
| 475 |
tmp_string = NULL;
|
| 476 |
|
| 477 |
/* verify the existance of required files */
|
| 478 |
if (print_debug)
|
| 479 |
printf("Verification of the required files.\n");
|
| 480 |
|
| 481 |
#ifndef SB_HAVE_64BIT_ARCH
|
| 482 |
if (file_exist(sandbox_lib, 0) <= 0) {
|
| 483 |
fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
|
| 484 |
return -1;
|
| 485 |
}
|
| 486 |
#endif
|
| 487 |
if (file_exist(sandbox_rc, 0) <= 0) {
|
| 488 |
fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
|
| 489 |
return -1;
|
| 490 |
}
|
| 491 |
|
| 492 |
/* set up the required environment variables */
|
| 493 |
if (print_debug)
|
| 494 |
printf("Setting up the required environment variables.\n");
|
| 495 |
|
| 496 |
/* Generate sandbox log full path */
|
| 497 |
tmp_string = get_sandbox_log();
|
| 498 |
strncpy(sandbox_log, tmp_string, 254);
|
| 499 |
if (tmp_string)
|
| 500 |
free(tmp_string);
|
| 501 |
tmp_string = NULL;
|
| 502 |
|
| 503 |
sprintf(pid_string, "%d", getpid());
|
| 504 |
snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
|
| 505 |
DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
|
| 506 |
|
| 507 |
home_dir = getenv("HOME");
|
| 508 |
if (!home_dir) {
|
| 509 |
home_dir = "/tmp";
|
| 510 |
setenv("HOME", home_dir, 1);
|
| 511 |
}
|
| 512 |
|
| 513 |
if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR")
|
| 514 |
: "/var/tmp/portage",
|
| 515 |
portage_tmp_dir)) {
|
| 516 |
perror(">>> get portage_tmp_dir");
|
| 517 |
exit(1);
|
| 518 |
}
|
| 519 |
if (NULL == realpath("/var/tmp", var_tmp_dir)) {
|
| 520 |
perror(">>> get var_tmp_dir");
|
| 521 |
exit(1);
|
| 522 |
}
|
| 523 |
if (NULL == realpath("/tmp", tmp_dir)) {
|
| 524 |
perror(">>> get tmp_dir");
|
| 525 |
exit(1);
|
| 526 |
}
|
| 527 |
|
| 528 |
/* This one should not be child only, as we check above to see
|
| 529 |
* if we are already running (check sandbox_setup_environ).
|
| 530 |
* This needs to be set before calling sandbox_setup_environ(),
|
| 531 |
* else its not set for the child */
|
| 532 |
setenv(ENV_SANDBOX_ON, "1", 0);
|
| 533 |
|
| 534 |
/* Setup the child environment stuff */
|
| 535 |
get_sandbox_write_envvar(sandbox_write_envvar, home_dir,
|
| 536 |
portage_tmp_dir, var_tmp_dir, tmp_dir);
|
| 537 |
get_sandbox_predict_envvar(sandbox_predict_envvar, home_dir);
|
| 538 |
sandbox_environ = sandbox_setup_environ(sandbox_dir, sandbox_lib,
|
| 539 |
sandbox_rc, sandbox_log, sandbox_debug_log,
|
| 540 |
sandbox_write_envvar, sandbox_predict_envvar);
|
| 541 |
if (NULL == sandbox_environ) {
|
| 542 |
perror(">>> out of memory (environ)");
|
| 543 |
exit(1);
|
| 544 |
}
|
| 545 |
|
| 546 |
/* if the portage temp dir was present, cd into it */
|
| 547 |
if (NULL != portage_tmp_dir)
|
| 548 |
chdir(portage_tmp_dir);
|
| 549 |
|
| 550 |
argv_bash = (char **)malloc(6 * sizeof(char *));
|
| 551 |
argv_bash[0] = strdup("/bin/bash");
|
| 552 |
argv_bash[1] = strdup("-rcfile");
|
| 553 |
argv_bash[2] = strdup(sandbox_rc);
|
| 554 |
|
| 555 |
if (argc < 2)
|
| 556 |
argv_bash[3] = NULL;
|
| 557 |
else
|
| 558 |
argv_bash[3] = strdup(run_str); /* "-c" */
|
| 559 |
|
| 560 |
argv_bash[4] = NULL; /* strdup(run_arg); */
|
| 561 |
argv_bash[5] = NULL;
|
| 562 |
|
| 563 |
if (argc >= 2) {
|
| 564 |
for (i = 1; i < argc; i++) {
|
| 565 |
if (NULL == argv_bash[4])
|
| 566 |
len = 0;
|
| 567 |
else
|
| 568 |
len = strlen(argv_bash[4]);
|
| 569 |
|
| 570 |
argv_bash[4] = (char *)realloc(argv_bash[4],
|
| 571 |
(len + strlen(argv[i]) + 2) * sizeof(char));
|
| 572 |
|
| 573 |
if (0 == len)
|
| 574 |
argv_bash[4][0] = 0;
|
| 575 |
if (1 != i)
|
| 576 |
strcat(argv_bash[4], " ");
|
| 577 |
|
| 578 |
strcat(argv_bash[4], argv[i]);
|
| 579 |
}
|
| 580 |
}
|
| 581 |
|
| 582 |
/* set up the required signal handlers */
|
| 583 |
signal(SIGHUP, &stop);
|
| 584 |
signal(SIGINT, &stop);
|
| 585 |
signal(SIGQUIT, &stop);
|
| 586 |
signal(SIGTERM, &stop);
|
| 587 |
|
| 588 |
/* Load our PID into PIDs file */
|
| 589 |
success = 1;
|
| 590 |
if (file_exist(sandbox_pids_file, 1) < 0) {
|
| 591 |
success = 0;
|
| 592 |
fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file);
|
| 593 |
} else {
|
| 594 |
pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
|
| 595 |
if (-1 == pids_file)
|
| 596 |
success = 0;
|
| 597 |
}
|
| 598 |
if (1 == success) {
|
| 599 |
/* Grab still active pids */
|
| 600 |
num_of_pids = load_active_pids(pids_file, &pids_array);
|
| 601 |
|
| 602 |
/* Zero out file */
|
| 603 |
file_truncate(pids_file);
|
| 604 |
|
| 605 |
/* Output active pids, and append our pid */
|
| 606 |
for (i = 0; i < num_of_pids + 1; i++) {
|
| 607 |
/* Time for our entry */
|
| 608 |
if (i == num_of_pids)
|
| 609 |
sprintf(pid_string, "%d\n", getpid());
|
| 610 |
else
|
| 611 |
sprintf(pid_string, "%d\n", pids_array[i]);
|
| 612 |
|
| 613 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
|
| 614 |
perror(">>> pids file write");
|
| 615 |
success = 0;
|
| 616 |
break;
|
| 617 |
}
|
| 618 |
}
|
| 619 |
/* Clean pids_array */
|
| 620 |
if (pids_array)
|
| 621 |
free(pids_array);
|
| 622 |
pids_array = NULL;
|
| 623 |
num_of_pids = 0;
|
| 624 |
|
| 625 |
/* We're done with the pids file */
|
| 626 |
file_close(pids_file);
|
| 627 |
}
|
| 628 |
|
| 629 |
/* Something went wrong, bail out */
|
| 630 |
if (0 == success) {
|
| 631 |
perror(">>> pids file write");
|
| 632 |
exit(1);
|
| 633 |
}
|
| 634 |
|
| 635 |
/* STARTING PROTECTED ENVIRONMENT */
|
| 636 |
if (print_debug) {
|
| 637 |
printf("The protected environment has been started.\n");
|
| 638 |
printf("--------------------------------------------------------------------------------\n");
|
| 639 |
}
|
| 640 |
|
| 641 |
if (print_debug)
|
| 642 |
printf("Shell being started in forked process.\n");
|
| 643 |
|
| 644 |
/* Start Bash */
|
| 645 |
if (!spawn_shell(argv_bash, sandbox_environ)) {
|
| 646 |
if (print_debug)
|
| 647 |
fprintf(stderr, ">>> shell process failed to spawn\n");
|
| 648 |
success = 0;
|
| 649 |
}
|
| 650 |
|
| 651 |
/* Free bash stuff */
|
| 652 |
for (i = 0; i < 6; i++) {
|
| 653 |
if (argv_bash[i])
|
| 654 |
free(argv_bash[i]);
|
| 655 |
argv_bash[i] = NULL;
|
| 656 |
}
|
| 657 |
if (argv_bash)
|
| 658 |
free(argv_bash);
|
| 659 |
argv_bash = NULL;
|
| 660 |
|
| 661 |
if (print_debug)
|
| 662 |
printf("Cleaning up sandbox process\n");
|
| 663 |
|
| 664 |
cleanup();
|
| 665 |
|
| 666 |
if (print_debug) {
|
| 667 |
printf("========================== Gentoo linux path sandbox ===========================\n");
|
| 668 |
printf("The protected environment has been shut down.\n");
|
| 669 |
}
|
| 670 |
|
| 671 |
if (file_exist(sandbox_log, 0)) {
|
| 672 |
sandbox_log_presence = 1;
|
| 673 |
success = 1;
|
| 674 |
if (!print_sandbox_log(sandbox_log))
|
| 675 |
success = 0;
|
| 676 |
|
| 677 |
#if 0
|
| 678 |
if (!success)
|
| 679 |
exit(1);
|
| 680 |
#endif
|
| 681 |
|
| 682 |
sandbox_log_file = -1;
|
| 683 |
} else if (print_debug) {
|
| 684 |
printf("--------------------------------------------------------------------------------\n");
|
| 685 |
}
|
| 686 |
|
| 687 |
free(sandbox_pids_file);
|
| 688 |
|
| 689 |
if ((sandbox_log_presence) || (!success))
|
| 690 |
return 1;
|
| 691 |
else
|
| 692 |
return 0;
|
| 693 |
}
|
| 694 |
}
|
| 695 |
|
| 696 |
// vim:noexpandtab noai:cindent ai
|