2012-10-30 01:37:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "linker_environ.h"
|
|
|
|
|
|
|
|
#include <linux/auxvec.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-10-10 00:50:50 +02:00
|
|
|
#include "private/KernelArgumentBlock.h"
|
2013-02-07 19:14:39 +01:00
|
|
|
|
2012-10-30 01:37:13 +01:00
|
|
|
static char** _envp;
|
|
|
|
static bool _AT_SECURE_value = true;
|
|
|
|
|
|
|
|
bool get_AT_SECURE() {
|
|
|
|
return _AT_SECURE_value;
|
|
|
|
}
|
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
static void __init_AT_SECURE(KernelArgumentBlock& args) {
|
2012-11-02 20:40:11 +01:00
|
|
|
// Check auxv for AT_SECURE first to see if program is setuid, setgid,
|
|
|
|
// has file caps, or caused a SELinux/AppArmor domain transition.
|
2013-02-07 19:14:39 +01:00
|
|
|
bool kernel_supplied_AT_SECURE;
|
|
|
|
_AT_SECURE_value = args.getauxval(AT_SECURE, &kernel_supplied_AT_SECURE);
|
2012-11-02 20:40:11 +01:00
|
|
|
|
|
|
|
// We don't support ancient kernels.
|
2013-02-07 19:14:39 +01:00
|
|
|
if (!kernel_supplied_AT_SECURE) {
|
|
|
|
const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
|
|
|
|
write(2, msg, strlen(msg));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-11-02 20:40:11 +01:00
|
|
|
}
|
2012-10-30 01:37:13 +01:00
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
// Check if the environment variable definition at 'envstr'
|
|
|
|
// starts with '<name>=', and if so return the address of the
|
2014-08-29 21:02:36 +02:00
|
|
|
// first character after the equal sign. Otherwise return null.
|
2012-11-02 20:40:11 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2012-11-02 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool __is_valid_environment_variable(const char* name) {
|
2012-10-30 01:37:13 +01:00
|
|
|
// 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;
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
if (name == nullptr) {
|
2012-11-02 20:40:11 +01:00
|
|
|
return false;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the string, looking for the first '=' there, and its size.
|
2012-11-02 20:40:11 +01:00
|
|
|
int pos = 0;
|
|
|
|
int first_equal_pos = -1;
|
2012-10-30 01:37:13 +01:00
|
|
|
while (pos < MAX_ENV_LEN) {
|
2012-11-02 20:40:11 +01:00
|
|
|
if (name[pos] == '\0') {
|
2012-10-30 01:37:13 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-11-02 20:40:11 +01:00
|
|
|
if (name[pos] == '=' && first_equal_pos < 0) {
|
2012-10-30 01:37:13 +01:00
|
|
|
first_equal_pos = pos;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
// Check that it's smaller than MAX_ENV_LEN (to detect non-zero terminated strings).
|
2012-10-30 01:37:13 +01:00
|
|
|
if (pos >= MAX_ENV_LEN) {
|
2012-11-02 20:40:11 +01:00
|
|
|
return false;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
// Check that it contains at least one equal sign that is not the first character
|
2012-10-30 01:37:13 +01:00
|
|
|
if (first_equal_pos < 1) {
|
2012-11-02 20:40:11 +01:00
|
|
|
return false;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
return true;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
static bool __is_unsafe_environment_variable(const char* name) {
|
2012-10-30 01:37:13 +01:00
|
|
|
// None of these should be allowed in setuid programs.
|
|
|
|
static const char* const UNSAFE_VARIABLE_NAMES[] = {
|
|
|
|
"GCONV_PATH",
|
|
|
|
"GETCONF_DIR",
|
|
|
|
"HOSTALIASES",
|
|
|
|
"LD_AOUT_LIBRARY_PATH",
|
|
|
|
"LD_AOUT_PRELOAD",
|
|
|
|
"LD_AUDIT",
|
|
|
|
"LD_DEBUG",
|
|
|
|
"LD_DEBUG_OUTPUT",
|
|
|
|
"LD_DYNAMIC_WEAK",
|
|
|
|
"LD_LIBRARY_PATH",
|
|
|
|
"LD_ORIGIN_PATH",
|
|
|
|
"LD_PRELOAD",
|
|
|
|
"LD_PROFILE",
|
|
|
|
"LD_SHOW_AUXV",
|
|
|
|
"LD_USE_LOAD_BIAS",
|
|
|
|
"LOCALDOMAIN",
|
|
|
|
"LOCPATH",
|
|
|
|
"MALLOC_CHECK_",
|
|
|
|
"MALLOC_TRACE",
|
|
|
|
"NIS_PATH",
|
|
|
|
"NLSPATH",
|
|
|
|
"RESOLV_HOST_CONF",
|
|
|
|
"RES_OPTIONS",
|
|
|
|
"TMPDIR",
|
|
|
|
"TZDIR",
|
2014-08-29 21:02:36 +02:00
|
|
|
nullptr
|
2012-10-30 01:37:13 +01:00
|
|
|
};
|
2014-08-29 21:02:36 +02:00
|
|
|
for (size_t i = 0; UNSAFE_VARIABLE_NAMES[i] != nullptr; ++i) {
|
|
|
|
if (env_match(name, UNSAFE_VARIABLE_NAMES[i]) != nullptr) {
|
2012-11-02 20:40:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
2012-11-02 20:40:11 +01:00
|
|
|
return false;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 20:40:11 +01:00
|
|
|
static void __sanitize_environment_variables() {
|
2012-10-30 01:37:13 +01:00
|
|
|
char** src = _envp;
|
|
|
|
char** dst = _envp;
|
2014-08-29 21:02:36 +02:00
|
|
|
for (; src[0] != nullptr; ++src) {
|
2012-11-02 20:40:11 +01:00
|
|
|
if (!__is_valid_environment_variable(src[0])) {
|
2012-10-30 01:37:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-02 20:40:11 +01:00
|
|
|
// Remove various unsafe environment variables if we're loading a setuid program.
|
|
|
|
if (get_AT_SECURE() && __is_unsafe_environment_variable(src[0])) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-30 01:37:13 +01:00
|
|
|
dst[0] = src[0];
|
|
|
|
++dst;
|
|
|
|
}
|
2014-08-29 21:02:36 +02:00
|
|
|
dst[0] = nullptr;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
void linker_env_init(KernelArgumentBlock& args) {
|
2014-08-29 21:02:36 +02:00
|
|
|
// Store environment pointer - can't be null.
|
2013-02-07 19:14:39 +01:00
|
|
|
_envp = args.envp;
|
2012-10-30 01:37:13 +01:00
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
__init_AT_SECURE(args);
|
2012-11-02 20:40:11 +01:00
|
|
|
__sanitize_environment_variables();
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* linker_env_get(const char* name) {
|
2014-08-29 21:02:36 +02:00
|
|
|
if (name == nullptr || name[0] == '\0') {
|
|
|
|
return nullptr;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
for (char** p = _envp; p[0] != nullptr; ++p) {
|
2012-11-02 20:40:11 +01:00
|
|
|
const char* val = env_match(p[0], name);
|
2014-08-29 21:02:36 +02:00
|
|
|
if (val != nullptr) {
|
2012-10-30 01:37:13 +01:00
|
|
|
if (val[0] == '\0') {
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr; // Return null for empty strings.
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|