1 //
2 // Copyright (C) 2018 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 #include <errno.h>
18 #include <getopt.h>
19 #include <pwd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <fstream>
24 #include <iostream>
25 #include <iterator>
26 #include <map>
27 #include <set>
28 #include <string>
29 #include <vector>
30 
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/parseint.h>
34 #include <android-base/strings.h>
35 #include <generated_android_ids.h>
36 #include <hidl/metadata.h>
37 #include <property_info_parser/property_info_parser.h>
38 #include <property_info_serializer/property_info_serializer.h>
39 
40 #include "action.h"
41 #include "action_manager.h"
42 #include "action_parser.h"
43 #include "check_builtins.h"
44 #include "host_import_parser.h"
45 #include "host_init_stubs.h"
46 #include "interface_utils.h"
47 #include "parser.h"
48 #include "result.h"
49 #include "service.h"
50 #include "service_list.h"
51 #include "service_parser.h"
52 
53 using namespace std::literals;
54 
55 using android::base::EndsWith;
56 using android::base::ParseInt;
57 using android::base::ReadFileToString;
58 using android::base::Split;
59 using android::properties::ParsePropertyInfoFile;
60 using android::properties::PropertyInfoEntry;
61 
62 static std::vector<std::string> passwd_files;
63 
64 // NOTE: Keep this in sync with the order used by init.cpp LoadBootScripts()
65 static const std::vector<std::string> partition_search_order =
66         std::vector<std::string>({"system", "system_ext", "odm", "vendor", "product"});
67 
GetVendorPasswd(const std::string & passwd_file)68 static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) {
69     std::string passwd;
70     if (!ReadFileToString(passwd_file, &passwd)) {
71         return {};
72     }
73 
74     std::vector<std::pair<std::string, int>> result;
75     auto passwd_lines = Split(passwd, "\n");
76     for (const auto& line : passwd_lines) {
77         auto split_line = Split(line, ":");
78         if (split_line.size() < 3) {
79             continue;
80         }
81         int uid = 0;
82         if (!ParseInt(split_line[2], &uid)) {
83             continue;
84         }
85         result.emplace_back(split_line[0], uid);
86     }
87     return result;
88 }
89 
GetVendorPasswd()90 static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
91     std::vector<std::pair<std::string, int>> result;
92     for (const auto& passwd_file : passwd_files) {
93         auto individual_result = GetVendorPasswd(passwd_file);
94         std::move(individual_result.begin(), individual_result.end(),
95                   std::back_insert_iterator(result));
96     }
97     return result;
98 }
99 
getpwnam(const char * login)100 passwd* getpwnam(const char* login) {  // NOLINT: implementing bad function.
101     // This isn't thread safe, but that's okay for our purposes.
102     static char static_name[32] = "";
103     static char static_dir[32] = "/";
104     static char static_shell[32] = "/system/bin/sh";
105     static passwd static_passwd = {
106         .pw_name = static_name,
107         .pw_dir = static_dir,
108         .pw_uid = 0,
109         .pw_gid = 0,
110         .pw_shell = static_shell,
111     };
112 
113     for (size_t n = 0; n < android_id_count; ++n) {
114         if (!strcmp(android_ids[n].name, login)) {
115             snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
116             static_passwd.pw_uid = android_ids[n].aid;
117             static_passwd.pw_gid = android_ids[n].aid;
118             return &static_passwd;
119         }
120     }
121 
122     static const auto vendor_passwd = GetVendorPasswd();
123 
124     for (const auto& [name, uid] : vendor_passwd) {
125         if (name == login) {
126             snprintf(static_name, sizeof(static_name), "%s", name.c_str());
127             static_passwd.pw_uid = uid;
128             static_passwd.pw_gid = uid;
129             return &static_passwd;
130         }
131     }
132 
133     unsigned int oem_uid;
134     if (sscanf(login, "oem_%u", &oem_uid) == 1) {
135         snprintf(static_name, sizeof(static_name), "%s", login);
136         static_passwd.pw_uid = oem_uid;
137         static_passwd.pw_gid = oem_uid;
138         return &static_passwd;
139     }
140 
141     errno = ENOENT;
142     return nullptr;
143 }
144 
145 namespace android {
146 namespace init {
147 
PrintUsage()148 void PrintUsage() {
149     fprintf(stdout, R"(usage: host_init_verifier [options]
150 
151 Tests init script(s) for correctness.
152 
153 Generic options:
154   -p FILE                     Search this passwd file for users and groups.
155   --property_contexts=FILE    Use this file for property_contexts.
156 
157 Single script mode options:
158   [init rc file]              Positional argument; test this init script.
159 
160 Multiple script mode options:
161   --out_system=DIR            Path to the output product directory for the system partition.
162   --out_system_ext=DIR        Path to the output product directory for the system_ext partition.
163   --out_odm=DIR               Path to the output product directory for the odm partition.
164   --out_vendor=DIR            Path to the output product directory for the vendor partition.
165   --out_product=DIR           Path to the output product directory for the product partition.
166 )");
167 }
168 
ReadInterfaceInheritanceHierarchy()169 Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy() {
170     InterfaceInheritanceHierarchyMap result;
171     for (const HidlInterfaceMetadata& iface : HidlInterfaceMetadata::all()) {
172         std::set<FQName> inherited_interfaces;
173         for (const std::string& intf : iface.inherited) {
174             FQName fqname;
175             if (!fqname.setTo(intf)) {
176                 return Error() << "Unable to parse interface '" << intf << "'";
177             }
178             inherited_interfaces.insert(fqname);
179         }
180         FQName fqname;
181         if (!fqname.setTo(iface.name)) {
182             return Error() << "Unable to parse interface '" << iface.name << "'";
183         }
184         result[fqname] = inherited_interfaces;
185     }
186 
187     return result;
188 }
189 
HandlePropertyContexts(const std::string & filename,std::vector<PropertyInfoEntry> * property_infos)190 void HandlePropertyContexts(const std::string& filename,
191                             std::vector<PropertyInfoEntry>* property_infos) {
192     auto file_contents = std::string();
193     if (!ReadFileToString(filename, &file_contents)) {
194         PLOG(ERROR) << "Could not read properties from '" << filename << "'";
195         exit(EXIT_FAILURE);
196     }
197 
198     auto errors = std::vector<std::string>{};
199     ParsePropertyInfoFile(file_contents, true, property_infos, &errors);
200     for (const auto& error : errors) {
201         LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
202     }
203     if (!errors.empty()) {
204         exit(EXIT_FAILURE);
205     }
206 }
207 
main(int argc,char ** argv)208 int main(int argc, char** argv) {
209     android::base::InitLogging(argv, &android::base::StdioLogger);
210     android::base::SetMinimumLogSeverity(android::base::ERROR);
211 
212     auto property_infos = std::vector<PropertyInfoEntry>();
213     std::map<std::string, std::string> partition_map;
214 
215     while (true) {
216         static const char kPropertyContexts[] = "property-contexts=";
217         static const struct option long_options[] = {
218                 {"help", no_argument, nullptr, 'h'},
219                 {kPropertyContexts, required_argument, nullptr, 0},
220                 {"out_system", required_argument, nullptr, 0},
221                 {"out_system_ext", required_argument, nullptr, 0},
222                 {"out_odm", required_argument, nullptr, 0},
223                 {"out_vendor", required_argument, nullptr, 0},
224                 {"out_product", required_argument, nullptr, 0},
225                 {nullptr, 0, nullptr, 0},
226         };
227 
228         int option_index;
229         int arg = getopt_long(argc, argv, "p:", long_options, &option_index);
230 
231         if (arg == -1) {
232             break;
233         }
234 
235         switch (arg) {
236             case 0:
237                 if (long_options[option_index].name == kPropertyContexts) {
238                     HandlePropertyContexts(optarg, &property_infos);
239                 }
240                 for (const auto& p : partition_search_order) {
241                     if (long_options[option_index].name == "out_" + p) {
242                         if (partition_map.find(p) != partition_map.end()) {
243                             PrintUsage();
244                             return EXIT_FAILURE;
245                         }
246                         partition_map[p] =
247                                 EndsWith(optarg, "/") ? optarg : std::string(optarg) + "/";
248                     }
249                 }
250                 break;
251             case 'h':
252                 PrintUsage();
253                 return EXIT_FAILURE;
254             case 'p':
255                 passwd_files.emplace_back(optarg);
256                 break;
257             default:
258                 std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
259                 return EXIT_FAILURE;
260         }
261     }
262 
263     argc -= optind;
264     argv += optind;
265 
266     // If provided, use the partition map to check multiple init rc files.
267     // Otherwise, check a single init rc file.
268     if ((!partition_map.empty() && argc != 0) || (partition_map.empty() && argc != 1)) {
269         PrintUsage();
270         return EXIT_FAILURE;
271     }
272 
273     auto interface_inheritance_hierarchy_map = ReadInterfaceInheritanceHierarchy();
274     if (!interface_inheritance_hierarchy_map.ok()) {
275         LOG(ERROR) << interface_inheritance_hierarchy_map.error();
276         return EXIT_FAILURE;
277     }
278     SetKnownInterfaces(*interface_inheritance_hierarchy_map);
279 
280     if (auto result = InitializeHostPropertyInfoArea(property_infos); !result.ok()) {
281         LOG(ERROR) << result.error();
282         return EXIT_FAILURE;
283     }
284 
285     if (!partition_map.empty()) {
286         std::vector<std::string> vendor_prefixes;
287         for (const auto& partition : {"vendor", "odm"}) {
288             if (partition_map.find(partition) != partition_map.end()) {
289                 vendor_prefixes.push_back(partition_map.at(partition));
290             }
291         }
292         InitializeHostSubcontext(vendor_prefixes);
293     }
294 
295     const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
296     Action::set_function_map(&function_map);
297     ActionManager& am = ActionManager::GetInstance();
298     ServiceList& sl = ServiceList::GetInstance();
299     Parser parser;
300     parser.AddSectionParser("service",
301                             std::make_unique<ServiceParser>(&sl, GetSubcontext(),
302                                                             *interface_inheritance_hierarchy_map));
303     parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, GetSubcontext()));
304     parser.AddSectionParser("import", std::make_unique<HostImportParser>());
305 
306     if (!partition_map.empty()) {
307         for (const auto& p : partition_search_order) {
308             if (partition_map.find(p) != partition_map.end()) {
309                 parser.ParseConfig(partition_map.at(p) + "etc/init");
310             }
311         }
312     } else {
313         if (!parser.ParseConfigFileInsecure(*argv, true /* follow_symlinks */)) {
314           // Follow symlinks as inputs during build execution in Bazel's
315           // execution root are symlinks, unlike Soong or Make.
316             LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
317             return EXIT_FAILURE;
318         }
319     }
320     size_t failures = parser.parse_error_count() + am.CheckAllCommands() + sl.CheckAllCommands();
321     if (failures > 0) {
322         LOG(ERROR) << "Failed to parse init scripts with " << failures << " error(s).";
323         return EXIT_FAILURE;
324     }
325     return EXIT_SUCCESS;
326 }
327 
328 }  // namespace init
329 }  // namespace android
330 
main(int argc,char ** argv)331 int main(int argc, char** argv) {
332     return android::init::main(argc, argv);
333 }
334