1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _INIT_DEVICES_H
18 #define _INIT_DEVICES_H
19 
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <algorithm>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include <android-base/file.h>
29 #include <selinux/label.h>
30 
31 #include "uevent.h"
32 #include "uevent_handler.h"
33 
34 namespace android {
35 namespace init {
36 
37 class Permissions {
38   public:
39     friend void TestPermissions(const Permissions& expected, const Permissions& test);
40 
41     Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid, bool no_fnm_pathname);
42 
43     bool Match(const std::string& path) const;
44 
perm()45     mode_t perm() const { return perm_; }
uid()46     uid_t uid() const { return uid_; }
gid()47     gid_t gid() const { return gid_; }
48 
49   protected:
name()50     const std::string& name() const { return name_; }
51 
52   private:
53     std::string name_;
54     mode_t perm_;
55     uid_t uid_;
56     gid_t gid_;
57     bool prefix_;
58     bool wildcard_;
59     bool no_fnm_pathname_;
60 };
61 
62 class SysfsPermissions : public Permissions {
63   public:
64     friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
65 
SysfsPermissions(const std::string & name,const std::string & attribute,mode_t perm,uid_t uid,gid_t gid,bool no_fnm_pathname)66     SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
67                      gid_t gid, bool no_fnm_pathname)
68         : Permissions(name, perm, uid, gid, no_fnm_pathname), attribute_(attribute) {}
69 
70     bool MatchWithSubsystem(const std::string& path, const std::string& subsystem) const;
71     void SetPermissions(const std::string& path) const;
72 
73   private:
74     const std::string attribute_;
75 };
76 
77 class Subsystem {
78   public:
79     friend class SubsystemParser;
80     friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
81 
82     enum DevnameSource {
83         DEVNAME_UEVENT_DEVNAME,
84         DEVNAME_UEVENT_DEVPATH,
85         DEVNAME_SYS_NAME,
86     };
87 
Subsystem()88     Subsystem() {}
Subsystem(std::string name)89     Subsystem(std::string name) : name_(std::move(name)) {}
Subsystem(std::string name,DevnameSource source,std::string dir_name)90     Subsystem(std::string name, DevnameSource source, std::string dir_name)
91         : name_(std::move(name)), devname_source_(source), dir_name_(std::move(dir_name)) {}
92 
93     // Returns the full path for a uevent of a device that is a member of this subsystem,
94     // according to the rules parsed from ueventd.rc
ParseDevPath(const Uevent & uevent)95     std::string ParseDevPath(const Uevent& uevent) const {
96         std::string devname;
97         if (devname_source_ == DEVNAME_UEVENT_DEVNAME) {
98             devname = uevent.device_name;
99         } else if (devname_source_ == DEVNAME_UEVENT_DEVPATH) {
100             devname = android::base::Basename(uevent.path);
101         } else if (devname_source_ == DEVNAME_SYS_NAME) {
102             if (android::base::ReadFileToString("/sys/" + uevent.path + "/name", &devname)) {
103                 devname.pop_back();  // Remove terminating newline
104             } else {
105                 devname = uevent.device_name;
106             }
107         }
108         return dir_name_ + "/" + devname;
109     }
110 
111     bool operator==(const std::string& string_name) const { return name_ == string_name; }
112 
113   private:
114     std::string name_;
115     DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
116     std::string dir_name_ = "/dev";
117 };
118 
119 class DeviceHandler : public UeventHandler {
120   public:
121     friend class DeviceHandlerTester;
122 
123     DeviceHandler();
124     DeviceHandler(std::vector<Permissions> dev_permissions,
125                   std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems,
126                   std::set<std::string> boot_devices, bool skip_restorecon);
127     virtual ~DeviceHandler() = default;
128 
129     void HandleUevent(const Uevent& uevent) override;
130 
131     // `androidboot.partition_map` allows associating a partition name for a raw block device
132     // through a comma separated and semicolon deliminated list. For example,
133     // `androidboot.partition_map=vdb,metadata;vdc,userdata` maps `vdb` to `metadata` and `vdc` to
134     // `userdata`.
135     static std::string GetPartitionNameForDevice(const std::string& device);
136 
137   private:
138     void ColdbootDone() override;
139     bool FindPlatformDevice(std::string path, std::string* platform_device_path) const;
140     std::tuple<mode_t, uid_t, gid_t> GetDevicePermissions(
141         const std::string& path, const std::vector<std::string>& links) const;
142     void MakeDevice(const std::string& path, bool block, int major, int minor,
143                     const std::vector<std::string>& links) const;
144     std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
145     void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
146                       int minor, const std::vector<std::string>& links) const;
147     void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
148     void HandleAshmemUevent(const Uevent& uevent);
149 
150     std::vector<Permissions> dev_permissions_;
151     std::vector<SysfsPermissions> sysfs_permissions_;
152     std::vector<Subsystem> subsystems_;
153     std::set<std::string> boot_devices_;
154     bool skip_restorecon_;
155     std::string sysfs_mount_point_;
156 };
157 
158 // Exposed for testing
159 void SanitizePartitionName(std::string* string);
160 
161 }  // namespace init
162 }  // namespace android
163 
164 #endif
165