forked from osquery/osquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sudoers table: Support file and directory includes (osquery#5350)
Summary: This adds support for the `#includedir` and `#include` directives to the `sudoers` table, making `sudoers` behave more like the actual `sudo` rule parser: * When an `includefile` directive is encountered, the referenced file will be parsed using the same rules as the top-level sudoers file. * When an `includedir` directive is encountered, the referenced directory will be listed and each valid file within (i.e., each file *not* containing a `.` and *not* ending with `~`) will be parsed using the same rules as the top-level sudoers file. * An additional `source` column tracks the file that provides the row's rule. * Like `sudoers(5)`, nesting is limited to 128 individual files, with directory inclusions being counted once for each file they contain. Pull Request resolved: osquery#5350 Differential Revision: D13717394 Pulled By: akindyakov fbshipit-source-id: 9659526f21e82c712c495caa80775b15d7e47e37
- Loading branch information
1 parent
e401a5e
commit bab228b
Showing
6 changed files
with
300 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under both the Apache 2.0 license (found in the | ||
* LICENSE file in the root directory of this source tree) and the GPLv2 (found | ||
* in the COPYING file in the root directory of this source tree). | ||
* You may select, at your option, one of the above-listed licenses. | ||
*/ | ||
|
||
#include <osquery/query.h> | ||
#include <osquery/tables.h> | ||
|
||
#include <string> | ||
|
||
namespace osquery { | ||
namespace tables { | ||
|
||
void genSudoersFile(const std::string& filename, | ||
unsigned int level, | ||
QueryData& results); | ||
|
||
QueryData genSudoers(QueryContext& context); | ||
|
||
} // namespace tables | ||
} // namespace osquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under both the Apache 2.0 license (found in the | ||
* LICENSE file in the root directory of this source tree) and the GPLv2 (found | ||
* in the COPYING file in the root directory of this source tree). | ||
* You may select, at your option, one of the above-listed licenses. | ||
*/ | ||
|
||
#include <fstream> | ||
|
||
#include <boost/filesystem.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <osquery/sql.h> | ||
#include <osquery/tables/system/posix/sudoers.h> | ||
#include <osquery/utils/scope_guard.h> | ||
|
||
namespace fs = boost::filesystem; | ||
|
||
namespace osquery { | ||
namespace tables { | ||
|
||
static fs::path real_temp_path() { | ||
auto temp_dir = fs::temp_directory_path(); | ||
|
||
// NOTE(ww): The sudoers table expands paths to their canonical | ||
// form when listing directories, so we need to make sure that | ||
// the temp directory is canonicalized as well. | ||
return fs::canonical(temp_dir); | ||
} | ||
|
||
class SudoersTests : public testing::Test {}; | ||
|
||
TEST_F(SudoersTests, basic_sudoers) { | ||
auto directory = | ||
real_temp_path() / fs::unique_path("osquery.sudoers_tests.%%%%-%%%%"); | ||
|
||
ASSERT_TRUE(fs::create_directories(directory)); | ||
|
||
auto const path_guard = | ||
scope_guard::create([directory]() { fs::remove_all(directory); }); | ||
|
||
auto sudoers_file = directory / fs::path("sudoers"); | ||
|
||
{ | ||
auto fout = std::ofstream(sudoers_file.native()); | ||
fout << "Defaults env_reset" << '\n'; | ||
} | ||
|
||
auto results = QueryData{}; | ||
genSudoersFile(sudoers_file.string(), 1, results); | ||
|
||
ASSERT_EQ(results.size(), 1); | ||
|
||
const auto& row = results[0]; | ||
ASSERT_EQ(row.at("source"), sudoers_file.string()); | ||
ASSERT_EQ(row.at("header"), "Defaults"); | ||
ASSERT_EQ(row.at("rule_details"), "env_reset"); | ||
} | ||
|
||
TEST_F(SudoersTests, include_file) { | ||
auto directory = | ||
real_temp_path() / fs::unique_path("osquery.sudoers_tests.%%%%-%%%%"); | ||
|
||
ASSERT_TRUE(fs::create_directories(directory)); | ||
|
||
auto const path_guard = | ||
scope_guard::create([directory]() { fs::remove_all(directory); }); | ||
|
||
auto sudoers_top = directory / fs::path("sudoers"); | ||
auto sudoers_inc = directory / fs::path("sudoers_inc"); | ||
|
||
{ | ||
auto fout_top = std::ofstream(sudoers_top.native()); | ||
fout_top << "#include sudoers_inc" << '\n'; | ||
|
||
auto fout_inc = std::ofstream(sudoers_inc.native()); | ||
fout_inc << "Defaults env_reset" << '\n'; | ||
} | ||
|
||
auto results = QueryData{}; | ||
genSudoersFile(sudoers_top.string(), 1, results); | ||
|
||
ASSERT_EQ(results.size(), 2); | ||
|
||
const auto& first_row = results[0]; | ||
ASSERT_EQ(first_row.at("source"), sudoers_top.string()); | ||
ASSERT_EQ(first_row.at("header"), "#include"); | ||
ASSERT_EQ(first_row.at("rule_details"), sudoers_inc.string()); | ||
|
||
const auto& second_row = results[1]; | ||
ASSERT_EQ(second_row.at("source"), sudoers_inc.string()); | ||
ASSERT_EQ(second_row.at("header"), "Defaults"); | ||
ASSERT_EQ(second_row.at("rule_details"), "env_reset"); | ||
} | ||
|
||
TEST_F(SudoersTests, include_dir) { | ||
auto directory = | ||
real_temp_path() / fs::unique_path("osquery.sudoers_tests.%%%%-%%%%"); | ||
|
||
ASSERT_TRUE(fs::create_directories(directory)); | ||
|
||
auto const path_guard = | ||
scope_guard::create([directory]() { fs::remove_all(directory); }); | ||
|
||
auto sudoers_top = directory / fs::path("sudoers"); | ||
auto sudoers_dir = directory / fs::path("sudoers.d"); | ||
auto sudoers_inc = sudoers_dir / fs::path("sudoers_inc"); | ||
|
||
ASSERT_TRUE(fs::create_directories(sudoers_dir)); | ||
|
||
{ | ||
auto fout_top = std::ofstream(sudoers_top.native()); | ||
fout_top << "#includedir " << sudoers_dir.string() << '\n'; | ||
|
||
auto fout_inc = std::ofstream(sudoers_inc.native()); | ||
fout_inc << "Defaults env_reset" << '\n'; | ||
} | ||
|
||
auto results = QueryData{}; | ||
genSudoersFile(sudoers_top.string(), 1, results); | ||
|
||
ASSERT_EQ(results.size(), 2); | ||
|
||
const auto& first_row = results[0]; | ||
ASSERT_EQ(first_row.at("source"), sudoers_top.string()); | ||
ASSERT_EQ(first_row.at("header"), "#includedir"); | ||
ASSERT_EQ(first_row.at("rule_details"), sudoers_dir.string()); | ||
|
||
const auto& second_row = results[1]; | ||
ASSERT_EQ(second_row.at("source"), sudoers_inc.string()); | ||
ASSERT_EQ(second_row.at("header"), "Defaults"); | ||
ASSERT_EQ(second_row.at("rule_details"), "env_reset"); | ||
} | ||
|
||
} // namespace tables | ||
} // namespace osquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters