Check for existing dso in LD_PRELOAD and only add it if it is not

already present.
This commit is contained in:
Todd C. Miller
2015-12-20 14:08:47 -07:00
parent 584aebe738
commit 88a634b7fb

View File

@@ -46,6 +46,7 @@ preload_dso(char *envp[], const char *dso_file)
char *preload = NULL; char *preload = NULL;
int env_len; int env_len;
int preload_idx = -1; int preload_idx = -1;
bool present = false;
# ifdef RTLD_PRELOAD_ENABLE_VAR # ifdef RTLD_PRELOAD_ENABLE_VAR
bool enabled = false; bool enabled = false;
# else # else
@@ -61,7 +62,27 @@ preload_dso(char *envp[], const char *dso_file)
/* Count entries in envp, looking for LD_PRELOAD as we go. */ /* Count entries in envp, looking for LD_PRELOAD as we go. */
for (env_len = 0; envp[env_len] != NULL; env_len++) { for (env_len = 0; envp[env_len] != NULL; env_len++) {
if (strncmp(envp[env_len], RTLD_PRELOAD_VAR "=", sizeof(RTLD_PRELOAD_VAR)) == 0) { if (preload_idx == -1 && strncmp(envp[env_len], RTLD_PRELOAD_VAR "=",
sizeof(RTLD_PRELOAD_VAR)) == 0) {
const char *cp = envp[env_len] + sizeof(RTLD_PRELOAD_VAR);
const char *end = cp + strlen(cp);
const char *ep;
const size_t dso_len = strlen(dso_file);
/* Check to see if dso_file is already present. */
for (cp = sudo_strsplit(cp, end, RTLD_PRELOAD_DELIM, &ep);
cp != NULL; cp = sudo_strsplit(NULL, end, RTLD_PRELOAD_DELIM,
&ep)) {
if ((size_t)(ep - cp) == dso_len) {
if (memcmp(cp, dso_file, dso_len) == 0) {
/* already present */
present = true;
break;
}
}
}
/* Save index of existing LD_PRELOAD variable. */
preload_idx = env_len; preload_idx = env_len;
continue; continue;
} }
@@ -90,6 +111,7 @@ preload_dso(char *envp[], const char *dso_file)
} }
/* Prepend our LD_PRELOAD to existing value or add new entry at the end. */ /* Prepend our LD_PRELOAD to existing value or add new entry at the end. */
if (!present) {
if (preload_idx == -1) { if (preload_idx == -1) {
# ifdef RTLD_PRELOAD_DEFAULT # ifdef RTLD_PRELOAD_DEFAULT
asprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, dso_file, asprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, dso_file,
@@ -97,17 +119,22 @@ preload_dso(char *envp[], const char *dso_file)
# else # else
preload = sudo_new_key_val(RTLD_PRELOAD_VAR, dso_file); preload = sudo_new_key_val(RTLD_PRELOAD_VAR, dso_file);
# endif # endif
if (preload == NULL) if (preload == NULL) {
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_fatalx(U_("%s: %s"), __func__,
U_("unable to allocate memory"));
}
envp[env_len++] = preload; envp[env_len++] = preload;
envp[env_len] = NULL; envp[env_len] = NULL;
} else { } else {
int len = asprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR, int len = asprintf(&preload, "%s=%s%s%s", RTLD_PRELOAD_VAR,
dso_file, RTLD_PRELOAD_DELIM, envp[preload_idx]); dso_file, RTLD_PRELOAD_DELIM, envp[preload_idx]);
if (len == -1) if (len == -1) {
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_fatalx(U_("%s: %s"), __func__,
U_("unable to allocate memory"));
}
envp[preload_idx] = preload; envp[preload_idx] = preload;
} }
}
# ifdef RTLD_PRELOAD_ENABLE_VAR # ifdef RTLD_PRELOAD_ENABLE_VAR
if (!enabled) { if (!enabled) {
envp[env_len++] = RTLD_PRELOAD_ENABLE_VAR "="; envp[env_len++] = RTLD_PRELOAD_ENABLE_VAR "=";