blob: dc536b2d2dee229f268d5cec79dd5c6128489a40 [file] [log] [blame]
Kenny Root66269ea2011-07-12 14:14:01 -07001/*
2 * Copyright (C) 2011 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#define LOG_TAG "NativeLibraryHelper"
18//#define LOG_NDEBUG 0
19
Andreas Gampeed6b9df2014-11-20 22:02:20 -080020#include "core_jni_helpers.h"
Kenny Root66269ea2011-07-12 14:14:01 -070021
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/ScopedUtfChars.h>
Ramin Zaghi1378aba2014-02-28 15:03:19 +000023#include <androidfw/ZipFileRO.h>
24#include <androidfw/ZipUtils.h>
25#include <utils/Log.h>
26#include <utils/Vector.h>
Kenny Root66269ea2011-07-12 14:14:01 -070027
28#include <zlib.h>
29
Josh Gao5fc8bbe2018-07-17 15:00:31 -070030#include <errno.h>
Kenny Root66269ea2011-07-12 14:14:01 -070031#include <fcntl.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
Dmitriy Ivanovdec46882014-09-30 15:10:48 -070036#include <inttypes.h>
Kenny Root66269ea2011-07-12 14:14:01 -070037#include <sys/stat.h>
38#include <sys/types.h>
39
Elliott Hughes93bb56e2016-09-11 14:50:12 -070040#include <memory>
Kenny Root66269ea2011-07-12 14:14:01 -070041
42#define APK_LIB "lib/"
43#define APK_LIB_LEN (sizeof(APK_LIB) - 1)
44
45#define LIB_PREFIX "/lib"
46#define LIB_PREFIX_LEN (sizeof(LIB_PREFIX) - 1)
47
48#define LIB_SUFFIX ".so"
49#define LIB_SUFFIX_LEN (sizeof(LIB_SUFFIX) - 1)
50
Narayan Kamathd47e38b2014-05-16 16:45:56 +010051#define RS_BITCODE_SUFFIX ".bc"
Narayan Kamathd47e38b2014-05-16 16:45:56 +010052
Kenny Root66269ea2011-07-12 14:14:01 -070053#define TMP_FILE_PATTERN "/tmp.XXXXXX"
54#define TMP_FILE_PATTERN_LEN (sizeof(TMP_FILE_PATTERN) - 1)
55
56namespace android {
57
Kenny Root66269ea2011-07-12 14:14:01 -070058// These match PackageManager.java install codes
Ramin Zaghi1378aba2014-02-28 15:03:19 +000059enum install_status_t {
Kenny Root1ebd74a2011-08-03 15:09:44 -070060 INSTALL_SUCCEEDED = 1,
Kenny Root66269ea2011-07-12 14:14:01 -070061 INSTALL_FAILED_INVALID_APK = -2,
62 INSTALL_FAILED_INSUFFICIENT_STORAGE = -4,
Kenny Root1ebd74a2011-08-03 15:09:44 -070063 INSTALL_FAILED_CONTAINER_ERROR = -18,
64 INSTALL_FAILED_INTERNAL_ERROR = -110,
Narayan Kamathd11f2232014-04-10 10:37:17 +010065 INSTALL_FAILED_NO_MATCHING_ABIS = -113,
66 NO_NATIVE_LIBRARIES = -114
Ramin Zaghi1378aba2014-02-28 15:03:19 +000067};
Kenny Root66269ea2011-07-12 14:14:01 -070068
Kenny Root1ebd74a2011-08-03 15:09:44 -070069typedef install_status_t (*iterFunc)(JNIEnv*, void*, ZipFileRO*, ZipEntryRO, const char*);
70
Ramin Zaghi1378aba2014-02-28 15:03:19 +000071// Equivalent to android.os.FileUtils.isFilenameSafe
Kenny Root66269ea2011-07-12 14:14:01 -070072static bool
73isFilenameSafe(const char* filename)
74{
75 off_t offset = 0;
76 for (;;) {
77 switch (*(filename + offset)) {
78 case 0:
79 // Null.
80 // If we've reached the end, all the other characters are good.
81 return true;
82
83 case 'A' ... 'Z':
84 case 'a' ... 'z':
85 case '0' ... '9':
86 case '+':
87 case ',':
88 case '-':
89 case '.':
90 case '/':
91 case '=':
92 case '_':
93 offset++;
94 break;
95
96 default:
97 // We found something that is not good.
98 return false;
99 }
100 }
101 // Should not reach here.
102}
103
104static bool
Narayan Kamath407753c2015-06-16 12:02:57 +0100105isFileDifferent(const char* filePath, uint32_t fileSize, time_t modifiedTime,
106 uint32_t zipCrc, struct stat64* st)
Kenny Root66269ea2011-07-12 14:14:01 -0700107{
108 if (lstat64(filePath, st) < 0) {
109 // File is not found or cannot be read.
Steve Block71f2cf12011-10-20 11:56:00 +0100110 ALOGV("Couldn't stat %s, copying: %s\n", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700111 return true;
112 }
113
114 if (!S_ISREG(st->st_mode)) {
115 return true;
116 }
117
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800118 if (static_cast<uint64_t>(st->st_size) != static_cast<uint64_t>(fileSize)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700119 return true;
120 }
121
122 // For some reason, bionic doesn't define st_mtime as time_t
123 if (time_t(st->st_mtime) != modifiedTime) {
Steve Block71f2cf12011-10-20 11:56:00 +0100124 ALOGV("mod time doesn't match: %ld vs. %ld\n", st->st_mtime, modifiedTime);
Kenny Root66269ea2011-07-12 14:14:01 -0700125 return true;
126 }
127
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800128 int fd = TEMP_FAILURE_RETRY(open(filePath, O_RDONLY | O_CLOEXEC));
Kenny Root66269ea2011-07-12 14:14:01 -0700129 if (fd < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100130 ALOGV("Couldn't open file %s: %s", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700131 return true;
132 }
133
Narayan Kamath407753c2015-06-16 12:02:57 +0100134 // uLong comes from zlib.h. It's a bit of a wart that they're
135 // potentially using a 64-bit type for a 32-bit CRC.
136 uLong crc = crc32(0L, Z_NULL, 0);
Kenny Root66269ea2011-07-12 14:14:01 -0700137 unsigned char crcBuffer[16384];
138 ssize_t numBytes;
139 while ((numBytes = TEMP_FAILURE_RETRY(read(fd, crcBuffer, sizeof(crcBuffer)))) > 0) {
140 crc = crc32(crc, crcBuffer, numBytes);
141 }
142 close(fd);
143
Narayan Kamath407753c2015-06-16 12:02:57 +0100144 ALOGV("%s: crc = %lx, zipCrc = %" PRIu32 "\n", filePath, crc, zipCrc);
Kenny Root66269ea2011-07-12 14:14:01 -0700145
Narayan Kamath407753c2015-06-16 12:02:57 +0100146 if (crc != static_cast<uLong>(zipCrc)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700147 return true;
148 }
149
150 return false;
151}
152
Kenny Root1ebd74a2011-08-03 15:09:44 -0700153static install_status_t
Narayan Kamathafd31e02013-12-03 13:16:03 +0000154sumFiles(JNIEnv*, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char*)
Kenny Root66269ea2011-07-12 14:14:01 -0700155{
156 size_t* total = (size_t*) arg;
Narayan Kamath407753c2015-06-16 12:02:57 +0100157 uint32_t uncompLen;
Kenny Root66269ea2011-07-12 14:14:01 -0700158
159 if (!zipFile->getEntryInfo(zipEntry, NULL, &uncompLen, NULL, NULL, NULL, NULL)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700160 return INSTALL_FAILED_INVALID_APK;
Kenny Root66269ea2011-07-12 14:14:01 -0700161 }
162
Narayan Kamath407753c2015-06-16 12:02:57 +0100163 *total += static_cast<size_t>(uncompLen);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700164
165 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700166}
167
168/*
169 * Copy the native library if needed.
170 *
171 * This function assumes the library and path names passed in are considered safe.
172 */
Kenny Root1ebd74a2011-08-03 15:09:44 -0700173static install_status_t
Kenny Root66269ea2011-07-12 14:14:01 -0700174copyFileIfChanged(JNIEnv *env, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char* fileName)
175{
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700176 void** args = reinterpret_cast<void**>(arg);
177 jstring* javaNativeLibPath = (jstring*) args[0];
178 jboolean extractNativeLibs = *(jboolean*) args[1];
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700179
Kenny Root66269ea2011-07-12 14:14:01 -0700180 ScopedUtfChars nativeLibPath(env, *javaNativeLibPath);
181
Narayan Kamath407753c2015-06-16 12:02:57 +0100182 uint32_t uncompLen;
183 uint32_t when;
184 uint32_t crc;
Kenny Root66269ea2011-07-12 14:14:01 -0700185
Narayan Kamath407753c2015-06-16 12:02:57 +0100186 uint16_t method;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700187 off64_t offset;
188
189 if (!zipFile->getEntryInfo(zipEntry, &method, &uncompLen, NULL, &offset, &when, &crc)) {
Steve Block5baa3a62011-12-20 16:23:08 +0000190 ALOGD("Couldn't read zip entry info\n");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700191 return INSTALL_FAILED_INVALID_APK;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700192 }
193
194 if (!extractNativeLibs) {
195 // check if library is uncompressed and page-aligned
196 if (method != ZipFileRO::kCompressStored) {
197 ALOGD("Library '%s' is compressed - will not be able to open it directly from apk.\n",
198 fileName);
199 return INSTALL_FAILED_INVALID_APK;
200 }
201
202 if (offset % PAGE_SIZE != 0) {
203 ALOGD("Library '%s' is not page-aligned - will not be able to open it directly from"
204 " apk.\n", fileName);
205 return INSTALL_FAILED_INVALID_APK;
206 }
207
dimitry6e44c302018-10-04 15:57:14 +0200208 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700209 }
210
211 // Build local file path
212 const size_t fileNameLen = strlen(fileName);
213 char localFileName[nativeLibPath.size() + fileNameLen + 2];
214
215 if (strlcpy(localFileName, nativeLibPath.c_str(), sizeof(localFileName)) != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000216 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700217 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700218 }
219
220 *(localFileName + nativeLibPath.size()) = '/';
221
222 if (strlcpy(localFileName + nativeLibPath.size() + 1, fileName, sizeof(localFileName)
223 - nativeLibPath.size() - 1) != fileNameLen) {
Steve Block5baa3a62011-12-20 16:23:08 +0000224 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700225 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700226 }
227
228 // Only copy out the native file if it's different.
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700229 struct tm t;
230 ZipUtils::zipTimeToTimespec(when, &t);
Narayan Kamath407753c2015-06-16 12:02:57 +0100231 const time_t modTime = mktime(&t);
Elliott Hughesad076282014-01-17 18:27:27 -0800232 struct stat64 st;
Kenny Root66269ea2011-07-12 14:14:01 -0700233 if (!isFileDifferent(localFileName, uncompLen, modTime, crc, &st)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700234 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700235 }
236
Ivan Lozano62fc1482017-12-14 19:03:56 -0800237 char localTmpFileName[nativeLibPath.size() + TMP_FILE_PATTERN_LEN + 1];
Kenny Root66269ea2011-07-12 14:14:01 -0700238 if (strlcpy(localTmpFileName, nativeLibPath.c_str(), sizeof(localTmpFileName))
239 != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000240 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700241 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700242 }
243
Kenny Root66269ea2011-07-12 14:14:01 -0700244 if (strlcpy(localTmpFileName + nativeLibPath.size(), TMP_FILE_PATTERN,
Ivan Lozano62fc1482017-12-14 19:03:56 -0800245 TMP_FILE_PATTERN_LEN + 1) != TMP_FILE_PATTERN_LEN) {
Steve Block6215d3f2012-01-04 20:05:49 +0000246 ALOGI("Couldn't allocate temporary file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700247 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700248 }
249
250 int fd = mkstemp(localTmpFileName);
251 if (fd < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000252 ALOGI("Couldn't open temporary file name: %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root1ebd74a2011-08-03 15:09:44 -0700253 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700254 }
255
256 if (!zipFile->uncompressEntry(zipEntry, fd)) {
Steve Block6215d3f2012-01-04 20:05:49 +0000257 ALOGI("Failed uncompressing %s to %s\n", fileName, localTmpFileName);
Kenny Root66269ea2011-07-12 14:14:01 -0700258 close(fd);
259 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700260 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700261 }
262
263 close(fd);
264
265 // Set the modification time for this file to the ZIP's mod time.
266 struct timeval times[2];
267 times[0].tv_sec = st.st_atime;
268 times[1].tv_sec = modTime;
269 times[0].tv_usec = times[1].tv_usec = 0;
270 if (utimes(localTmpFileName, times) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000271 ALOGI("Couldn't change modification time on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700272 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700273 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700274 }
275
276 // Set the mode to 755
277 static const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
278 if (chmod(localTmpFileName, mode) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000279 ALOGI("Couldn't change permissions on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700280 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700281 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700282 }
283
284 // Finally, rename it to the final name.
285 if (rename(localTmpFileName, localFileName) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000286 ALOGI("Couldn't rename %s to %s: %s\n", localTmpFileName, localFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700287 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700288 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700289 }
290
Steve Block71f2cf12011-10-20 11:56:00 +0100291 ALOGV("Successfully moved %s to %s\n", localTmpFileName, localFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700292
293 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700294}
295
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000296/*
297 * An iterator over all shared libraries in a zip file. An entry is
298 * considered to be a shared library if all of the conditions below are
299 * satisfied :
300 *
301 * - The entry is under the lib/ directory.
302 * - The entry name ends with ".so" and the entry name starts with "lib",
303 * an exception is made for entries whose name is "gdbserver".
304 * - The entry filename is "safe" (as determined by isFilenameSafe).
305 *
306 */
307class NativeLibrariesIterator {
308private:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100309 NativeLibrariesIterator(ZipFileRO* zipFile, bool debuggable, void* cookie)
310 : mZipFile(zipFile), mDebuggable(debuggable), mCookie(cookie), mLastSlash(NULL) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000311 fileName[0] = '\0';
Kenny Root66269ea2011-07-12 14:14:01 -0700312 }
313
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000314public:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100315 static NativeLibrariesIterator* create(ZipFileRO* zipFile, bool debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000316 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700317 // Do not specify a suffix to find both .so files and gdbserver.
318 if (!zipFile->startIteration(&cookie, APK_LIB, NULL /* suffix */)) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000319 return NULL;
320 }
321
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100322 return new NativeLibrariesIterator(zipFile, debuggable, cookie);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000323 }
324
325 ZipEntryRO next() {
326 ZipEntryRO next = NULL;
327 while ((next = mZipFile->nextEntry(mCookie)) != NULL) {
328 // Make sure this entry has a filename.
329 if (mZipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
330 continue;
331 }
332
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000333 // Make sure the filename is at least to the minimum library name size.
334 const size_t fileNameLen = strlen(fileName);
335 static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN;
336 if (fileNameLen < minLength) {
337 continue;
338 }
339
340 const char* lastSlash = strrchr(fileName, '/');
341 ALOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName);
342
Andreas Gampec012ae32016-12-15 16:12:39 -0800343 // Skip directories.
344 if (*(lastSlash + 1) == 0) {
345 continue;
346 }
347
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000348 // Make sure the filename is safe.
349 if (!isFilenameSafe(lastSlash + 1)) {
350 continue;
351 }
352
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100353 if (!mDebuggable) {
354 // Make sure the filename starts with lib and ends with ".so".
355 if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN)
356 || strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)) {
357 continue;
358 }
359 }
360
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000361 mLastSlash = lastSlash;
362 break;
363 }
364
365 return next;
366 }
367
368 inline const char* currentEntry() const {
369 return fileName;
370 }
371
372 inline const char* lastSlash() const {
373 return mLastSlash;
374 }
375
376 virtual ~NativeLibrariesIterator() {
377 mZipFile->endIteration(mCookie);
378 }
379private:
380
Kenny Root66269ea2011-07-12 14:14:01 -0700381 char fileName[PATH_MAX];
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000382 ZipFileRO* const mZipFile;
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100383 const bool mDebuggable;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000384 void* mCookie;
385 const char* mLastSlash;
386};
Kenny Root66269ea2011-07-12 14:14:01 -0700387
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000388static install_status_t
389iterateOverNativeFiles(JNIEnv *env, jlong apkHandle, jstring javaCpuAbi,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100390 jboolean debuggable, iterFunc callFunc, void* callArg) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000391 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
392 if (zipFile == NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000393 return INSTALL_FAILED_INVALID_APK;
394 }
Kenny Root66269ea2011-07-12 14:14:01 -0700395
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100396 std::unique_ptr<NativeLibrariesIterator> it(
397 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000398 if (it.get() == NULL) {
399 return INSTALL_FAILED_INVALID_APK;
400 }
401
402 const ScopedUtfChars cpuAbi(env, javaCpuAbi);
403 if (cpuAbi.c_str() == NULL) {
404 // This would've thrown, so this return code isn't observable by
405 // Java.
406 return INSTALL_FAILED_INVALID_APK;
407 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000408 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000409 while ((entry = it->next()) != NULL) {
410 const char* fileName = it->currentEntry();
411 const char* lastSlash = it->lastSlash();
Kenny Root66269ea2011-07-12 14:14:01 -0700412
413 // Check to make sure the CPU ABI of this file is one we support.
414 const char* cpuAbiOffset = fileName + APK_LIB_LEN;
415 const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset;
416
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000417 if (cpuAbi.size() == cpuAbiRegionSize && !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) {
418 install_status_t ret = callFunc(env, callArg, zipFile, entry, lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700419
420 if (ret != INSTALL_SUCCEEDED) {
Steve Block71f2cf12011-10-20 11:56:00 +0100421 ALOGV("Failure for entry %s", lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700422 return ret;
423 }
Kenny Root66269ea2011-07-12 14:14:01 -0700424 }
425 }
426
427 return INSTALL_SUCCEEDED;
428}
429
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000430
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100431static int findSupportedAbi(JNIEnv *env, jlong apkHandle, jobjectArray supportedAbisArray,
432 jboolean debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000433 const int numAbis = env->GetArrayLength(supportedAbisArray);
434 Vector<ScopedUtfChars*> supportedAbis;
435
436 for (int i = 0; i < numAbis; ++i) {
437 supportedAbis.add(new ScopedUtfChars(env,
438 (jstring) env->GetObjectArrayElement(supportedAbisArray, i)));
439 }
440
441 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
442 if (zipFile == NULL) {
443 return INSTALL_FAILED_INVALID_APK;
444 }
445
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100446 std::unique_ptr<NativeLibrariesIterator> it(
447 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000448 if (it.get() == NULL) {
449 return INSTALL_FAILED_INVALID_APK;
450 }
451
452 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000453 int status = NO_NATIVE_LIBRARIES;
454 while ((entry = it->next()) != NULL) {
455 // We're currently in the lib/ directory of the APK, so it does have some native
456 // code. We should return INSTALL_FAILED_NO_MATCHING_ABIS if none of the
457 // libraries match.
458 if (status == NO_NATIVE_LIBRARIES) {
459 status = INSTALL_FAILED_NO_MATCHING_ABIS;
460 }
461
462 const char* fileName = it->currentEntry();
463 const char* lastSlash = it->lastSlash();
464
465 // Check to see if this CPU ABI matches what we are looking for.
466 const char* abiOffset = fileName + APK_LIB_LEN;
467 const size_t abiSize = lastSlash - abiOffset;
468 for (int i = 0; i < numAbis; i++) {
469 const ScopedUtfChars* abi = supportedAbis[i];
470 if (abi->size() == abiSize && !strncmp(abiOffset, abi->c_str(), abiSize)) {
471 // The entry that comes in first (i.e. with a lower index) has the higher priority.
472 if (((i < status) && (status >= 0)) || (status < 0) ) {
473 status = i;
474 }
475 }
476 }
477 }
478
479 for (int i = 0; i < numAbis; ++i) {
480 delete supportedAbis[i];
481 }
482
483 return status;
484}
485
Kenny Root66269ea2011-07-12 14:14:01 -0700486static jint
487com_android_internal_content_NativeLibraryHelper_copyNativeBinaries(JNIEnv *env, jclass clazz,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700488 jlong apkHandle, jstring javaNativeLibPath, jstring javaCpuAbi,
dimitry6e44c302018-10-04 15:57:14 +0200489 jboolean extractNativeLibs, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700490{
dimitry6e44c302018-10-04 15:57:14 +0200491 void* args[] = { &javaNativeLibPath, &extractNativeLibs };
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100492 return (jint) iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700493 copyFileIfChanged, reinterpret_cast<void*>(args));
Kenny Root66269ea2011-07-12 14:14:01 -0700494}
495
496static jlong
497com_android_internal_content_NativeLibraryHelper_sumNativeBinaries(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100498 jlong apkHandle, jstring javaCpuAbi, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700499{
500 size_t totalSize = 0;
501
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100502 iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable, sumFiles, &totalSize);
Kenny Root66269ea2011-07-12 14:14:01 -0700503
504 return totalSize;
505}
506
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000507static jint
508com_android_internal_content_NativeLibraryHelper_findSupportedAbi(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100509 jlong apkHandle, jobjectArray javaCpuAbisToSearch, jboolean debuggable)
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000510{
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100511 return (jint) findSupportedAbi(env, apkHandle, javaCpuAbisToSearch, debuggable);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000512}
513
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100514enum bitcode_scan_result_t {
515 APK_SCAN_ERROR = -1,
516 NO_BITCODE_PRESENT = 0,
517 BITCODE_PRESENT = 1,
518};
519
520static jint
521com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode(JNIEnv *env, jclass clazz,
522 jlong apkHandle) {
523 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
524 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700525 if (!zipFile->startIteration(&cookie, NULL /* prefix */, RS_BITCODE_SUFFIX)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100526 return APK_SCAN_ERROR;
527 }
528
529 char fileName[PATH_MAX];
530 ZipEntryRO next = NULL;
531 while ((next = zipFile->nextEntry(cookie)) != NULL) {
532 if (zipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
533 continue;
534 }
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100535 const char* lastSlash = strrchr(fileName, '/');
536 const char* baseName = (lastSlash == NULL) ? fileName : fileName + 1;
Yusuke Sato957c2372015-08-03 16:17:16 -0700537 if (isFilenameSafe(baseName)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100538 zipFile->endIteration(cookie);
539 return BITCODE_PRESENT;
540 }
541 }
542
543 zipFile->endIteration(cookie);
544 return NO_BITCODE_PRESENT;
545}
546
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000547static jlong
548com_android_internal_content_NativeLibraryHelper_openApk(JNIEnv *env, jclass, jstring apkPath)
549{
550 ScopedUtfChars filePath(env, apkPath);
551 ZipFileRO* zipFile = ZipFileRO::open(filePath.c_str());
552
553 return reinterpret_cast<jlong>(zipFile);
554}
555
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000556static jlong
557com_android_internal_content_NativeLibraryHelper_openApkFd(JNIEnv *env, jclass,
558 jobject fileDescriptor, jstring debugPathName)
559{
560 ScopedUtfChars debugFilePath(env, debugPathName);
561
562 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
563 if (fd < 0) {
564 jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor");
565 return 0;
566 }
567
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800568 int dupedFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Josh Gao5fc8bbe2018-07-17 15:00:31 -0700569 if (dupedFd == -1) {
570 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
571 "Failed to dup FileDescriptor: %s", strerror(errno));
572 return 0;
573 }
574
575 ZipFileRO* zipFile = ZipFileRO::openFd(dupedFd, debugFilePath.c_str());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000576
577 return reinterpret_cast<jlong>(zipFile);
578}
579
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000580static void
581com_android_internal_content_NativeLibraryHelper_close(JNIEnv *env, jclass, jlong apkHandle)
582{
583 delete reinterpret_cast<ZipFileRO*>(apkHandle);
584}
585
Daniel Micay76f6a862015-09-19 17:31:01 -0400586static const JNINativeMethod gMethods[] = {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000587 {"nativeOpenApk",
588 "(Ljava/lang/String;)J",
589 (void *)com_android_internal_content_NativeLibraryHelper_openApk},
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000590 {"nativeOpenApkFd",
591 "(Ljava/io/FileDescriptor;Ljava/lang/String;)J",
592 (void *)com_android_internal_content_NativeLibraryHelper_openApkFd},
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000593 {"nativeClose",
594 "(J)V",
595 (void *)com_android_internal_content_NativeLibraryHelper_close},
Kenny Root66269ea2011-07-12 14:14:01 -0700596 {"nativeCopyNativeBinaries",
dimitry6e44c302018-10-04 15:57:14 +0200597 "(JLjava/lang/String;Ljava/lang/String;ZZ)I",
Kenny Root66269ea2011-07-12 14:14:01 -0700598 (void *)com_android_internal_content_NativeLibraryHelper_copyNativeBinaries},
599 {"nativeSumNativeBinaries",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100600 "(JLjava/lang/String;Z)J",
Kenny Root66269ea2011-07-12 14:14:01 -0700601 (void *)com_android_internal_content_NativeLibraryHelper_sumNativeBinaries},
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000602 {"nativeFindSupportedAbi",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100603 "(J[Ljava/lang/String;Z)I",
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000604 (void *)com_android_internal_content_NativeLibraryHelper_findSupportedAbi},
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100605 {"hasRenderscriptBitcode", "(J)I",
606 (void *)com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode},
Kenny Root66269ea2011-07-12 14:14:01 -0700607};
608
609
610int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env)
611{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800612 return RegisterMethodsOrDie(env,
613 "com/android/internal/content/NativeLibraryHelper", gMethods, NELEM(gMethods));
Kenny Root66269ea2011-07-12 14:14:01 -0700614}
615
616};