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.
selinux_settings: New table that presents effective SELinux settings (o…
…squery#6118) * selinux_settings: New table that presents effective SELinux settings * selinux_settings: Use the SELinux root path from the mounted fs The code that was originally directly implemented inside the `mounts` table has been moved outside so that it can be reused by the selinux_settings table. This also updates the code to use getmntent_r instead of getmntent.
- Loading branch information
1 parent
0b2aa61
commit 8d9059f
Showing
13 changed files
with
588 additions
and
37 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,113 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#include <mntent.h> | ||
#include <sys/vfs.h> | ||
|
||
#include <osquery/filesystem/linux/mounts.h> | ||
#include <osquery/logger.h> | ||
#include <osquery/utils/system/filepath.h> | ||
|
||
namespace osquery { | ||
namespace { | ||
const std::string kMountsPseudoFile{"/proc/mounts"}; | ||
|
||
struct MountDataDeleter final { | ||
void operator()(FILE* ptr) { | ||
if (ptr == nullptr) { | ||
return; | ||
} | ||
|
||
endmntent(ptr); | ||
} | ||
}; | ||
|
||
using MountData = std::unique_ptr<FILE, MountDataDeleter>; | ||
|
||
Status getMountData(MountData& obj) { | ||
obj = {}; | ||
|
||
auto mount_data = setmntent(kMountsPseudoFile.c_str(), "r"); | ||
if (mount_data == nullptr) { | ||
return Status::failure("Failed to open the '" + kMountsPseudoFile + | ||
"' pseudo file"); | ||
} | ||
|
||
obj.reset(mount_data); | ||
return Status::success(); | ||
} | ||
} // namespace | ||
|
||
Status getMountedFilesystemMap(MountedFilesystemMap& mounted_fs_info) { | ||
mounted_fs_info = {}; | ||
|
||
MountData mount_data; | ||
auto status = getMountData(mount_data); | ||
if (!status.ok()) { | ||
return status; | ||
} | ||
|
||
std::vector<char> string_buffer(4096); | ||
|
||
for (;;) { | ||
mntent ent = {}; | ||
if (getmntent_r(mount_data.get(), | ||
&ent, | ||
string_buffer.data(), | ||
string_buffer.size()) == nullptr) { | ||
if (errno != ENOENT) { | ||
LOG(ERROR) << "getmntent_r failed with errno " << std::to_string(errno); | ||
} | ||
|
||
break; | ||
} | ||
|
||
MountInformation mount_info = {}; | ||
mount_info.type = ent.mnt_type; | ||
mount_info.device = ent.mnt_fsname; | ||
mount_info.device_alias = canonicalize_file_name(ent.mnt_fsname); | ||
mount_info.path = ent.mnt_dir; | ||
mount_info.flags = ent.mnt_opts; | ||
|
||
if (mount_info.type == "autofs") { | ||
VLOG(1) << "Skipping statfs information for autofs mount: " | ||
<< mount_info.path; | ||
|
||
} else { | ||
struct statfs stats = {}; | ||
if (statfs(mount_info.path.c_str(), &stats) == 0) { | ||
MountInformation::StatFsInfo statfs_info = {}; | ||
|
||
statfs_info.block_size = static_cast<std::uint32_t>(stats.f_bsize); | ||
statfs_info.block_count = static_cast<std::uint32_t>(stats.f_blocks); | ||
|
||
statfs_info.free_block_count = | ||
static_cast<std::uint32_t>(stats.f_bfree); | ||
|
||
statfs_info.unprivileged_free_block_count = | ||
static_cast<std::uint32_t>(stats.f_bavail); | ||
|
||
statfs_info.inode_count = static_cast<std::uint32_t>(stats.f_files); | ||
|
||
statfs_info.free_inode_count = | ||
static_cast<std::uint32_t>(stats.f_ffree); | ||
|
||
mount_info.optional_statfs_info = std::move(statfs_info); | ||
|
||
} else { | ||
LOG(ERROR) << "statfs failed with errno " << std::to_string(errno) | ||
<< " on path " << mount_info.path; | ||
} | ||
} | ||
|
||
mounted_fs_info.insert({mount_info.path, std::move(mount_info)}); | ||
} | ||
|
||
return Status::success(); | ||
} | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#include <boost/optional.hpp> | ||
#include <unordered_map> | ||
|
||
#include <osquery/core.h> | ||
#include <osquery/filesystem/filesystem.h> | ||
|
||
namespace osquery { | ||
// Information about a single mounted filesystem | ||
struct MountInformation final { | ||
struct StatFsInfo final { | ||
// Optimal transfer block size (statfs::f_bsize) | ||
std::uint32_t block_size{0U}; | ||
|
||
// Total data blocks in file system (statfs::f_blocks) | ||
std::uint32_t block_count{0U}; | ||
|
||
// Free blocks in filesystem (statfs::f_bfree) | ||
std::uint32_t free_block_count{0U}; | ||
|
||
// Free blocks available to unprivileged user (statfs::f_bavail) | ||
std::uint32_t unprivileged_free_block_count{0U}; | ||
|
||
// Total file nodes in filesystem (statfs::f_files) | ||
std::uint32_t inode_count{0U}; | ||
|
||
// Free file nodes in filesystem (statfs::f_ffree) | ||
std::uint32_t free_inode_count{0U}; | ||
}; | ||
|
||
// Filesystem type | ||
std::string type; | ||
|
||
// Device path | ||
std::string device; | ||
|
||
// Canonicalized device path | ||
std::string device_alias; | ||
|
||
// Mount path | ||
std::string path; | ||
|
||
// Mount options | ||
std::string flags; | ||
|
||
// statfs information; may not be set if the statfs operation | ||
// has failed | ||
boost::optional<StatFsInfo> optional_statfs_info; | ||
}; | ||
|
||
// Information about all mounted filesystems | ||
using MountedFilesystemMap = std::unordered_map<std::string, MountInformation>; | ||
|
||
Status getMountedFilesystemMap(MountedFilesystemMap& mounted_fs_info); | ||
} // 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
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
Oops, something went wrong.