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> |
David Anderson | 515a5bd | 2020-10-19 19:54:18 -0700 | [diff] [blame] | 25 | #include <filesystem> |
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> |
Bart Van Assche | 564d970 | 2024-05-22 13:07:13 -0700 | [diff] [blame] | 28 | #include <string_view> |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 29 | #include <thread> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 30 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 31 | #include <android-base/chrono_utils.h> |
| 32 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Yi-Yo Chiang | 79ad1e2 | 2023-07-29 17:37:23 +0800 | [diff] [blame] | 36 | #include <fs_mgr.h> |
David Anderson | 59abbfe | 2023-05-16 15:53:57 -0700 | [diff] [blame] | 37 | #include <libdm/dm.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | #include <selinux/android.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 40 | #include <selinux/selinux.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 41 | |
Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame] | 42 | #include "selabel.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 43 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 45 | using namespace std::chrono_literals; |
| 46 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 47 | using android::base::Basename; |
Douglas Anderson | 743e8f1 | 2024-11-06 09:24:50 -0800 | [diff] [blame] | 48 | using android::base::ConsumePrefix; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 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; |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 53 | using android::base::Split; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 54 | using android::base::StartsWith; |
| 55 | using android::base::StringPrintf; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 56 | using android::base::Trim; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 57 | |
| 58 | namespace android { |
| 59 | namespace init { |
| 60 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 61 | /* Given a path that may start with a PCI device, populate the supplied buffer |
| 62 | * with the PCI domain/bus number and the peripheral ID and return 0. |
| 63 | * 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] | 64 | static bool FindPciDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 65 | result->clear(); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 66 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 67 | if (!StartsWith(path, "/devices/pci")) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 68 | |
| 69 | /* Beginning of the prefix is the initial "pci" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 70 | std::string::size_type start = 9; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 71 | |
| 72 | /* End of the prefix is two path '/' later, capturing the domain/bus number |
| 73 | * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 74 | auto end = path.find('/', start); |
| 75 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 76 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 77 | end = path.find('/', end + 1); |
| 78 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 79 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 80 | auto length = end - start; |
| 81 | if (length <= 4) { |
| 82 | // The minimum string that will get to this check is 'pci/', which is malformed, |
| 83 | // so return false |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | *result = path.substr(start, length); |
| 88 | return true; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 91 | /* Given a path that may start with a virtual block device, populate |
| 92 | * the supplied buffer with the virtual block device ID and return 0. |
| 93 | * If it doesn't start with a virtual block device, or there is some |
| 94 | * error, return -1 */ |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 95 | static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 96 | result->clear(); |
| 97 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 98 | if (!StartsWith(path, "/devices/vbd-")) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 99 | |
| 100 | /* Beginning of the prefix is the initial "vbd-" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 101 | std::string::size_type start = 13; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 102 | |
| 103 | /* End of the prefix is one path '/' later, capturing the |
| 104 | virtual block device ID. Example: 768 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 105 | auto end = path.find('/', start); |
| 106 | if (end == std::string::npos) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 107 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 108 | auto length = end - start; |
| 109 | if (length == 0) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 110 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 111 | *result = path.substr(start, length); |
| 112 | return true; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 115 | // Given a path that may start with a virtual dm block device, populate |
| 116 | // the supplied buffer with the dm module's instantiated name. |
| 117 | // If it doesn't start with a virtual block device, or there is some |
| 118 | // error, return false. |
David Anderson | 59abbfe | 2023-05-16 15:53:57 -0700 | [diff] [blame] | 119 | static bool FindDmDevice(const Uevent& uevent, std::string* name, std::string* uuid) { |
| 120 | if (!StartsWith(uevent.path, "/devices/virtual/block/dm-")) return false; |
| 121 | if (uevent.action == "remove") return false; // Avoid error spam from ioctl |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 122 | |
David Anderson | 59abbfe | 2023-05-16 15:53:57 -0700 | [diff] [blame] | 123 | dev_t dev = makedev(uevent.major, uevent.minor); |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 124 | |
David Anderson | 59abbfe | 2023-05-16 15:53:57 -0700 | [diff] [blame] | 125 | auto& dm = android::dm::DeviceMapper::Instance(); |
| 126 | return dm.GetDeviceNameAndUuid(dev, name, uuid); |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 129 | Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, |
| 130 | bool no_fnm_pathname) |
| 131 | : name_(name), |
| 132 | perm_(perm), |
| 133 | uid_(uid), |
| 134 | gid_(gid), |
| 135 | prefix_(false), |
| 136 | wildcard_(false), |
| 137 | no_fnm_pathname_(no_fnm_pathname) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 138 | // Set 'prefix_' or 'wildcard_' based on the below cases: |
| 139 | // |
| 140 | // 1) No '*' in 'name' -> Neither are set and Match() checks a given path for strict |
| 141 | // equality with 'name' |
| 142 | // |
| 143 | // 2) '*' only appears as the last character in 'name' -> 'prefix'_ is set to true and |
| 144 | // Match() checks if 'name' is a prefix of a given path. |
| 145 | // |
| 146 | // 3) '*' appears elsewhere -> 'wildcard_' is set to true and Match() uses fnmatch() |
| 147 | // with FNM_PATHNAME to compare 'name' to a given path. |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 148 | auto wildcard_position = name_.find('*'); |
| 149 | if (wildcard_position != std::string::npos) { |
| 150 | if (wildcard_position == name_.length() - 1) { |
| 151 | prefix_ = true; |
| 152 | name_.pop_back(); |
| 153 | } else { |
| 154 | wildcard_ = true; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | } |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 156 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 159 | bool Permissions::Match(const std::string& path) const { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 160 | if (prefix_) return StartsWith(path, name_); |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 161 | if (wildcard_) |
| 162 | return fnmatch(name_.c_str(), path.c_str(), no_fnm_pathname_ ? 0 : FNM_PATHNAME) == 0; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 163 | return path == name_; |
| 164 | } |
| 165 | |
| 166 | bool SysfsPermissions::MatchWithSubsystem(const std::string& path, |
| 167 | const std::string& subsystem) const { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 168 | std::string path_basename = Basename(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 169 | if (name().find(subsystem) != std::string::npos) { |
| 170 | if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true; |
| 171 | if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true; |
| 172 | } |
| 173 | return Match(path); |
| 174 | } |
| 175 | |
| 176 | void SysfsPermissions::SetPermissions(const std::string& path) const { |
| 177 | std::string attribute_file = path + "/" + attribute_; |
| 178 | LOG(VERBOSE) << "fixup " << attribute_file << " " << uid() << " " << gid() << " " << std::oct |
| 179 | << perm(); |
| 180 | |
| 181 | if (access(attribute_file.c_str(), F_OK) == 0) { |
| 182 | if (chown(attribute_file.c_str(), uid(), gid()) != 0) { |
| 183 | PLOG(ERROR) << "chown(" << attribute_file << ", " << uid() << ", " << gid() |
| 184 | << ") failed"; |
| 185 | } |
| 186 | if (chmod(attribute_file.c_str(), perm()) != 0) { |
| 187 | PLOG(ERROR) << "chmod(" << attribute_file << ", " << perm() << ") failed"; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 192 | BlockDeviceInfo DeviceHandler::GetBlockDeviceInfo(const std::string& uevent_path) const { |
| 193 | BlockDeviceInfo info; |
| 194 | |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 195 | if (!boot_part_uuid_.empty()) { |
Douglas Anderson | dd8edea | 2024-11-14 13:53:54 -0800 | [diff] [blame] | 196 | // Only use the more specific "MMC" / "NVME" / "SCSI" match if a |
| 197 | // partition UUID was passed. |
| 198 | // |
| 199 | // Old bootloaders that aren't passing the partition UUID instead |
| 200 | // pass the path to the closest "platform" device. It would |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 201 | // break them if we chose this deeper (more specific) path. |
| 202 | // |
| 203 | // When we have a UUID we _want_ the more specific path since it can |
| 204 | // handle, for instance, differentiating two USB disks that are on |
| 205 | // the same USB controller. Using the closest platform device would |
| 206 | // classify them both the same by using the path to the USB controller. |
| 207 | if (FindMmcDevice(uevent_path, &info.str)) { |
| 208 | info.type = "mmc"; |
Douglas Anderson | dd8edea | 2024-11-14 13:53:54 -0800 | [diff] [blame] | 209 | } else if (FindNvmeDevice(uevent_path, &info.str)) { |
| 210 | info.type = "nvme"; |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 211 | } else if (FindScsiDevice(uevent_path, &info.str)) { |
| 212 | info.type = "scsi"; |
| 213 | } |
| 214 | } else if (FindPlatformDevice(uevent_path, &info.str)) { |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 215 | info.type = "platform"; |
| 216 | } else if (FindPciDevicePrefix(uevent_path, &info.str)) { |
| 217 | info.type = "pci"; |
| 218 | } else if (FindVbdDevicePrefix(uevent_path, &info.str)) { |
| 219 | info.type = "vbd"; |
| 220 | } else { |
| 221 | // Re-clear device to be extra certain in case one of the FindXXX() |
| 222 | // functions returned false but still modified it. |
| 223 | info.str = ""; |
| 224 | } |
| 225 | |
| 226 | info.is_boot_device = boot_devices_.find(info.str) != boot_devices_.end(); |
| 227 | |
| 228 | return info; |
| 229 | } |
| 230 | |
Douglas Anderson | eb3d280 | 2024-11-05 09:15:25 -0800 | [diff] [blame] | 231 | bool DeviceHandler::IsBootDeviceStrict() const { |
| 232 | // When using the newer "boot_part_uuid" to specify the boot device then |
| 233 | // we require all core system partitions to be on the boot device. |
| 234 | return !boot_part_uuid_.empty(); |
| 235 | } |
| 236 | |
| 237 | bool DeviceHandler::IsBootDevice(const Uevent& uevent) const { |
| 238 | auto device = GetBlockDeviceInfo(uevent.path); |
| 239 | return device.is_boot_device; |
| 240 | } |
| 241 | |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 242 | std::string DeviceHandler::GetPartitionNameForDevice(const std::string& query_device) { |
| 243 | static const auto partition_map = [] { |
| 244 | std::vector<std::pair<std::string, std::string>> partition_map; |
| 245 | auto parser = [&partition_map](const std::string& key, const std::string& value) { |
| 246 | if (key != "androidboot.partition_map") { |
| 247 | return; |
| 248 | } |
| 249 | for (const auto& map : Split(value, ";")) { |
| 250 | auto map_pieces = Split(map, ","); |
| 251 | if (map_pieces.size() != 2) { |
| 252 | LOG(ERROR) << "Expected a comma separated device,partition mapping, but found '" |
| 253 | << map << "'"; |
| 254 | continue; |
| 255 | } |
| 256 | partition_map.emplace_back(map_pieces[0], map_pieces[1]); |
| 257 | } |
| 258 | }; |
Yi-Yo Chiang | da5323e | 2023-08-02 01:08:23 +0800 | [diff] [blame] | 259 | android::fs_mgr::ImportKernelCmdline(parser); |
Yi-Yo Chiang | 79ad1e2 | 2023-07-29 17:37:23 +0800 | [diff] [blame] | 260 | android::fs_mgr::ImportBootconfig(parser); |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 261 | return partition_map; |
| 262 | }(); |
| 263 | |
| 264 | for (const auto& [device, partition] : partition_map) { |
| 265 | if (query_device == device) { |
| 266 | return partition; |
| 267 | } |
| 268 | } |
| 269 | return {}; |
| 270 | } |
| 271 | |
Douglas Anderson | 6519e6d | 2024-10-22 14:09:26 -0700 | [diff] [blame] | 272 | // Given a path to a device that may have a parent in the passed set of |
| 273 | // subsystems, find the parent device that's in the passed set of subsystems. |
| 274 | // If we don't find a parent in the passed set of subsystems, return false. |
| 275 | bool DeviceHandler::FindSubsystemDevice(std::string path, std::string* device_path, |
| 276 | const std::set<std::string>& subsystem_paths) const { |
| 277 | device_path->clear(); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 278 | |
| 279 | // Uevents don't contain the mount point, so we need to add it here. |
| 280 | path.insert(0, sysfs_mount_point_); |
| 281 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 282 | std::string directory = Dirname(path); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 283 | |
| 284 | while (directory != "/" && directory != ".") { |
| 285 | std::string subsystem_link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 286 | if (Realpath(directory + "/subsystem", &subsystem_link_path) && |
Douglas Anderson | 6519e6d | 2024-10-22 14:09:26 -0700 | [diff] [blame] | 287 | subsystem_paths.find(subsystem_link_path) != subsystem_paths.end()) { |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 288 | // We need to remove the mount point that we added above before returning. |
| 289 | directory.erase(0, sysfs_mount_point_.size()); |
Douglas Anderson | 3de05fc | 2024-10-22 14:13:19 -0700 | [diff] [blame] | 290 | |
| 291 | // Skip /devices/platform or /devices/ if present |
| 292 | static constexpr std::string_view devices_platform_prefix = "/devices/platform/"; |
| 293 | static constexpr std::string_view devices_prefix = "/devices/"; |
| 294 | std::string_view sv = directory; |
| 295 | |
| 296 | if (!ConsumePrefix(&sv, devices_platform_prefix)) { |
| 297 | ConsumePrefix(&sv, devices_prefix); |
| 298 | } |
| 299 | *device_path = sv; |
| 300 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 301 | return true; |
| 302 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 303 | |
| 304 | auto last_slash = path.rfind('/'); |
| 305 | if (last_slash == std::string::npos) return false; |
| 306 | |
| 307 | path.erase(last_slash); |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 308 | directory = Dirname(path); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 309 | } |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 310 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
Douglas Anderson | 46afe22 | 2024-11-08 14:38:41 -0800 | [diff] [blame] | 314 | bool DeviceHandler::FindPlatformDevice(const std::string& path, |
| 315 | std::string* platform_device_path) const { |
Douglas Anderson | 6519e6d | 2024-10-22 14:09:26 -0700 | [diff] [blame] | 316 | const std::set<std::string> subsystem_paths = { |
| 317 | sysfs_mount_point_ + "/bus/platform", |
| 318 | sysfs_mount_point_ + "/bus/amba", |
| 319 | }; |
| 320 | |
| 321 | return FindSubsystemDevice(path, platform_device_path, subsystem_paths); |
| 322 | } |
| 323 | |
Douglas Anderson | 46afe22 | 2024-11-08 14:38:41 -0800 | [diff] [blame] | 324 | bool DeviceHandler::FindMmcDevice(const std::string& path, std::string* mmc_device_path) const { |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 325 | const std::set<std::string> subsystem_paths = { |
| 326 | sysfs_mount_point_ + "/bus/mmc", |
| 327 | }; |
| 328 | |
| 329 | return FindSubsystemDevice(path, mmc_device_path, subsystem_paths); |
| 330 | } |
| 331 | |
Douglas Anderson | dd8edea | 2024-11-14 13:53:54 -0800 | [diff] [blame] | 332 | bool DeviceHandler::FindNvmeDevice(const std::string& path, std::string* nvme_device_path) const { |
| 333 | const std::set<std::string> subsystem_paths = { |
| 334 | sysfs_mount_point_ + "/class/nvme", |
| 335 | }; |
| 336 | |
| 337 | return FindSubsystemDevice(path, nvme_device_path, subsystem_paths); |
| 338 | } |
| 339 | |
Douglas Anderson | 46afe22 | 2024-11-08 14:38:41 -0800 | [diff] [blame] | 340 | bool DeviceHandler::FindScsiDevice(const std::string& path, std::string* scsi_device_path) const { |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 341 | const std::set<std::string> subsystem_paths = { |
| 342 | sysfs_mount_point_ + "/bus/scsi", |
| 343 | }; |
| 344 | |
| 345 | return FindSubsystemDevice(path, scsi_device_path, subsystem_paths); |
| 346 | } |
| 347 | |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 348 | void DeviceHandler::TrackDeviceUevent(const Uevent& uevent) { |
| 349 | // No need to track any events if we won't bother handling any bind events |
| 350 | // later. |
| 351 | if (drivers_.size() == 0) return; |
| 352 | |
| 353 | // Only track add, and not for block devices. We don't track remove because |
| 354 | // unbind events may arrive after remove events, so unbind will be the |
| 355 | // trigger to untrack those events. |
| 356 | if ((uevent.action != "add") || uevent.subsystem == "block" || |
| 357 | (uevent.major < 0 || uevent.minor < 0)) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | std::string path = sysfs_mount_point_ + uevent.path + "/device"; |
| 362 | std::string device; |
| 363 | if (!Realpath(path, &device)) return; |
| 364 | |
| 365 | tracked_uevents_.emplace_back(uevent, device); |
| 366 | } |
| 367 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 368 | void DeviceHandler::FixupSysPermissions(const std::string& upath, |
| 369 | const std::string& subsystem) const { |
| 370 | // upaths omit the "/sys" that paths in this list |
| 371 | // contain, so we prepend it... |
| 372 | std::string path = "/sys" + upath; |
| 373 | |
| 374 | for (const auto& s : sysfs_permissions_) { |
| 375 | if (s.MatchWithSubsystem(path, subsystem)) s.SetPermissions(path); |
| 376 | } |
| 377 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 378 | if (!skip_restorecon_ && access(path.c_str(), F_OK) == 0) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 379 | LOG(VERBOSE) << "restorecon_recursive: " << path; |
| 380 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
| 381 | PLOG(ERROR) << "selinux_android_restorecon(" << path << ") failed"; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | std::tuple<mode_t, uid_t, gid_t> DeviceHandler::GetDevicePermissions( |
| 387 | const std::string& path, const std::vector<std::string>& links) const { |
| 388 | // Search the perms list in reverse so that ueventd.$hardware can override ueventd.rc. |
| 389 | for (auto it = dev_permissions_.crbegin(); it != dev_permissions_.crend(); ++it) { |
| 390 | if (it->Match(path) || std::any_of(links.cbegin(), links.cend(), |
| 391 | [it](const auto& link) { return it->Match(link); })) { |
| 392 | return {it->perm(), it->uid(), it->gid()}; |
| 393 | } |
| 394 | } |
| 395 | /* Default if nothing found. */ |
| 396 | return {0600, 0, 0}; |
| 397 | } |
| 398 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 399 | 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] | 400 | const std::vector<std::string>& links) const { |
Eric Caruso | bb4a409 | 2024-08-30 13:34:24 -0400 | [diff] [blame] | 401 | auto [mode, uid, gid] = GetDevicePermissions(path, links); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 402 | mode |= (block ? S_IFBLK : S_IFCHR); |
| 403 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 404 | std::string secontext; |
| 405 | if (!SelabelLookupFileContextBestMatch(path, links, mode, &secontext)) { |
| 406 | PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label"; |
| 407 | return; |
| 408 | } |
| 409 | if (!secontext.empty()) { |
| 410 | setfscreatecon(secontext.c_str()); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 411 | } |
| 412 | |
David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 413 | gid_t new_group = -1; |
| 414 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 415 | dev_t dev = makedev(major, minor); |
| 416 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 417 | * device node. Unforunately changing the euid would prevent creation of |
| 418 | * some device nodes, so the uid has to be set with chown() and is still |
| 419 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 420 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 421 | if (setegid(gid)) { |
| 422 | PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed"; |
| 423 | goto out; |
| 424 | } |
zexin.hou | 76cba8a | 2022-04-14 16:08:17 +0800 | [diff] [blame] | 425 | /* If the node already exists update its SELinux label and the file mode to handle cases when |
| 426 | * it was created with the wrong context and file mode during coldboot procedure. */ |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 427 | if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && !secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 428 | char* fcon = nullptr; |
| 429 | int rc = lgetfilecon(path.c_str(), &fcon); |
| 430 | if (rc < 0) { |
| 431 | PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device"; |
| 432 | goto out; |
| 433 | } |
| 434 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 435 | bool different = fcon != secontext; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 436 | freecon(fcon); |
| 437 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 438 | if (different && lsetfilecon(path.c_str(), secontext.c_str())) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 439 | PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path |
| 440 | << "' device"; |
| 441 | } |
David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 442 | |
| 443 | struct stat s; |
| 444 | if (stat(path.c_str(), &s) == 0) { |
| 445 | if (gid != s.st_gid) { |
| 446 | new_group = gid; |
| 447 | } |
Eric Caruso | 23276f6 | 2024-08-30 10:51:38 -0400 | [diff] [blame] | 448 | if (mode != s.st_mode) { |
| 449 | if (chmod(path.c_str(), mode) != 0) { |
| 450 | PLOG(ERROR) << "Cannot chmod " << path << " to " << mode; |
| 451 | } |
zexin.hou | 76cba8a | 2022-04-14 16:08:17 +0800 | [diff] [blame] | 452 | } |
David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 453 | } else { |
| 454 | PLOG(ERROR) << "Cannot stat " << path; |
| 455 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | out: |
David Anderson | f8825fa | 2021-07-02 20:47:27 -0700 | [diff] [blame] | 459 | if (chown(path.c_str(), uid, new_group) < 0) { |
| 460 | PLOG(ERROR) << "Cannot chown " << path << " " << uid << " " << new_group; |
| 461 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 462 | if (setegid(AID_ROOT)) { |
| 463 | PLOG(FATAL) << "setegid(AID_ROOT) failed"; |
| 464 | } |
| 465 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 466 | if (!secontext.empty()) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 467 | setfscreatecon(nullptr); |
| 468 | } |
| 469 | } |
| 470 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 471 | // replaces any unacceptable characters with '_', the |
| 472 | // length of the resulting string is equal to the input string |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 473 | void SanitizePartitionName(std::string* string) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 474 | const char* accept = |
| 475 | "abcdefghijklmnopqrstuvwxyz" |
| 476 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 477 | "0123456789" |
| 478 | "_-."; |
| 479 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 480 | if (!string) return; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 481 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 482 | std::string::size_type pos = 0; |
| 483 | while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) { |
| 484 | (*string)[pos] = '_'; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 488 | std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 489 | BlockDeviceInfo info; |
Mark Salyzyn | e419a79 | 2019-03-06 15:18:46 -0800 | [diff] [blame] | 490 | std::string partition; |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 491 | std::string uuid; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 492 | |
Douglas Anderson | 9f760f8 | 2024-10-15 14:22:02 -0700 | [diff] [blame] | 493 | if (FindDmDevice(uevent, &partition, &uuid)) { |
| 494 | std::vector<std::string> symlinks = {"/dev/block/mapper/" + partition}; |
| 495 | if (!uuid.empty()) { |
| 496 | symlinks.emplace_back("/dev/block/mapper/by-uuid/" + uuid); |
| 497 | } |
| 498 | return symlinks; |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 499 | } |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 500 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 501 | info = GetBlockDeviceInfo(uevent.path); |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame] | 502 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 503 | if (info.type.empty()) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 504 | return {}; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 505 | } |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 506 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 507 | std::vector<std::string> links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 508 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 509 | LOG(VERBOSE) << "found " << info.type << " device " << info.str; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 510 | |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 511 | auto link_path = "/dev/block/" + info.type + "/" + info.str; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 512 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 513 | if (!uevent.partition_name.empty()) { |
| 514 | std::string partition_name_sanitized(uevent.partition_name); |
| 515 | SanitizePartitionName(&partition_name_sanitized); |
| 516 | if (partition_name_sanitized != uevent.partition_name) { |
| 517 | LOG(VERBOSE) << "Linking partition '" << uevent.partition_name << "' as '" |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 518 | << partition_name_sanitized << "'"; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 519 | } |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 520 | links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 521 | // Adds symlink: /dev/block/by-name/<partition_name>. |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 522 | if (info.is_boot_device) { |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 523 | links.emplace_back("/dev/block/by-name/" + partition_name_sanitized); |
| 524 | } |
Douglas Anderson | 9481f97 | 2024-11-06 09:28:07 -0800 | [diff] [blame] | 525 | } else if (info.is_boot_device) { |
Tom Cherry | 2c56d7c | 2018-12-20 14:36:49 -0800 | [diff] [blame] | 526 | // If we don't have a partition name but we are a partition on a boot device, create a |
| 527 | // symlink of /dev/block/by-name/<device_name> for symmetry. |
| 528 | links.emplace_back("/dev/block/by-name/" + uevent.device_name); |
Tom Cherry | 96e5f9b | 2021-07-30 09:54:36 -0700 | [diff] [blame] | 529 | auto partition_name = GetPartitionNameForDevice(uevent.device_name); |
| 530 | if (!partition_name.empty()) { |
| 531 | links.emplace_back("/dev/block/by-name/" + partition_name); |
| 532 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Jaegeuk Kim | b92e5b5 | 2023-01-12 13:08:15 -0800 | [diff] [blame] | 535 | std::string model; |
| 536 | if (ReadFileToString("/sys/class/block/" + uevent.device_name + "/queue/zoned", &model) && |
| 537 | !StartsWith(model, "none")) { |
| 538 | links.emplace_back("/dev/block/by-name/zoned_device"); |
Bart Van Assche | 262f1e8 | 2024-05-23 11:20:07 -0700 | [diff] [blame] | 539 | links.emplace_back("/dev/sys/block/by-name/zoned_device"); |
Jaegeuk Kim | b92e5b5 | 2023-01-12 13:08:15 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 542 | auto last_slash = uevent.path.rfind('/'); |
| 543 | links.emplace_back(link_path + "/" + uevent.path.substr(last_slash + 1)); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 544 | |
| 545 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 546 | } |
| 547 | |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 548 | static void RemoveDeviceMapperLinks(const std::string& devpath) { |
| 549 | std::vector<std::string> dirs = { |
| 550 | "/dev/block/mapper", |
| 551 | "/dev/block/mapper/by-uuid", |
| 552 | }; |
| 553 | for (const auto& dir : dirs) { |
| 554 | if (access(dir.c_str(), F_OK) != 0) continue; |
| 555 | |
| 556 | std::unique_ptr<DIR, decltype(&closedir)> dh(opendir(dir.c_str()), closedir); |
| 557 | if (!dh) { |
| 558 | PLOG(ERROR) << "Failed to open directory " << dir; |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | struct dirent* dp; |
| 563 | std::string link_path; |
| 564 | while ((dp = readdir(dh.get())) != nullptr) { |
| 565 | if (dp->d_type != DT_LNK) continue; |
| 566 | |
| 567 | auto path = dir + "/" + dp->d_name; |
| 568 | if (Readlink(path, &link_path) && link_path == devpath) { |
| 569 | unlink(path.c_str()); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 575 | 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] | 576 | int major, int minor, const std::vector<std::string>& links) const { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 577 | if (action == "add") { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 578 | MakeDevice(devpath, block, major, minor, links); |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 579 | } |
| 580 | |
David Anderson | d6bf86b | 2022-12-08 09:53:03 -0800 | [diff] [blame] | 581 | // Handle device-mapper nodes. |
| 582 | // On kernels <= 5.10, the "add" event is fired on DM_DEV_CREATE, but does not contain name |
| 583 | // information until DM_TABLE_LOAD - thus, we wait for a "change" event. |
| 584 | // On kernels >= 5.15, the "add" event is fired on DM_TABLE_LOAD, followed by a "change" |
| 585 | // event. |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 586 | if (action == "add" || (action == "change" && StartsWith(devpath, "/dev/block/dm-"))) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 587 | for (const auto& link : links) { |
Bart Van Assche | 262f1e8 | 2024-05-23 11:20:07 -0700 | [diff] [blame] | 588 | std::string target; |
| 589 | if (StartsWith(link, "/dev/block/")) { |
| 590 | target = devpath; |
| 591 | } else if (StartsWith(link, "/dev/sys/block/")) { |
| 592 | target = "/sys/class/block/" + Basename(devpath); |
| 593 | } else { |
| 594 | LOG(ERROR) << "Unrecognized link type: " << link; |
| 595 | continue; |
| 596 | } |
| 597 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 598 | if (!mkdir_recursive(Dirname(link), 0755)) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 599 | PLOG(ERROR) << "Failed to create directory " << Dirname(link); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Jiyong Park | ccce03d | 2025-01-23 13:04:41 +0900 | [diff] [blame] | 602 | // Create symlink and make sure it's correctly labeled |
| 603 | std::string secontext; |
| 604 | // Passing 0 for mode should work. |
| 605 | if (SelabelLookupFileContext(link, 0, &secontext) && !secontext.empty()) { |
| 606 | setfscreatecon(secontext.c_str()); |
| 607 | } |
| 608 | |
| 609 | int rc = symlink(target.c_str(), link.c_str()); |
| 610 | |
| 611 | if (!secontext.empty()) { |
| 612 | int save_errno = errno; |
| 613 | setfscreatecon(nullptr); |
| 614 | errno = save_errno; |
| 615 | } |
| 616 | |
| 617 | if (rc < 0) { |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 618 | if (errno != EEXIST) { |
| 619 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link; |
| 620 | } else if (std::string link_path; |
| 621 | Readlink(link, &link_path) && link_path != devpath) { |
| 622 | PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link |
| 623 | << ", which already links to: " << link_path; |
| 624 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 625 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 626 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 629 | if (action == "remove") { |
David Anderson | 924858c | 2019-06-26 17:00:00 -0700 | [diff] [blame] | 630 | if (StartsWith(devpath, "/dev/block/dm-")) { |
| 631 | RemoveDeviceMapperLinks(devpath); |
| 632 | } |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 633 | for (const auto& link : links) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 634 | std::string link_path; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 635 | if (Readlink(link, &link_path) && link_path == devpath) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 636 | unlink(link.c_str()); |
| 637 | } |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 638 | } |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 639 | unlink(devpath.c_str()); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 640 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 643 | void DeviceHandler::HandleAshmemUevent(const Uevent& uevent) { |
| 644 | if (uevent.device_name == "ashmem") { |
| 645 | static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id"; |
| 646 | std::string boot_id; |
| 647 | if (!ReadFileToString(boot_id_path, &boot_id)) { |
| 648 | PLOG(ERROR) << "Cannot duplicate ashmem device node. Failed to read " << boot_id_path; |
| 649 | return; |
Eric Caruso | bb4a409 | 2024-08-30 13:34:24 -0400 | [diff] [blame] | 650 | } |
Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 651 | boot_id = Trim(boot_id); |
| 652 | |
| 653 | Uevent dup_ashmem_uevent = uevent; |
| 654 | dup_ashmem_uevent.device_name += boot_id; |
| 655 | dup_ashmem_uevent.path += boot_id; |
| 656 | HandleUevent(dup_ashmem_uevent); |
| 657 | } |
| 658 | } |
| 659 | |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 660 | // Check Uevents looking for the kernel's boot partition UUID |
| 661 | // |
| 662 | // When we can stop checking uevents (either because we're done or because |
| 663 | // we weren't looking for the kernel's boot partition UUID) then return |
| 664 | // true. Return false if we're not done yet. |
| 665 | bool DeviceHandler::CheckUeventForBootPartUuid(const Uevent& uevent) { |
| 666 | // If we aren't using boot_part_uuid then we're done. |
| 667 | if (boot_part_uuid_.empty()) { |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | // Finding the boot partition is a one-time thing that we do at init |
| 672 | // time, not steady state. This is because the boot partition isn't |
| 673 | // allowed to go away or change. Once we found the boot partition we don't |
| 674 | // expect to run again. |
| 675 | if (found_boot_part_uuid_) { |
| 676 | LOG(WARNING) << __PRETTY_FUNCTION__ |
| 677 | << " shouldn't run after kernel boot partition is found"; |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | // We only need to look at newly-added block devices. Note that if someone |
| 682 | // is replaying events all existing devices will get "add"ed. |
| 683 | if (uevent.subsystem != "block" || uevent.action != "add") { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | // If it's not the partition we care about then move on. |
| 688 | if (uevent.partition_uuid != boot_part_uuid_) { |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | auto device = GetBlockDeviceInfo(uevent.path); |
| 693 | |
| 694 | LOG(INFO) << "Boot device " << device.str << " found via partition UUID"; |
| 695 | found_boot_part_uuid_ = true; |
| 696 | boot_devices_.clear(); |
| 697 | boot_devices_.insert(device.str); |
| 698 | |
| 699 | return true; |
| 700 | } |
| 701 | |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 702 | void DeviceHandler::HandleBindInternal(std::string driver_name, std::string action, |
| 703 | const Uevent& uevent) { |
| 704 | if (uevent.subsystem == "block") { |
| 705 | LOG(FATAL) << "Tried to handle bind event for block device"; |
| 706 | } |
| 707 | |
| 708 | // Get tracked uevents for all devices that have this uevent's path as |
| 709 | // their canonical device path. Then handle those again if their driver |
| 710 | // is one of the ones we're interested in. |
| 711 | const auto driver = std::find(drivers_.cbegin(), drivers_.cend(), driver_name); |
| 712 | if (driver == drivers_.cend()) return; |
| 713 | |
| 714 | std::string bind_path = sysfs_mount_point_ + uevent.path; |
| 715 | for (const TrackedUevent& tracked : tracked_uevents_) { |
| 716 | if (tracked.canonical_device_path != bind_path) continue; |
| 717 | |
| 718 | LOG(VERBOSE) << "Propagating " << uevent.action << " as " << action << " for " |
| 719 | << uevent.path; |
| 720 | |
| 721 | std::string devpath = driver->ParseDevPath(tracked.uevent); |
| 722 | mkdir_recursive(Dirname(devpath), 0755); |
| 723 | HandleDevice(action, devpath, false, tracked.uevent.major, tracked.uevent.minor, |
| 724 | std::vector<std::string>{}); |
| 725 | } |
| 726 | } |
| 727 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 728 | void DeviceHandler::HandleUevent(const Uevent& uevent) { |
Eric Caruso | bb4a409 | 2024-08-30 13:34:24 -0400 | [diff] [blame] | 729 | if (uevent.action == "add" || uevent.action == "change" || uevent.action == "bind" || |
| 730 | uevent.action == "online") { |
| 731 | FixupSysPermissions(uevent.path, uevent.subsystem); |
| 732 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 733 | |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 734 | if (uevent.action == "bind") { |
| 735 | bound_drivers_[uevent.path] = uevent.driver; |
| 736 | HandleBindInternal(uevent.driver, "add", uevent); |
| 737 | return; |
| 738 | } else if (uevent.action == "unbind") { |
| 739 | if (bound_drivers_.count(uevent.path) == 0) return; |
| 740 | HandleBindInternal(bound_drivers_[uevent.path], "remove", uevent); |
| 741 | |
| 742 | std::string sys_path = sysfs_mount_point_ + uevent.path; |
| 743 | std::erase_if(tracked_uevents_, [&sys_path](const TrackedUevent& tracked) { |
| 744 | return sys_path == tracked.canonical_device_path; |
| 745 | }); |
| 746 | return; |
| 747 | } |
| 748 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 749 | // if it's not a /dev device, nothing to do |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 750 | if (uevent.major < 0 || uevent.minor < 0) return; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 751 | |
Tom Cherry | 3fa4673 | 2017-04-11 14:19:50 -0700 | [diff] [blame] | 752 | std::string devpath; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 753 | std::vector<std::string> links; |
| 754 | bool block = false; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 755 | |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 756 | TrackDeviceUevent(uevent); |
| 757 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 758 | if (uevent.subsystem == "block") { |
| 759 | block = true; |
| 760 | devpath = "/dev/block/" + Basename(uevent.path); |
| 761 | |
| 762 | if (StartsWith(uevent.path, "/devices")) { |
| 763 | links = GetBlockDeviceSymlinks(uevent); |
| 764 | } |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 765 | } else if (const auto subsystem = |
| 766 | std::find(subsystems_.cbegin(), subsystems_.cend(), uevent.subsystem); |
| 767 | subsystem != subsystems_.cend()) { |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 768 | devpath = subsystem->ParseDevPath(uevent); |
Tom Cherry | 9c8d6dd | 2017-08-17 09:38:01 -0700 | [diff] [blame] | 769 | } else if (uevent.subsystem == "usb") { |
| 770 | if (!uevent.device_name.empty()) { |
| 771 | devpath = "/dev/" + uevent.device_name; |
| 772 | } else { |
| 773 | // This imitates the file system that would be created |
| 774 | // if we were using devfs instead. |
| 775 | // Minors are broken up into groups of 128, starting at "001" |
| 776 | int bus_id = uevent.minor / 128 + 1; |
| 777 | int device_id = uevent.minor % 128 + 1; |
| 778 | devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 779 | } |
| 780 | } else if (StartsWith(uevent.subsystem, "usb")) { |
| 781 | // ignore other USB events |
| 782 | return; |
David Anderson | 515a5bd | 2020-10-19 19:54:18 -0700 | [diff] [blame] | 783 | } else if (uevent.subsystem == "misc" && StartsWith(uevent.device_name, "dm-user/")) { |
| 784 | devpath = "/dev/dm-user/" + uevent.device_name.substr(8); |
Jakob Vukalovic | e377432 | 2023-07-11 10:28:53 +0100 | [diff] [blame] | 785 | } else if (uevent.subsystem == "misc" && uevent.device_name == "vfio/vfio") { |
| 786 | devpath = "/dev/" + uevent.device_name; |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 787 | } else { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 788 | devpath = "/dev/" + Basename(uevent.path); |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame] | 789 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 790 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 791 | mkdir_recursive(Dirname(devpath), 0755); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 792 | |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 793 | HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links); |
Tri Vo | ff89b8d | 2019-09-24 13:00:43 -0700 | [diff] [blame] | 794 | |
| 795 | // Duplicate /dev/ashmem device and name it /dev/ashmem<boot_id>. |
| 796 | // TODO(b/111903542): remove once all users of /dev/ashmem are migrated to libcutils API. |
| 797 | HandleAshmemUevent(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 800 | void DeviceHandler::ColdbootDone() { |
Oleksiy Avramchenko | dd5802a | 2018-11-02 10:06:47 +0100 | [diff] [blame] | 801 | skip_restorecon_ = false; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 804 | DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions, |
| 805 | std::vector<SysfsPermissions> sysfs_permissions, |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 806 | std::vector<Subsystem> drivers, std::vector<Subsystem> subsystems, |
| 807 | std::set<std::string> boot_devices, std::string boot_part_uuid, |
| 808 | bool skip_restorecon) |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 809 | : dev_permissions_(std::move(dev_permissions)), |
| 810 | sysfs_permissions_(std::move(sysfs_permissions)), |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 811 | drivers_(std::move(drivers)), |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 812 | subsystems_(std::move(subsystems)), |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 813 | boot_devices_(std::move(boot_devices)), |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 814 | boot_part_uuid_(boot_part_uuid), |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 815 | skip_restorecon_(skip_restorecon), |
Douglas Anderson | e9de310 | 2024-10-22 14:34:30 -0700 | [diff] [blame] | 816 | sysfs_mount_point_("/sys") { |
| 817 | // If both a boot partition UUID and a list of boot devices are |
| 818 | // specified then we ignore the boot_devices in favor of boot_part_uuid. |
| 819 | if (boot_devices_.size() && !boot_part_uuid.empty()) { |
| 820 | LOG(WARNING) << "Both boot_devices and boot_part_uuid provided; ignoring bootdevices"; |
| 821 | boot_devices_.clear(); |
| 822 | } |
| 823 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 824 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 825 | DeviceHandler::DeviceHandler() |
| 826 | : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, |
Eric Caruso | d17d5c5 | 2024-10-28 15:30:11 -0400 | [diff] [blame] | 827 | std::vector<Subsystem>{}, std::vector<Subsystem>{}, std::set<std::string>{}, "", |
| 828 | false) {} |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 829 | |
| 830 | } // namespace init |
| 831 | } // namespace android |