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