blob: db127d3f2b27c23f9007c8ef72045de97ac54f3a [file] [log] [blame]
Tom Cherryde6bd502018-02-13 16:50:08 -08001//
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
Tom Cherryb5f2ec02019-11-08 17:54:27 -080017#include "host_init_verifier.h"
18
Tom Cherry31525f52018-05-09 18:33:31 -070019#include <errno.h>
Tom Cherry3f1bce82019-06-05 09:13:11 -070020#include <getopt.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080021#include <pwd.h>
Tom Cherry31525f52018-05-09 18:33:31 -070022#include <stdio.h>
Tom Cherry863d8082018-06-12 14:40:38 -070023#include <stdlib.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080024
Daniel Norman3f42a762019-07-09 11:00:53 -070025#include <fstream>
Tom Cherry194b5d12018-05-09 17:38:30 -070026#include <iostream>
Tom Cherry3f1bce82019-06-05 09:13:11 -070027#include <iterator>
Daniel Normanf597fa52020-11-09 17:28:24 -080028#include <map>
29#include <set>
Tom Cherry194b5d12018-05-09 17:38:30 -070030#include <string>
Tom Cherry31525f52018-05-09 18:33:31 -070031#include <vector>
Tom Cherry194b5d12018-05-09 17:38:30 -070032
Tom Cherry31525f52018-05-09 18:33:31 -070033#include <android-base/file.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080034#include <android-base/logging.h>
Tom Cherry31525f52018-05-09 18:33:31 -070035#include <android-base/parseint.h>
Tom Cherry194b5d12018-05-09 17:38:30 -070036#include <android-base/strings.h>
Tom Cherry6ad4d0a2020-03-04 13:35:28 -080037#include <generated_android_ids.h>
Steven Moreland422a7582019-10-15 14:53:19 -070038#include <hidl/metadata.h>
Tom Cherryb5f2ec02019-11-08 17:54:27 -080039#include <property_info_serializer/property_info_serializer.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080040
41#include "action.h"
42#include "action_manager.h"
43#include "action_parser.h"
Tom Cherry4772f1d2019-07-30 09:34:41 -070044#include "check_builtins.h"
Tom Cherry194b5d12018-05-09 17:38:30 -070045#include "host_import_parser.h"
46#include "host_init_stubs.h"
Daniel Normand2533c32019-08-02 15:13:50 -070047#include "interface_utils.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080048#include "parser.h"
49#include "result.h"
50#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070051#include "service_list.h"
52#include "service_parser.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080053
Tom Cherry194b5d12018-05-09 17:38:30 -070054using namespace std::literals;
55
Daniel Normanf597fa52020-11-09 17:28:24 -080056using android::base::EndsWith;
Tom Cherry31525f52018-05-09 18:33:31 -070057using android::base::ParseInt;
58using android::base::ReadFileToString;
Tom Cherry194b5d12018-05-09 17:38:30 -070059using android::base::Split;
Tom Cherryb5f2ec02019-11-08 17:54:27 -080060using android::properties::BuildTrie;
61using android::properties::ParsePropertyInfoFile;
62using android::properties::PropertyInfoArea;
63using android::properties::PropertyInfoEntry;
Tom Cherry194b5d12018-05-09 17:38:30 -070064
Tom Cherry3f1bce82019-06-05 09:13:11 -070065static std::vector<std::string> passwd_files;
Tom Cherry31525f52018-05-09 18:33:31 -070066
Daniel Normanf597fa52020-11-09 17:28:24 -080067// NOTE: Keep this in sync with the order used by init.cpp LoadBootScripts()
68static const std::vector<std::string> partition_search_order =
69 std::vector<std::string>({"system", "system_ext", "odm", "vendor", "product"});
70
Tom Cherry3f1bce82019-06-05 09:13:11 -070071static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) {
Tom Cherry31525f52018-05-09 18:33:31 -070072 std::string passwd;
Tom Cherry863d8082018-06-12 14:40:38 -070073 if (!ReadFileToString(passwd_file, &passwd)) {
Tom Cherry31525f52018-05-09 18:33:31 -070074 return {};
75 }
76
77 std::vector<std::pair<std::string, int>> result;
78 auto passwd_lines = Split(passwd, "\n");
79 for (const auto& line : passwd_lines) {
80 auto split_line = Split(line, ":");
81 if (split_line.size() < 3) {
82 continue;
83 }
84 int uid = 0;
85 if (!ParseInt(split_line[2], &uid)) {
86 continue;
87 }
88 result.emplace_back(split_line[0], uid);
89 }
90 return result;
91}
92
Tom Cherry3f1bce82019-06-05 09:13:11 -070093static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
94 std::vector<std::pair<std::string, int>> result;
95 for (const auto& passwd_file : passwd_files) {
96 auto individual_result = GetVendorPasswd(passwd_file);
97 std::move(individual_result.begin(), individual_result.end(),
98 std::back_insert_iterator(result));
99 }
100 return result;
101}
102
Tom Cherryde6bd502018-02-13 16:50:08 -0800103passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Tom Cherry31525f52018-05-09 18:33:31 -0700104 // This isn't thread safe, but that's okay for our purposes.
105 static char static_name[32] = "";
106 static char static_dir[32] = "/";
107 static char static_shell[32] = "/system/bin/sh";
108 static passwd static_passwd = {
109 .pw_name = static_name,
110 .pw_dir = static_dir,
111 .pw_shell = static_shell,
112 .pw_uid = 0,
113 .pw_gid = 0,
Tom Cherryde6bd502018-02-13 16:50:08 -0800114 };
Tom Cherry31525f52018-05-09 18:33:31 -0700115
116 for (size_t n = 0; n < android_id_count; ++n) {
117 if (!strcmp(android_ids[n].name, login)) {
118 snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
119 static_passwd.pw_uid = android_ids[n].aid;
120 static_passwd.pw_gid = android_ids[n].aid;
121 return &static_passwd;
122 }
123 }
124
125 static const auto vendor_passwd = GetVendorPasswd();
126
127 for (const auto& [name, uid] : vendor_passwd) {
128 if (name == login) {
129 snprintf(static_name, sizeof(static_name), "%s", name.c_str());
130 static_passwd.pw_uid = uid;
131 static_passwd.pw_gid = uid;
132 return &static_passwd;
133 }
134 }
135
Tom Cherry290427b2018-06-14 13:40:20 -0700136 unsigned int oem_uid;
137 if (sscanf(login, "oem_%u", &oem_uid) == 1) {
138 snprintf(static_name, sizeof(static_name), "%s", login);
139 static_passwd.pw_uid = oem_uid;
140 static_passwd.pw_gid = oem_uid;
141 return &static_passwd;
142 }
143
Tom Cherry31525f52018-05-09 18:33:31 -0700144 errno = ENOENT;
145 return nullptr;
Tom Cherryde6bd502018-02-13 16:50:08 -0800146}
147
148namespace android {
149namespace init {
150
Tom Cherry4772f1d2019-07-30 09:34:41 -0700151static Result<void> check_stub(const BuiltinArguments& args) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700152 return {};
Tom Cherryde6bd502018-02-13 16:50:08 -0800153}
154
155#include "generated_stub_builtin_function_map.h"
156
Tom Cherry3f1bce82019-06-05 09:13:11 -0700157void PrintUsage() {
Daniel Normanf597fa52020-11-09 17:28:24 -0800158 fprintf(stdout, R"(usage: host_init_verifier [options]
159
160Tests init script(s) for correctness.
161
162Generic options:
163 -p FILE Search this passwd file for users and groups.
164 --property_contexts=FILE Use this file for property_contexts.
165
166Single script mode options:
167 [init rc file] Positional argument; test this init script.
168
169Multiple script mode options:
170 --out_system=DIR Path to the output product directory for the system partition.
171 --out_system_ext=DIR Path to the output product directory for the system_ext partition.
172 --out_odm=DIR Path to the output product directory for the odm partition.
173 --out_vendor=DIR Path to the output product directory for the vendor partition.
174 --out_product=DIR Path to the output product directory for the product partition.
175)");
Tom Cherry3f1bce82019-06-05 09:13:11 -0700176}
177
Steven Moreland422a7582019-10-15 14:53:19 -0700178Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy() {
179 InterfaceInheritanceHierarchyMap result;
180 for (const HidlInterfaceMetadata& iface : HidlInterfaceMetadata::all()) {
181 std::set<FQName> inherited_interfaces;
182 for (const std::string& intf : iface.inherited) {
183 FQName fqname;
184 if (!fqname.setTo(intf)) {
185 return Error() << "Unable to parse interface '" << intf << "'";
186 }
187 inherited_interfaces.insert(fqname);
188 }
189 FQName fqname;
190 if (!fqname.setTo(iface.name)) {
191 return Error() << "Unable to parse interface '" << iface.name << "'";
192 }
193 result[fqname] = inherited_interfaces;
194 }
195
196 return result;
197}
198
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800199const PropertyInfoArea* property_info_area;
200
201void HandlePropertyContexts(const std::string& filename,
202 std::vector<PropertyInfoEntry>* property_infos) {
203 auto file_contents = std::string();
204 if (!ReadFileToString(filename, &file_contents)) {
205 PLOG(ERROR) << "Could not read properties from '" << filename << "'";
206 exit(EXIT_FAILURE);
207 }
208
209 auto errors = std::vector<std::string>{};
Tom Cherry4b077c52019-12-11 07:56:51 -0800210 ParsePropertyInfoFile(file_contents, true, property_infos, &errors);
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800211 for (const auto& error : errors) {
212 LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
213 }
214 if (!errors.empty()) {
215 exit(EXIT_FAILURE);
216 }
217}
218
Tom Cherryde6bd502018-02-13 16:50:08 -0800219int main(int argc, char** argv) {
Elliott Hughes1be0d142018-05-23 09:16:46 -0700220 android::base::InitLogging(argv, &android::base::StdioLogger);
Tom Cherry194b5d12018-05-09 17:38:30 -0700221 android::base::SetMinimumLogSeverity(android::base::ERROR);
Tom Cherry863d8082018-06-12 14:40:38 -0700222
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800223 auto property_infos = std::vector<PropertyInfoEntry>();
Daniel Normanf597fa52020-11-09 17:28:24 -0800224 std::map<std::string, std::string> partition_map;
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800225
Tom Cherry3f1bce82019-06-05 09:13:11 -0700226 while (true) {
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800227 static const char kPropertyContexts[] = "property-contexts=";
Tom Cherry3f1bce82019-06-05 09:13:11 -0700228 static const struct option long_options[] = {
229 {"help", no_argument, nullptr, 'h'},
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800230 {kPropertyContexts, required_argument, nullptr, 0},
Daniel Normanf597fa52020-11-09 17:28:24 -0800231 {"out_system", required_argument, nullptr, 0},
232 {"out_system_ext", required_argument, nullptr, 0},
233 {"out_odm", required_argument, nullptr, 0},
234 {"out_vendor", required_argument, nullptr, 0},
235 {"out_product", required_argument, nullptr, 0},
Tom Cherry3f1bce82019-06-05 09:13:11 -0700236 {nullptr, 0, nullptr, 0},
237 };
238
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800239 int option_index;
240 int arg = getopt_long(argc, argv, "p:", long_options, &option_index);
Tom Cherry3f1bce82019-06-05 09:13:11 -0700241
242 if (arg == -1) {
243 break;
244 }
245
246 switch (arg) {
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800247 case 0:
248 if (long_options[option_index].name == kPropertyContexts) {
249 HandlePropertyContexts(optarg, &property_infos);
250 }
Daniel Normanf597fa52020-11-09 17:28:24 -0800251 for (const auto& p : partition_search_order) {
252 if (long_options[option_index].name == "out_" + p) {
253 if (partition_map.find(p) != partition_map.end()) {
254 PrintUsage();
255 return EXIT_FAILURE;
256 }
257 partition_map[p] =
258 EndsWith(optarg, "/") ? optarg : std::string(optarg) + "/";
259 }
260 }
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800261 break;
Tom Cherry3f1bce82019-06-05 09:13:11 -0700262 case 'h':
263 PrintUsage();
264 return EXIT_FAILURE;
265 case 'p':
266 passwd_files.emplace_back(optarg);
267 break;
268 default:
269 std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
270 return EXIT_FAILURE;
271 }
Tom Cherryde6bd502018-02-13 16:50:08 -0800272 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700273
Tom Cherry3f1bce82019-06-05 09:13:11 -0700274 argc -= optind;
275 argv += optind;
276
Daniel Normanf597fa52020-11-09 17:28:24 -0800277 // If provided, use the partition map to check multiple init rc files.
278 // Otherwise, check a single init rc file.
279 if ((!partition_map.empty() && argc != 0) || (partition_map.empty() && argc != 1)) {
Tom Cherry3f1bce82019-06-05 09:13:11 -0700280 PrintUsage();
281 return EXIT_FAILURE;
Tom Cherry194b5d12018-05-09 17:38:30 -0700282 }
283
Steven Moreland422a7582019-10-15 14:53:19 -0700284 auto interface_inheritance_hierarchy_map = ReadInterfaceInheritanceHierarchy();
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900285 if (!interface_inheritance_hierarchy_map.ok()) {
Daniel Normand2533c32019-08-02 15:13:50 -0700286 LOG(ERROR) << interface_inheritance_hierarchy_map.error();
287 return EXIT_FAILURE;
288 }
289 SetKnownInterfaces(*interface_inheritance_hierarchy_map);
290
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800291 std::string serialized_contexts;
292 std::string trie_error;
293 if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts,
294 &trie_error)) {
295 LOG(ERROR) << "Unable to serialize property contexts: " << trie_error;
296 return EXIT_FAILURE;
297 }
298
299 property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_contexts.c_str());
300
Daniel Normanf597fa52020-11-09 17:28:24 -0800301 if (!partition_map.empty()) {
302 std::vector<std::string> vendor_prefixes;
303 for (const auto& partition : {"vendor", "odm"}) {
304 if (partition_map.find(partition) != partition_map.end()) {
305 vendor_prefixes.push_back(partition_map.at(partition));
306 }
307 }
308 InitializeHostSubcontext(vendor_prefixes);
309 }
310
Tom Cherryd52a5b32019-07-22 16:05:36 -0700311 const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
Tom Cherryde6bd502018-02-13 16:50:08 -0800312 Action::set_function_map(&function_map);
313 ActionManager& am = ActionManager::GetInstance();
314 ServiceList& sl = ServiceList::GetInstance();
315 Parser parser;
Daniel Normanf597fa52020-11-09 17:28:24 -0800316 parser.AddSectionParser("service",
317 std::make_unique<ServiceParser>(&sl, GetSubcontext(),
318 *interface_inheritance_hierarchy_map));
319 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, GetSubcontext()));
Tom Cherry863d8082018-06-12 14:40:38 -0700320 parser.AddSectionParser("import", std::make_unique<HostImportParser>());
Tom Cherryde6bd502018-02-13 16:50:08 -0800321
Daniel Normanf597fa52020-11-09 17:28:24 -0800322 if (!partition_map.empty()) {
323 for (const auto& p : partition_search_order) {
324 if (partition_map.find(p) != partition_map.end()) {
325 parser.ParseConfig(partition_map.at(p) + "etc/init");
326 }
327 }
328 } else {
329 if (!parser.ParseConfigFileInsecure(*argv)) {
330 LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
331 return EXIT_FAILURE;
332 }
Tom Cherryde6bd502018-02-13 16:50:08 -0800333 }
Tom Cherry6737a6b2019-08-05 15:03:58 -0700334 size_t failures = parser.parse_error_count() + am.CheckAllCommands() + sl.CheckAllCommands();
Tom Cherry4772f1d2019-07-30 09:34:41 -0700335 if (failures > 0) {
Daniel Normanf597fa52020-11-09 17:28:24 -0800336 LOG(ERROR) << "Failed to parse init scripts with " << failures << " error(s).";
Tom Cherry863d8082018-06-12 14:40:38 -0700337 return EXIT_FAILURE;
Tom Cherryde6bd502018-02-13 16:50:08 -0800338 }
Tom Cherry863d8082018-06-12 14:40:38 -0700339 return EXIT_SUCCESS;
Tom Cherryde6bd502018-02-13 16:50:08 -0800340}
341
342} // namespace init
343} // namespace android
344
345int main(int argc, char** argv) {
Tom Cherry863d8082018-06-12 14:40:38 -0700346 return android::init::main(argc, argv);
Tom Cherryde6bd502018-02-13 16:50:08 -0800347}