The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 2 | * Copyright (C) 2007 The Android Open Source Project |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 17 | #include "devices.h" |
| 18 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Elliott Hughes | 51056c4 | 2017-05-18 09:13:15 -0700 | [diff] [blame] | 21 | #include <sys/sysmacros.h> |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <map> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 26 | #include <memory> |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <thread> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 29 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 30 | #include <android-base/chrono_utils.h> |
| 31 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
| 36 | #include <selinux/android.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 37 | #include <selinux/selinux.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 38 | |
Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame^] | 39 | #include "selabel.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 40 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
Tom Cherry | e7656b7 | 2017-05-01 17:10:09 -0700 | [diff] [blame] | 42 | #ifdef _INIT_INIT_H |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 43 | #error "Do not include init.h in files used by ueventd; it will expose init's globals" |
Tom Cherry | e7656b7 | 2017-05-01 17:10:09 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 46 | using namespace std::chrono_literals; |
| 47 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 48 | using android::base::Basename; |
| 49 | using android::base::Dirname; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 50 | using android::base::ReadFileToString; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 51 | using android::base::Readlink; |
| 52 | using android::base::Realpath; |
| 53 | using android::base::StartsWith; |
| 54 | using android::base::StringPrintf; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 55 | using android::base::Trim; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 56 | |
| 57 | namespace android { |
| 58 | namespace init { |
| 59 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 60 | /* Given a path that may start with a PCI device, populate the supplied buffer |
| 61 | * with the PCI domain/bus number and the peripheral ID and return 0. |
| 62 | * If it doesn't start with a PCI device, or there is some error, return -1 */ |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 63 | static bool FindPciDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 64 | result->clear(); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 65 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 66 | if (!StartsWith(path, "/devices/pci")) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 67 | |
| 68 | /* Beginning of the prefix is the initial "pci" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 69 | std::string::size_type start = 9; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 70 | |
| 71 | /* End of the prefix is two path '/' later, capturing the domain/bus number |
| 72 | * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 73 | auto end = path.find('/', start); |
| 74 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 75 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 76 | end = path.find('/', end + 1); |
| 77 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 78 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 79 | auto length = end - start; |
| 80 | if (length <= 4) { |
| 81 | // The minimum string that will get to this check is 'pci/', which is malformed, |
| 82 | // so return false |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | *result = path.substr(start, length); |
| 87 | return true; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 90 | /* Given a path that may start with a virtual block device, populate |
| 91 | * the supplied buffer with the virtual block device ID and return 0. |
| 92 | * If it doesn't start with a virtual block device, or there is some |
| 93 | * error, return -1 */ |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 94 | static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 95 | result->clear(); |
| 96 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 97 | if (!StartsWith(path, "/devices/vbd-")) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 98 | |
| 99 | /* Beginning of the prefix is the initial "vbd-" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 100 | std::string::size_type start = 13; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 101 | |
| 102 | /* End of the prefix is one path '/' later, capturing the |
| 103 | virtual block device ID. Example: 768 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 104 | auto end = path.find('/', start); |
| 105 | if (end == std::string::npos) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 106 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 107 | auto length = end - start; |
| 108 | if (length == 0) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 109 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 110 | *result = path.substr(start, length); |
| 111 | return true; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 114 | // Given a path that may start with a virtual dm block device, populate |
| 115 | // the supplied buffer with the dm module's instantiated name. |
| 116 | // If it doesn't start with a virtual block device, or there is some |
| 117 | // error, return false. |
| 118 | static bool FindDmDevicePartition(const std::string& path, std::string* result) { |
| 119 | result->clear(); |
| 120 | if (!StartsWith(path, "/devices/virtual/block/dm-")) return false; |
| 121 | if (getpid() == 1) return false; // first_stage_init has no sepolicy needs |
| 122 | |
| 123 | static std::map<std::string, std::string> cache; |
| 124 | // wait_for_file will not work, the content is also delayed ... |
| 125 | for (android::base::Timer t; t.duration() < 200ms; std::this_thread::sleep_for(10ms)) { |
| 126 | if (ReadFileToString("/sys" + path + "/dm/name", result) && !result->empty()) { |
| 127 | // Got it, set cache with result, when node arrives |
| 128 | cache[path] = *result = Trim(*result); |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | auto it = cache.find(path); |
| 133 | if ((it == cache.end()) || (it->second.empty())) return false; |
| 134 | // Return cached results, when node goes away |
| 135 | *result = it->second; |
| 136 | return true; |
| 137 | } |
| 138 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 139 | Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid) |
| 140 | : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) { |
| 141 | // Set 'prefix_' or 'wildcard_' based on the below cases: |
| 142 | // |
| 143 | // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict |
| 144 | // equality with 'name' |
| 145 | // |
| 146 | // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and |
| 147 | // Match() checks if 'name' is a prefix of a given path. |
| 148 | // |
| 149 | // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch() |
| 150 | // with FNM_PATHNAME to compare 'name' to a given path. |
| 151 | |
| 152 | auto wildcard_position = name_.find('*'); |
| 153 | if (wildcard_position != std::string::npos) { |
| 154 | if (wildcard_position == name_.length() - 1) { |
| 155 | prefix_ = true; |
| 156 | name_.pop_back(); |
| 157 | } else { |
| 158 | wildcard_ = true; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | } |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 160 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 163 | bool Permissions::Match(const std::string& path) const { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 164 | if (prefix_) return StartsWith(path, name_); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 165 | if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0; |
| 166 | return path == name_; |
| 167 | } |
| 168 | |
| 169 | bool SysfsPermissions::MatchWithSubsystem(const std::string& path, |
| 170 | const std::string& subsystem) const { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 171 | std::string path_basename = Basename(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 172 | if (name().find(subsystem) != std::string::npos) { |
| 173 | if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true; |
| 174 | if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true; |
| 175 | } |
| 176 | return Match(path); |
| 177 | } |
| 178 | |
| 179 | void SysfsPermissions::SetPermissions(const std::string& path) const { |
| 180 | std::string attribute_file = path + "/" + attribute_; |
| 181 | LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct |
| 182 | << perm(); |
| 183 | |
| 184 | if (access(attribute_file.c_str(), F_OK) == 0) { |
| 185 | if (chown(attribute_file.c_str(), uid(), gid()) != 0) { |
| 186 | PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid() |
| 187 | << ") failed"; |
| 188 | } |
| 189 | if (chmod(attribute_file.c_str(), perm()) != 0) { |
| 190 | PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed"; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 195 | // Given a path that may start with a platform device, find the parent platform device by finding a |
| 196 | // parent directory with a 'subsystem' symlink that points to the platform bus. |
| 197 | // If it doesn't start with a platform device, return false |
| 198 | bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const { |
| 199 | platform_device_path->clear(); |
| 200 | |
| 201 | // Uevents don't contain the mount point, so we need to add it here. |
| 202 | path.insert(0, sysfs_mount_point_); |
| 203 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 204 | std::string directory = Dirname(path); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 205 | |
| 206 | while (directory != "/" && directory != ".") { |
| 207 | std::string subsystem_link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 208 | if (Realpath(directory + "/subsystem", &subsystem_link_path) && |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 209 | subsystem_link_path == sysfs_mount_point_ + "/bus/platform") { |
| 210 | // We need to remove the mount point that we added above before returning. |
| 211 | directory.erase(0, sysfs_mount_point_.size()); |
| 212 | *platform_device_path = directory; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 213 | return true; |
| 214 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 215 | |
| 216 | auto last_slash = path.rfind('/'); |
| 217 | if (last_slash == std::string::npos) return false; |
| 218 | |
| 219 | path.erase(last_slash); |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 220 | directory = Dirname(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 221 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 222 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | void DeviceHandler::FixupSysPermissions(const std::string& upath, |
| 227 | const std::string& subsystem) const { |
| 228 | // upaths omit the "/sys" that paths in this list |
| 229 | // contain, so we prepend it... |
| 230 | std::string path = "/sys" + upath; |
| 231 | |
| 232 | for (const auto& s : sysfs_permissions_) { |
| 233 | if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path); |
| 234 | } |
| 235 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 236 | if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 237 | LOG(VERBOSE) << "restorecon_recursive: " << path; |
| 238 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
| 239 | PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed"; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions( |
| 245 | const std::string& path, const std::vector<std::string>& links) const { |
| 246 | // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc. |
| 247 | for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) { |
| 248 | if (it->Match(path) || std::any_of(links.cbegin(), links.cend(), |
| 249 | [it](const auto& link) { return it->Match(link); })) { |
| 250 | return {it->perm(), it->uid(), it->gid()}; |
| 251 | } |
| 252 | } |
| 253 | /* Default if nothing found. */ |
| 254 | return {0600, 0, 0}; |
| 255 | } |
| 256 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 257 | void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, int minor, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 258 | const std::vector<std::string>& links) const { |
| 259 | auto[mode, uid, gid] = GetDevicePermissions(path, links); |
| 260 | mode |= (block ? S_IFBLK : S_IFCHR); |
| 261 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 262 | std::string secontext; |
| 263 | if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) { |
| 264 | PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label"; |
| 265 | return; |
| 266 | } |
| 267 | if (!secontext.empty()) { |
| 268 | setfscreatecon(secontext.c_str()); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | dev_t dev = makedev(major, minor); |
| 272 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 273 | * device node. Unforunately changing the euid would prevent creation of |
| 274 | * some device nodes, so the uid has to be set with chown() and is still |
| 275 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 276 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 277 | if (setegid(gid)) { |
| 278 | PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed"; |
| 279 | goto out; |
| 280 | } |
| 281 | /* If the node already exists update its SELinux label to handle cases when |
| 282 | * it was created with the wrong context during coldboot procedure. */ |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 283 | if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 284 | char* fcon = nullptr; |
| 285 | int rc = lgetfilecon(path.c_str(), &fcon); |
| 286 | if (rc < 0) { |
| 287 | PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device"; |
| 288 | goto out; |
| 289 | } |
| 290 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 291 | bool different = fcon != secontext; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 292 | freecon(fcon); |
| 293 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 294 | if (different && lsetfilecon(path.c_str(), secontext.c_str())) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 295 | PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path |
| 296 | << "' device"; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | out: |
| 301 | chown(path.c_str(), uid, -1); |
| 302 | if (setegid(AID_ROOT)) { |
| 303 | PLOG(FATAL) << "setegid(AID_ROOT) failed"; |
| 304 | } |
| 305 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 306 | if (!secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 307 | setfscreatecon(nullptr); |
| 308 | } |
| 309 | } |
| 310 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 311 | // replaces any unacceptable characters with '_', the |
| 312 | // length of the resulting string is equal to the input string |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 313 | void SanitizePartitionName(std::string* string) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 314 | const char* accept = |
| 315 | "abcdefghijklmnopqrstuvwxyz" |
| 316 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 317 | "0123456789" |
| 318 | "_-."; |
| 319 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 320 | if (!string) return; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 321 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 322 | std::string::size_type pos = 0; |
| 323 | while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) { |
| 324 | (*string)[pos] = '_'; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 328 | std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 329 | std::string device; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 330 | std::string type; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 331 | std::string partition; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 332 | |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 333 | if (FindPlatformDevice(uevent.path, &device)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 334 | // Skip /devices/platform or /devices/ if present |
| 335 | static const std::string devices_platform_prefix = "/devices/platform/"; |
| 336 | static const std::string devices_prefix = "/devices/"; |
| 337 | |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 338 | if (StartsWith(device, devices_platform_prefix)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 339 | device = device.substr(devices_platform_prefix.length()); |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 340 | } else if (StartsWith(device, devices_prefix)) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 341 | device = device.substr(devices_prefix.length()); |
| 342 | } |
| 343 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 344 | type = "platform"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 345 | } else if (FindPciDevicePrefix(uevent.path, &device)) { |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 346 | type = "pci"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 347 | } else if (FindVbdDevicePrefix(uevent.path, &device)) { |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 348 | type = "vbd"; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 349 | } else if (FindDmDevicePartition(uevent.path, &partition)) { |
| 350 | return {"/dev/block/mapper/" + partition}; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 351 | } else { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 352 | return {}; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 353 | } |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 354 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 355 | std::vector<std::string> links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 356 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 357 | LOG(VERBOSE) << "found " << type << " device " << device; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 358 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 359 | auto link_path = "/dev/block/" + type + "/" + device; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 360 | |
Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 361 | bool is_boot_device = boot_devices_.find(device) != boot_devices_.end(); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 362 | if (!uevent.partition_name.empty()) { |
| 363 | std::string partition_name_sanitized(uevent.partition_name); |
| 364 | SanitizePartitionName(&partition_name_sanitized); |
| 365 | if (partition_name_sanitized != uevent.partition_name) { |
| 366 | LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '" |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 367 | << partition_name_sanitized << "'"; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 368 | } |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 369 | links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 370 | // Adds symlink: /dev/block/by-name/<partition_name>. |
Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 371 | if (is_boot_device) { |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 372 | links.emplace_back("/dev/block/by-name/" + partition_name_sanitized); |
| 373 | } |
Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 374 | } else if (is_boot_device) { |
| 375 | // If we don't have a partition name but we are a partition on a boot device, create a |
| 376 | // symlink of /dev/block/by-name/<device_name> for symmetry. |
| 377 | links.emplace_back("/dev/block/by-name/" + uevent.device_name); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 380 | auto last_slash = uevent.path.rfind('/'); |
| 381 | links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1)); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 382 | |
| 383 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 386 | void DeviceHandler::HandleDevice(const std::string& action, const std::string& devpath, bool block, |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 387 | int major, int minor, const std::vector<std::string>& links) const { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 388 | if (action == "add") { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 389 | MakeDevice(devpath, block, major, minor, links); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 390 | for (const auto& link : links) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 391 | if (!mkdir_recursive(Dirname(link), 0755)) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 392 | PLOG(ERROR) << "Failed to create directory " << Dirname(link); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 395 | if (symlink(devpath.c_str(), link.c_str())) { |
| 396 | if (errno != EEXIST) { |
| 397 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link; |
| 398 | } else if (std::string link_path; |
| 399 | Readlink(link, &link_path) && link_path != devpath) { |
| 400 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link |
| 401 | << ", which already links to: " << link_path; |
| 402 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 403 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 404 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 407 | if (action == "remove") { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 408 | for (const auto& link : links) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 409 | std::string link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 410 | if (Readlink(link, &link_path) && link_path == devpath) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 411 | unlink(link.c_str()); |
| 412 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 413 | } |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 414 | unlink(devpath.c_str()); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 415 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 418 | void DeviceHandler::HandleUevent(const Uevent& uevent) { |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 419 | if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") { |
| 420 | FixupSysPermissions(uevent.path, uevent.subsystem); |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 421 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 422 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 423 | // if it's not a /dev device, nothing to do |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 424 | if (uevent.major < 0 || uevent.minor < 0) return; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 425 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 426 | std::string devpath; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 427 | std::vector<std::string> links; |
| 428 | bool block = false; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 429 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 430 | if (uevent.subsystem == "block") { |
| 431 | block = true; |
| 432 | devpath = "/dev/block/" + Basename(uevent.path); |
| 433 | |
| 434 | if (StartsWith(uevent.path, "/devices")) { |
| 435 | links = GetBlockDeviceSymlinks(uevent); |
| 436 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 437 | } else if (const auto subsystem = |
| 438 | std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem); |
| 439 | subsystem != subsystems_.cend()) { |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 440 | devpath = subsystem->ParseDevPath(uevent); |
Tom Cherry | 9c8d6dd | 2017-08-17 09:38:01 -0700 | [diff] [blame] | 441 | } else if (uevent.subsystem == "usb") { |
| 442 | if (!uevent.device_name.empty()) { |
| 443 | devpath = "/dev/" + uevent.device_name; |
| 444 | } else { |
| 445 | // This imitates the file system that would be created |
| 446 | // if we were using devfs instead. |
| 447 | // Minors are broken up into groups of 128, starting at "001" |
| 448 | int bus_id = uevent.minor / 128 + 1; |
| 449 | int device_id = uevent.minor % 128 + 1; |
| 450 | devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 451 | } |
| 452 | } else if (StartsWith(uevent.subsystem, "usb")) { |
| 453 | // ignore other USB events |
| 454 | return; |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 455 | } else { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 456 | devpath = "/dev/" + Basename(uevent.path); |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 457 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 458 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 459 | mkdir_recursive(Dirname(devpath), 0755); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 460 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 461 | HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 464 | void DeviceHandler::ColdbootDone() { |
Oleksiy Avramchenko | dd5802a | 2018-11-02 10:06:47 +0100 | [diff] [blame] | 465 | skip_restorecon_ = false; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 468 | DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions, |
| 469 | std::vector<SysfsPermissions> sysfs_permissions, |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 470 | std::vector<Subsystem> subsystems, std::set<std::string> boot_devices, |
| 471 | bool skip_restorecon) |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 472 | : dev_permissions_(std::move(dev_permissions)), |
| 473 | sysfs_permissions_(std::move(sysfs_permissions)), |
| 474 | subsystems_(std::move(subsystems)), |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 475 | boot_devices_(std::move(boot_devices)), |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 476 | skip_restorecon_(skip_restorecon), |
| 477 | sysfs_mount_point_("/sys") {} |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 478 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 479 | DeviceHandler::DeviceHandler() |
| 480 | : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 481 | std::vector<Subsystem>{}, std::set<std::string>{}, false) {} |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 482 | |
| 483 | } // namespace init |
| 484 | } // namespace android |