aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2012-12-27 01:28:17 -0500
committerMike Frysinger <vapier@gentoo.org>2013-02-24 23:05:05 -0500
commit97ea4d6d14ae90bd6371936fd6a718df0f62efba (patch)
treeea707ff91d0e1c0c38f499c9fa14c4f97dd52f7c
parentenviron: merge is_env_{on,off} into a single file (diff)
downloadsandbox-97ea4d6d14ae90bd6371936fd6a718df0f62efba.tar.gz
sandbox-97ea4d6d14ae90bd6371936fd6a718df0f62efba.tar.bz2
sandbox-97ea4d6d14ae90bd6371936fd6a718df0f62efba.zip
environ: add a new is_env_var helper for checking var names
This is laying the ground work for processing more vars in the future than just LD_PRELOAD. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
-rw-r--r--libsandbox/wrapper-funcs/__wrapper_exec.c16
-rw-r--r--libsbutil/sbutil.h9
-rw-r--r--src/environ.c4
3 files changed, 16 insertions, 13 deletions
diff --git a/libsandbox/wrapper-funcs/__wrapper_exec.c b/libsandbox/wrapper-funcs/__wrapper_exec.c
index b7c7dfa..3ac936c 100644
--- a/libsandbox/wrapper-funcs/__wrapper_exec.c
+++ b/libsandbox/wrapper-funcs/__wrapper_exec.c
@@ -98,11 +98,11 @@ static char **_sb_check_envp(char **envp, bool is_environ)
char *entry;
char *ld_preload = NULL;
char *old_ld_preload = NULL;
- size_t count, ld_preload_eq_len;
+ size_t count, ld_preload_len;
- ld_preload_eq_len = strlen(LD_PRELOAD_EQ);
+ ld_preload_len = strlen(ENV_LD_PRELOAD);
str_list_for_each_item(envp, entry, count) {
- if (strncmp(entry, LD_PRELOAD_EQ, ld_preload_eq_len))
+ if (!is_env_var(entry, ENV_LD_PRELOAD, ld_preload_len))
continue;
/* Check if we do not have to do anything */
@@ -122,18 +122,18 @@ static char **_sb_check_envp(char **envp, bool is_environ)
/* Ok, we need to create our own envp, as we need to add LD_PRELOAD,
* and we should not touch the user's envp. First we add LD_PRELOAD,
* and just all the rest. */
- count = ld_preload_eq_len + (strlen(sandbox_lib) + 1) +
- (old_ld_preload ? strlen(old_ld_preload) - ld_preload_eq_len + 1 : 0);
+ count = ld_preload_len + 1 + strlen(sandbox_lib) + 1 +
+ (old_ld_preload ? strlen(old_ld_preload) - ld_preload_len : 0);
ld_preload = xmalloc(count * sizeof(char));
- snprintf(ld_preload, count, "%s%s%s%s", LD_PRELOAD_EQ, sandbox_lib,
+ snprintf(ld_preload, count, "%s=%s%s%s", ENV_LD_PRELOAD, sandbox_lib,
(old_ld_preload) ? " " : "",
- (old_ld_preload) ? old_ld_preload + ld_preload_eq_len : "");
+ (old_ld_preload) ? old_ld_preload + ld_preload_len + 1 : "");
if (!is_environ) {
str_list_add_item(my_env, ld_preload, error);
str_list_for_each_item(envp, entry, count) {
- if (strncmp(entry, LD_PRELOAD_EQ, ld_preload_eq_len)) {
+ if (!is_env_var(entry, ENV_LD_PRELOAD, ld_preload_len)) {
str_list_add_item(my_env, entry, error);
continue;
}
diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h
index 67a8aaa..6c90f35 100644
--- a/libsbutil/sbutil.h
+++ b/libsbutil/sbutil.h
@@ -19,7 +19,6 @@
#define SANDBOX_CONF_FILE ETCDIR "/sandbox.conf"
#define SANDBOX_CONFD_DIR ETCDIR "/sandbox.d"
-#define LD_PRELOAD_EQ "LD_PRELOAD="
#define LIB_NAME "libsandbox.so"
#define BASHRC_NAME "sandbox.bashrc"
#define TMPDIR "/tmp"
@@ -79,8 +78,12 @@ void get_sandbox_rc(char *path);
void get_sandbox_log(char *path, const char *tmpdir);
void get_sandbox_debug_log(char *path, const char *tmpdir);
int get_tmp_dir(char *path);
-bool is_env_on (const char *);
-bool is_env_off (const char *);
+bool is_env_on(const char *);
+bool is_env_off(const char *);
+static inline bool is_env_var(const char *env, const char *var, size_t vlen)
+{
+ return !strncmp(env, var, vlen) && env[vlen] == '=';
+}
/* proc helpers */
extern const char sb_fd_dir[];
diff --git a/src/environ.c b/src/environ.c
index 45bf9a6..30ba003 100644
--- a/src/environ.c
+++ b/src/environ.c
@@ -318,9 +318,9 @@ char **setup_environ(struct sandbox_info_t *sandbox_info, bool interactive)
/* Now add the rest */
env_ptr = environ;
+ size_t vlen = strlen(ENV_LD_PRELOAD);
while (NULL != *env_ptr) {
- if ((1 == have_ld_preload) &&
- (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
+ if ((1 == have_ld_preload) && is_env_var(*env_ptr, ENV_LD_PRELOAD, vlen))
/* If LD_PRELOAD was set, and this is it in the original
* environment, replace it with our new copy */
/* XXX: The following works as it just add whatever as