blob: b80c8755f23e983cda36d825184f520d6c349760 [file] [log] [blame]
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +00001/*
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
24const std::string_view APK_LIB = "lib/";
25const size_t APK_LIB_LEN = APK_LIB.size();
26
27const std::string_view LIB_PREFIX = "/lib";
28const size_t LIB_PREFIX_LEN = LIB_PREFIX.size();
29
30const std::string_view LIB_SUFFIX = ".so";
31const size_t LIB_SUFFIX_LEN = LIB_SUFFIX.size();
32
33static const std::array<std::string_view, 2> abis = {"arm64-v8a", "x86_64"};
34
35namespace android::util {
Elliott Hughesac794472025-01-13 13:38:52 -080036const char* ValidLibraryPathLastSlash(const char* fileName, bool suppress64Bit) {
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +000037 // 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
Iurii Makhnoa30d8ba2023-02-27 16:27:26 +000059 // Make sure file starts with 'lib/' prefix.
60 if (strncmp(fileName, APK_LIB.data(), APK_LIB_LEN) != 0) {
61 return nullptr;
62 }
63
Jeremy Meyer12312e02023-02-14 00:21:19 +000064 // Make sure there aren't subdirectories by checking if the next / after lib/ is the last slash
65 if (memchr(fileName + APK_LIB_LEN, '/', fileNameLen - APK_LIB_LEN) != lastSlash) {
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +000066 return nullptr;
67 }
68
Jeremy Meyer5fd34ea2022-12-15 18:43:36 +000069 // Don't include 64 bit versions if they are suppressed
70 if (suppress64Bit && std::find(abis.begin(), abis.end(), std::string_view(
71 fileName + APK_LIB_LEN, lastSlash - fileName - APK_LIB_LEN)) != abis.end()) {
72 return nullptr;
73 }
74
75 return lastSlash;
76}
77
78bool isFilenameSafe(const char* filename) {
79 off_t offset = 0;
80 for (;;) {
81 switch (*(filename + offset)) {
82 case 0:
83 // Null.
84 // If we've reached the end, all the other characters are good.
85 return true;
86
87 case 'A' ... 'Z':
88 case 'a' ... 'z':
89 case '0' ... '9':
90 case '+':
91 case ',':
92 case '-':
93 case '.':
94 case '/':
95 case '=':
96 case '_':
97 offset++;
98 break;
99
100 default:
101 // We found something that is not good.
102 return false;
103 }
104 }
105 // Should not reach here.
106}
107}