blob: 1d1914177b56cf71489bd57303a16a188ee227a3 [file] [log] [blame]
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001/*
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
28using android::base::StringPrintf;
29using namespace std::literals;
30
31namespace android::vold {
32
33binder::Status Ok() {
34 return binder::Status::ok();
35}
36
37binder::Status Exception(uint32_t code, const std::string& msg) {
38 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
39}
40
41binder::Status CheckPermission(const char* permission) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -070042 int32_t pid;
43 int32_t uid;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070044
Alex Buynytskyy605a44f2020-04-02 15:21:47 -070045 if (checkCallingPermission(String16(permission), &pid, &uid)) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070046 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
53binder::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
63binder::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
76binder::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
97binder::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
Yurii Zubrytskyi18eb8772021-10-18 22:33:15 -0700108binder::Status CheckIncrementalPath(IncrementalPathKind kind, const std::string& path) {
109 if (auto status = CheckArgumentPath(path); !status.isOk()) {
110 return status;
111 }
112 if (kind == IncrementalPathKind::MountSource || kind == IncrementalPathKind::MountTarget ||
113 kind == IncrementalPathKind::Any) {
114 if (android::base::StartsWith(path, "/data/incremental/MT_")) {
115 if (kind != IncrementalPathKind::MountSource &&
116 (android::base::EndsWith(path, "/mount") || path.find("/mount/") != path.npos)) {
117 return Ok();
118 }
119 if (kind != IncrementalPathKind::MountTarget &&
120 (android::base::EndsWith(path, "/backing_store") ||
121 path.find("/backing_store/") != path.npos)) {
122 return Ok();
123 }
124 }
125 }
126 if (kind == IncrementalPathKind::Bind || kind == IncrementalPathKind::Any) {
127 if (android::base::StartsWith(path, "/data/app/")) {
128 return Ok();
129 }
130 }
131 return Exception(binder::Status::EX_ILLEGAL_ARGUMENT,
132 StringPrintf("Path '%s' is not allowed", path.c_str()));
133}
134
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700135} // namespace android::vold