San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_VOLD_VOLUME_MANAGER_H |
| 18 | #define ANDROID_VOLD_VOLUME_MANAGER_H |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 19 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 24 | #include <list> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 25 | #include <mutex> |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 26 | #include <set> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 27 | #include <string> |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | #include <unordered_set> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 30 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 32 | #include <cutils/multiuser.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 33 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | #include <utils/List.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 35 | #include <utils/Timers.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 37 | #include "android/os/IVoldListener.h" |
| 38 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 39 | #include "model/Disk.h" |
| 40 | #include "model/VolumeBase.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | |
| 42 | class VolumeManager { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 43 | private: |
| 44 | static VolumeManager* sInstance; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 46 | bool mDebug; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 48 | public: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | virtual ~VolumeManager(); |
| 50 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 51 | // TODO: pipe all requests through VM to avoid exposing this lock |
| 52 | std::mutex& getLock() { return mLock; } |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 53 | std::mutex& getCryptLock() { return mCryptLock; } |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 54 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 55 | void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; } |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 56 | android::sp<android::os::IVoldListener> getListener() const { return mListener; } |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 57 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 58 | int start(); |
| 59 | int stop(); |
| 60 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 61 | void handleBlockEvent(NetlinkEvent* evt); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 63 | class DiskSource { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 64 | public: |
| 65 | DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) |
| 66 | : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 67 | |
| 68 | bool matches(const std::string& sysPath) { |
| 69 | return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); |
| 70 | } |
| 71 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 72 | const std::string& getNickname() const { return mNickname; } |
| 73 | int getFlags() const { return mFlags; } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 74 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 75 | private: |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | std::string mSysPattern; |
| 77 | std::string mNickname; |
| 78 | int mFlags; |
| 79 | }; |
| 80 | |
| 81 | void addDiskSource(const std::shared_ptr<DiskSource>& diskSource); |
| 82 | |
| 83 | std::shared_ptr<android::vold::Disk> findDisk(const std::string& id); |
| 84 | std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id); |
| 85 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 86 | template <typename Fn> |
| 87 | std::shared_ptr<android::vold::VolumeBase> findVolumeWithFilter(Fn fn) { |
| 88 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 89 | if (fn(*vol)) { |
| 90 | return vol; |
| 91 | } |
| 92 | } |
| 93 | for (const auto& disk : mDisks) { |
| 94 | for (const auto& vol : disk->getVolumes()) { |
| 95 | if (fn(*vol)) { |
| 96 | return vol; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 104 | void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 105 | |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 106 | const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 107 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 108 | int forgetPartition(const std::string& partGuid, const std::string& fsUuid); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 109 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 110 | int onUserAdded(userid_t userId, int userSerialNumber); |
| 111 | int onUserRemoved(userid_t userId); |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 112 | int onUserStarted(userid_t userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 113 | int onUserStopped(userid_t userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 114 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 115 | void createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 116 | int onSecureKeyguardStateChanged(bool isShowing); |
| 117 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 118 | int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol); |
| 119 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 120 | int remountUid(uid_t uid, int32_t remountMode); |
Ricky Wai | 6b12257 | 2020-02-28 16:30:47 +0000 | [diff] [blame] | 121 | int remountAppStorageDirs(int uid, int pid, const std::vector<std::string>& packageNames); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 122 | |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame^] | 123 | /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */ |
| 124 | int abortFuse(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 125 | /* Reset all internal state, typically during framework boot */ |
| 126 | int reset(); |
| 127 | /* Prepare for device shutdown, safely unmounting all devices */ |
| 128 | int shutdown(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 129 | /* Unmount all volumes, usually for encryption */ |
| 130 | int unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 132 | int updateVirtualDisk(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 133 | int setDebug(bool enable); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 134 | |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [diff] [blame] | 135 | bool forkAndRemountStorage(int uid, int pid, const std::vector<std::string>& packageNames); |
| 136 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 137 | static VolumeManager* Instance(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 138 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 139 | /* |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 140 | * Creates a directory 'path' for an application, automatically creating |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 141 | * directories along the given path if they don't exist yet. |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 142 | * |
| 143 | * Example: |
| 144 | * path = /storage/emulated/0/Android/data/com.foo/files/ |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 145 | * |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 146 | * This function will first match the first part of the path with the volume |
| 147 | * root of any known volumes; in this case, "/storage/emulated/0" matches |
| 148 | * with the volume root of the emulated volume for user 0. |
| 149 | * |
| 150 | * The subseqent part of the path must start with one of the well-known |
| 151 | * Android/ data directories, /Android/data, /Android/obb or |
| 152 | * /Android/media. |
| 153 | * |
| 154 | * The final part of the path is application specific. This function will |
| 155 | * create all directories, including the application-specific ones, and |
| 156 | * set the UID of all app-specific directories below the well-known data |
| 157 | * directories to the 'appUid' argument. In the given example, the UID |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 158 | * of /storage/emulated/0/Android/data/com.foo and |
| 159 | * /storage/emulated/0/Android/data/com.foo/files would be set to 'appUid'. |
| 160 | * |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 161 | * The UID/GID of the parent directories will be set according to the |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 162 | * requirements of the underlying filesystem and are of no concern to the |
| 163 | * caller. |
| 164 | * |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 165 | * If fixupExistingOnly is set, we make sure to fixup any existing dirs and |
| 166 | * files in the passed in path, but only if that path exists; if it doesn't |
| 167 | * exist, this function doesn't create them. |
| 168 | * |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 169 | * Validates that given paths are absolute and that they contain no relative |
| 170 | * "." or ".." paths or symlinks. Last path segment is treated as filename |
| 171 | * and ignored, unless the path ends with "/". Also ensures that path |
| 172 | * belongs to a volume managed by vold. |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 173 | */ |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 174 | int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false); |
| 175 | |
| 176 | /** |
| 177 | * Fixes up an existing application directory, as if it was created with |
| 178 | * setupAppDir() above. This includes fixing up the UID/GID, permissions and |
| 179 | * project IDs of the contained files and directories. |
| 180 | */ |
| 181 | int fixupAppDir(const std::string& path, int32_t appUid); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 182 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 183 | int createObb(const std::string& path, const std::string& key, int32_t ownerGid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 184 | std::string* outVolId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 185 | int destroyObb(const std::string& volId); |
| 186 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 187 | int createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 188 | const std::string& fsType, const std::string& fsUuid, |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 189 | const std::string& fsLabel, int32_t flags, std::string* outVolId); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 190 | int destroyStubVolume(const std::string& volId); |
| 191 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 192 | int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd); |
| 193 | int unmountAppFuse(uid_t uid, int mountId); |
| 194 | int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 195 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 196 | private: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | VolumeManager(); |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 198 | void readInitialState(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 199 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 200 | int linkPrimary(userid_t userId); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 201 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 202 | void createEmulatedVolumesForUser(userid_t userId); |
| 203 | void destroyEmulatedVolumesForUser(userid_t userId); |
| 204 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 205 | void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk); |
| 206 | void handleDiskChanged(dev_t device); |
| 207 | void handleDiskRemoved(dev_t device); |
| 208 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 209 | bool updateFuseMountedProperty(); |
| 210 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 211 | std::mutex mLock; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 212 | std::mutex mCryptLock; |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 213 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 214 | android::sp<android::os::IVoldListener> mListener; |
| 215 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 216 | std::list<std::shared_ptr<DiskSource>> mDiskSources; |
| 217 | std::list<std::shared_ptr<android::vold::Disk>> mDisks; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 218 | std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 219 | std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 220 | std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 222 | std::unordered_map<userid_t, int> mAddedUsers; |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 223 | // This needs to be a regular set because we care about the ordering here; |
| 224 | // user 0 should always go first, because it is responsible for sdcardfs. |
| 225 | std::set<userid_t> mStartedUsers; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 226 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 227 | std::string mVirtualDiskPath; |
| 228 | std::shared_ptr<android::vold::Disk> mVirtualDisk; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 229 | std::shared_ptr<android::vold::VolumeBase> mPrimary; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 230 | |
| 231 | int mNextObbId; |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 232 | int mNextStubId; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 233 | bool mSecureKeyguardShowing; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 234 | }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 235 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 236 | #endif |