blob: 9a0f0099ff0aa20c9b06f0f2ccb272b425711b56 [file] [log] [blame]
Elliott Hughes18a206c2012-10-29 17:37:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "linker_environ.h"
30
31#include <linux/auxvec.h>
32#include <stddef.h>
33#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080034#include <string.h>
Elliott Hughes18a206c2012-10-29 17:37:13 -070035#include <unistd.h>
36
Elliott Hugheseb847bc2013-10-09 15:50:50 -070037#include "private/KernelArgumentBlock.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080038
Elliott Hughes18a206c2012-10-29 17:37:13 -070039static char** _envp;
40static bool _AT_SECURE_value = true;
41
42bool get_AT_SECURE() {
43 return _AT_SECURE_value;
44}
45
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080046static void __init_AT_SECURE(KernelArgumentBlock& args) {
Elliott Hughes18a206c2012-10-29 17:37:13 -070047 // Check auxv for AT_SECURE first to see if program is setuid, setgid,
48 // has file caps, or caused a SELinux/AppArmor domain transition.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080049 bool kernel_supplied_AT_SECURE;
50 _AT_SECURE_value = args.getauxval(AT_SECURE, &kernel_supplied_AT_SECURE);
Elliott Hughes18a206c2012-10-29 17:37:13 -070051
52 // We don't support ancient kernels.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080053 if (!kernel_supplied_AT_SECURE) {
54 const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
55 write(2, msg, strlen(msg));
56 exit(EXIT_FAILURE);
57 }
Elliott Hughes18a206c2012-10-29 17:37:13 -070058}
59
Elliott Hughes0894b2c2012-11-02 12:40:11 -070060// Check if the environment variable definition at 'envstr'
61// starts with '<name>=', and if so return the address of the
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070062// first character after the equal sign. Otherwise return null.
Elliott Hughes0894b2c2012-11-02 12:40:11 -070063static const char* env_match(const char* envstr, const char* name) {
64 size_t i = 0;
65
66 while (envstr[i] == name[i] && name[i] != '\0') {
67 ++i;
68 }
69
70 if (name[i] == '\0' && envstr[i] == '=') {
71 return envstr + i + 1;
72 }
73
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070074 return nullptr;
Elliott Hughes0894b2c2012-11-02 12:40:11 -070075}
76
77static bool __is_valid_environment_variable(const char* name) {
78 // According to its sources, the kernel uses 32*PAGE_SIZE by default
79 // as the maximum size for an env. variable definition.
80 const int MAX_ENV_LEN = 32*4096;
81
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070082 if (name == nullptr) {
Elliott Hughes0894b2c2012-11-02 12:40:11 -070083 return false;
84 }
85
86 // Parse the string, looking for the first '=' there, and its size.
87 int pos = 0;
88 int first_equal_pos = -1;
89 while (pos < MAX_ENV_LEN) {
90 if (name[pos] == '\0') {
91 break;
92 }
93 if (name[pos] == '=' && first_equal_pos < 0) {
94 first_equal_pos = pos;
95 }
96 pos++;
97 }
98
99 // Check that it's smaller than MAX_ENV_LEN (to detect non-zero terminated strings).
100 if (pos >= MAX_ENV_LEN) {
101 return false;
102 }
103
104 // Check that it contains at least one equal sign that is not the first character
105 if (first_equal_pos < 1) {
106 return false;
107 }
108
109 return true;
110}
111
112static bool __is_unsafe_environment_variable(const char* name) {
Elliott Hughes18a206c2012-10-29 17:37:13 -0700113 // None of these should be allowed in setuid programs.
114 static const char* const UNSAFE_VARIABLE_NAMES[] = {
115 "GCONV_PATH",
116 "GETCONF_DIR",
117 "HOSTALIASES",
Nick Kraleviche001ca32015-03-27 14:01:00 -0700118 "JE_MALLOC_CONF",
Elliott Hughes18a206c2012-10-29 17:37:13 -0700119 "LD_AOUT_LIBRARY_PATH",
120 "LD_AOUT_PRELOAD",
121 "LD_AUDIT",
122 "LD_DEBUG",
123 "LD_DEBUG_OUTPUT",
124 "LD_DYNAMIC_WEAK",
125 "LD_LIBRARY_PATH",
126 "LD_ORIGIN_PATH",
127 "LD_PRELOAD",
128 "LD_PROFILE",
129 "LD_SHOW_AUXV",
130 "LD_USE_LOAD_BIAS",
131 "LOCALDOMAIN",
132 "LOCPATH",
133 "MALLOC_CHECK_",
Nick Kraleviche001ca32015-03-27 14:01:00 -0700134 "MALLOC_CONF",
Elliott Hughes18a206c2012-10-29 17:37:13 -0700135 "MALLOC_TRACE",
136 "NIS_PATH",
137 "NLSPATH",
138 "RESOLV_HOST_CONF",
139 "RES_OPTIONS",
140 "TMPDIR",
141 "TZDIR",
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700142 nullptr
Elliott Hughes18a206c2012-10-29 17:37:13 -0700143 };
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700144 for (size_t i = 0; UNSAFE_VARIABLE_NAMES[i] != nullptr; ++i) {
145 if (env_match(name, UNSAFE_VARIABLE_NAMES[i]) != nullptr) {
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700146 return true;
147 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700148 }
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700149 return false;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700150}
151
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700152static void __sanitize_environment_variables() {
Elliott Hughes18a206c2012-10-29 17:37:13 -0700153 char** src = _envp;
154 char** dst = _envp;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700155 for (; src[0] != nullptr; ++src) {
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700156 if (!__is_valid_environment_variable(src[0])) {
Elliott Hughes18a206c2012-10-29 17:37:13 -0700157 continue;
158 }
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700159 // Remove various unsafe environment variables if we're loading a setuid program.
160 if (get_AT_SECURE() && __is_unsafe_environment_variable(src[0])) {
161 continue;
162 }
Elliott Hughes18a206c2012-10-29 17:37:13 -0700163 dst[0] = src[0];
164 ++dst;
165 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700166 dst[0] = nullptr;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700167}
168
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800169void linker_env_init(KernelArgumentBlock& args) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700170 // Store environment pointer - can't be null.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800171 _envp = args.envp;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700172
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800173 __init_AT_SECURE(args);
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700174 __sanitize_environment_variables();
Elliott Hughes18a206c2012-10-29 17:37:13 -0700175}
176
Elliott Hughes18a206c2012-10-29 17:37:13 -0700177const char* linker_env_get(const char* name) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700178 if (name == nullptr || name[0] == '\0') {
179 return nullptr;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700180 }
181
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700182 for (char** p = _envp; p[0] != nullptr; ++p) {
Elliott Hughes0894b2c2012-11-02 12:40:11 -0700183 const char* val = env_match(p[0], name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700184 if (val != nullptr) {
Elliott Hughes18a206c2012-10-29 17:37:13 -0700185 if (val[0] == '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700186 return nullptr; // Return null for empty strings.
Elliott Hughes18a206c2012-10-29 17:37:13 -0700187 }
188 return val;
189 }
190 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700191 return nullptr;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700192}