Skip to content

Commit

Permalink
libear: fix crash "corrupted double-linked list"
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Aug 21, 2016
1 parent dc6c8e3 commit daa6526
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions libear/ear.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#if defined HAVE_NSGETENVIRON
# include <crt_externs.h>
static char **environ;
#else
extern char **environ;
#endif
Expand Down Expand Up @@ -65,11 +66,9 @@ extern char **environ;
typedef char const * bear_env_t[ENV_SIZE];

static int bear_capture_env_t(bear_env_t *env);
static int bear_reset_env_t(bear_env_t *env);
static void bear_release_env_t(bear_env_t *env);
static char const **bear_update_environment(char *const envp[], bear_env_t *env);
static char const **bear_update_environ(char const **in, char const *key, char const *value);
static char **bear_get_environment();
static void bear_report_call(char const *fun, char const *const argv[]);
static char const **bear_strings_build(char const *arg, va_list *ap);
static char const **bear_strings_copy(char const **const in);
Expand Down Expand Up @@ -141,6 +140,9 @@ static int call_posix_spawnp(pid_t *restrict pid, const char *restrict file,

static void on_load(void) {
pthread_mutex_lock(&mutex);
#ifdef HAVE_NSGETENVIRON
environ = *_NSGetEnviron();
#endif
if (!initialized)
initialized = bear_capture_env_t(&initial_env);
pthread_mutex_unlock(&mutex);
Expand Down Expand Up @@ -170,8 +172,7 @@ int execve(const char *path, char *const argv[], char *const envp[]) {
#endif
int execv(const char *path, char *const argv[]) {
bear_report_call(__func__, (char const *const *)argv);
char * const * envp = bear_get_environment();
return call_execve(path, argv, envp);
return call_execve(path, argv, environ);
}
#endif

Expand Down Expand Up @@ -214,8 +215,7 @@ int execl(const char *path, const char *arg, ...) {
va_end(args);

bear_report_call(__func__, (char const *const *)argv);
char * const * envp = bear_get_environment();
int const result = call_execve(path, (char *const *)argv, envp);
int const result = call_execve(path, (char *const *)argv, environ);

bear_strings_release(argv);
return result;
Expand Down Expand Up @@ -318,12 +318,12 @@ static int call_execvp(const char *file, char *const argv[]) {

DLSYM(func, fp, "execvp");

bear_env_t current_env;
bear_capture_env_t(&current_env);
bear_reset_env_t(&initial_env);
char **const original = environ;
char const **const modified = bear_update_environment(original, &initial_env);
environ = (char **)modified;
int const result = (*fp)(file, argv);
bear_reset_env_t(&current_env);
bear_release_env_t(&current_env);
environ = original;
bear_strings_release(modified);

return result;
}
Expand All @@ -336,12 +336,12 @@ static int call_execvP(const char *file, const char *search_path,

DLSYM(func, fp, "execvP");

bear_env_t current_env;
bear_capture_env_t(&current_env);
bear_reset_env_t(&initial_env);
char **const original = environ;
char const **const modified = bear_update_environment(original, &initial_env);
environ = (char **)modified;
int const result = (*fp)(file, search_path, argv);
bear_reset_env_t(&current_env);
bear_release_env_t(&current_env);
environ = original;
bear_strings_release(modified);

return result;
}
Expand Down Expand Up @@ -462,18 +462,6 @@ static int bear_capture_env_t(bear_env_t *env) {
return status;
}

static int bear_reset_env_t(bear_env_t *env) {
int status = 1;
for (size_t it = 0; it < ENV_SIZE; ++it) {
if ((*env)[it]) {
setenv(env_names[it], (*env)[it], 1);
} else {
unsetenv(env_names[it]);
}
}
return status;
}

static void bear_release_env_t(bear_env_t *env) {
for (size_t it = 0; it < ENV_SIZE; ++it) {
free((void *)(*env)[it]);
Expand Down Expand Up @@ -518,14 +506,6 @@ static char const **bear_update_environ(char const *envs[], char const *key, cha
return bear_strings_append(envs, env);
}

static char **bear_get_environment() {
#if defined HAVE_NSGETENVIRON
return *_NSGetEnviron();
#else
return environ;
#endif
}

/* util methods to deal with string arrays. environment and process arguments
* are both represented as string arrays. */

Expand Down

0 comments on commit daa6526

Please sign in to comment.