Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 18 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 19 | #include "VoldNativeService.h" |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 20 | #include "Benchmark.h" |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 21 | #include "CheckEncryption.h" |
| 22 | #include "IdleMaint.h" |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 23 | #include "MoveStorage.h" |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 24 | #include "Process.h" |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 25 | #include "VolumeManager.h" |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 26 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 27 | #include "Ext4Crypt.h" |
| 28 | #include "MetadataCrypt.h" |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 29 | #include "cryptfs.h" |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 30 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 31 | #include <fstream> |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 32 | #include <thread> |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 33 | |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <android-base/strings.h> |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 37 | #include <ext4_utils/ext4_crypt.h> |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 38 | #include <fs_mgr.h> |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 41 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | using std::endl; |
| 44 | |
| 45 | namespace android { |
| 46 | namespace vold { |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | constexpr const char* kDump = "android.permission.DUMP"; |
| 51 | |
| 52 | static binder::Status ok() { |
| 53 | return binder::Status::ok(); |
| 54 | } |
| 55 | |
| 56 | static binder::Status exception(uint32_t code, const std::string& msg) { |
| 57 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 58 | } |
| 59 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 60 | static binder::Status error(const std::string& msg) { |
| 61 | PLOG(ERROR) << msg; |
| 62 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 63 | } |
| 64 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 65 | static binder::Status translate(int status) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 66 | if (status == 0) { |
| 67 | return binder::Status::ok(); |
| 68 | } else { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 69 | return binder::Status::fromServiceSpecificError(status); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 73 | static binder::Status translateBool(bool status) { |
| 74 | if (status) { |
| 75 | return binder::Status::ok(); |
| 76 | } else { |
| 77 | return binder::Status::fromServiceSpecificError(status); |
| 78 | } |
| 79 | } |
| 80 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 81 | binder::Status checkPermission(const char* permission) { |
| 82 | pid_t pid; |
| 83 | uid_t uid; |
| 84 | |
| 85 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 86 | reinterpret_cast<int32_t*>(&uid))) { |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 87 | return ok(); |
| 88 | } else { |
| 89 | return exception(binder::Status::EX_SECURITY, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 90 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | binder::Status checkUid(uid_t expectedUid) { |
| 95 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 96 | if (uid == expectedUid || uid == AID_ROOT) { |
| 97 | return ok(); |
| 98 | } else { |
| 99 | return exception(binder::Status::EX_SECURITY, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 100 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 104 | binder::Status checkArgumentId(const std::string& id) { |
| 105 | if (id.empty()) { |
| 106 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing ID"); |
| 107 | } |
| 108 | for (const char& c : id) { |
| 109 | if (!std::isalnum(c) && c != ':' && c != ',') { |
| 110 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 111 | StringPrintf("ID %s is malformed", id.c_str())); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | return ok(); |
| 115 | } |
| 116 | |
| 117 | binder::Status checkArgumentPath(const std::string& path) { |
| 118 | if (path.empty()) { |
| 119 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path"); |
| 120 | } |
| 121 | if (path[0] != '/') { |
| 122 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 123 | StringPrintf("Path %s is relative", path.c_str())); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 124 | } |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 125 | if ((path + '/').find("/../") != std::string::npos) { |
| 126 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 127 | StringPrintf("Path %s is shady", path.c_str())); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 128 | } |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 129 | for (const char& c : path) { |
| 130 | if (c == '\0' || c == '\n') { |
| 131 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 132 | StringPrintf("Path %s is malformed", path.c_str())); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return ok(); |
| 136 | } |
| 137 | |
| 138 | binder::Status checkArgumentHex(const std::string& hex) { |
| 139 | // Empty hex strings are allowed |
| 140 | for (const char& c : hex) { |
| 141 | if (!std::isxdigit(c) && c != ':' && c != '-') { |
| 142 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 143 | StringPrintf("Hex %s is malformed", hex.c_str())); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | return ok(); |
| 147 | } |
| 148 | |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 149 | binder::Status checkArgumentPackageName(const std::string& packageName) { |
| 150 | // This logic is borrowed from PackageParser.java |
| 151 | bool hasSep = false; |
| 152 | bool front = true; |
| 153 | |
| 154 | for (size_t i = 0; i < packageName.length(); ++i) { |
| 155 | char c = packageName[i]; |
| 156 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 157 | front = false; |
| 158 | continue; |
| 159 | } |
| 160 | if (!front) { |
| 161 | if ((c >= '0' && c <= '9') || c == '_') { |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | if (c == '.') { |
| 166 | hasSep = true; |
| 167 | front = true; |
| 168 | continue; |
| 169 | } |
| 170 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 171 | StringPrintf("Bad package character %c in %s", c, packageName.c_str())); |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (front) { |
| 175 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 176 | StringPrintf("Missing separator in %s", packageName.c_str())); |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return ok(); |
| 180 | } |
| 181 | |
| 182 | binder::Status checkArgumentPackageNames(const std::vector<std::string>& packageNames) { |
| 183 | for (size_t i = 0; i < packageNames.size(); ++i) { |
| 184 | binder::Status status = checkArgumentPackageName(packageNames[i]); |
| 185 | if (!status.isOk()) { |
| 186 | return status; |
| 187 | } |
| 188 | } |
| 189 | return ok(); |
| 190 | } |
| 191 | |
| 192 | binder::Status checkArgumentSandboxId(const std::string& sandboxId) { |
| 193 | // sandboxId will be in either the format shared:<shared-user-id> or <package-name> |
| 194 | // and <shared-user-id> name has same requirements as <package-name>. |
| 195 | std::size_t nameStartIndex = 0; |
Greg Kaiser | e3f5932 | 2018-08-06 06:16:29 -0700 | [diff] [blame] | 196 | if (android::base::StartsWith(sandboxId, "shared:")) { |
| 197 | nameStartIndex = 7; // len("shared:") |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 198 | } |
| 199 | return checkArgumentPackageName(sandboxId.substr(nameStartIndex)); |
| 200 | } |
| 201 | |
| 202 | binder::Status checkArgumentSandboxIds(const std::vector<std::string>& sandboxIds) { |
| 203 | for (size_t i = 0; i < sandboxIds.size(); ++i) { |
| 204 | binder::Status status = checkArgumentSandboxId(sandboxIds[i]); |
| 205 | if (!status.isOk()) { |
| 206 | return status; |
| 207 | } |
| 208 | } |
| 209 | return ok(); |
| 210 | } |
| 211 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 212 | #define ENFORCE_UID(uid) \ |
| 213 | { \ |
| 214 | binder::Status status = checkUid((uid)); \ |
| 215 | if (!status.isOk()) { \ |
| 216 | return status; \ |
| 217 | } \ |
| 218 | } |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 219 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 220 | #define CHECK_ARGUMENT_ID(id) \ |
| 221 | { \ |
| 222 | binder::Status status = checkArgumentId((id)); \ |
| 223 | if (!status.isOk()) { \ |
| 224 | return status; \ |
| 225 | } \ |
| 226 | } |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 227 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 228 | #define CHECK_ARGUMENT_PATH(path) \ |
| 229 | { \ |
| 230 | binder::Status status = checkArgumentPath((path)); \ |
| 231 | if (!status.isOk()) { \ |
| 232 | return status; \ |
| 233 | } \ |
| 234 | } |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 235 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 236 | #define CHECK_ARGUMENT_HEX(hex) \ |
| 237 | { \ |
| 238 | binder::Status status = checkArgumentHex((hex)); \ |
| 239 | if (!status.isOk()) { \ |
| 240 | return status; \ |
| 241 | } \ |
| 242 | } |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 243 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 244 | #define CHECK_ARGUMENT_PACKAGE_NAMES(packageNames) \ |
| 245 | { \ |
| 246 | binder::Status status = checkArgumentPackageNames((packageNames)); \ |
| 247 | if (!status.isOk()) { \ |
| 248 | return status; \ |
| 249 | } \ |
| 250 | } |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 251 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 252 | #define CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds) \ |
| 253 | { \ |
| 254 | binder::Status status = checkArgumentSandboxIds((sandboxIds)); \ |
| 255 | if (!status.isOk()) { \ |
| 256 | return status; \ |
| 257 | } \ |
| 258 | } |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 259 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 260 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) \ |
| 261 | { \ |
| 262 | binder::Status status = checkArgumentPackageName((packageName)); \ |
| 263 | if (!status.isOk()) { \ |
| 264 | return status; \ |
| 265 | } \ |
| 266 | } |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 267 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 268 | #define CHECK_ARGUMENT_SANDBOX_ID(sandboxId) \ |
| 269 | { \ |
| 270 | binder::Status status = checkArgumentSandboxId((sandboxId)); \ |
| 271 | if (!status.isOk()) { \ |
| 272 | return status; \ |
| 273 | } \ |
| 274 | } |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 275 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 276 | #define ACQUIRE_LOCK \ |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 277 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \ |
| 278 | ATRACE_CALL(); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 279 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 280 | #define ACQUIRE_CRYPT_LOCK \ |
Jeff Sharkey | 67b8c49 | 2017-09-21 17:08:43 -0600 | [diff] [blame] | 281 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \ |
| 282 | ATRACE_CALL(); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 283 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 284 | } // namespace |
| 285 | |
| 286 | status_t VoldNativeService::start() { |
| 287 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 288 | status_t ret = BinderService<VoldNativeService>::publish(); |
| 289 | if (ret != android::OK) { |
| 290 | return ret; |
| 291 | } |
| 292 | sp<ProcessState> ps(ProcessState::self()); |
| 293 | ps->startThreadPool(); |
| 294 | ps->giveThreadPoolName(); |
| 295 | return android::OK; |
| 296 | } |
| 297 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 298 | status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) { |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 299 | auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); |
| 300 | const binder::Status dump_permission = checkPermission(kDump); |
| 301 | if (!dump_permission.isOk()) { |
| 302 | out << dump_permission.toString8() << endl; |
| 303 | return PERMISSION_DENIED; |
| 304 | } |
| 305 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 306 | ACQUIRE_LOCK; |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 307 | out << "vold is happy!" << endl; |
| 308 | out.flush(); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 309 | return NO_ERROR; |
| 310 | } |
| 311 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 312 | binder::Status VoldNativeService::setListener( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 313 | const android::sp<android::os::IVoldListener>& listener) { |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 314 | ENFORCE_UID(AID_SYSTEM); |
| 315 | ACQUIRE_LOCK; |
| 316 | |
| 317 | VolumeManager::Instance()->setListener(listener); |
| 318 | return ok(); |
| 319 | } |
| 320 | |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 321 | binder::Status VoldNativeService::monitor() { |
| 322 | ENFORCE_UID(AID_SYSTEM); |
| 323 | |
| 324 | // Simply acquire/release each lock for watchdog |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 325 | { ACQUIRE_LOCK; } |
| 326 | { ACQUIRE_CRYPT_LOCK; } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 327 | |
| 328 | return ok(); |
| 329 | } |
| 330 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 331 | binder::Status VoldNativeService::reset() { |
| 332 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 333 | ACQUIRE_LOCK; |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 334 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 335 | return translate(VolumeManager::Instance()->reset()); |
| 336 | } |
| 337 | |
| 338 | binder::Status VoldNativeService::shutdown() { |
| 339 | ENFORCE_UID(AID_SYSTEM); |
| 340 | ACQUIRE_LOCK; |
| 341 | |
| 342 | return translate(VolumeManager::Instance()->shutdown()); |
| 343 | } |
| 344 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 345 | binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) { |
| 346 | ENFORCE_UID(AID_SYSTEM); |
| 347 | ACQUIRE_LOCK; |
| 348 | |
| 349 | return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial)); |
| 350 | } |
| 351 | |
| 352 | binder::Status VoldNativeService::onUserRemoved(int32_t userId) { |
| 353 | ENFORCE_UID(AID_SYSTEM); |
| 354 | ACQUIRE_LOCK; |
| 355 | |
| 356 | return translate(VolumeManager::Instance()->onUserRemoved(userId)); |
| 357 | } |
| 358 | |
Sudheer Shanka | ebaad1c | 2018-07-31 16:39:59 -0700 | [diff] [blame] | 359 | binder::Status VoldNativeService::onUserStarted(int32_t userId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 360 | const std::vector<std::string>& packageNames) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 361 | ENFORCE_UID(AID_SYSTEM); |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 362 | CHECK_ARGUMENT_PACKAGE_NAMES(packageNames); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 363 | ACQUIRE_LOCK; |
| 364 | |
Sudheer Shanka | ebaad1c | 2018-07-31 16:39:59 -0700 | [diff] [blame] | 365 | return translate(VolumeManager::Instance()->onUserStarted(userId, packageNames)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | binder::Status VoldNativeService::onUserStopped(int32_t userId) { |
| 369 | ENFORCE_UID(AID_SYSTEM); |
| 370 | ACQUIRE_LOCK; |
| 371 | |
| 372 | return translate(VolumeManager::Instance()->onUserStopped(userId)); |
| 373 | } |
| 374 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 375 | binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 376 | const std::vector<int32_t>& appIds) { |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 377 | ENFORCE_UID(AID_SYSTEM); |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 378 | CHECK_ARGUMENT_PACKAGE_NAMES(packageNames); |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 379 | ACQUIRE_LOCK; |
| 380 | |
| 381 | return translate(VolumeManager::Instance()->addAppIds(packageNames, appIds)); |
| 382 | } |
| 383 | |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 384 | binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 385 | const std::vector<std::string>& sandboxIds) { |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 386 | ENFORCE_UID(AID_SYSTEM); |
Sudheer Shanka | cc0df59 | 2018-08-02 10:21:42 -0700 | [diff] [blame] | 387 | CHECK_ARGUMENT_SANDBOX_IDS(sandboxIds); |
Sudheer Shanka | d484aa9 | 2018-07-31 10:07:34 -0700 | [diff] [blame] | 388 | ACQUIRE_LOCK; |
| 389 | |
| 390 | return translate(VolumeManager::Instance()->addSandboxIds(appIds, sandboxIds)); |
| 391 | } |
| 392 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 393 | binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) { |
| 394 | ENFORCE_UID(AID_SYSTEM); |
| 395 | ACQUIRE_LOCK; |
| 396 | |
| 397 | return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing)); |
| 398 | } |
| 399 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 400 | binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 401 | int32_t ratio) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 402 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 403 | CHECK_ARGUMENT_ID(diskId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 404 | ACQUIRE_LOCK; |
| 405 | |
| 406 | auto disk = VolumeManager::Instance()->findDisk(diskId); |
| 407 | if (disk == nullptr) { |
| 408 | return error("Failed to find disk " + diskId); |
| 409 | } |
| 410 | switch (partitionType) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 411 | case PARTITION_TYPE_PUBLIC: |
| 412 | return translate(disk->partitionPublic()); |
| 413 | case PARTITION_TYPE_PRIVATE: |
| 414 | return translate(disk->partitionPrivate()); |
| 415 | case PARTITION_TYPE_MIXED: |
| 416 | return translate(disk->partitionMixed(ratio)); |
| 417 | default: |
| 418 | return error("Unknown type " + std::to_string(partitionType)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 422 | binder::Status VoldNativeService::forgetPartition(const std::string& partGuid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 423 | const std::string& fsUuid) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 424 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 425 | CHECK_ARGUMENT_HEX(partGuid); |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 426 | CHECK_ARGUMENT_HEX(fsUuid); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 427 | ACQUIRE_LOCK; |
| 428 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 429 | return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 430 | } |
| 431 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 432 | binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 433 | int32_t mountUserId) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 434 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 435 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 436 | ACQUIRE_LOCK; |
| 437 | |
| 438 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 439 | if (vol == nullptr) { |
| 440 | return error("Failed to find volume " + volId); |
| 441 | } |
| 442 | |
| 443 | vol->setMountFlags(mountFlags); |
| 444 | vol->setMountUserId(mountUserId); |
| 445 | |
| 446 | int res = vol->mount(); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 447 | if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 448 | VolumeManager::Instance()->setPrimary(vol); |
| 449 | } |
| 450 | return translate(res); |
| 451 | } |
| 452 | |
| 453 | binder::Status VoldNativeService::unmount(const std::string& volId) { |
| 454 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 455 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 456 | ACQUIRE_LOCK; |
| 457 | |
| 458 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 459 | if (vol == nullptr) { |
| 460 | return error("Failed to find volume " + volId); |
| 461 | } |
| 462 | return translate(vol->unmount()); |
| 463 | } |
| 464 | |
| 465 | binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) { |
| 466 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 467 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 468 | ACQUIRE_LOCK; |
| 469 | |
| 470 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 471 | if (vol == nullptr) { |
| 472 | return error("Failed to find volume " + volId); |
| 473 | } |
| 474 | return translate(vol->format(fsType)); |
| 475 | } |
| 476 | |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 477 | static binder::Status pathForVolId(const std::string& volId, std::string* path) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 478 | if (volId == "private" || volId == "null") { |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 479 | *path = "/data"; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 480 | } else { |
| 481 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 482 | if (vol == nullptr) { |
| 483 | return error("Failed to find volume " + volId); |
| 484 | } |
| 485 | if (vol->getType() != VolumeBase::Type::kPrivate) { |
| 486 | return error("Volume " + volId + " not private"); |
| 487 | } |
| 488 | if (vol->getState() != VolumeBase::State::kMounted) { |
| 489 | return error("Volume " + volId + " not mounted"); |
| 490 | } |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 491 | *path = vol->getPath(); |
| 492 | if (path->empty()) { |
| 493 | return error("Volume " + volId + " missing path"); |
| 494 | } |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 495 | } |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 496 | return ok(); |
| 497 | } |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 498 | |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 499 | binder::Status VoldNativeService::benchmark( |
| 500 | const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) { |
| 501 | ENFORCE_UID(AID_SYSTEM); |
| 502 | CHECK_ARGUMENT_ID(volId); |
| 503 | ACQUIRE_LOCK; |
| 504 | |
| 505 | std::string path; |
| 506 | auto status = pathForVolId(volId, &path); |
| 507 | if (!status.isOk()) return status; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 508 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 509 | std::thread([=]() { android::vold::Benchmark(path, listener); }).detach(); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 510 | return ok(); |
| 511 | } |
| 512 | |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 513 | binder::Status VoldNativeService::checkEncryption(const std::string& volId) { |
| 514 | ENFORCE_UID(AID_SYSTEM); |
| 515 | CHECK_ARGUMENT_ID(volId); |
| 516 | ACQUIRE_LOCK; |
| 517 | |
| 518 | std::string path; |
| 519 | auto status = pathForVolId(volId, &path); |
| 520 | if (!status.isOk()) return status; |
| 521 | return translate(android::vold::CheckEncryption(path)); |
| 522 | } |
| 523 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 524 | binder::Status VoldNativeService::moveStorage( |
| 525 | const std::string& fromVolId, const std::string& toVolId, |
| 526 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 527 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 528 | CHECK_ARGUMENT_ID(fromVolId); |
| 529 | CHECK_ARGUMENT_ID(toVolId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 530 | ACQUIRE_LOCK; |
| 531 | |
| 532 | auto fromVol = VolumeManager::Instance()->findVolume(fromVolId); |
| 533 | auto toVol = VolumeManager::Instance()->findVolume(toVolId); |
| 534 | if (fromVol == nullptr) { |
| 535 | return error("Failed to find volume " + fromVolId); |
| 536 | } else if (toVol == nullptr) { |
| 537 | return error("Failed to find volume " + toVolId); |
| 538 | } |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 539 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 540 | std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach(); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 541 | return ok(); |
| 542 | } |
| 543 | |
| 544 | binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) { |
| 545 | ENFORCE_UID(AID_SYSTEM); |
| 546 | ACQUIRE_LOCK; |
| 547 | |
| 548 | std::string tmp; |
| 549 | switch (remountMode) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 550 | case REMOUNT_MODE_NONE: |
| 551 | tmp = "none"; |
| 552 | break; |
| 553 | case REMOUNT_MODE_DEFAULT: |
| 554 | tmp = "default"; |
| 555 | break; |
| 556 | case REMOUNT_MODE_READ: |
| 557 | tmp = "read"; |
| 558 | break; |
| 559 | case REMOUNT_MODE_WRITE: |
| 560 | tmp = "write"; |
| 561 | break; |
| 562 | default: |
| 563 | return error("Unknown mode " + std::to_string(remountMode)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 564 | } |
| 565 | return translate(VolumeManager::Instance()->remountUid(uid, tmp)); |
| 566 | } |
| 567 | |
| 568 | binder::Status VoldNativeService::mkdirs(const std::string& path) { |
| 569 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 570 | CHECK_ARGUMENT_PATH(path); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 571 | ACQUIRE_LOCK; |
| 572 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 573 | return translate(VolumeManager::Instance()->mkdirs(path)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 574 | } |
| 575 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 576 | binder::Status VoldNativeService::createObb(const std::string& sourcePath, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 577 | const std::string& sourceKey, int32_t ownerGid, |
| 578 | std::string* _aidl_return) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 579 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 580 | CHECK_ARGUMENT_PATH(sourcePath); |
| 581 | CHECK_ARGUMENT_HEX(sourceKey); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 582 | ACQUIRE_LOCK; |
| 583 | |
| 584 | return translate( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 585 | VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return)); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | binder::Status VoldNativeService::destroyObb(const std::string& volId) { |
| 589 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 590 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 591 | ACQUIRE_LOCK; |
| 592 | |
| 593 | return translate(VolumeManager::Instance()->destroyObb(volId)); |
| 594 | } |
| 595 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 596 | binder::Status VoldNativeService::fstrim( |
| 597 | int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 598 | ENFORCE_UID(AID_SYSTEM); |
| 599 | ACQUIRE_LOCK; |
| 600 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 601 | std::thread([=]() { android::vold::Trim(listener); }).detach(); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 602 | return ok(); |
| 603 | } |
| 604 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 605 | binder::Status VoldNativeService::runIdleMaint( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 606 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 607 | ENFORCE_UID(AID_SYSTEM); |
| 608 | ACQUIRE_LOCK; |
| 609 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 610 | std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach(); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 611 | return ok(); |
| 612 | } |
| 613 | |
| 614 | binder::Status VoldNativeService::abortIdleMaint( |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 615 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 616 | ENFORCE_UID(AID_SYSTEM); |
| 617 | ACQUIRE_LOCK; |
| 618 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 619 | std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach(); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 620 | return ok(); |
| 621 | } |
| 622 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 623 | binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 624 | android::base::unique_fd* _aidl_return) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 625 | ENFORCE_UID(AID_SYSTEM); |
| 626 | ACQUIRE_LOCK; |
| 627 | |
| 628 | return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return)); |
| 629 | } |
| 630 | |
| 631 | binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) { |
| 632 | ENFORCE_UID(AID_SYSTEM); |
| 633 | ACQUIRE_LOCK; |
| 634 | |
| 635 | return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId)); |
| 636 | } |
| 637 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 638 | binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) { |
| 639 | ENFORCE_UID(AID_SYSTEM); |
| 640 | ACQUIRE_CRYPT_LOCK; |
| 641 | |
| 642 | return translate(cryptfs_check_passwd(password.c_str())); |
| 643 | } |
| 644 | |
| 645 | binder::Status VoldNativeService::fdeRestart() { |
| 646 | ENFORCE_UID(AID_SYSTEM); |
| 647 | ACQUIRE_CRYPT_LOCK; |
| 648 | |
| 649 | // Spawn as thread so init can issue commands back to vold without |
| 650 | // causing deadlock, usually as a result of prep_data_fs. |
| 651 | std::thread(&cryptfs_restart).detach(); |
| 652 | return ok(); |
| 653 | } |
| 654 | |
| 655 | binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) { |
| 656 | ENFORCE_UID(AID_SYSTEM); |
| 657 | ACQUIRE_CRYPT_LOCK; |
| 658 | |
| 659 | *_aidl_return = cryptfs_crypto_complete(); |
| 660 | return ok(); |
| 661 | } |
| 662 | |
| 663 | static int fdeEnableInternal(int32_t passwordType, const std::string& password, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 664 | int32_t encryptionFlags) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 665 | bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0; |
| 666 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 667 | for (int tries = 0; tries < 2; ++tries) { |
| 668 | int rc; |
| 669 | if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) { |
Paul Lawrence | 7ee87cf | 2017-12-22 10:12:06 -0800 | [diff] [blame] | 670 | rc = cryptfs_enable_default(noUi); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 671 | } else { |
Paul Lawrence | 7ee87cf | 2017-12-22 10:12:06 -0800 | [diff] [blame] | 672 | rc = cryptfs_enable(passwordType, password.c_str(), noUi); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | if (rc == 0) { |
| 676 | return 0; |
| 677 | } else if (tries == 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 678 | KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | return -1; |
| 683 | } |
| 684 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 685 | binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password, |
| 686 | int32_t encryptionFlags) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 687 | ENFORCE_UID(AID_SYSTEM); |
| 688 | ACQUIRE_CRYPT_LOCK; |
| 689 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 690 | LOG(DEBUG) << "fdeEnable(" << passwordType << ", *, " << encryptionFlags << ")"; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 691 | if (e4crypt_is_native()) { |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 692 | LOG(ERROR) << "e4crypt_is_native, fdeEnable invalid"; |
| 693 | return error("e4crypt_is_native, fdeEnable invalid"); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 694 | } |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 695 | LOG(DEBUG) << "!e4crypt_is_native, spawning fdeEnableInternal"; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 696 | |
| 697 | // Spawn as thread so init can issue commands back to vold without |
| 698 | // causing deadlock, usually as a result of prep_data_fs. |
| 699 | std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach(); |
| 700 | return ok(); |
| 701 | } |
| 702 | |
| 703 | binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 704 | const std::string& password) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 705 | ENFORCE_UID(AID_SYSTEM); |
| 706 | ACQUIRE_CRYPT_LOCK; |
| 707 | |
| 708 | return translate(cryptfs_changepw(passwordType, password.c_str())); |
| 709 | } |
| 710 | |
| 711 | binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) { |
| 712 | ENFORCE_UID(AID_SYSTEM); |
| 713 | ACQUIRE_CRYPT_LOCK; |
| 714 | |
| 715 | return translate(cryptfs_verify_passwd(password.c_str())); |
| 716 | } |
| 717 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 718 | binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 719 | ENFORCE_UID(AID_SYSTEM); |
| 720 | ACQUIRE_CRYPT_LOCK; |
| 721 | |
| 722 | char buf[PROPERTY_VALUE_MAX]; |
| 723 | if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) { |
| 724 | return error(StringPrintf("Failed to read field %s", key.c_str())); |
| 725 | } else { |
| 726 | *_aidl_return = buf; |
| 727 | return ok(); |
| 728 | } |
| 729 | } |
| 730 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 731 | binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 732 | ENFORCE_UID(AID_SYSTEM); |
| 733 | ACQUIRE_CRYPT_LOCK; |
| 734 | |
| 735 | return translate(cryptfs_setfield(key.c_str(), value.c_str())); |
| 736 | } |
| 737 | |
| 738 | binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) { |
| 739 | ENFORCE_UID(AID_SYSTEM); |
| 740 | ACQUIRE_CRYPT_LOCK; |
| 741 | |
| 742 | *_aidl_return = cryptfs_get_password_type(); |
| 743 | return ok(); |
| 744 | } |
| 745 | |
| 746 | binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) { |
| 747 | ENFORCE_UID(AID_SYSTEM); |
| 748 | ACQUIRE_CRYPT_LOCK; |
| 749 | |
| 750 | const char* res = cryptfs_get_password(); |
| 751 | if (res != nullptr) { |
| 752 | *_aidl_return = res; |
| 753 | } |
| 754 | return ok(); |
| 755 | } |
| 756 | |
| 757 | binder::Status VoldNativeService::fdeClearPassword() { |
| 758 | ENFORCE_UID(AID_SYSTEM); |
| 759 | ACQUIRE_CRYPT_LOCK; |
| 760 | |
| 761 | cryptfs_clear_password(); |
| 762 | return ok(); |
| 763 | } |
| 764 | |
| 765 | binder::Status VoldNativeService::fbeEnable() { |
| 766 | ENFORCE_UID(AID_SYSTEM); |
| 767 | ACQUIRE_CRYPT_LOCK; |
| 768 | |
| 769 | return translateBool(e4crypt_initialize_global_de()); |
| 770 | } |
| 771 | |
| 772 | binder::Status VoldNativeService::mountDefaultEncrypted() { |
| 773 | ENFORCE_UID(AID_SYSTEM); |
| 774 | ACQUIRE_CRYPT_LOCK; |
| 775 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 776 | if (!e4crypt_is_native()) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 777 | // Spawn as thread so init can issue commands back to vold without |
| 778 | // causing deadlock, usually as a result of prep_data_fs. |
| 779 | std::thread(&cryptfs_mount_default_encrypted).detach(); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 780 | } |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 781 | return ok(); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | binder::Status VoldNativeService::initUser0() { |
| 785 | ENFORCE_UID(AID_SYSTEM); |
| 786 | ACQUIRE_CRYPT_LOCK; |
| 787 | |
| 788 | return translateBool(e4crypt_init_user0()); |
| 789 | } |
| 790 | |
| 791 | binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) { |
| 792 | ENFORCE_UID(AID_SYSTEM); |
| 793 | ACQUIRE_CRYPT_LOCK; |
| 794 | |
| 795 | *_aidl_return = cryptfs_isConvertibleToFBE() != 0; |
| 796 | return ok(); |
| 797 | } |
| 798 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 799 | binder::Status VoldNativeService::mountFstab(const std::string& mountPoint) { |
| 800 | ENFORCE_UID(AID_SYSTEM); |
| 801 | ACQUIRE_LOCK; |
| 802 | |
| 803 | return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, false)); |
| 804 | } |
| 805 | |
| 806 | binder::Status VoldNativeService::encryptFstab(const std::string& mountPoint) { |
| 807 | ENFORCE_UID(AID_SYSTEM); |
| 808 | ACQUIRE_LOCK; |
| 809 | |
| 810 | return translateBool(e4crypt_mount_metadata_encrypted(mountPoint, true)); |
| 811 | } |
| 812 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 813 | binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, bool ephemeral) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 814 | ENFORCE_UID(AID_SYSTEM); |
| 815 | ACQUIRE_CRYPT_LOCK; |
| 816 | |
| 817 | return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral)); |
| 818 | } |
| 819 | |
| 820 | binder::Status VoldNativeService::destroyUserKey(int32_t userId) { |
| 821 | ENFORCE_UID(AID_SYSTEM); |
| 822 | ACQUIRE_CRYPT_LOCK; |
| 823 | |
| 824 | return translateBool(e4crypt_destroy_user_key(userId)); |
| 825 | } |
| 826 | |
| 827 | binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 828 | const std::string& token, |
| 829 | const std::string& secret) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 830 | ENFORCE_UID(AID_SYSTEM); |
| 831 | ACQUIRE_CRYPT_LOCK; |
| 832 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 833 | return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret)); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) { |
| 837 | ENFORCE_UID(AID_SYSTEM); |
| 838 | ACQUIRE_CRYPT_LOCK; |
| 839 | |
| 840 | return translateBool(e4crypt_fixate_newest_user_key_auth(userId)); |
| 841 | } |
| 842 | |
| 843 | binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 844 | const std::string& token, |
| 845 | const std::string& secret) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 846 | ENFORCE_UID(AID_SYSTEM); |
| 847 | ACQUIRE_CRYPT_LOCK; |
| 848 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 849 | return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret)); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | binder::Status VoldNativeService::lockUserKey(int32_t userId) { |
| 853 | ENFORCE_UID(AID_SYSTEM); |
| 854 | ACQUIRE_CRYPT_LOCK; |
| 855 | |
| 856 | return translateBool(e4crypt_lock_user_key(userId)); |
| 857 | } |
| 858 | |
| 859 | binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 860 | int32_t userId, int32_t userSerial, |
| 861 | int32_t flags) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 862 | ENFORCE_UID(AID_SYSTEM); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 863 | std::string empty_string = ""; |
| 864 | auto uuid_ = uuid ? *uuid : empty_string; |
Paul Crowley | 06f762d | 2017-10-16 10:59:51 -0700 | [diff] [blame] | 865 | CHECK_ARGUMENT_HEX(uuid_); |
| 866 | |
| 867 | ACQUIRE_CRYPT_LOCK; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 868 | return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags)); |
| 869 | } |
| 870 | |
| 871 | binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 872 | int32_t userId, int32_t flags) { |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 873 | ENFORCE_UID(AID_SYSTEM); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 874 | std::string empty_string = ""; |
| 875 | auto uuid_ = uuid ? *uuid : empty_string; |
Paul Crowley | 06f762d | 2017-10-16 10:59:51 -0700 | [diff] [blame] | 876 | CHECK_ARGUMENT_HEX(uuid_); |
| 877 | |
| 878 | ACQUIRE_CRYPT_LOCK; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 879 | return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags)); |
| 880 | } |
| 881 | |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 882 | binder::Status VoldNativeService::mountExternalStorageForApp(const std::string& packageName, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 883 | int32_t appId, |
| 884 | const std::string& sandboxId, |
| 885 | int32_t userId) { |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 886 | ENFORCE_UID(AID_SYSTEM); |
| 887 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 888 | CHECK_ARGUMENT_SANDBOX_ID(sandboxId); |
| 889 | ACQUIRE_LOCK; |
| 890 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 891 | return translate(VolumeManager::Instance()->mountExternalStorageForApp(packageName, appId, |
| 892 | sandboxId, userId)); |
Sudheer Shanka | c756209 | 2018-08-24 10:20:56 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 895 | } // namespace vold |
| 896 | } // namespace android |