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