index 357be6d58c383b8883ac22a809034387adaeb0f8..daee56f7e9974b86cbc62b4d2a78a87b4a1c1054 100644 (file)
#include <stdlib.h>
#include <unistd.h>
+#include "private/KernelArgumentBlock.h"
+
static char** _envp;
static bool _AT_SECURE_value = true;
return _AT_SECURE_value;
}
-/* Returns 1 if 'str' points to a valid environment variable definition.
- * For now, we check that:
- * - It is smaller than MAX_ENV_LEN (to detect non-zero terminated strings)
- * - It contains at least one equal sign that is not the first character
- */
-static int _is_valid_definition(const char* str) {
- int pos = 0;
- int first_equal_pos = -1;
+static void __init_AT_SECURE(KernelArgumentBlock& args) {
+ // Check auxv for AT_SECURE first to see if program is setuid, setgid,
+ // has file caps, or caused a SELinux/AppArmor domain transition.
+ bool kernel_supplied_AT_SECURE;
+ _AT_SECURE_value = args.getauxval(AT_SECURE, &kernel_supplied_AT_SECURE);
+
+ // We don't support ancient kernels.
+ if (!kernel_supplied_AT_SECURE) {
+ const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
+ write(2, msg, strlen(msg));
+ exit(EXIT_FAILURE);
+ }
+}
+
+// Check if the environment variable definition at 'envstr'
+// starts with '<name>=', and if so return the address of the
+// first character after the equal sign. Otherwise return null.
+static const char* env_match(const char* envstr, const char* name) {
+ size_t i = 0;
+
+ while (envstr[i] == name[i] && name[i] != '\0') {
+ ++i;
+ }
+
+ if (name[i] == '\0' && envstr[i] == '=') {
+ return envstr + i + 1;
+ }
+
+ return nullptr;
+}
+static bool __is_valid_environment_variable(const char* name) {
// According to its sources, the kernel uses 32*PAGE_SIZE by default
// as the maximum size for an env. variable definition.
const int MAX_ENV_LEN = 32*4096;
- if (str == NULL) {
- return 0;
+ if (name == nullptr) {
+ return false;
}
// Parse the string, looking for the first '=' there, and its size.
+ int pos = 0;
+ int first_equal_pos = -1;
while (pos < MAX_ENV_LEN) {
- if (str[pos] == '\0') {
+ if (name[pos] == '\0') {
break;
}
- if (str[pos] == '=' && first_equal_pos < 0) {
+ if (name[pos] == '=' && first_equal_pos < 0) {
first_equal_pos = pos;
}
pos++;
}
+ // Check that it's smaller than MAX_ENV_LEN (to detect non-zero terminated strings).
if (pos >= MAX_ENV_LEN) {
- return 0; // Too large.
+ return false;
}
+ // Check that it contains at least one equal sign that is not the first character
if (first_equal_pos < 1) {
- return 0; // No equals sign, or it's the first character.
- }
-
- return 1;
-}
-
-static void __init_AT_SECURE(unsigned* auxv) {
- // Check auxv for AT_SECURE first to see if program is setuid, setgid,
- // has file caps, or caused a SELinux/AppArmor domain transition.
- for (unsigned* v = auxv; v[0]; v += 2) {
- if (v[0] == AT_SECURE) {
- // Kernel told us whether to enable secure mode.
- _AT_SECURE_value = v[1];
- return;
- }
+ return false;
}
- // We don't support ancient kernels.
- const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
- write(2, msg, strlen(msg));
- exit(EXIT_FAILURE);
+ return true;
}
-static void __remove_unsafe_environment_variables() {
+static bool __is_unsafe_environment_variable(const char* name) {
// None of these should be allowed in setuid programs.
static const char* const UNSAFE_VARIABLE_NAMES[] = {
"GCONV_PATH",
"RES_OPTIONS",
"TMPDIR",
"TZDIR",
- NULL
+ nullptr
};
- for (size_t i = 0; UNSAFE_VARIABLE_NAMES[i] != NULL; ++i) {
- linker_env_unset(UNSAFE_VARIABLE_NAMES[i]);
+ for (size_t i = 0; UNSAFE_VARIABLE_NAMES[i] != nullptr; ++i) {
+ if (env_match(name, UNSAFE_VARIABLE_NAMES[i]) != nullptr) {
+ return true;
+ }
}
+ return false;
}
-static void __remove_invalid_environment_variables() {
+static void __sanitize_environment_variables() {
char** src = _envp;
char** dst = _envp;
- for (; src[0] != NULL; ++src) {
- if (!_is_valid_definition(src[0])) {
+ for (; src[0] != nullptr; ++src) {
+ if (!__is_valid_environment_variable(src[0])) {
continue;
}
+ // Remove various unsafe environment variables if we're loading a setuid program.
+ if (get_AT_SECURE() && __is_unsafe_environment_variable(src[0])) {
+ continue;
+ }
dst[0] = src[0];
++dst;
}
- dst[0] = NULL;
+ dst[0] = nullptr;
}
-unsigned* linker_env_init(unsigned* environment_and_aux_vectors) {
- // Store environment pointer - can't be NULL.
- _envp = reinterpret_cast<char**>(environment_and_aux_vectors);
-
- // Skip over all environment variable definitions.
- // The end of the environment block is marked by two NULL pointers.
- unsigned* aux_vectors = environment_and_aux_vectors;
- while (aux_vectors[0] != 0) {
- ++aux_vectors;
- }
- ++aux_vectors;
-
- __remove_invalid_environment_variables();
- __init_AT_SECURE(aux_vectors);
-
- // Sanitize environment if we're loading a setuid program.
- if (get_AT_SECURE()) {
- __remove_unsafe_environment_variables();
- }
-
- return aux_vectors;
-}
-
-/* Check if the environment variable definition at 'envstr'
- * starts with '<name>=', and if so return the address of the
- * first character after the equal sign. Otherwise return NULL.
- */
-static char* env_match(char* envstr, const char* name) {
- size_t i = 0;
-
- while (envstr[i] == name[i] && name[i] != '\0') {
- ++i;
- }
-
- if (name[i] == '\0' && envstr[i] == '=') {
- return envstr + i + 1;
- }
+void linker_env_init(KernelArgumentBlock& args) {
+ // Store environment pointer - can't be null.
+ _envp = args.envp;
- return NULL;
+ __init_AT_SECURE(args);
+ __sanitize_environment_variables();
}
const char* linker_env_get(const char* name) {
- if (name == NULL || name[0] == '\0') {
- return NULL;
+ if (name == nullptr || name[0] == '\0') {
+ return nullptr;
}
- for (char** p = _envp; p[0] != NULL; ++p) {
- char* val = env_match(p[0], name);
- if (val != NULL) {
+ for (char** p = _envp; p[0] != nullptr; ++p) {
+ const char* val = env_match(p[0], name);
+ if (val != nullptr) {
if (val[0] == '\0') {
- return NULL; // Return NULL for empty strings.
+ return nullptr; // Return null for empty strings.
}
return val;
}
}
- return NULL;
-}
-
-void linker_env_unset(const char* name) {
- char** readp = _envp;
- char** writep = readp;
-
- if (name == NULL || name[0] == '\0') {
- return;
- }
-
- for ( ; readp[0] != NULL; readp++ ) {
- if (env_match(readp[0], name)) {
- continue;
- }
- writep[0] = readp[0];
- writep++;
- }
- /* end list with a NULL */
- writep[0] = NULL;
+ return nullptr;
}