Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Fat.h" |
| 18 | #include "PublicVolume.h" |
| 19 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include "VolumeManager.h" |
| 21 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 23 | #include <base/stringprintf.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 25 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | static const char* kFusePath = "/system/bin/sdcard"; |
| 41 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 42 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | |
| 44 | PublicVolume::PublicVolume(dev_t device) : |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 45 | VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) { |
| 46 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 47 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | PublicVolume::~PublicVolume() { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 54 | status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 55 | notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType); |
| 56 | notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid); |
| 57 | notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 58 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | status_t PublicVolume::initAsecStage() { |
| 62 | std::string legacyPath(mRawPath + "/android_secure"); |
| 63 | std::string securePath(mRawPath + "/.android_secure"); |
| 64 | |
| 65 | // Recover legacy secure path |
| 66 | if (!access(legacyPath.c_str(), R_OK | X_OK) |
| 67 | && access(securePath.c_str(), R_OK | X_OK)) { |
| 68 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 69 | PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 74 | if (errno != EEXIST) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 75 | PLOG(WARNING) << getId() << " creating ASEC stage failed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | return -errno; |
| 77 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 80 | BindMount(securePath, kAsecPath); |
| 81 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 82 | return OK; |
| 83 | } |
| 84 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 85 | status_t PublicVolume::doCreate() { |
| 86 | return CreateDeviceNode(mDevPath, mDevice); |
| 87 | } |
| 88 | |
| 89 | status_t PublicVolume::doDestroy() { |
| 90 | return DestroyDeviceNode(mDevPath); |
| 91 | } |
| 92 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 93 | status_t PublicVolume::doMount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 94 | // TODO: expand to support mounting other filesystems |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 95 | readMetadata(); |
| 96 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 97 | if (Fat::check(mDevPath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 98 | LOG(ERROR) << getId() << " failed filesystem check"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 99 | return -EIO; |
| 100 | } |
| 101 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 102 | // Use UUID as stable name, if available |
| 103 | std::string stableName = getId(); |
| 104 | if (!mFsUuid.empty()) { |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 105 | stableName = mFsUuid; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
| 109 | mFusePath = StringPrintf("/storage/%s", stableName.c_str()); |
Jeff Sharkey | 8d15cb9 | 2015-05-13 12:36:48 -0700 | [diff] [blame^] | 110 | setInternalPath(mRawPath); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 111 | setPath(mFusePath); |
| 112 | |
| 113 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 114 | PLOG(ERROR) << getId() << " failed to create mount point " << mRawPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 115 | return -errno; |
| 116 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 117 | if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 118 | PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 119 | return -errno; |
| 120 | } |
| 121 | |
| 122 | if (Fat::doMount(mDevPath.c_str(), mRawPath.c_str(), false, false, false, |
| 123 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 124 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 125 | return -EIO; |
| 126 | } |
| 127 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 128 | if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 129 | initAsecStage(); |
| 130 | } |
| 131 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 132 | // TODO: teach FUSE daemon to protect itself with user-specific GID |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 133 | if (!(mFusePid = fork())) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 134 | if (!(getMountFlags() & MountFlags::kVisible)) { |
| 135 | // TODO: mount so that only system apps can access |
| 136 | if (execl(kFusePath, kFusePath, |
| 137 | "-u", "1023", // AID_MEDIA_RW |
| 138 | "-g", "1023", // AID_MEDIA_RW |
| 139 | mRawPath.c_str(), |
| 140 | mFusePath.c_str(), |
| 141 | NULL)) { |
| 142 | PLOG(ERROR) << "Failed to exec"; |
| 143 | } |
| 144 | } else if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 145 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 146 | "-u", "1023", // AID_MEDIA_RW |
| 147 | "-g", "1023", // AID_MEDIA_RW |
| 148 | "-d", |
| 149 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 150 | mFusePath.c_str(), |
| 151 | NULL)) { |
| 152 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 153 | } |
| 154 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 155 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 156 | "-u", "1023", // AID_MEDIA_RW |
| 157 | "-g", "1023", // AID_MEDIA_RW |
| 158 | "-w", "1023", // AID_MEDIA_RW |
| 159 | "-d", |
| 160 | mRawPath.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 161 | mFusePath.c_str(), |
| 162 | NULL)) { |
| 163 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 167 | PLOG(DEBUG) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 168 | _exit(1); |
| 169 | } |
| 170 | |
| 171 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 172 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 173 | return -errno; |
| 174 | } |
| 175 | |
| 176 | return OK; |
| 177 | } |
| 178 | |
| 179 | status_t PublicVolume::doUnmount() { |
| 180 | if (mFusePid > 0) { |
| 181 | kill(mFusePid, SIGTERM); |
| 182 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 183 | mFusePid = 0; |
| 184 | } |
| 185 | |
Jeff Sharkey | 48d81d3 | 2015-04-12 21:50:32 -0700 | [diff] [blame] | 186 | ForceUnmount(kAsecPath); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 187 | ForceUnmount(mFusePath); |
| 188 | ForceUnmount(mRawPath); |
| 189 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | if (TEMP_FAILURE_RETRY(rmdir(mRawPath.c_str()))) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 191 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mRawPath; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 192 | } |
| 193 | if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 194 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | mFusePath.clear(); |
| 198 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 199 | |
| 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | status_t PublicVolume::doFormat() { |
| 204 | if (Fat::format(mDevPath.c_str(), 0, true)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 205 | LOG(ERROR) << getId() << " failed to format"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 206 | return -errno; |
| 207 | } |
| 208 | return OK; |
| 209 | } |
| 210 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 211 | } // namespace vold |
| 212 | } // namespace android |