| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 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 | |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 17 | #include "host_init_verifier.h" |
| 18 | |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 20 | #include <getopt.h> |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 21 | #include <pwd.h> |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 24 | |
| Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 25 | #include <fstream> |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 26 | #include <iostream> |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 27 | #include <iterator> |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 28 | #include <string> |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 29 | #include <vector> |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 30 | |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 33 | #include <android-base/parseint.h> |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> |
| Tom Cherry | 6ad4d0a | 2020-03-04 13:35:28 -0800 | [diff] [blame] | 35 | #include <generated_android_ids.h> |
| Steven Moreland | 422a758 | 2019-10-15 14:53:19 -0700 | [diff] [blame] | 36 | #include <hidl/metadata.h> |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 37 | #include <property_info_serializer/property_info_serializer.h> |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 38 | |
| 39 | #include "action.h" |
| 40 | #include "action_manager.h" |
| 41 | #include "action_parser.h" |
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 42 | #include "check_builtins.h" |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 43 | #include "host_import_parser.h" |
| 44 | #include "host_init_stubs.h" |
| Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame] | 45 | #include "interface_utils.h" |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 46 | #include "parser.h" |
| 47 | #include "result.h" |
| 48 | #include "service.h" |
| Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 49 | #include "service_list.h" |
| 50 | #include "service_parser.h" |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 51 | |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 52 | using namespace std::literals; |
| 53 | |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 54 | using android::base::ParseInt; |
| 55 | using android::base::ReadFileToString; |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 56 | using android::base::Split; |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 57 | using android::properties::BuildTrie; |
| 58 | using android::properties::ParsePropertyInfoFile; |
| 59 | using android::properties::PropertyInfoArea; |
| 60 | using android::properties::PropertyInfoEntry; |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 61 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 62 | static std::vector<std::string> passwd_files; |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 63 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 64 | static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) { |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 65 | std::string passwd; |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 66 | if (!ReadFileToString(passwd_file, &passwd)) { |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 67 | return {}; |
| 68 | } |
| 69 | |
| 70 | std::vector<std::pair<std::string, int>> result; |
| 71 | auto passwd_lines = Split(passwd, "\n"); |
| 72 | for (const auto& line : passwd_lines) { |
| 73 | auto split_line = Split(line, ":"); |
| 74 | if (split_line.size() < 3) { |
| 75 | continue; |
| 76 | } |
| 77 | int uid = 0; |
| 78 | if (!ParseInt(split_line[2], &uid)) { |
| 79 | continue; |
| 80 | } |
| 81 | result.emplace_back(split_line[0], uid); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 86 | static std::vector<std::pair<std::string, int>> GetVendorPasswd() { |
| 87 | std::vector<std::pair<std::string, int>> result; |
| 88 | for (const auto& passwd_file : passwd_files) { |
| 89 | auto individual_result = GetVendorPasswd(passwd_file); |
| 90 | std::move(individual_result.begin(), individual_result.end(), |
| 91 | std::back_insert_iterator(result)); |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 96 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 97 | // This isn't thread safe, but that's okay for our purposes. |
| 98 | static char static_name[32] = ""; |
| 99 | static char static_dir[32] = "/"; |
| 100 | static char static_shell[32] = "/system/bin/sh"; |
| 101 | static passwd static_passwd = { |
| 102 | .pw_name = static_name, |
| 103 | .pw_dir = static_dir, |
| 104 | .pw_shell = static_shell, |
| 105 | .pw_uid = 0, |
| 106 | .pw_gid = 0, |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 107 | }; |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 108 | |
| 109 | for (size_t n = 0; n < android_id_count; ++n) { |
| 110 | if (!strcmp(android_ids[n].name, login)) { |
| 111 | snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name); |
| 112 | static_passwd.pw_uid = android_ids[n].aid; |
| 113 | static_passwd.pw_gid = android_ids[n].aid; |
| 114 | return &static_passwd; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static const auto vendor_passwd = GetVendorPasswd(); |
| 119 | |
| 120 | for (const auto& [name, uid] : vendor_passwd) { |
| 121 | if (name == login) { |
| 122 | snprintf(static_name, sizeof(static_name), "%s", name.c_str()); |
| 123 | static_passwd.pw_uid = uid; |
| 124 | static_passwd.pw_gid = uid; |
| 125 | return &static_passwd; |
| 126 | } |
| 127 | } |
| 128 | |
| Tom Cherry | 290427b | 2018-06-14 13:40:20 -0700 | [diff] [blame] | 129 | unsigned int oem_uid; |
| 130 | if (sscanf(login, "oem_%u", &oem_uid) == 1) { |
| 131 | snprintf(static_name, sizeof(static_name), "%s", login); |
| 132 | static_passwd.pw_uid = oem_uid; |
| 133 | static_passwd.pw_gid = oem_uid; |
| 134 | return &static_passwd; |
| 135 | } |
| 136 | |
| Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 137 | errno = ENOENT; |
| 138 | return nullptr; |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | namespace android { |
| 142 | namespace init { |
| 143 | |
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 144 | static Result<void> check_stub(const BuiltinArguments& args) { |
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 145 | return {}; |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #include "generated_stub_builtin_function_map.h" |
| 149 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 150 | void PrintUsage() { |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 151 | std::cout << "usage: host_init_verifier [options] <init rc file>\n" |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 152 | "\n" |
| 153 | "Tests an init script for correctness\n" |
| 154 | "\n" |
| 155 | "-p FILE\tSearch this passwd file for users and groups\n" |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 156 | "--property_contexts=FILE\t Use this file for property_contexts\n" |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 157 | << std::endl; |
| 158 | } |
| 159 | |
| Steven Moreland | 422a758 | 2019-10-15 14:53:19 -0700 | [diff] [blame] | 160 | Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy() { |
| 161 | InterfaceInheritanceHierarchyMap result; |
| 162 | for (const HidlInterfaceMetadata& iface : HidlInterfaceMetadata::all()) { |
| 163 | std::set<FQName> inherited_interfaces; |
| 164 | for (const std::string& intf : iface.inherited) { |
| 165 | FQName fqname; |
| 166 | if (!fqname.setTo(intf)) { |
| 167 | return Error() << "Unable to parse interface '" << intf << "'"; |
| 168 | } |
| 169 | inherited_interfaces.insert(fqname); |
| 170 | } |
| 171 | FQName fqname; |
| 172 | if (!fqname.setTo(iface.name)) { |
| 173 | return Error() << "Unable to parse interface '" << iface.name << "'"; |
| 174 | } |
| 175 | result[fqname] = inherited_interfaces; |
| 176 | } |
| 177 | |
| 178 | return result; |
| 179 | } |
| 180 | |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 181 | const PropertyInfoArea* property_info_area; |
| 182 | |
| 183 | void HandlePropertyContexts(const std::string& filename, |
| 184 | std::vector<PropertyInfoEntry>* property_infos) { |
| 185 | auto file_contents = std::string(); |
| 186 | if (!ReadFileToString(filename, &file_contents)) { |
| 187 | PLOG(ERROR) << "Could not read properties from '" << filename << "'"; |
| 188 | exit(EXIT_FAILURE); |
| 189 | } |
| 190 | |
| 191 | auto errors = std::vector<std::string>{}; |
| Tom Cherry | 4b077c5 | 2019-12-11 07:56:51 -0800 | [diff] [blame] | 192 | ParsePropertyInfoFile(file_contents, true, property_infos, &errors); |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 193 | for (const auto& error : errors) { |
| 194 | LOG(ERROR) << "Could not read line from '" << filename << "': " << error; |
| 195 | } |
| 196 | if (!errors.empty()) { |
| 197 | exit(EXIT_FAILURE); |
| 198 | } |
| 199 | } |
| 200 | |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 201 | int main(int argc, char** argv) { |
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 202 | android::base::InitLogging(argv, &android::base::StdioLogger); |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 203 | android::base::SetMinimumLogSeverity(android::base::ERROR); |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 204 | |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 205 | auto property_infos = std::vector<PropertyInfoEntry>(); |
| 206 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 207 | while (true) { |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 208 | static const char kPropertyContexts[] = "property-contexts="; |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 209 | static const struct option long_options[] = { |
| 210 | {"help", no_argument, nullptr, 'h'}, |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 211 | {kPropertyContexts, required_argument, nullptr, 0}, |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 212 | {nullptr, 0, nullptr, 0}, |
| 213 | }; |
| 214 | |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 215 | int option_index; |
| 216 | int arg = getopt_long(argc, argv, "p:", long_options, &option_index); |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 217 | |
| 218 | if (arg == -1) { |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | switch (arg) { |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 223 | case 0: |
| 224 | if (long_options[option_index].name == kPropertyContexts) { |
| 225 | HandlePropertyContexts(optarg, &property_infos); |
| 226 | } |
| 227 | break; |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 228 | case 'h': |
| 229 | PrintUsage(); |
| 230 | return EXIT_FAILURE; |
| 231 | case 'p': |
| 232 | passwd_files.emplace_back(optarg); |
| 233 | break; |
| 234 | default: |
| 235 | std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl; |
| 236 | return EXIT_FAILURE; |
| 237 | } |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 238 | } |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 239 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 240 | argc -= optind; |
| 241 | argv += optind; |
| 242 | |
| Steven Moreland | 422a758 | 2019-10-15 14:53:19 -0700 | [diff] [blame] | 243 | if (argc != 1) { |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 244 | PrintUsage(); |
| 245 | return EXIT_FAILURE; |
| Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| Steven Moreland | 422a758 | 2019-10-15 14:53:19 -0700 | [diff] [blame] | 248 | auto interface_inheritance_hierarchy_map = ReadInterfaceInheritanceHierarchy(); |
| Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 249 | if (!interface_inheritance_hierarchy_map.ok()) { |
| Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame] | 250 | LOG(ERROR) << interface_inheritance_hierarchy_map.error(); |
| 251 | return EXIT_FAILURE; |
| 252 | } |
| 253 | SetKnownInterfaces(*interface_inheritance_hierarchy_map); |
| 254 | |
| Tom Cherry | b5f2ec0 | 2019-11-08 17:54:27 -0800 | [diff] [blame] | 255 | std::string serialized_contexts; |
| 256 | std::string trie_error; |
| 257 | if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts, |
| 258 | &trie_error)) { |
| 259 | LOG(ERROR) << "Unable to serialize property contexts: " << trie_error; |
| 260 | return EXIT_FAILURE; |
| 261 | } |
| 262 | |
| 263 | property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_contexts.c_str()); |
| 264 | |
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 265 | const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap(); |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 266 | Action::set_function_map(&function_map); |
| 267 | ActionManager& am = ActionManager::GetInstance(); |
| 268 | ServiceList& sl = ServiceList::GetInstance(); |
| 269 | Parser parser; |
| Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame] | 270 | parser.AddSectionParser("service", std::make_unique<ServiceParser>( |
| 271 | &sl, nullptr, *interface_inheritance_hierarchy_map)); |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 272 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 273 | parser.AddSectionParser("import", std::make_unique<HostImportParser>()); |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 274 | |
| Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 275 | if (!parser.ParseConfigFileInsecure(*argv)) { |
| 276 | LOG(ERROR) << "Failed to open init rc script '" << *argv << "'"; |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 277 | return EXIT_FAILURE; |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 278 | } |
| Tom Cherry | 6737a6b | 2019-08-05 15:03:58 -0700 | [diff] [blame] | 279 | size_t failures = parser.parse_error_count() + am.CheckAllCommands() + sl.CheckAllCommands(); |
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 280 | if (failures > 0) { |
| 281 | LOG(ERROR) << "Failed to parse init script '" << *argv << "' with " << failures |
| 282 | << " errors"; |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 283 | return EXIT_FAILURE; |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 284 | } |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 285 | return EXIT_SUCCESS; |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | } // namespace init |
| 289 | } // namespace android |
| 290 | |
| 291 | int main(int argc, char** argv) { |
| Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 292 | return android::init::main(argc, argv); |
| Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 293 | } |