1 /*
2  * Copyright (C) 2020 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 "VoldNativeServiceValidation.h"
18 
19 #include <android-base/stringprintf.h>
20 #include <android-base/strings.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <private/android_filesystem_config.h>
24 
25 #include <cctype>
26 #include <string_view>
27 
28 using android::base::StringPrintf;
29 using namespace std::literals;
30 
31 namespace android::vold {
32 
Ok()33 binder::Status Ok() {
34     return binder::Status::ok();
35 }
36 
Exception(uint32_t code,const std::string & msg)37 binder::Status Exception(uint32_t code, const std::string& msg) {
38     return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
39 }
40 
CheckPermission(const char * permission)41 binder::Status CheckPermission(const char* permission) {
42     int32_t pid;
43     int32_t uid;
44 
45     if (checkCallingPermission(String16(permission), &pid, &uid)) {
46         return Ok();
47     } else {
48         return Exception(binder::Status::EX_SECURITY,
49                          StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
50     }
51 }
52 
CheckUidOrRoot(uid_t expectedUid)53 binder::Status CheckUidOrRoot(uid_t expectedUid) {
54     uid_t uid = IPCThreadState::self()->getCallingUid();
55     if (uid == expectedUid || uid == AID_ROOT) {
56         return Ok();
57     } else {
58         return Exception(binder::Status::EX_SECURITY,
59                          StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
60     }
61 }
62 
CheckArgumentId(const std::string & id)63 binder::Status CheckArgumentId(const std::string& id) {
64     if (id.empty()) {
65         return Exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID");
66     }
67     for (const char& c : id) {
68         if (!std::isalnum(c) && c != ':' && c != ',' && c != ';') {
69             return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
70                              StringPrintf("ID %s is malformed", id.c_str()));
71         }
72     }
73     return Ok();
74 }
75 
CheckArgumentPath(const std::string & path)76 binder::Status CheckArgumentPath(const std::string& path) {
77     if (path.empty()) {
78         return Exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path");
79     }
80     if (path[0] != '/') {
81         return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
82                          StringPrintf("Path %s is relative", path.c_str()));
83     }
84     if (path.find("/../"sv) != path.npos || android::base::EndsWith(path, "/.."sv)) {
85         return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
86                          StringPrintf("Path %s is shady", path.c_str()));
87     }
88     for (const char& c : path) {
89         if (c == '\0' || c == '\n') {
90             return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
91                              StringPrintf("Path %s is malformed", path.c_str()));
92         }
93     }
94     return Ok();
95 }
96 
CheckArgumentHex(const std::string & hex)97 binder::Status CheckArgumentHex(const std::string& hex) {
98     // Empty hex strings are allowed
99     for (const char& c : hex) {
100         if (!std::isxdigit(c) && c != ':' && c != '-') {
101             return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
102                              StringPrintf("Hex %s is malformed", hex.c_str()));
103         }
104     }
105     return Ok();
106 }
107 
108 }  // namespace android::vold
109