• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
ValidLibraryPathLastSlash(const char * fileName,bool suppress64Bit)36 const char* ValidLibraryPathLastSlash(const char* fileName, bool suppress64Bit) {
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 file starts with 'lib/' prefix.
60     if (strncmp(fileName, APK_LIB.data(), APK_LIB_LEN) != 0) {
61         return nullptr;
62     }
63 
64     // 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) {
66         return nullptr;
67     }
68 
69     // 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 
isFilenameSafe(const char * filename)78 bool 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 }