diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp index 7e93b448c..84077291c 100644 --- a/init/host_init_verifier.cpp +++ b/init/host_init_verifier.cpp @@ -146,7 +146,7 @@ int main(int argc, char** argv) { parser.AddSectionParser("on", std::make_unique(&am, nullptr)); parser.AddSectionParser("import", std::make_unique()); - if (!parser.ParseConfig(argv[1])) { + if (!parser.ParseConfigFileInsecure(argv[1])) { LOG(ERROR) << "Failed to open init rc script '" << argv[1] << "'"; return EXIT_FAILURE; } diff --git a/init/parser.cpp b/init/parser.cpp index 4f1cac495..fa0fd11be 100644 --- a/init/parser.cpp +++ b/init/parser.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -39,14 +40,13 @@ void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callbac line_callbacks_.emplace_back(prefix, callback); } -void Parser::ParseData(const std::string& filename, const std::string& data) { - // TODO: Use a parser with const input and remove this copy - std::vector data_copy(data.begin(), data.end()); - data_copy.push_back('\0'); +void Parser::ParseData(const std::string& filename, std::string* data) { + data->push_back('\n'); // TODO: fix tokenizer + data->push_back('\0'); parse_state state; state.line = 0; - state.ptr = &data_copy[0]; + state.ptr = data->data(); state.nexttoken = 0; SectionParser* section_parser = nullptr; @@ -69,6 +69,11 @@ void Parser::ParseData(const std::string& filename, const std::string& data) { switch (next_token(&state)) { case T_EOF: end_section(); + + for (const auto& [section_name, section_parser] : section_parsers_) { + section_parser->EndFile(); + } + return; case T_NEWLINE: { state.line++; @@ -118,6 +123,16 @@ void Parser::ParseData(const std::string& filename, const std::string& data) { } } +bool Parser::ParseConfigFileInsecure(const std::string& path) { + std::string config_contents; + if (!android::base::ReadFileToString(path, &config_contents)) { + return false; + } + + ParseData(path, &config_contents); + return true; +} + bool Parser::ParseConfigFile(const std::string& path) { LOG(INFO) << "Parsing file " << path << "..."; android::base::Timer t; @@ -127,11 +142,7 @@ bool Parser::ParseConfigFile(const std::string& path) { return false; } - config_contents->push_back('\n'); // TODO: fix parse_config. - ParseData(path, *config_contents); - for (const auto& [section_name, section_parser] : section_parsers_) { - section_parser->EndFile(); - } + ParseData(path, &config_contents.value()); LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)"; return true; diff --git a/init/parser.h b/init/parser.h index 3501d8c0b..2454b6a6f 100644 --- a/init/parser.h +++ b/init/parser.h @@ -75,10 +75,13 @@ class Parser { void AddSectionParser(const std::string& name, std::unique_ptr parser); void AddSingleLineParser(const std::string& prefix, LineCallback callback); + // Host init verifier check file permissions. + bool ParseConfigFileInsecure(const std::string& path); + size_t parse_error_count() const { return parse_error_count_; } private: - void ParseData(const std::string& filename, const std::string& data); + void ParseData(const std::string& filename, std::string* data); bool ParseConfigFile(const std::string& path); bool ParseConfigDir(const std::string& path);