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