1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "berberis/kernel_api/exec_emulation.h"
18
19 #include <unistd.h>
20
21 #include <cstring>
22
23 #include "berberis/base/mmap.h"
24 #include "berberis/base/strings.h"
25
26 namespace berberis {
27
28 namespace {
29
IsPlatformVar(const char * s)30 bool IsPlatformVar(const char* s) {
31 return StartsWith(s, "LD_CONFIG_FILE=") || StartsWith(s, "LD_LIBRARY_PATH=") ||
32 StartsWith(s, "LD_DEBUG=") || StartsWith(s, "LD_PRELOAD=");
33 }
34
MangleGuestEnvp(ScopedMmap * dst,char * const * envp)35 char* const* MangleGuestEnvp(ScopedMmap* dst, char* const* envp) {
36 if (envp == nullptr) {
37 return nullptr;
38 }
39
40 int env_count = 0;
41 int text_size = 0;
42 int mangle_count = 0;
43
44 for (;; ++env_count) {
45 char* env = envp[env_count];
46 if (env == nullptr) {
47 break;
48 }
49
50 if (IsPlatformVar(env)) {
51 ++mangle_count;
52 }
53
54 text_size += strlen(env) + 1; // count terminating '\0'
55 }
56
57 if (mangle_count == 0) {
58 return envp;
59 }
60
61 auto [guest_prefix, guest_prefix_size] = GetGuestPlatformVarPrefixWithSize();
62
63 size_t array_size = sizeof(char*) * (env_count + 1); // text pointers + terminating nullptr
64 dst->Init(array_size + text_size + // array + orig text
65 guest_prefix_size * mangle_count); // added prefixes
66
67 char** new_array = static_cast<char**>(dst->data());
68 char* new_text = static_cast<char*>(dst->data()) + array_size;
69
70 for (int i = 0; i < env_count; ++i) {
71 char* env = envp[i];
72 new_array[i] = new_text;
73
74 if (IsPlatformVar(env)) {
75 strcpy(new_text, guest_prefix);
76 new_text += guest_prefix_size;
77 }
78
79 strcpy(new_text, env);
80 new_text += strlen(env) + 1; // count terminating '\0'
81 }
82
83 new_array[env_count] = nullptr; // add terminating nullptr
84
85 return new_array;
86 }
87
88 } // namespace
89
DemangleGuestEnvp(char ** dst,char ** envp)90 char** DemangleGuestEnvp(char** dst, char** envp) {
91 auto [guest_prefix, guest_prefix_size] = GetGuestPlatformVarPrefixWithSize();
92
93 for (; *envp; ++envp) {
94 char* env = *envp;
95 if (IsPlatformVar(env)) {
96 continue;
97 }
98 if (StartsWith(env, guest_prefix) && IsPlatformVar(env + guest_prefix_size)) {
99 env += guest_prefix_size;
100 }
101 *dst++ = env;
102 }
103
104 *dst++ = nullptr;
105 return dst;
106 }
107
ExecveForGuest(const char * filename,char * const argv[],char * const envp[])108 int ExecveForGuest(const char* filename, char* const argv[], char* const envp[]) {
109 ScopedMmap new_envp;
110 return execve(filename, argv, MangleGuestEnvp(&new_envp, envp));
111 }
112
113 } // namespace berberis
114