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