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 | |
| 305 | binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) { |
| 306 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 307 | CHECK_ARGUMENT_HEX(partGuid); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 308 | ACQUIRE_LOCK; |
| 309 | |
| 310 | return translate(VolumeManager::Instance()->forgetPartition(partGuid)); |
| 311 | } |
| 312 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 313 | binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags, |
| 314 | int32_t mountUserId) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 315 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 316 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 317 | ACQUIRE_LOCK; |
| 318 | |
| 319 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 320 | if (vol == nullptr) { |
| 321 | return error("Failed to find volume " + volId); |
| 322 | } |
| 323 | |
| 324 | vol->setMountFlags(mountFlags); |
| 325 | vol->setMountUserId(mountUserId); |
| 326 | |
| 327 | int res = vol->mount(); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 328 | if ((mountFlags & MOUNT_FLAG_PRIMARY) != 0) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 329 | VolumeManager::Instance()->setPrimary(vol); |
| 330 | } |
| 331 | return translate(res); |
| 332 | } |
| 333 | |
| 334 | binder::Status VoldNativeService::unmount(const std::string& volId) { |
| 335 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 336 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 337 | ACQUIRE_LOCK; |
| 338 | |
| 339 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 340 | if (vol == nullptr) { |
| 341 | return error("Failed to find volume " + volId); |
| 342 | } |
| 343 | return translate(vol->unmount()); |
| 344 | } |
| 345 | |
| 346 | binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) { |
| 347 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 348 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 349 | ACQUIRE_LOCK; |
| 350 | |
| 351 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 352 | if (vol == nullptr) { |
| 353 | return error("Failed to find volume " + volId); |
| 354 | } |
| 355 | return translate(vol->format(fsType)); |
| 356 | } |
| 357 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 358 | binder::Status VoldNativeService::benchmark(const std::string& volId, |
| 359 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 360 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 361 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 362 | ACQUIRE_LOCK; |
| 363 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 364 | std::string path; |
| 365 | if (volId == "private" || volId == "null") { |
| 366 | path = "/data"; |
| 367 | } else { |
| 368 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 369 | if (vol == nullptr) { |
| 370 | return error("Failed to find volume " + volId); |
| 371 | } |
| 372 | if (vol->getType() != VolumeBase::Type::kPrivate) { |
| 373 | return error("Volume " + volId + " not private"); |
| 374 | } |
| 375 | if (vol->getState() != VolumeBase::State::kMounted) { |
| 376 | return error("Volume " + volId + " not mounted"); |
| 377 | } |
| 378 | path = vol->getPath(); |
| 379 | } |
| 380 | |
| 381 | if (path.empty()) { |
| 382 | return error("Volume " + volId + " missing path"); |
| 383 | } |
| 384 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame^] | 385 | std::thread([=]() { |
| 386 | android::vold::Benchmark(path, listener); |
| 387 | }).detach(); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 388 | return ok(); |
| 389 | } |
| 390 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 391 | binder::Status VoldNativeService::moveStorage(const std::string& fromVolId, |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 392 | const std::string& toVolId, const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 393 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 394 | CHECK_ARGUMENT_ID(fromVolId); |
| 395 | CHECK_ARGUMENT_ID(toVolId); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 396 | ACQUIRE_LOCK; |
| 397 | |
| 398 | auto fromVol = VolumeManager::Instance()->findVolume(fromVolId); |
| 399 | auto toVol = VolumeManager::Instance()->findVolume(toVolId); |
| 400 | if (fromVol == nullptr) { |
| 401 | return error("Failed to find volume " + fromVolId); |
| 402 | } else if (toVol == nullptr) { |
| 403 | return error("Failed to find volume " + toVolId); |
| 404 | } |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame^] | 405 | |
| 406 | std::thread([=]() { |
| 407 | android::vold::MoveStorage(fromVol, toVol, listener); |
| 408 | }).detach(); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 409 | return ok(); |
| 410 | } |
| 411 | |
| 412 | binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) { |
| 413 | ENFORCE_UID(AID_SYSTEM); |
| 414 | ACQUIRE_LOCK; |
| 415 | |
| 416 | std::string tmp; |
| 417 | switch (remountMode) { |
| 418 | case REMOUNT_MODE_NONE: tmp = "none"; break; |
| 419 | case REMOUNT_MODE_DEFAULT: tmp = "default"; break; |
| 420 | case REMOUNT_MODE_READ: tmp = "read"; break; |
| 421 | case REMOUNT_MODE_WRITE: tmp = "write"; break; |
| 422 | default: return error("Unknown mode " + std::to_string(remountMode)); |
| 423 | } |
| 424 | return translate(VolumeManager::Instance()->remountUid(uid, tmp)); |
| 425 | } |
| 426 | |
| 427 | binder::Status VoldNativeService::mkdirs(const std::string& path) { |
| 428 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 429 | CHECK_ARGUMENT_PATH(path); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 430 | ACQUIRE_LOCK; |
| 431 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 432 | return translate(VolumeManager::Instance()->mkdirs(path)); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame] | 433 | } |
| 434 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 435 | binder::Status VoldNativeService::createObb(const std::string& sourcePath, |
| 436 | const std::string& sourceKey, int32_t ownerGid, std::string* _aidl_return) { |
| 437 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 438 | CHECK_ARGUMENT_PATH(sourcePath); |
| 439 | CHECK_ARGUMENT_HEX(sourceKey); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 440 | ACQUIRE_LOCK; |
| 441 | |
| 442 | return translate( |
| 443 | VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return)); |
| 444 | } |
| 445 | |
| 446 | binder::Status VoldNativeService::destroyObb(const std::string& volId) { |
| 447 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | ec4fda2 | 2017-09-12 13:19:24 -0600 | [diff] [blame] | 448 | CHECK_ARGUMENT_ID(volId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 449 | ACQUIRE_LOCK; |
| 450 | |
| 451 | return translate(VolumeManager::Instance()->destroyObb(volId)); |
| 452 | } |
| 453 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 454 | binder::Status VoldNativeService::fstrim(int32_t fstrimFlags, |
| 455 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 456 | ENFORCE_UID(AID_SYSTEM); |
| 457 | ACQUIRE_LOCK; |
| 458 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame^] | 459 | std::thread([=]() { |
| 460 | android::vold::Trim(listener); |
| 461 | }).detach(); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 462 | return ok(); |
| 463 | } |
| 464 | |
| 465 | binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t pid, int32_t mountId, |
| 466 | android::base::unique_fd* _aidl_return) { |
| 467 | ENFORCE_UID(AID_SYSTEM); |
| 468 | ACQUIRE_LOCK; |
| 469 | |
| 470 | return translate(VolumeManager::Instance()->mountAppFuse(uid, pid, mountId, _aidl_return)); |
| 471 | } |
| 472 | |
| 473 | binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t pid, int32_t mountId) { |
| 474 | ENFORCE_UID(AID_SYSTEM); |
| 475 | ACQUIRE_LOCK; |
| 476 | |
| 477 | return translate(VolumeManager::Instance()->unmountAppFuse(uid, pid, mountId)); |
| 478 | } |
| 479 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 480 | binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) { |
| 481 | ENFORCE_UID(AID_SYSTEM); |
| 482 | ACQUIRE_CRYPT_LOCK; |
| 483 | |
| 484 | return translate(cryptfs_check_passwd(password.c_str())); |
| 485 | } |
| 486 | |
| 487 | binder::Status VoldNativeService::fdeRestart() { |
| 488 | ENFORCE_UID(AID_SYSTEM); |
| 489 | ACQUIRE_CRYPT_LOCK; |
| 490 | |
| 491 | // Spawn as thread so init can issue commands back to vold without |
| 492 | // causing deadlock, usually as a result of prep_data_fs. |
| 493 | std::thread(&cryptfs_restart).detach(); |
| 494 | return ok(); |
| 495 | } |
| 496 | |
| 497 | binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) { |
| 498 | ENFORCE_UID(AID_SYSTEM); |
| 499 | ACQUIRE_CRYPT_LOCK; |
| 500 | |
| 501 | *_aidl_return = cryptfs_crypto_complete(); |
| 502 | return ok(); |
| 503 | } |
| 504 | |
| 505 | static int fdeEnableInternal(int32_t passwordType, const std::string& password, |
| 506 | int32_t encryptionFlags) { |
| 507 | bool noUi = (encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_NO_UI) != 0; |
| 508 | |
| 509 | std::string how; |
| 510 | if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_IN_PLACE) != 0) { |
| 511 | how = "inplace"; |
| 512 | } else if ((encryptionFlags & VoldNativeService::ENCRYPTION_FLAG_WIPE) != 0) { |
| 513 | how = "wipe"; |
| 514 | } else { |
| 515 | LOG(ERROR) << "Missing encryption flag"; |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | for (int tries = 0; tries < 2; ++tries) { |
| 520 | int rc; |
| 521 | if (passwordType == VoldNativeService::PASSWORD_TYPE_DEFAULT) { |
| 522 | rc = cryptfs_enable_default(how.c_str(), noUi); |
| 523 | } else { |
| 524 | rc = cryptfs_enable(how.c_str(), passwordType, password.c_str(), noUi); |
| 525 | } |
| 526 | |
| 527 | if (rc == 0) { |
| 528 | return 0; |
| 529 | } else if (tries == 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 530 | KillProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | binder::Status VoldNativeService::fdeEnable(int32_t passwordType, |
| 538 | const std::string& password, int32_t encryptionFlags) { |
| 539 | ENFORCE_UID(AID_SYSTEM); |
| 540 | ACQUIRE_CRYPT_LOCK; |
| 541 | |
| 542 | if (e4crypt_is_native()) { |
| 543 | if (passwordType != PASSWORD_TYPE_DEFAULT) { |
| 544 | return error("Unexpected password type"); |
| 545 | } |
| 546 | if (encryptionFlags != (ENCRYPTION_FLAG_IN_PLACE | ENCRYPTION_FLAG_NO_UI)) { |
| 547 | return error("Unexpected flags"); |
| 548 | } |
| 549 | return translateBool(e4crypt_enable_crypto()); |
| 550 | } |
| 551 | |
| 552 | // Spawn as thread so init can issue commands back to vold without |
| 553 | // causing deadlock, usually as a result of prep_data_fs. |
| 554 | std::thread(&fdeEnableInternal, passwordType, password, encryptionFlags).detach(); |
| 555 | return ok(); |
| 556 | } |
| 557 | |
| 558 | binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType, |
| 559 | const std::string& password) { |
| 560 | ENFORCE_UID(AID_SYSTEM); |
| 561 | ACQUIRE_CRYPT_LOCK; |
| 562 | |
| 563 | return translate(cryptfs_changepw(passwordType, password.c_str())); |
| 564 | } |
| 565 | |
| 566 | binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) { |
| 567 | ENFORCE_UID(AID_SYSTEM); |
| 568 | ACQUIRE_CRYPT_LOCK; |
| 569 | |
| 570 | return translate(cryptfs_verify_passwd(password.c_str())); |
| 571 | } |
| 572 | |
| 573 | binder::Status VoldNativeService::fdeGetField(const std::string& key, |
| 574 | std::string* _aidl_return) { |
| 575 | ENFORCE_UID(AID_SYSTEM); |
| 576 | ACQUIRE_CRYPT_LOCK; |
| 577 | |
| 578 | char buf[PROPERTY_VALUE_MAX]; |
| 579 | if (cryptfs_getfield(key.c_str(), buf, sizeof(buf)) != CRYPTO_GETFIELD_OK) { |
| 580 | return error(StringPrintf("Failed to read field %s", key.c_str())); |
| 581 | } else { |
| 582 | *_aidl_return = buf; |
| 583 | return ok(); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | binder::Status VoldNativeService::fdeSetField(const std::string& key, |
| 588 | const std::string& value) { |
| 589 | ENFORCE_UID(AID_SYSTEM); |
| 590 | ACQUIRE_CRYPT_LOCK; |
| 591 | |
| 592 | return translate(cryptfs_setfield(key.c_str(), value.c_str())); |
| 593 | } |
| 594 | |
| 595 | binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) { |
| 596 | ENFORCE_UID(AID_SYSTEM); |
| 597 | ACQUIRE_CRYPT_LOCK; |
| 598 | |
| 599 | *_aidl_return = cryptfs_get_password_type(); |
| 600 | return ok(); |
| 601 | } |
| 602 | |
| 603 | binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) { |
| 604 | ENFORCE_UID(AID_SYSTEM); |
| 605 | ACQUIRE_CRYPT_LOCK; |
| 606 | |
| 607 | const char* res = cryptfs_get_password(); |
| 608 | if (res != nullptr) { |
| 609 | *_aidl_return = res; |
| 610 | } |
| 611 | return ok(); |
| 612 | } |
| 613 | |
| 614 | binder::Status VoldNativeService::fdeClearPassword() { |
| 615 | ENFORCE_UID(AID_SYSTEM); |
| 616 | ACQUIRE_CRYPT_LOCK; |
| 617 | |
| 618 | cryptfs_clear_password(); |
| 619 | return ok(); |
| 620 | } |
| 621 | |
| 622 | binder::Status VoldNativeService::fbeEnable() { |
| 623 | ENFORCE_UID(AID_SYSTEM); |
| 624 | ACQUIRE_CRYPT_LOCK; |
| 625 | |
| 626 | return translateBool(e4crypt_initialize_global_de()); |
| 627 | } |
| 628 | |
| 629 | binder::Status VoldNativeService::mountDefaultEncrypted() { |
| 630 | ENFORCE_UID(AID_SYSTEM); |
| 631 | ACQUIRE_CRYPT_LOCK; |
| 632 | |
| 633 | if (e4crypt_is_native()) { |
| 634 | return translateBool(e4crypt_mount_metadata_encrypted()); |
| 635 | } else { |
| 636 | // Spawn as thread so init can issue commands back to vold without |
| 637 | // causing deadlock, usually as a result of prep_data_fs. |
| 638 | std::thread(&cryptfs_mount_default_encrypted).detach(); |
| 639 | return ok(); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | binder::Status VoldNativeService::initUser0() { |
| 644 | ENFORCE_UID(AID_SYSTEM); |
| 645 | ACQUIRE_CRYPT_LOCK; |
| 646 | |
| 647 | return translateBool(e4crypt_init_user0()); |
| 648 | } |
| 649 | |
| 650 | binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) { |
| 651 | ENFORCE_UID(AID_SYSTEM); |
| 652 | ACQUIRE_CRYPT_LOCK; |
| 653 | |
| 654 | *_aidl_return = cryptfs_isConvertibleToFBE() != 0; |
| 655 | return ok(); |
| 656 | } |
| 657 | |
| 658 | binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial, |
| 659 | bool ephemeral) { |
| 660 | ENFORCE_UID(AID_SYSTEM); |
| 661 | ACQUIRE_CRYPT_LOCK; |
| 662 | |
| 663 | return translateBool(e4crypt_vold_create_user_key(userId, userSerial, ephemeral)); |
| 664 | } |
| 665 | |
| 666 | binder::Status VoldNativeService::destroyUserKey(int32_t userId) { |
| 667 | ENFORCE_UID(AID_SYSTEM); |
| 668 | ACQUIRE_CRYPT_LOCK; |
| 669 | |
| 670 | return translateBool(e4crypt_destroy_user_key(userId)); |
| 671 | } |
| 672 | |
| 673 | binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial, |
| 674 | const std::string& token, const std::string& secret) { |
| 675 | ENFORCE_UID(AID_SYSTEM); |
| 676 | ACQUIRE_CRYPT_LOCK; |
| 677 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 678 | return translateBool(e4crypt_add_user_key_auth(userId, userSerial, token, secret)); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) { |
| 682 | ENFORCE_UID(AID_SYSTEM); |
| 683 | ACQUIRE_CRYPT_LOCK; |
| 684 | |
| 685 | return translateBool(e4crypt_fixate_newest_user_key_auth(userId)); |
| 686 | } |
| 687 | |
| 688 | binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial, |
| 689 | const std::string& token, const std::string& secret) { |
| 690 | ENFORCE_UID(AID_SYSTEM); |
| 691 | ACQUIRE_CRYPT_LOCK; |
| 692 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 693 | return translateBool(e4crypt_unlock_user_key(userId, userSerial, token, secret)); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | binder::Status VoldNativeService::lockUserKey(int32_t userId) { |
| 697 | ENFORCE_UID(AID_SYSTEM); |
| 698 | ACQUIRE_CRYPT_LOCK; |
| 699 | |
| 700 | return translateBool(e4crypt_lock_user_key(userId)); |
| 701 | } |
| 702 | |
| 703 | binder::Status VoldNativeService::prepareUserStorage(const std::unique_ptr<std::string>& uuid, |
| 704 | int32_t userId, int32_t userSerial, int32_t flags) { |
| 705 | ENFORCE_UID(AID_SYSTEM); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 706 | std::string empty_string = ""; |
| 707 | auto uuid_ = uuid ? *uuid : empty_string; |
Paul Crowley | 06f762d | 2017-10-16 10:59:51 -0700 | [diff] [blame] | 708 | CHECK_ARGUMENT_HEX(uuid_); |
| 709 | |
| 710 | ACQUIRE_CRYPT_LOCK; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 711 | return translateBool(e4crypt_prepare_user_storage(uuid_, userId, userSerial, flags)); |
| 712 | } |
| 713 | |
| 714 | binder::Status VoldNativeService::destroyUserStorage(const std::unique_ptr<std::string>& uuid, |
| 715 | int32_t userId, int32_t flags) { |
| 716 | ENFORCE_UID(AID_SYSTEM); |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 717 | std::string empty_string = ""; |
| 718 | auto uuid_ = uuid ? *uuid : empty_string; |
Paul Crowley | 06f762d | 2017-10-16 10:59:51 -0700 | [diff] [blame] | 719 | CHECK_ARGUMENT_HEX(uuid_); |
| 720 | |
| 721 | ACQUIRE_CRYPT_LOCK; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 722 | return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags)); |
| 723 | } |
| 724 | |
| 725 | binder::Status VoldNativeService::secdiscard(const std::string& path) { |
| 726 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame^] | 727 | CHECK_ARGUMENT_PATH(path); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 728 | ACQUIRE_CRYPT_LOCK; |
| 729 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 730 | return translateBool(e4crypt_secdiscard(path)); |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 731 | } |
| 732 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 733 | } // namespace vold |
| 734 | } // namespace android |