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