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,bool debuggable)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 by checking if the next / after lib/ is the last slash
60     if (memchr(fileName + APK_LIB_LEN, '/', fileNameLen - APK_LIB_LEN) != lastSlash) {
61         return nullptr;
62     }
63 
64     if (!debuggable) {
65         // Make sure the filename starts with lib and ends with ".so".
66         if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX.data(), LIB_SUFFIX_LEN) != 0
67             || strncmp(lastSlash, LIB_PREFIX.data(), LIB_PREFIX_LEN) != 0) {
68             return nullptr;
69         }
70     }
71 
72     // Don't include 64 bit versions if they are suppressed
73     if (suppress64Bit && std::find(abis.begin(), abis.end(), std::string_view(
74         fileName + APK_LIB_LEN, lastSlash - fileName - APK_LIB_LEN)) != abis.end()) {
75       return nullptr;
76     }
77 
78     return lastSlash;
79 }
80 
isFilenameSafe(const char * filename)81 bool isFilenameSafe(const char* filename) {
82     off_t offset = 0;
83     for (;;) {
84         switch (*(filename + offset)) {
85         case 0:
86             // Null.
87             // If we've reached the end, all the other characters are good.
88             return true;
89 
90         case 'A' ... 'Z':
91         case 'a' ... 'z':
92         case '0' ... '9':
93         case '+':
94         case ',':
95         case '-':
96         case '.':
97         case '/':
98         case '=':
99         case '_':
100             offset++;
101             break;
102 
103         default:
104             // We found something that is not good.
105             return false;
106         }
107     }
108     // Should not reach here.
109 }
110 }