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