blob: 3acc3cc9719b2ea7d48fdbb34889a4ac3a67ea5f [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>
Tom Cherry194b5d12018-05-09 17:38:30 -070028#include <string>
Tom Cherry31525f52018-05-09 18:33:31 -070029#include <vector>
Tom Cherry194b5d12018-05-09 17:38:30 -070030
Tom Cherry31525f52018-05-09 18:33:31 -070031#include <android-base/file.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080032#include <android-base/logging.h>
Tom Cherry31525f52018-05-09 18:33:31 -070033#include <android-base/parseint.h>
Tom Cherry194b5d12018-05-09 17:38:30 -070034#include <android-base/strings.h>
Steven Moreland422a7582019-10-15 14:53:19 -070035#include <hidl/metadata.h>
Tom Cherryb5f2ec02019-11-08 17:54:27 -080036#include <property_info_serializer/property_info_serializer.h>
Tom Cherryde6bd502018-02-13 16:50:08 -080037
38#include "action.h"
39#include "action_manager.h"
40#include "action_parser.h"
Tom Cherry4772f1d2019-07-30 09:34:41 -070041#include "check_builtins.h"
Tom Cherry194b5d12018-05-09 17:38:30 -070042#include "host_import_parser.h"
43#include "host_init_stubs.h"
Daniel Normand2533c32019-08-02 15:13:50 -070044#include "interface_utils.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080045#include "parser.h"
46#include "result.h"
47#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070048#include "service_list.h"
49#include "service_parser.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080050
Tom Cherry31525f52018-05-09 18:33:31 -070051#define EXCLUDE_FS_CONFIG_STRUCTURES
52#include "generated_android_ids.h"
53
Tom Cherry194b5d12018-05-09 17:38:30 -070054using namespace std::literals;
55
Tom Cherry31525f52018-05-09 18:33:31 -070056using android::base::ParseInt;
57using android::base::ReadFileToString;
Tom Cherry194b5d12018-05-09 17:38:30 -070058using android::base::Split;
Tom Cherryb5f2ec02019-11-08 17:54:27 -080059using android::properties::BuildTrie;
60using android::properties::ParsePropertyInfoFile;
61using android::properties::PropertyInfoArea;
62using android::properties::PropertyInfoEntry;
Tom Cherry194b5d12018-05-09 17:38:30 -070063
Tom Cherry3f1bce82019-06-05 09:13:11 -070064static std::vector<std::string> passwd_files;
Tom Cherry31525f52018-05-09 18:33:31 -070065
Tom Cherry3f1bce82019-06-05 09:13:11 -070066static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) {
Tom Cherry31525f52018-05-09 18:33:31 -070067 std::string passwd;
Tom Cherry863d8082018-06-12 14:40:38 -070068 if (!ReadFileToString(passwd_file, &passwd)) {
Tom Cherry31525f52018-05-09 18:33:31 -070069 return {};
70 }
71
72 std::vector<std::pair<std::string, int>> result;
73 auto passwd_lines = Split(passwd, "\n");
74 for (const auto& line : passwd_lines) {
75 auto split_line = Split(line, ":");
76 if (split_line.size() < 3) {
77 continue;
78 }
79 int uid = 0;
80 if (!ParseInt(split_line[2], &uid)) {
81 continue;
82 }
83 result.emplace_back(split_line[0], uid);
84 }
85 return result;
86}
87
Tom Cherry3f1bce82019-06-05 09:13:11 -070088static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
89 std::vector<std::pair<std::string, int>> result;
90 for (const auto& passwd_file : passwd_files) {
91 auto individual_result = GetVendorPasswd(passwd_file);
92 std::move(individual_result.begin(), individual_result.end(),
93 std::back_insert_iterator(result));
94 }
95 return result;
96}
97
Tom Cherryde6bd502018-02-13 16:50:08 -080098passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Tom Cherry31525f52018-05-09 18:33:31 -070099 // This isn't thread safe, but that's okay for our purposes.
100 static char static_name[32] = "";
101 static char static_dir[32] = "/";
102 static char static_shell[32] = "/system/bin/sh";
103 static passwd static_passwd = {
104 .pw_name = static_name,
105 .pw_dir = static_dir,
106 .pw_shell = static_shell,
107 .pw_uid = 0,
108 .pw_gid = 0,
Tom Cherryde6bd502018-02-13 16:50:08 -0800109 };
Tom Cherry31525f52018-05-09 18:33:31 -0700110
111 for (size_t n = 0; n < android_id_count; ++n) {
112 if (!strcmp(android_ids[n].name, login)) {
113 snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
114 static_passwd.pw_uid = android_ids[n].aid;
115 static_passwd.pw_gid = android_ids[n].aid;
116 return &static_passwd;
117 }
118 }
119
120 static const auto vendor_passwd = GetVendorPasswd();
121
122 for (const auto& [name, uid] : vendor_passwd) {
123 if (name == login) {
124 snprintf(static_name, sizeof(static_name), "%s", name.c_str());
125 static_passwd.pw_uid = uid;
126 static_passwd.pw_gid = uid;
127 return &static_passwd;
128 }
129 }
130
Tom Cherry290427b2018-06-14 13:40:20 -0700131 unsigned int oem_uid;
132 if (sscanf(login, "oem_%u", &oem_uid) == 1) {
133 snprintf(static_name, sizeof(static_name), "%s", login);
134 static_passwd.pw_uid = oem_uid;
135 static_passwd.pw_gid = oem_uid;
136 return &static_passwd;
137 }
138
Tom Cherry31525f52018-05-09 18:33:31 -0700139 errno = ENOENT;
140 return nullptr;
Tom Cherryde6bd502018-02-13 16:50:08 -0800141}
142
143namespace android {
144namespace init {
145
Tom Cherry4772f1d2019-07-30 09:34:41 -0700146static Result<void> check_stub(const BuiltinArguments& args) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700147 return {};
Tom Cherryde6bd502018-02-13 16:50:08 -0800148}
149
150#include "generated_stub_builtin_function_map.h"
151
Tom Cherry3f1bce82019-06-05 09:13:11 -0700152void PrintUsage() {
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800153 std::cout << "usage: host_init_verifier [options] <init rc file>\n"
Tom Cherry3f1bce82019-06-05 09:13:11 -0700154 "\n"
155 "Tests an init script for correctness\n"
156 "\n"
157 "-p FILE\tSearch this passwd file for users and groups\n"
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800158 "--property_contexts=FILE\t Use this file for property_contexts\n"
Tom Cherry3f1bce82019-06-05 09:13:11 -0700159 << std::endl;
160}
161
Steven Moreland422a7582019-10-15 14:53:19 -0700162Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy() {
163 InterfaceInheritanceHierarchyMap result;
164 for (const HidlInterfaceMetadata& iface : HidlInterfaceMetadata::all()) {
165 std::set<FQName> inherited_interfaces;
166 for (const std::string& intf : iface.inherited) {
167 FQName fqname;
168 if (!fqname.setTo(intf)) {
169 return Error() << "Unable to parse interface '" << intf << "'";
170 }
171 inherited_interfaces.insert(fqname);
172 }
173 FQName fqname;
174 if (!fqname.setTo(iface.name)) {
175 return Error() << "Unable to parse interface '" << iface.name << "'";
176 }
177 result[fqname] = inherited_interfaces;
178 }
179
180 return result;
181}
182
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800183const PropertyInfoArea* property_info_area;
184
185void HandlePropertyContexts(const std::string& filename,
186 std::vector<PropertyInfoEntry>* property_infos) {
187 auto file_contents = std::string();
188 if (!ReadFileToString(filename, &file_contents)) {
189 PLOG(ERROR) << "Could not read properties from '" << filename << "'";
190 exit(EXIT_FAILURE);
191 }
192
193 auto errors = std::vector<std::string>{};
194 ParsePropertyInfoFile(file_contents, property_infos, &errors);
195 for (const auto& error : errors) {
196 LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
197 }
198 if (!errors.empty()) {
199 exit(EXIT_FAILURE);
200 }
201}
202
Tom Cherryde6bd502018-02-13 16:50:08 -0800203int main(int argc, char** argv) {
Elliott Hughes1be0d142018-05-23 09:16:46 -0700204 android::base::InitLogging(argv, &android::base::StdioLogger);
Tom Cherry194b5d12018-05-09 17:38:30 -0700205 android::base::SetMinimumLogSeverity(android::base::ERROR);
Tom Cherry863d8082018-06-12 14:40:38 -0700206
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800207 auto property_infos = std::vector<PropertyInfoEntry>();
208
Tom Cherry3f1bce82019-06-05 09:13:11 -0700209 while (true) {
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800210 static const char kPropertyContexts[] = "property-contexts=";
Tom Cherry3f1bce82019-06-05 09:13:11 -0700211 static const struct option long_options[] = {
212 {"help", no_argument, nullptr, 'h'},
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800213 {kPropertyContexts, required_argument, nullptr, 0},
Tom Cherry3f1bce82019-06-05 09:13:11 -0700214 {nullptr, 0, nullptr, 0},
215 };
216
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800217 int option_index;
218 int arg = getopt_long(argc, argv, "p:", long_options, &option_index);
Tom Cherry3f1bce82019-06-05 09:13:11 -0700219
220 if (arg == -1) {
221 break;
222 }
223
224 switch (arg) {
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800225 case 0:
226 if (long_options[option_index].name == kPropertyContexts) {
227 HandlePropertyContexts(optarg, &property_infos);
228 }
229 break;
Tom Cherry3f1bce82019-06-05 09:13:11 -0700230 case 'h':
231 PrintUsage();
232 return EXIT_FAILURE;
233 case 'p':
234 passwd_files.emplace_back(optarg);
235 break;
236 default:
237 std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
238 return EXIT_FAILURE;
239 }
Tom Cherryde6bd502018-02-13 16:50:08 -0800240 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700241
Tom Cherry3f1bce82019-06-05 09:13:11 -0700242 argc -= optind;
243 argv += optind;
244
Steven Moreland422a7582019-10-15 14:53:19 -0700245 if (argc != 1) {
Tom Cherry3f1bce82019-06-05 09:13:11 -0700246 PrintUsage();
247 return EXIT_FAILURE;
Tom Cherry194b5d12018-05-09 17:38:30 -0700248 }
249
Steven Moreland422a7582019-10-15 14:53:19 -0700250 auto interface_inheritance_hierarchy_map = ReadInterfaceInheritanceHierarchy();
Daniel Normand2533c32019-08-02 15:13:50 -0700251 if (!interface_inheritance_hierarchy_map) {
252 LOG(ERROR) << interface_inheritance_hierarchy_map.error();
253 return EXIT_FAILURE;
254 }
255 SetKnownInterfaces(*interface_inheritance_hierarchy_map);
256
Tom Cherryb5f2ec02019-11-08 17:54:27 -0800257 std::string serialized_contexts;
258 std::string trie_error;
259 if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts,
260 &trie_error)) {
261 LOG(ERROR) << "Unable to serialize property contexts: " << trie_error;
262 return EXIT_FAILURE;
263 }
264
265 property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_contexts.c_str());
266
Tom Cherryd52a5b32019-07-22 16:05:36 -0700267 const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
Tom Cherryde6bd502018-02-13 16:50:08 -0800268 Action::set_function_map(&function_map);
269 ActionManager& am = ActionManager::GetInstance();
270 ServiceList& sl = ServiceList::GetInstance();
271 Parser parser;
Daniel Normand2533c32019-08-02 15:13:50 -0700272 parser.AddSectionParser("service", std::make_unique<ServiceParser>(
273 &sl, nullptr, *interface_inheritance_hierarchy_map));
Tom Cherryde6bd502018-02-13 16:50:08 -0800274 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherry863d8082018-06-12 14:40:38 -0700275 parser.AddSectionParser("import", std::make_unique<HostImportParser>());
Tom Cherryde6bd502018-02-13 16:50:08 -0800276
Tom Cherry3f1bce82019-06-05 09:13:11 -0700277 if (!parser.ParseConfigFileInsecure(*argv)) {
278 LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
Tom Cherry863d8082018-06-12 14:40:38 -0700279 return EXIT_FAILURE;
Tom Cherryde6bd502018-02-13 16:50:08 -0800280 }
Tom Cherry6737a6b2019-08-05 15:03:58 -0700281 size_t failures = parser.parse_error_count() + am.CheckAllCommands() + sl.CheckAllCommands();
Tom Cherry4772f1d2019-07-30 09:34:41 -0700282 if (failures > 0) {
283 LOG(ERROR) << "Failed to parse init script '" << *argv << "' with " << failures
284 << " errors";
Tom Cherry863d8082018-06-12 14:40:38 -0700285 return EXIT_FAILURE;
Tom Cherryde6bd502018-02-13 16:50:08 -0800286 }
Tom Cherry863d8082018-06-12 14:40:38 -0700287 return EXIT_SUCCESS;
Tom Cherryde6bd502018-02-13 16:50:08 -0800288}
289
290} // namespace init
291} // namespace android
292
293int main(int argc, char** argv) {
Tom Cherry863d8082018-06-12 14:40:38 -0700294 return android::init::main(argc, argv);
Tom Cherryde6bd502018-02-13 16:50:08 -0800295}