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