| 1 |
ferringb |
2 |
/*
|
| 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 |
azarah |
30 |
/* 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 |
ferringb |
2 |
{
|
| 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 |
azarah |
30 |
data = (char *)malloc((len + 1) * sizeof(char));
|
| 55 |
ferringb |
2 |
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 |
azarah |
30 |
break; /* No more PIDs */
|
| 71 |
ferringb |
2 |
|
| 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 |
azarah |
30 |
pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
|
| 80 |
ferringb |
2 |
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 |
azarah |
30 |
void cleanup()
|
| 96 |
ferringb |
2 |
{
|
| 97 |
|
|
int i = 0;
|
| 98 |
|
|
int success = 1;
|
| 99 |
|
|
int pids_file = -1, num_of_pids = 0;
|
| 100 |
|
|
int *pids_array = NULL;
|
| 101 |
azarah |
93 |
char pid_string[SB_BUF_LEN];
|
| 102 |
ferringb |
2 |
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 |
azarah |
90 |
* file if we have not already done so */
|
| 109 |
|
|
if (0 == cleaned_up) {
|
| 110 |
ferringb |
2 |
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 |
ferringb |
34 |
fprintf(stderr, ">>> pids file is not a regular file\n");
|
| 119 |
ferringb |
2 |
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 |
azarah |
30 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
|
| 146 |
ferringb |
2 |
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 |
azarah |
88 |
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 |
azarah |
30 |
void stop(int signum)
|
| 226 |
ferringb |
2 |
{
|
| 227 |
|
|
if (stop_called == 0) {
|
| 228 |
|
|
stop_called = 1;
|
| 229 |
|
|
printf("Caught signal %d in pid %d\r\n", signum, getpid());
|
| 230 |
azarah |
30 |
cleanup();
|
| 231 |
ferringb |
2 |
} else {
|
| 232 |
|
|
fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
|
| 233 |
|
|
}
|
| 234 |
|
|
}
|
| 235 |
|
|
|
| 236 |
azarah |
89 |
void get_sandbox_write_envvar(char *buf, char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
|
| 237 |
ferringb |
2 |
{
|
| 238 |
azarah |
89 |
/* bzero out entire buffer then append trailing 0 */
|
| 239 |
azarah |
93 |
memset(buf, 0, SB_BUF_LEN);
|
| 240 |
azarah |
30 |
|
| 241 |
azarah |
89 |
/* 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 |
ferringb |
2 |
/* bzero out entire buffer then append trailing 0 */
|
| 258 |
azarah |
93 |
memset(buf, 0, SB_BUF_LEN);
|
| 259 |
ferringb |
2 |
|
| 260 |
azarah |
89 |
/* 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 |
ferringb |
2 |
|
| 272 |
azarah |
89 |
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 |
azarah |
96 |
/* FIXME: Should probably free this at some stage - more neatness than
|
| 281 |
|
|
* a real leak that will cause issues. */
|
| 282 |
azarah |
89 |
tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
|
| 283 |
|
|
if (NULL == tmp_string) {
|
| 284 |
|
|
perror(">>> out of memory (sandbox_setenv)");
|
| 285 |
|
|
exit(1);
|
| 286 |
ferringb |
2 |
}
|
| 287 |
azarah |
89 |
|
| 288 |
|
|
snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
|
| 289 |
|
|
name, val);
|
| 290 |
|
|
*tmp_env = tmp_string;
|
| 291 |
|
|
|
| 292 |
|
|
return 0;
|
| 293 |
ferringb |
2 |
}
|
| 294 |
|
|
|
| 295 |
azarah |
89 |
/* We setup the environment child side only to prevent issues with
|
| 296 |
|
|
* setting LD_PRELOAD parent side */
|
| 297 |
|
|
char **sandbox_setup_environ(char *sandbox_dir, char *sandbox_lib, char *sandbox_rc, char *sandbox_log,
|
| 298 |
|
|
char *sandbox_debug_log, char *sandbox_write_envvar, char *sandbox_predict_envvar)
|
| 299 |
ferringb |
2 |
{
|
| 300 |
azarah |
89 |
int env_size = 0;
|
| 301 |
|
|
|
| 302 |
|
|
char **new_environ;
|
| 303 |
|
|
char **env_ptr = environ;
|
| 304 |
|
|
char *ld_preload_envvar = NULL;
|
| 305 |
ferringb |
2 |
|
| 306 |
azarah |
89 |
/* Unset these, as its easier than replacing when setting up our
|
| 307 |
|
|
* new environment below */
|
| 308 |
|
|
unsetenv(ENV_SANDBOX_DIR);
|
| 309 |
|
|
unsetenv(ENV_SANDBOX_LIB);
|
| 310 |
|
|
unsetenv(ENV_SANDBOX_BASHRC);
|
| 311 |
|
|
unsetenv(ENV_SANDBOX_LOG);
|
| 312 |
|
|
unsetenv(ENV_SANDBOX_DEBUG_LOG);
|
| 313 |
|
|
|
| 314 |
|
|
if (NULL != getenv("LD_PRELOAD")) {
|
| 315 |
azarah |
96 |
/* FIXME: Should probably free this at some stage - more neatness
|
| 316 |
|
|
* than a real leak that will cause issues. */
|
| 317 |
azarah |
89 |
ld_preload_envvar = malloc(strlen(getenv("LD_PRELOAD")) +
|
| 318 |
|
|
strlen(sandbox_lib) + 2);
|
| 319 |
|
|
if (NULL == ld_preload_envvar)
|
| 320 |
|
|
return NULL;
|
| 321 |
|
|
strncpy(ld_preload_envvar, sandbox_lib, strlen(sandbox_lib));
|
| 322 |
|
|
strncat(ld_preload_envvar, " ", 1);
|
| 323 |
|
|
strncat(ld_preload_envvar, getenv("LD_PRELOAD"),
|
| 324 |
|
|
strlen(getenv("LD_PRELOAD")));
|
| 325 |
|
|
} else {
|
| 326 |
azarah |
96 |
/* FIXME: Should probably free this at some stage - more neatness
|
| 327 |
|
|
* than a real leak that will cause issues. */
|
| 328 |
azarah |
89 |
ld_preload_envvar = strndup(sandbox_lib, strlen(sandbox_lib));
|
| 329 |
|
|
if (NULL == ld_preload_envvar)
|
| 330 |
|
|
return NULL;
|
| 331 |
|
|
}
|
| 332 |
|
|
unsetenv("LD_PRELOAD");
|
| 333 |
ferringb |
2 |
|
| 334 |
azarah |
89 |
while (NULL != *env_ptr) {
|
| 335 |
|
|
env_size++;
|
| 336 |
|
|
env_ptr++;
|
| 337 |
|
|
}
|
| 338 |
ferringb |
2 |
|
| 339 |
azarah |
96 |
/* FIXME: Should probably free this at some stage - more neatness than
|
| 340 |
|
|
* a real leak that will cause issues. */
|
| 341 |
azarah |
89 |
new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
|
| 342 |
|
|
if (NULL == new_environ)
|
| 343 |
|
|
return NULL;
|
| 344 |
|
|
|
| 345 |
|
|
/* First add our new variables to the beginning - this is due to some
|
| 346 |
|
|
* weirdness that I cannot remember */
|
| 347 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_dir);
|
| 348 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_lib);
|
| 349 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_rc);
|
| 350 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_log);
|
| 351 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log);
|
| 352 |
|
|
sandbox_setenv(new_environ, "LD_PRELOAD", ld_preload_envvar);
|
| 353 |
|
|
|
| 354 |
|
|
if (!getenv(ENV_SANDBOX_DENY))
|
| 355 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
|
| 356 |
|
|
|
| 357 |
|
|
if (!getenv(ENV_SANDBOX_READ))
|
| 358 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
|
| 359 |
|
|
|
| 360 |
|
|
if (!getenv(ENV_SANDBOX_WRITE))
|
| 361 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
|
| 362 |
|
|
|
| 363 |
|
|
if (!getenv(ENV_SANDBOX_PREDICT))
|
| 364 |
|
|
sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
|
| 365 |
|
|
|
| 366 |
|
|
/* This one should NEVER be set in ebuilds, as it is the one
|
| 367 |
|
|
* private thing libsandbox.so use to test if the sandbox
|
| 368 |
|
|
* should be active for this pid, or not.
|
| 369 |
|
|
*
|
| 370 |
|
|
* azarah (3 Aug 2002)
|
| 371 |
|
|
*/
|
| 372 |
|
|
|
| 373 |
|
|
sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
|
| 374 |
|
|
|
| 375 |
|
|
env_size = 0;
|
| 376 |
|
|
while (NULL != new_environ[env_size])
|
| 377 |
|
|
env_size++;
|
| 378 |
|
|
|
| 379 |
|
|
/* Now add the rest */
|
| 380 |
|
|
env_ptr = environ;
|
| 381 |
|
|
while (NULL != *env_ptr) {
|
| 382 |
|
|
new_environ[env_size + (env_ptr - environ)] = *env_ptr;
|
| 383 |
|
|
env_ptr++;
|
| 384 |
ferringb |
2 |
}
|
| 385 |
azarah |
89 |
|
| 386 |
|
|
return new_environ;
|
| 387 |
ferringb |
2 |
}
|
| 388 |
|
|
|
| 389 |
azarah |
89 |
int spawn_shell(char *argv_bash[], char *env[])
|
| 390 |
ferringb |
2 |
{
|
| 391 |
|
|
int pid;
|
| 392 |
|
|
int status = 0;
|
| 393 |
|
|
int ret = 0;
|
| 394 |
|
|
|
| 395 |
|
|
pid = fork();
|
| 396 |
|
|
|
| 397 |
|
|
/* Child's process */
|
| 398 |
|
|
if (0 == pid) {
|
| 399 |
azarah |
89 |
execve(argv_bash[0], argv_bash, env);
|
| 400 |
ferringb |
2 |
return 0;
|
| 401 |
|
|
} else if (pid < 0) {
|
| 402 |
|
|
return 0;
|
| 403 |
|
|
}
|
| 404 |
|
|
ret = waitpid(pid, &status, 0);
|
| 405 |
|
|
if ((-1 == ret) || (status > 0))
|
| 406 |
|
|
return 0;
|
| 407 |
azarah |
86 |
|
| 408 |
azarah |
85 |
return 1;
|
| 409 |
ferringb |
2 |
}
|
| 410 |
|
|
|
| 411 |
azarah |
30 |
int main(int argc, char **argv)
|
| 412 |
ferringb |
2 |
{
|
| 413 |
azarah |
35 |
int ret = 0, i = 0, success = 1;
|
| 414 |
ferringb |
2 |
int sandbox_log_presence = 0;
|
| 415 |
|
|
int sandbox_log_file = -1;
|
| 416 |
|
|
int pids_file = -1;
|
| 417 |
|
|
long len;
|
| 418 |
|
|
|
| 419 |
|
|
int *pids_array = NULL;
|
| 420 |
|
|
int num_of_pids = 0;
|
| 421 |
|
|
|
| 422 |
|
|
// char run_arg[255];
|
| 423 |
azarah |
89 |
char **sandbox_environ;
|
| 424 |
azarah |
93 |
char sandbox_log[SB_PATH_MAX];
|
| 425 |
|
|
char sandbox_debug_log[SB_PATH_MAX];
|
| 426 |
|
|
char sandbox_dir[SB_PATH_MAX];
|
| 427 |
|
|
char sandbox_lib[SB_PATH_MAX];
|
| 428 |
|
|
char sandbox_rc[SB_PATH_MAX];
|
| 429 |
ferringb |
2 |
char *sandbox_pids_file;
|
| 430 |
azarah |
93 |
char portage_tmp_dir[SB_PATH_MAX];
|
| 431 |
|
|
char var_tmp_dir[SB_PATH_MAX];
|
| 432 |
|
|
char tmp_dir[SB_PATH_MAX];
|
| 433 |
|
|
char sandbox_write_envvar[SB_BUF_LEN];
|
| 434 |
|
|
char sandbox_predict_envvar[SB_BUF_LEN];
|
| 435 |
|
|
char pid_string[SB_BUF_LEN];
|
| 436 |
ferringb |
2 |
char **argv_bash = NULL;
|
| 437 |
|
|
|
| 438 |
|
|
char *run_str = "-c";
|
| 439 |
|
|
char *home_dir = NULL;
|
| 440 |
|
|
char *tmp_string = NULL;
|
| 441 |
|
|
|
| 442 |
|
|
/* Only print info if called with no arguments .... */
|
| 443 |
|
|
if (argc < 2)
|
| 444 |
|
|
print_debug = 1;
|
| 445 |
|
|
|
| 446 |
|
|
if (print_debug)
|
| 447 |
azarah |
30 |
printf("========================== Gentoo linux path sandbox ===========================\n");
|
| 448 |
ferringb |
2 |
|
| 449 |
|
|
/* check if a sandbox is already running */
|
| 450 |
|
|
if (NULL != getenv(ENV_SANDBOX_ON)) {
|
| 451 |
azarah |
30 |
fprintf(stderr, "Not launching a new sandbox instance\n");
|
| 452 |
|
|
fprintf(stderr, "Another one is already running in this process hierarchy.\n");
|
| 453 |
ferringb |
2 |
exit(1);
|
| 454 |
|
|
} else {
|
| 455 |
|
|
|
| 456 |
|
|
/* determine the location of all the sandbox support files */
|
| 457 |
|
|
if (print_debug)
|
| 458 |
|
|
printf("Detection of the support files.\n");
|
| 459 |
|
|
|
| 460 |
|
|
/* Generate base sandbox path */
|
| 461 |
|
|
tmp_string = get_sandbox_path(argv[0]);
|
| 462 |
|
|
strncpy(sandbox_dir, tmp_string, 254);
|
| 463 |
|
|
if (tmp_string)
|
| 464 |
|
|
free(tmp_string);
|
| 465 |
|
|
tmp_string = NULL;
|
| 466 |
|
|
strcat(sandbox_dir, "/");
|
| 467 |
|
|
|
| 468 |
|
|
/* Generate sandbox lib path */
|
| 469 |
|
|
tmp_string = get_sandbox_lib(sandbox_dir);
|
| 470 |
|
|
strncpy(sandbox_lib, tmp_string, 254);
|
| 471 |
|
|
if (tmp_string)
|
| 472 |
|
|
free(tmp_string);
|
| 473 |
|
|
tmp_string = NULL;
|
| 474 |
|
|
|
| 475 |
|
|
/* Generate sandbox pids-file path */
|
| 476 |
|
|
sandbox_pids_file = get_sandbox_pids_file();
|
| 477 |
|
|
|
| 478 |
|
|
/* Generate sandbox bashrc path */
|
| 479 |
|
|
tmp_string = get_sandbox_rc(sandbox_dir);
|
| 480 |
|
|
strncpy(sandbox_rc, tmp_string, 254);
|
| 481 |
|
|
if (tmp_string)
|
| 482 |
|
|
free(tmp_string);
|
| 483 |
|
|
tmp_string = NULL;
|
| 484 |
|
|
|
| 485 |
|
|
/* verify the existance of required files */
|
| 486 |
|
|
if (print_debug)
|
| 487 |
|
|
printf("Verification of the required files.\n");
|
| 488 |
|
|
|
| 489 |
|
|
#ifndef SB_HAVE_64BIT_ARCH
|
| 490 |
|
|
if (file_exist(sandbox_lib, 0) <= 0) {
|
| 491 |
azarah |
30 |
fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
|
| 492 |
ferringb |
2 |
return -1;
|
| 493 |
azarah |
30 |
}
|
| 494 |
ferringb |
2 |
#endif
|
| 495 |
|
|
if (file_exist(sandbox_rc, 0) <= 0) {
|
| 496 |
azarah |
30 |
fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
|
| 497 |
ferringb |
2 |
return -1;
|
| 498 |
|
|
}
|
| 499 |
|
|
|
| 500 |
|
|
/* set up the required environment variables */
|
| 501 |
|
|
if (print_debug)
|
| 502 |
|
|
printf("Setting up the required environment variables.\n");
|
| 503 |
|
|
|
| 504 |
|
|
/* Generate sandbox log full path */
|
| 505 |
|
|
tmp_string = get_sandbox_log();
|
| 506 |
|
|
strncpy(sandbox_log, tmp_string, 254);
|
| 507 |
|
|
if (tmp_string)
|
| 508 |
|
|
free(tmp_string);
|
| 509 |
|
|
tmp_string = NULL;
|
| 510 |
|
|
|
| 511 |
azarah |
35 |
sprintf(pid_string, "%d", getpid());
|
| 512 |
ferringb |
2 |
snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
|
| 513 |
azarah |
30 |
DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
|
| 514 |
ferringb |
2 |
|
| 515 |
|
|
home_dir = getenv("HOME");
|
| 516 |
|
|
if (!home_dir) {
|
| 517 |
|
|
home_dir = "/tmp";
|
| 518 |
|
|
setenv("HOME", home_dir, 1);
|
| 519 |
|
|
}
|
| 520 |
|
|
|
| 521 |
azarah |
35 |
if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR")
|
| 522 |
|
|
: "/var/tmp/portage",
|
| 523 |
|
|
portage_tmp_dir)) {
|
| 524 |
|
|
perror(">>> get portage_tmp_dir");
|
| 525 |
|
|
exit(1);
|
| 526 |
ferringb |
2 |
}
|
| 527 |
azarah |
35 |
if (NULL == realpath("/var/tmp", var_tmp_dir)) {
|
| 528 |
|
|
perror(">>> get var_tmp_dir");
|
| 529 |
|
|
exit(1);
|
| 530 |
|
|
}
|
| 531 |
|
|
if (NULL == realpath("/tmp", tmp_dir)) {
|
| 532 |
|
|
perror(">>> get tmp_dir");
|
| 533 |
|
|
exit(1);
|
| 534 |
|
|
}
|
| 535 |
ferringb |
2 |
|
| 536 |
azarah |
91 |
/* This one should not be child only, as we check above to see
|
| 537 |
|
|
* if we are already running (check sandbox_setup_environ).
|
| 538 |
|
|
* This needs to be set before calling sandbox_setup_environ(),
|
| 539 |
|
|
* else its not set for the child */
|
| 540 |
|
|
setenv(ENV_SANDBOX_ON, "1", 0);
|
| 541 |
|
|
|
| 542 |
|
|
/* Setup the child environment stuff */
|
| 543 |
azarah |
89 |
get_sandbox_write_envvar(sandbox_write_envvar, home_dir,
|
| 544 |
|
|
portage_tmp_dir, var_tmp_dir, tmp_dir);
|
| 545 |
|
|
get_sandbox_predict_envvar(sandbox_predict_envvar, home_dir);
|
| 546 |
|
|
sandbox_environ = sandbox_setup_environ(sandbox_dir, sandbox_lib,
|
| 547 |
|
|
sandbox_rc, sandbox_log, sandbox_debug_log,
|
| 548 |
|
|
sandbox_write_envvar, sandbox_predict_envvar);
|
| 549 |
|
|
if (NULL == sandbox_environ) {
|
| 550 |
|
|
perror(">>> out of memory (environ)");
|
| 551 |
|
|
exit(1);
|
| 552 |
azarah |
71 |
}
|
| 553 |
ferringb |
2 |
|
| 554 |
|
|
/* if the portage temp dir was present, cd into it */
|
| 555 |
|
|
if (NULL != portage_tmp_dir)
|
| 556 |
|
|
chdir(portage_tmp_dir);
|
| 557 |
|
|
|
| 558 |
azarah |
30 |
argv_bash = (char **)malloc(6 * sizeof(char *));
|
| 559 |
ferringb |
2 |
argv_bash[0] = strdup("/bin/bash");
|
| 560 |
|
|
argv_bash[1] = strdup("-rcfile");
|
| 561 |
|
|
argv_bash[2] = strdup(sandbox_rc);
|
| 562 |
|
|
|
| 563 |
|
|
if (argc < 2)
|
| 564 |
|
|
argv_bash[3] = NULL;
|
| 565 |
|
|
else
|
| 566 |
|
|
argv_bash[3] = strdup(run_str); /* "-c" */
|
| 567 |
|
|
|
| 568 |
azarah |
30 |
argv_bash[4] = NULL; /* strdup(run_arg); */
|
| 569 |
ferringb |
2 |
argv_bash[5] = NULL;
|
| 570 |
|
|
|
| 571 |
|
|
if (argc >= 2) {
|
| 572 |
|
|
for (i = 1; i < argc; i++) {
|
| 573 |
|
|
if (NULL == argv_bash[4])
|
| 574 |
|
|
len = 0;
|
| 575 |
|
|
else
|
| 576 |
|
|
len = strlen(argv_bash[4]);
|
| 577 |
|
|
|
| 578 |
azarah |
89 |
argv_bash[4] = (char *)realloc(argv_bash[4],
|
| 579 |
|
|
(len + strlen(argv[i]) + 2) * sizeof(char));
|
| 580 |
ferringb |
2 |
|
| 581 |
|
|
if (0 == len)
|
| 582 |
|
|
argv_bash[4][0] = 0;
|
| 583 |
|
|
if (1 != i)
|
| 584 |
|
|
strcat(argv_bash[4], " ");
|
| 585 |
|
|
|
| 586 |
|
|
strcat(argv_bash[4], argv[i]);
|
| 587 |
|
|
}
|
| 588 |
|
|
}
|
| 589 |
|
|
|
| 590 |
|
|
/* set up the required signal handlers */
|
| 591 |
|
|
signal(SIGHUP, &stop);
|
| 592 |
|
|
signal(SIGINT, &stop);
|
| 593 |
|
|
signal(SIGQUIT, &stop);
|
| 594 |
|
|
signal(SIGTERM, &stop);
|
| 595 |
|
|
|
| 596 |
|
|
/* Load our PID into PIDs file */
|
| 597 |
|
|
success = 1;
|
| 598 |
|
|
if (file_exist(sandbox_pids_file, 1) < 0) {
|
| 599 |
|
|
success = 0;
|
| 600 |
|
|
fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file);
|
| 601 |
|
|
} else {
|
| 602 |
|
|
pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
|
| 603 |
|
|
if (-1 == pids_file)
|
| 604 |
|
|
success = 0;
|
| 605 |
|
|
}
|
| 606 |
|
|
if (1 == success) {
|
| 607 |
|
|
/* Grab still active pids */
|
| 608 |
|
|
num_of_pids = load_active_pids(pids_file, &pids_array);
|
| 609 |
|
|
|
| 610 |
|
|
/* Zero out file */
|
| 611 |
|
|
file_truncate(pids_file);
|
| 612 |
|
|
|
| 613 |
|
|
/* Output active pids, and append our pid */
|
| 614 |
|
|
for (i = 0; i < num_of_pids + 1; i++) {
|
| 615 |
|
|
/* Time for our entry */
|
| 616 |
|
|
if (i == num_of_pids)
|
| 617 |
|
|
sprintf(pid_string, "%d\n", getpid());
|
| 618 |
|
|
else
|
| 619 |
|
|
sprintf(pid_string, "%d\n", pids_array[i]);
|
| 620 |
|
|
|
| 621 |
azarah |
30 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
|
| 622 |
ferringb |
2 |
perror(">>> pids file write");
|
| 623 |
|
|
success = 0;
|
| 624 |
|
|
break;
|
| 625 |
|
|
}
|
| 626 |
|
|
}
|
| 627 |
|
|
/* Clean pids_array */
|
| 628 |
|
|
if (pids_array)
|
| 629 |
|
|
free(pids_array);
|
| 630 |
|
|
pids_array = NULL;
|
| 631 |
|
|
num_of_pids = 0;
|
| 632 |
|
|
|
| 633 |
|
|
/* We're done with the pids file */
|
| 634 |
|
|
file_close(pids_file);
|
| 635 |
|
|
}
|
| 636 |
|
|
|
| 637 |
|
|
/* Something went wrong, bail out */
|
| 638 |
|
|
if (0 == success) {
|
| 639 |
|
|
perror(">>> pids file write");
|
| 640 |
|
|
exit(1);
|
| 641 |
|
|
}
|
| 642 |
|
|
|
| 643 |
|
|
/* STARTING PROTECTED ENVIRONMENT */
|
| 644 |
|
|
if (print_debug) {
|
| 645 |
|
|
printf("The protected environment has been started.\n");
|
| 646 |
azarah |
30 |
printf("--------------------------------------------------------------------------------\n");
|
| 647 |
ferringb |
2 |
}
|
| 648 |
|
|
|
| 649 |
|
|
if (print_debug)
|
| 650 |
|
|
printf("Shell being started in forked process.\n");
|
| 651 |
|
|
|
| 652 |
|
|
/* Start Bash */
|
| 653 |
azarah |
89 |
if (!spawn_shell(argv_bash, sandbox_environ)) {
|
| 654 |
ferringb |
2 |
if (print_debug)
|
| 655 |
|
|
fprintf(stderr, ">>> shell process failed to spawn\n");
|
| 656 |
|
|
success = 0;
|
| 657 |
|
|
}
|
| 658 |
|
|
|
| 659 |
|
|
/* Free bash stuff */
|
| 660 |
|
|
for (i = 0; i < 6; i++) {
|
| 661 |
|
|
if (argv_bash[i])
|
| 662 |
|
|
free(argv_bash[i]);
|
| 663 |
|
|
argv_bash[i] = NULL;
|
| 664 |
|
|
}
|
| 665 |
|
|
if (argv_bash)
|
| 666 |
|
|
free(argv_bash);
|
| 667 |
|
|
argv_bash = NULL;
|
| 668 |
|
|
|
| 669 |
|
|
if (print_debug)
|
| 670 |
|
|
printf("Cleaning up sandbox process\n");
|
| 671 |
|
|
|
| 672 |
|
|
cleanup();
|
| 673 |
|
|
|
| 674 |
|
|
if (print_debug) {
|
| 675 |
azarah |
30 |
printf("========================== Gentoo linux path sandbox ===========================\n");
|
| 676 |
ferringb |
2 |
printf("The protected environment has been shut down.\n");
|
| 677 |
|
|
}
|
| 678 |
|
|
|
| 679 |
|
|
if (file_exist(sandbox_log, 0)) {
|
| 680 |
|
|
sandbox_log_presence = 1;
|
| 681 |
|
|
success = 1;
|
| 682 |
|
|
if (!print_sandbox_log(sandbox_log))
|
| 683 |
|
|
success = 0;
|
| 684 |
|
|
|
| 685 |
|
|
#if 0
|
| 686 |
|
|
if (!success)
|
| 687 |
|
|
exit(1);
|
| 688 |
|
|
#endif
|
| 689 |
|
|
|
| 690 |
|
|
sandbox_log_file = -1;
|
| 691 |
|
|
} else if (print_debug) {
|
| 692 |
azarah |
30 |
printf("--------------------------------------------------------------------------------\n");
|
| 693 |
ferringb |
2 |
}
|
| 694 |
|
|
|
| 695 |
azarah |
35 |
free(sandbox_pids_file);
|
| 696 |
|
|
|
| 697 |
ferringb |
2 |
if ((sandbox_log_presence) || (!success))
|
| 698 |
|
|
return 1;
|
| 699 |
|
|
else
|
| 700 |
|
|
return 0;
|
| 701 |
|
|
}
|
| 702 |
|
|
}
|
| 703 |
|
|
|
| 704 |
azarah |
30 |
// vim:noexpandtab noai:cindent ai
|