blob: 7f71f6312f00d167d01c5066665ee9f76ff8f018 [file] [log] [blame]
Gustav Senntoncd165a92017-06-21 18:04:42 +01001/*
2 * Copyright (C) 2014 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// Uncomment for verbose logging.
18// #define LOG_NDEBUG 0
19#define LOG_TAG "webviewchromiumloader"
20
21#include <dlfcn.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <sys/mman.h>
Richard Uhlerfdd7a7e2018-09-14 09:39:28 +010029#include <sys/prctl.h>
Gustav Senntoncd165a92017-06-21 18:04:42 +010030#include <sys/stat.h>
31#include <sys/types.h>
32
33#include <jni.h>
34#include <android/dlext.h>
35#include <nativeloader/native_loader.h>
36#include <utils/Log.h>
37
38#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
39
40namespace android {
41namespace {
42
43void* gReservedAddress = NULL;
44size_t gReservedSize = 0;
45
46jint LIBLOAD_SUCCESS;
47jint LIBLOAD_FAILED_TO_OPEN_RELRO_FILE;
48jint LIBLOAD_FAILED_TO_LOAD_LIBRARY;
49jint LIBLOAD_FAILED_JNI_CALL;
50jint LIBLOAD_FAILED_TO_FIND_NAMESPACE;
51
52jboolean DoReserveAddressSpace(jlong size) {
53 size_t vsize = static_cast<size_t>(size);
54
55 void* addr = mmap(NULL, vsize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56 if (addr == MAP_FAILED) {
57 ALOGE("Failed to reserve %zd bytes of address space for future load of "
58 "libwebviewchromium.so: %s",
59 vsize, strerror(errno));
60 return JNI_FALSE;
61 }
Richard Uhlerfdd7a7e2018-09-14 09:39:28 +010062 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, vsize, "libwebview reservation");
Gustav Senntoncd165a92017-06-21 18:04:42 +010063 gReservedAddress = addr;
64 gReservedSize = vsize;
65 ALOGV("Reserved %zd bytes at %p", vsize, addr);
66 return JNI_TRUE;
67}
68
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -040069jboolean DoCreateRelroFile(JNIEnv* env, const char* lib, const char* relro,
70 jobject clazzLoader) {
Gustav Senntoncd165a92017-06-21 18:04:42 +010071 // Try to unlink the old file, since if this is being called, the old one is
72 // obsolete.
73 if (unlink(relro) != 0 && errno != ENOENT) {
74 // If something went wrong other than the file not existing, log a warning
75 // but continue anyway in the hope that we can successfully overwrite the
76 // existing file with rename() later.
77 ALOGW("Failed to unlink old file %s: %s", relro, strerror(errno));
78 }
79 static const char tmpsuffix[] = ".XXXXXX";
80 char relro_tmp[strlen(relro) + sizeof(tmpsuffix)];
81 strlcpy(relro_tmp, relro, sizeof(relro_tmp));
82 strlcat(relro_tmp, tmpsuffix, sizeof(relro_tmp));
83 int tmp_fd = TEMP_FAILURE_RETRY(mkstemp(relro_tmp));
84 if (tmp_fd == -1) {
85 ALOGE("Failed to create temporary file %s: %s", relro_tmp, strerror(errno));
86 return JNI_FALSE;
87 }
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -040088 android_namespace_t* ns =
89 android::FindNamespaceByClassLoader(env, clazzLoader);
90 if (ns == NULL) {
91 ALOGE("Failed to find classloader namespace");
92 return JNI_FALSE;
93 }
Gustav Senntoncd165a92017-06-21 18:04:42 +010094 android_dlextinfo extinfo;
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -040095 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_WRITE_RELRO |
96 ANDROID_DLEXT_USE_NAMESPACE;
Gustav Senntoncd165a92017-06-21 18:04:42 +010097 extinfo.reserved_addr = gReservedAddress;
98 extinfo.reserved_size = gReservedSize;
99 extinfo.relro_fd = tmp_fd;
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -0400100 extinfo.library_namespace = ns;
Gustav Senntoncd165a92017-06-21 18:04:42 +0100101 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo);
102 int close_result = close(tmp_fd);
103 if (handle == NULL) {
104 ALOGE("Failed to load library %s: %s", lib, dlerror());
105 unlink(relro_tmp);
106 return JNI_FALSE;
107 }
108 if (close_result != 0 ||
109 chmod(relro_tmp, S_IRUSR | S_IRGRP | S_IROTH) != 0 ||
110 rename(relro_tmp, relro) != 0) {
111 ALOGE("Failed to update relro file %s: %s", relro, strerror(errno));
112 unlink(relro_tmp);
113 return JNI_FALSE;
114 }
115 ALOGV("Created relro file %s for library %s", relro, lib);
116 return JNI_TRUE;
117}
118
119jint DoLoadWithRelroFile(JNIEnv* env, const char* lib, const char* relro,
120 jobject clazzLoader) {
121 int relro_fd = TEMP_FAILURE_RETRY(open(relro, O_RDONLY));
122 if (relro_fd == -1) {
123 ALOGE("Failed to open relro file %s: %s", relro, strerror(errno));
124 return LIBLOAD_FAILED_TO_OPEN_RELRO_FILE;
125 }
126 android_namespace_t* ns =
127 android::FindNamespaceByClassLoader(env, clazzLoader);
128 if (ns == NULL) {
129 ALOGE("Failed to find classloader namespace");
130 return LIBLOAD_FAILED_TO_FIND_NAMESPACE;
131 }
132 android_dlextinfo extinfo;
133 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_USE_RELRO |
134 ANDROID_DLEXT_USE_NAMESPACE;
135 extinfo.reserved_addr = gReservedAddress;
136 extinfo.reserved_size = gReservedSize;
137 extinfo.relro_fd = relro_fd;
138 extinfo.library_namespace = ns;
139 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo);
140 close(relro_fd);
141 if (handle == NULL) {
142 ALOGE("Failed to load library %s: %s", lib, dlerror());
143 return LIBLOAD_FAILED_TO_LOAD_LIBRARY;
144 }
145 ALOGV("Loaded library %s with relro file %s", lib, relro);
146 return LIBLOAD_SUCCESS;
147}
148
149/******************************************************************************/
150/* JNI wrappers - handle string lifetimes and 32/64 ABI choice */
151/******************************************************************************/
152
153jboolean ReserveAddressSpace(JNIEnv*, jclass, jlong size) {
154 return DoReserveAddressSpace(size);
155}
156
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -0400157jboolean CreateRelroFile(JNIEnv* env, jclass, jstring lib, jstring relro,
158 jobject clazzLoader) {
Gustav Senntoncd165a92017-06-21 18:04:42 +0100159 jboolean ret = JNI_FALSE;
160 const char* lib_utf8 = env->GetStringUTFChars(lib, NULL);
161 if (lib_utf8 != NULL) {
162 const char* relro_utf8 = env->GetStringUTFChars(relro, NULL);
163 if (relro_utf8 != NULL) {
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -0400164 ret = DoCreateRelroFile(env, lib_utf8, relro_utf8, clazzLoader);
Gustav Senntoncd165a92017-06-21 18:04:42 +0100165 env->ReleaseStringUTFChars(relro, relro_utf8);
166 }
167 env->ReleaseStringUTFChars(lib, lib_utf8);
168 }
169 return ret;
170}
171
Gustav Senntonae498f22017-10-05 15:34:13 +0100172jint LoadWithRelroFile(JNIEnv* env, jclass, jstring lib, jstring relro,
173 jobject clazzLoader) {
Gustav Senntoncd165a92017-06-21 18:04:42 +0100174 jint ret = LIBLOAD_FAILED_JNI_CALL;
175 const char* lib_utf8 = env->GetStringUTFChars(lib, NULL);
176 if (lib_utf8 != NULL) {
177 const char* relro_utf8 = env->GetStringUTFChars(relro, NULL);
178 if (relro_utf8 != NULL) {
179 ret = DoLoadWithRelroFile(env, lib_utf8, relro_utf8, clazzLoader);
180 env->ReleaseStringUTFChars(relro, relro_utf8);
181 }
182 env->ReleaseStringUTFChars(lib, lib_utf8);
183 }
184 return ret;
185}
186
187const char kWebViewFactoryClassName[] = "android/webkit/WebViewFactory";
188const char kWebViewLibraryLoaderClassName[] =
189 "android/webkit/WebViewLibraryLoader";
190const JNINativeMethod kJniMethods[] = {
191 { "nativeReserveAddressSpace", "(J)Z",
192 reinterpret_cast<void*>(ReserveAddressSpace) },
193 { "nativeCreateRelroFile",
Torne (Richard Coles)5abeed52018-08-06 17:08:53 -0400194 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)Z",
Gustav Senntoncd165a92017-06-21 18:04:42 +0100195 reinterpret_cast<void*>(CreateRelroFile) },
196 { "nativeLoadWithRelroFile",
Gustav Senntonae498f22017-10-05 15:34:13 +0100197 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)I",
Gustav Senntoncd165a92017-06-21 18:04:42 +0100198 reinterpret_cast<void*>(LoadWithRelroFile) },
199};
200
201} // namespace
202
203void RegisterWebViewFactory(JNIEnv* env) {
204 // If either of these fail, it will set an exception that will be thrown on
205 // return, so no need to handle errors here.
206 jclass clazz = env->FindClass(kWebViewFactoryClassName);
207 if (clazz) {
208 LIBLOAD_SUCCESS = env->GetStaticIntField(
209 clazz,
210 env->GetStaticFieldID(clazz, "LIBLOAD_SUCCESS", "I"));
211
212 LIBLOAD_FAILED_TO_OPEN_RELRO_FILE = env->GetStaticIntField(
213 clazz,
214 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_OPEN_RELRO_FILE", "I"));
215
216 LIBLOAD_FAILED_TO_LOAD_LIBRARY = env->GetStaticIntField(
217 clazz,
218 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_LOAD_LIBRARY", "I"));
219
220 LIBLOAD_FAILED_JNI_CALL = env->GetStaticIntField(
221 clazz,
222 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_JNI_CALL", "I"));
223
224 LIBLOAD_FAILED_TO_FIND_NAMESPACE = env->GetStaticIntField(
225 clazz,
226 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_FIND_NAMESPACE", "I"));
227 }
228}
229
230void RegisterWebViewLibraryLoader(JNIEnv* env) {
231 // If either of these fail, it will set an exception that will be thrown on
232 // return, so no need to handle errors here.
233 jclass clazz = env->FindClass(kWebViewLibraryLoaderClassName);
234 if (clazz) {
235 env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
236 }
237}
238
239} // namespace android
240
241JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
242 JNIEnv* env = NULL;
243 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
244 ALOGE("GetEnv failed");
245 return JNI_ERR;
246 }
247 android::RegisterWebViewFactory(env);
248 // Ensure there isn't a pending Java exception before registering methods from
249 // WebViewLibraryLoader
250 if (!env->ExceptionCheck()) {
251 android::RegisterWebViewLibraryLoader(env);
252 }
253 return JNI_VERSION_1_6;
254}