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 | |
| 17 | #include "VoldNativeService.h" |
| 18 | #include "VolumeManager.h" |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 19 | #include "MoveTask.h" |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 20 | |
| 21 | #include <fstream> |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #ifndef LOG_TAG |
| 29 | #define LOG_TAG "vold" |
| 30 | #endif |
| 31 | |
| 32 | using android::base::StringPrintf; |
| 33 | using std::endl; |
| 34 | |
| 35 | namespace android { |
| 36 | namespace vold { |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | constexpr const char* kDump = "android.permission.DUMP"; |
| 41 | |
| 42 | static binder::Status ok() { |
| 43 | return binder::Status::ok(); |
| 44 | } |
| 45 | |
| 46 | static binder::Status exception(uint32_t code, const std::string& msg) { |
| 47 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 48 | } |
| 49 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 50 | static binder::Status error(const std::string& msg) { |
| 51 | PLOG(ERROR) << msg; |
| 52 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 53 | } |
| 54 | |
| 55 | static binder::Status translate(uint32_t status) { |
| 56 | if (status == 0) { |
| 57 | return binder::Status::ok(); |
| 58 | } else { |
| 59 | return binder::Status::fromExceptionCode(status); |
| 60 | } |
| 61 | } |
| 62 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 63 | binder::Status checkPermission(const char* permission) { |
| 64 | pid_t pid; |
| 65 | uid_t uid; |
| 66 | |
| 67 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), |
| 68 | reinterpret_cast<int32_t*>(&uid))) { |
| 69 | return ok(); |
| 70 | } else { |
| 71 | return exception(binder::Status::EX_SECURITY, |
| 72 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | binder::Status checkUid(uid_t expectedUid) { |
| 77 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 78 | if (uid == expectedUid || uid == AID_ROOT) { |
| 79 | return ok(); |
| 80 | } else { |
| 81 | return exception(binder::Status::EX_SECURITY, |
| 82 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #define ENFORCE_UID(uid) { \ |
| 87 | binder::Status status = checkUid((uid)); \ |
| 88 | if (!status.isOk()) { \ |
| 89 | return status; \ |
| 90 | } \ |
| 91 | } |
| 92 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 93 | #define ACQUIRE_LOCK std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
| 94 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | status_t VoldNativeService::start() { |
| 98 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 99 | status_t ret = BinderService<VoldNativeService>::publish(); |
| 100 | if (ret != android::OK) { |
| 101 | return ret; |
| 102 | } |
| 103 | sp<ProcessState> ps(ProcessState::self()); |
| 104 | ps->startThreadPool(); |
| 105 | ps->giveThreadPoolName(); |
| 106 | return android::OK; |
| 107 | } |
| 108 | |
| 109 | status_t VoldNativeService::dump(int fd, const Vector<String16> & /* args */) { |
| 110 | auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); |
| 111 | const binder::Status dump_permission = checkPermission(kDump); |
| 112 | if (!dump_permission.isOk()) { |
| 113 | out << dump_permission.toString8() << endl; |
| 114 | return PERMISSION_DENIED; |
| 115 | } |
| 116 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 117 | ACQUIRE_LOCK; |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 118 | out << "vold is happy!" << endl; |
| 119 | out.flush(); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 120 | return NO_ERROR; |
| 121 | } |
| 122 | |
| 123 | binder::Status VoldNativeService::reset() { |
| 124 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 125 | ACQUIRE_LOCK; |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 126 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 127 | return translate(VolumeManager::Instance()->reset()); |
| 128 | } |
| 129 | |
| 130 | binder::Status VoldNativeService::shutdown() { |
| 131 | ENFORCE_UID(AID_SYSTEM); |
| 132 | ACQUIRE_LOCK; |
| 133 | |
| 134 | return translate(VolumeManager::Instance()->shutdown()); |
| 135 | } |
| 136 | |
| 137 | binder::Status VoldNativeService::setDebug(bool debug) { |
| 138 | ENFORCE_UID(AID_SYSTEM); |
| 139 | ACQUIRE_LOCK; |
| 140 | |
| 141 | return translate(VolumeManager::Instance()->setDebug(debug)); |
| 142 | } |
| 143 | |
| 144 | binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) { |
| 145 | ENFORCE_UID(AID_SYSTEM); |
| 146 | ACQUIRE_LOCK; |
| 147 | |
| 148 | return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial)); |
| 149 | } |
| 150 | |
| 151 | binder::Status VoldNativeService::onUserRemoved(int32_t userId) { |
| 152 | ENFORCE_UID(AID_SYSTEM); |
| 153 | ACQUIRE_LOCK; |
| 154 | |
| 155 | return translate(VolumeManager::Instance()->onUserRemoved(userId)); |
| 156 | } |
| 157 | |
| 158 | binder::Status VoldNativeService::onUserStarted(int32_t userId) { |
| 159 | ENFORCE_UID(AID_SYSTEM); |
| 160 | ACQUIRE_LOCK; |
| 161 | |
| 162 | return translate(VolumeManager::Instance()->onUserStarted(userId)); |
| 163 | } |
| 164 | |
| 165 | binder::Status VoldNativeService::onUserStopped(int32_t userId) { |
| 166 | ENFORCE_UID(AID_SYSTEM); |
| 167 | ACQUIRE_LOCK; |
| 168 | |
| 169 | return translate(VolumeManager::Instance()->onUserStopped(userId)); |
| 170 | } |
| 171 | |
| 172 | binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType, int32_t ratio) { |
| 173 | ENFORCE_UID(AID_SYSTEM); |
| 174 | ACQUIRE_LOCK; |
| 175 | |
| 176 | auto disk = VolumeManager::Instance()->findDisk(diskId); |
| 177 | if (disk == nullptr) { |
| 178 | return error("Failed to find disk " + diskId); |
| 179 | } |
| 180 | switch (partitionType) { |
| 181 | case PARTITION_TYPE_PUBLIC: return translate(disk->partitionPublic()); |
| 182 | case PARTITION_TYPE_PRIVATE: return translate(disk->partitionPrivate()); |
| 183 | case PARTITION_TYPE_MIXED: return translate(disk->partitionMixed(ratio)); |
| 184 | default: return error("Unknown type " + std::to_string(partitionType)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | binder::Status VoldNativeService::forgetPartition(const std::string& partGuid) { |
| 189 | ENFORCE_UID(AID_SYSTEM); |
| 190 | ACQUIRE_LOCK; |
| 191 | |
| 192 | return translate(VolumeManager::Instance()->forgetPartition(partGuid)); |
| 193 | } |
| 194 | |
| 195 | binder::Status VoldNativeService::mount(const std::string& volId, int32_t mountFlags, int32_t mountUserId) { |
| 196 | ENFORCE_UID(AID_SYSTEM); |
| 197 | ACQUIRE_LOCK; |
| 198 | |
| 199 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 200 | if (vol == nullptr) { |
| 201 | return error("Failed to find volume " + volId); |
| 202 | } |
| 203 | |
| 204 | vol->setMountFlags(mountFlags); |
| 205 | vol->setMountUserId(mountUserId); |
| 206 | |
| 207 | int res = vol->mount(); |
| 208 | if (mountFlags & MOUNT_FLAG_PRIMARY) { |
| 209 | VolumeManager::Instance()->setPrimary(vol); |
| 210 | } |
| 211 | return translate(res); |
| 212 | } |
| 213 | |
| 214 | binder::Status VoldNativeService::unmount(const std::string& volId) { |
| 215 | ENFORCE_UID(AID_SYSTEM); |
| 216 | ACQUIRE_LOCK; |
| 217 | |
| 218 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 219 | if (vol == nullptr) { |
| 220 | return error("Failed to find volume " + volId); |
| 221 | } |
| 222 | return translate(vol->unmount()); |
| 223 | } |
| 224 | |
| 225 | binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) { |
| 226 | ENFORCE_UID(AID_SYSTEM); |
| 227 | ACQUIRE_LOCK; |
| 228 | |
| 229 | auto vol = VolumeManager::Instance()->findVolume(volId); |
| 230 | if (vol == nullptr) { |
| 231 | return error("Failed to find volume " + volId); |
| 232 | } |
| 233 | return translate(vol->format(fsType)); |
| 234 | } |
| 235 | |
| 236 | binder::Status VoldNativeService::benchmark(const std::string& volId, int64_t* _aidl_return) { |
| 237 | ENFORCE_UID(AID_SYSTEM); |
| 238 | ACQUIRE_LOCK; |
| 239 | |
| 240 | *_aidl_return = VolumeManager::Instance()->benchmarkPrivate(volId); |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 241 | return ok(); |
| 242 | } |
| 243 | |
Jeff Sharkey | 9462bdd | 2017-09-07 15:27:28 -0600 | [diff] [blame^] | 244 | binder::Status VoldNativeService::moveStorage(const std::string& fromVolId, const std::string& toVolId) { |
| 245 | ENFORCE_UID(AID_SYSTEM); |
| 246 | ACQUIRE_LOCK; |
| 247 | |
| 248 | auto fromVol = VolumeManager::Instance()->findVolume(fromVolId); |
| 249 | auto toVol = VolumeManager::Instance()->findVolume(toVolId); |
| 250 | if (fromVol == nullptr) { |
| 251 | return error("Failed to find volume " + fromVolId); |
| 252 | } else if (toVol == nullptr) { |
| 253 | return error("Failed to find volume " + toVolId); |
| 254 | } |
| 255 | (new android::vold::MoveTask(fromVol, toVol))->start(); |
| 256 | return ok(); |
| 257 | } |
| 258 | |
| 259 | binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) { |
| 260 | ENFORCE_UID(AID_SYSTEM); |
| 261 | ACQUIRE_LOCK; |
| 262 | |
| 263 | std::string tmp; |
| 264 | switch (remountMode) { |
| 265 | case REMOUNT_MODE_NONE: tmp = "none"; break; |
| 266 | case REMOUNT_MODE_DEFAULT: tmp = "default"; break; |
| 267 | case REMOUNT_MODE_READ: tmp = "read"; break; |
| 268 | case REMOUNT_MODE_WRITE: tmp = "write"; break; |
| 269 | default: return error("Unknown mode " + std::to_string(remountMode)); |
| 270 | } |
| 271 | return translate(VolumeManager::Instance()->remountUid(uid, tmp)); |
| 272 | } |
| 273 | |
| 274 | binder::Status VoldNativeService::mkdirs(const std::string& path) { |
| 275 | ENFORCE_UID(AID_SYSTEM); |
| 276 | ACQUIRE_LOCK; |
| 277 | |
| 278 | return translate(VolumeManager::Instance()->mkdirs(path.c_str())); |
| 279 | } |
| 280 | |
Jeff Sharkey | 068c6be | 2017-09-06 13:47:40 -0600 | [diff] [blame] | 281 | } // namespace vold |
| 282 | } // namespace android |