Jeremy Meyer | 5fd34ea | 2022-12-15 18:43:36 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "androidfw/ApkParsing.h" |
| 18 | #include <algorithm> |
| 19 | #include <array> |
| 20 | #include <stdlib.h> |
| 21 | #include <string_view> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | const std::string_view APK_LIB = "lib/"; |
| 25 | const size_t APK_LIB_LEN = APK_LIB.size(); |
| 26 | |
| 27 | const std::string_view LIB_PREFIX = "/lib"; |
| 28 | const size_t LIB_PREFIX_LEN = LIB_PREFIX.size(); |
| 29 | |
| 30 | const std::string_view LIB_SUFFIX = ".so"; |
| 31 | const size_t LIB_SUFFIX_LEN = LIB_SUFFIX.size(); |
| 32 | |
| 33 | static const std::array<std::string_view, 2> abis = {"arm64-v8a", "x86_64"}; |
| 34 | |
| 35 | namespace android::util { |
| 36 | const char* ValidLibraryPathLastSlash(const char* fileName, bool suppress64Bit, bool debuggable) { |
| 37 | // Make sure the filename is at least to the minimum library name size. |
| 38 | const size_t fileNameLen = strlen(fileName); |
| 39 | static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN; |
| 40 | if (fileNameLen < minLength) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | const char* lastSlash = strrchr(fileName, '/'); |
| 45 | if (!lastSlash) { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | // Skip directories. |
| 50 | if (*(lastSlash + 1) == 0) { |
| 51 | return nullptr; |
| 52 | } |
| 53 | |
| 54 | // Make sure the filename is safe. |
| 55 | if (!isFilenameSafe(lastSlash + 1)) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | // Make sure there aren't subdirectories |
| 60 | const char* abiOffset = fileName + APK_LIB_LEN; |
| 61 | const size_t abiSize = lastSlash - abiOffset; |
| 62 | if (memchr(abiOffset, '/', abiSize)) { |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | if (!debuggable) { |
| 67 | // Make sure the filename starts with lib and ends with ".so". |
| 68 | if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX.data(), LIB_SUFFIX_LEN) != 0 |
| 69 | || strncmp(lastSlash, LIB_PREFIX.data(), LIB_PREFIX_LEN) != 0) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Don't include 64 bit versions if they are suppressed |
| 75 | if (suppress64Bit && std::find(abis.begin(), abis.end(), std::string_view( |
| 76 | fileName + APK_LIB_LEN, lastSlash - fileName - APK_LIB_LEN)) != abis.end()) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | return lastSlash; |
| 81 | } |
| 82 | |
| 83 | bool isFilenameSafe(const char* filename) { |
| 84 | off_t offset = 0; |
| 85 | for (;;) { |
| 86 | switch (*(filename + offset)) { |
| 87 | case 0: |
| 88 | // Null. |
| 89 | // If we've reached the end, all the other characters are good. |
| 90 | return true; |
| 91 | |
| 92 | case 'A' ... 'Z': |
| 93 | case 'a' ... 'z': |
| 94 | case '0' ... '9': |
| 95 | case '+': |
| 96 | case ',': |
| 97 | case '-': |
| 98 | case '.': |
| 99 | case '/': |
| 100 | case '=': |
| 101 | case '_': |
| 102 | offset++; |
| 103 | break; |
| 104 | |
| 105 | default: |
| 106 | // We found something that is not good. |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | // Should not reach here. |
| 111 | } |
| 112 | } |