2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-01-26 18:32:39 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-04 18:06:45 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-10-30 22:03:05 +00:00
|
|
|
#include <fcntl.h>
|
2015-06-22 07:54:19 +00:00
|
|
|
#include <glob.h>
|
2015-03-19 03:47:35 +00:00
|
|
|
#include <pwd.h>
|
2014-10-30 22:03:05 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-11-03 20:08:46 +00:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2014-08-04 18:06:45 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2015-04-10 23:58:11 +00:00
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
2014-10-01 02:49:38 +00:00
|
|
|
|
2015-01-26 18:32:39 +00:00
|
|
|
#include <osquery/core.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/filesystem.h>
|
2015-01-21 21:36:55 +00:00
|
|
|
#include <osquery/logger.h>
|
2015-02-09 00:00:43 +00:00
|
|
|
#include <osquery/sql.h>
|
2014-10-27 18:55:28 +00:00
|
|
|
|
2014-10-01 02:49:38 +00:00
|
|
|
namespace pt = boost::property_tree;
|
2014-11-03 20:08:46 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2014-10-01 02:49:38 +00:00
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
namespace osquery {
|
2014-08-04 18:06:45 +00:00
|
|
|
|
2015-06-30 18:48:11 +00:00
|
|
|
FLAG(uint64, read_max, 50 * 1024 * 1024, "Maximum file read size");
|
|
|
|
FLAG(uint64, read_user_max, 10 * 1024 * 1024, "Maximum non-su read size");
|
|
|
|
FLAG(bool, read_user_links, true, "Read user-owned filesystem links");
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status writeTextFile(const fs::path& path,
|
2014-11-03 20:08:46 +00:00
|
|
|
const std::string& content,
|
|
|
|
int permissions,
|
|
|
|
bool force_permissions) {
|
2014-10-30 22:03:05 +00:00
|
|
|
// Open the file with the request permissions.
|
2014-11-03 18:09:54 +00:00
|
|
|
int output_fd =
|
|
|
|
open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, permissions);
|
2014-10-30 22:03:05 +00:00
|
|
|
if (output_fd <= 0) {
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Could not create file: " + path.string());
|
2014-10-30 22:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the file existed with different permissions before our open
|
|
|
|
// they must be restricted.
|
|
|
|
if (chmod(path.c_str(), permissions) != 0) {
|
|
|
|
// Could not change the file to the requested permissions.
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Failed to change permissions for file: " + path.string());
|
2014-10-30 22:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto bytes = write(output_fd, content.c_str(), content.size());
|
|
|
|
if (bytes != content.size()) {
|
|
|
|
close(output_fd);
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Failed to write contents to file: " + path.string());
|
2014-10-30 22:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(output_fd);
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status readFile(const fs::path& path, std::string& content) {
|
2015-06-30 18:48:11 +00:00
|
|
|
struct stat file;
|
|
|
|
if (lstat(path.string().c_str(), &file) == 0) {
|
|
|
|
if (file.st_uid != 0 && !FLAGS_read_user_links) {
|
|
|
|
return Status(1, "User link reads disabled");
|
|
|
|
}
|
|
|
|
} else if (stat(path.string().c_str(), &file) < 0) {
|
|
|
|
return Status(1, "Cannot access path: " + path.string());
|
2014-08-04 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 18:48:11 +00:00
|
|
|
// Apply the max byte-read based on file/link target ownership.
|
|
|
|
size_t read_max = (file.st_uid == 0)
|
|
|
|
? FLAGS_read_max
|
|
|
|
: std::min(FLAGS_read_max, FLAGS_read_user_max);
|
|
|
|
std::ifstream is(path.string(), std::ifstream::binary);
|
|
|
|
if (!is) {
|
|
|
|
return Status(1, "Error reading file: " + path.string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to read the file size.
|
|
|
|
ssize_t size = file.st_size;
|
|
|
|
|
|
|
|
// Erase/clear provided string buffer.
|
|
|
|
content.erase();
|
|
|
|
if (file.st_size > read_max) {
|
|
|
|
VLOG(1) << "Cannot read " << path
|
|
|
|
<< " size exceeds limit: " << file.st_size << " > " << read_max;
|
|
|
|
return Status(1, "File exceeds read limits");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == -1 || size == 0) {
|
|
|
|
// Size could not be determined. This may be a special device.
|
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << is.rdbuf();
|
|
|
|
if (is.bad()) {
|
|
|
|
return Status(1, "Error reading special file: " + path.string());
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
content.assign(std::move(buffer.str()));
|
2014-08-04 18:06:45 +00:00
|
|
|
} else {
|
2015-06-30 18:48:11 +00:00
|
|
|
content = std::string(size, '\0');
|
|
|
|
is.read(&content[0], size);
|
2014-08-21 21:35:51 +00:00
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(0, "OK");
|
2014-08-04 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status isWritable(const fs::path& path) {
|
2014-10-30 22:03:05 +00:00
|
|
|
auto path_exists = pathExists(path);
|
|
|
|
if (!path_exists.ok()) {
|
|
|
|
return path_exists;
|
2014-10-27 01:39:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 18:55:28 +00:00
|
|
|
if (access(path.c_str(), W_OK) == 0) {
|
|
|
|
return Status(0, "OK");
|
2014-10-27 01:39:03 +00:00
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Path is not writable: " + path.string());
|
2014-10-27 01:39:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status isReadable(const fs::path& path) {
|
2014-10-30 22:03:05 +00:00
|
|
|
auto path_exists = pathExists(path);
|
|
|
|
if (!path_exists.ok()) {
|
|
|
|
return path_exists;
|
2014-10-29 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (access(path.c_str(), R_OK) == 0) {
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Path is not readable: " + path.string());
|
2014-10-29 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status pathExists(const fs::path& path) {
|
2014-11-03 20:08:46 +00:00
|
|
|
if (path.empty()) {
|
2014-10-30 22:03:05 +00:00
|
|
|
return Status(1, "-1");
|
2014-09-09 17:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A tri-state determination of presence
|
2014-11-06 21:20:22 +00:00
|
|
|
try {
|
2015-05-03 20:37:11 +00:00
|
|
|
if (!fs::exists(path)) {
|
2014-11-06 21:20:22 +00:00
|
|
|
return Status(1, "0");
|
|
|
|
}
|
2015-03-24 05:34:22 +00:00
|
|
|
} catch (const fs::filesystem_error& e) {
|
2014-11-06 21:20:22 +00:00
|
|
|
return Status(1, e.what());
|
2014-09-09 17:56:48 +00:00
|
|
|
}
|
|
|
|
return Status(0, "1");
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status remove(const fs::path& path) {
|
2015-02-04 03:55:16 +00:00
|
|
|
auto status_code = std::remove(path.string().c_str());
|
|
|
|
return Status(status_code, "N/A");
|
|
|
|
}
|
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
static void genGlobs(std::string path,
|
|
|
|
std::vector<std::string>& results,
|
|
|
|
GlobLimits limits) {
|
|
|
|
// Replace SQL-wildcard '%' with globbing wildcard '*'.
|
|
|
|
if (path.find("%") != std::string::npos) {
|
|
|
|
boost::replace_all(path, "%", "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relative paths are a bad idea, but we try to accommodate.
|
|
|
|
if ((path.size() == 0 || path[0] != '/') && path[0] != '~') {
|
|
|
|
path = (fs::initial_path() / path).string();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a glob set and recurse for double star.
|
|
|
|
while (true) {
|
|
|
|
glob_t data;
|
|
|
|
glob(path.c_str(), GLOB_TILDE | GLOB_MARK | GLOB_BRACE, nullptr, &data);
|
|
|
|
size_t count = data.gl_pathc;
|
|
|
|
for (size_t index = 0; index < count; index++) {
|
|
|
|
results.push_back(data.gl_pathv[index]);
|
2014-08-14 23:27:20 +00:00
|
|
|
}
|
2015-06-22 07:54:19 +00:00
|
|
|
globfree(&data);
|
|
|
|
// The end state is a non-recursive ending or empty set of matches.
|
|
|
|
size_t wild = path.rfind("**");
|
|
|
|
// Allow a trailing slash after the double wild indicator.
|
|
|
|
if (count == 0 || wild > path.size() || wild < path.size() - 3) {
|
|
|
|
break;
|
2014-08-14 23:27:20 +00:00
|
|
|
}
|
2015-06-22 07:54:19 +00:00
|
|
|
path += "/**";
|
2015-03-17 18:52:56 +00:00
|
|
|
}
|
2014-08-14 23:27:20 +00:00
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
// Prune results based on settings/requested glob limitations.
|
|
|
|
auto end = std::remove_if(
|
|
|
|
results.begin(), results.end(), [limits](const std::string& found) {
|
|
|
|
return !((found[found.length() - 1] == '/' && limits & GLOB_FOLDERS) ||
|
|
|
|
(found[found.length() - 1] != '/' && limits & GLOB_FILES));
|
|
|
|
});
|
|
|
|
results.erase(end, results.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status resolveFilePattern(const fs::path& fs_path,
|
|
|
|
std::vector<std::string>& results) {
|
|
|
|
return resolveFilePattern(fs_path, results, GLOB_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status resolveFilePattern(const fs::path& fs_path,
|
|
|
|
std::vector<std::string>& results,
|
|
|
|
GlobLimits setting) {
|
|
|
|
genGlobs(fs_path.string(), results, setting);
|
2015-03-17 18:52:56 +00:00
|
|
|
return Status(0, "OK");
|
2014-08-14 23:27:20 +00:00
|
|
|
}
|
2014-10-01 02:49:38 +00:00
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
inline Status listInAbsoluteDirectory(const fs::path& path,
|
|
|
|
std::vector<std::string>& results,
|
|
|
|
GlobLimits limits) {
|
2015-03-16 22:44:24 +00:00
|
|
|
try {
|
2015-06-22 07:54:19 +00:00
|
|
|
if (path.filename() == "*" && !fs::exists(path.parent_path())) {
|
|
|
|
return Status(1, "Directory not found: " + path.parent_path().string());
|
2015-01-26 18:32:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
if (path.filename() == "*" && !fs::is_directory(path.parent_path())) {
|
|
|
|
return Status(1, "Path not a directory: " + path.parent_path().string());
|
2015-01-26 18:32:39 +00:00
|
|
|
}
|
2015-03-24 05:34:22 +00:00
|
|
|
} catch (const fs::filesystem_error& e) {
|
2015-03-16 22:44:24 +00:00
|
|
|
return Status(1, e.what());
|
2015-03-17 18:52:56 +00:00
|
|
|
}
|
2015-06-22 07:54:19 +00:00
|
|
|
genGlobs(path.string(), results, limits);
|
2015-03-17 18:52:56 +00:00
|
|
|
return Status(0, "OK");
|
2015-01-26 18:32:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 07:54:19 +00:00
|
|
|
Status listFilesInDirectory(const fs::path& path,
|
|
|
|
std::vector<std::string>& results,
|
|
|
|
bool ignore_error) {
|
|
|
|
return listInAbsoluteDirectory((path / "*"), results, GLOB_FILES);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status listDirectoriesInDirectory(const fs::path& path,
|
|
|
|
std::vector<std::string>& results,
|
|
|
|
bool ignore_error) {
|
|
|
|
return listInAbsoluteDirectory((path / "*"), results, GLOB_FOLDERS);
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status getDirectory(const fs::path& path, fs::path& dirpath) {
|
2014-10-07 18:20:47 +00:00
|
|
|
if (!isDirectory(path).ok()) {
|
2015-03-24 05:34:22 +00:00
|
|
|
dirpath = fs::path(path).parent_path().string();
|
2014-10-06 21:23:26 +00:00
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
dirpath = path;
|
2015-01-26 08:02:02 +00:00
|
|
|
return Status(1, "Path is a directory: " + path.string());
|
2014-10-06 21:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:34:22 +00:00
|
|
|
Status isDirectory(const fs::path& path) {
|
2015-03-02 05:15:42 +00:00
|
|
|
try {
|
2015-03-24 05:34:22 +00:00
|
|
|
if (fs::is_directory(path)) {
|
2015-03-02 05:15:42 +00:00
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
return Status(1, "Path is not a directory: " + path.string());
|
2015-03-24 05:34:22 +00:00
|
|
|
} catch (const fs::filesystem_error& e) {
|
2015-03-02 05:15:42 +00:00
|
|
|
return Status(1, e.what());
|
2014-10-06 21:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 00:00:43 +00:00
|
|
|
|
2015-03-02 05:15:42 +00:00
|
|
|
std::set<fs::path> getHomeDirectories() {
|
|
|
|
std::set<fs::path> results;
|
|
|
|
|
|
|
|
auto users = SQL::selectAllFrom("users");
|
|
|
|
for (const auto& user : users) {
|
|
|
|
if (user.at("directory").size() > 0) {
|
|
|
|
results.insert(user.at("directory"));
|
2015-02-09 00:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 05:15:42 +00:00
|
|
|
|
2015-02-09 00:00:43 +00:00
|
|
|
return results;
|
|
|
|
}
|
2015-02-10 05:41:17 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
bool safePermissions(const std::string& dir,
|
|
|
|
const std::string& path,
|
|
|
|
bool executable) {
|
2015-03-04 16:45:21 +00:00
|
|
|
struct stat file_stat, link_stat, dir_stat;
|
|
|
|
if (lstat(path.c_str(), &link_stat) < 0 || stat(path.c_str(), &file_stat) ||
|
|
|
|
stat(dir.c_str(), &dir_stat)) {
|
|
|
|
// Path was not real, had too may links, or could not be accessed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_stat.st_mode & (1 << 9)) {
|
|
|
|
// Do not load modules from /tmp-like directories.
|
|
|
|
return false;
|
|
|
|
} else if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
// Only load file-like nodes (not directories).
|
|
|
|
return false;
|
|
|
|
} else if (file_stat.st_uid == getuid() || file_stat.st_uid == 0) {
|
|
|
|
// Otherwise, require matching or root file ownership.
|
2015-04-10 23:58:11 +00:00
|
|
|
if (executable && !(file_stat.st_mode & S_IXUSR)) {
|
2015-03-13 15:11:08 +00:00
|
|
|
// Require executable, implies by the owner.
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-04 16:45:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Do not load modules not owned by the user.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 03:47:35 +00:00
|
|
|
const std::string& osqueryHomeDirectory() {
|
|
|
|
static std::string homedir;
|
|
|
|
if (homedir.size() == 0) {
|
|
|
|
// Try to get the caller's home directory using HOME and getpwuid.
|
|
|
|
auto user = getpwuid(getuid());
|
|
|
|
if (getenv("HOME") != nullptr && isWritable(getenv("HOME")).ok()) {
|
|
|
|
homedir = std::string(getenv("HOME")) + "/.osquery";
|
|
|
|
} else if (user != nullptr && user->pw_dir != nullptr) {
|
|
|
|
homedir = std::string(user->pw_dir) + "/.osquery";
|
|
|
|
} else {
|
|
|
|
// Failover to a temporary directory (used for the shell).
|
|
|
|
homedir = "/tmp/osquery";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return homedir;
|
|
|
|
}
|
|
|
|
|
2015-02-10 05:41:17 +00:00
|
|
|
std::string lsperms(int mode) {
|
|
|
|
static const char rwx[] = {'0', '1', '2', '3', '4', '5', '6', '7'};
|
|
|
|
std::string bits;
|
|
|
|
|
|
|
|
bits += rwx[(mode >> 9) & 7];
|
|
|
|
bits += rwx[(mode >> 6) & 7];
|
|
|
|
bits += rwx[(mode >> 3) & 7];
|
|
|
|
bits += rwx[(mode >> 0) & 7];
|
|
|
|
return bits;
|
|
|
|
}
|
2015-04-10 23:58:11 +00:00
|
|
|
|
2015-05-03 20:37:11 +00:00
|
|
|
Status parseJSON(const fs::path& path, pt::ptree& tree) {
|
2015-04-10 23:58:11 +00:00
|
|
|
std::string json_data;
|
|
|
|
if (!readFile(path, json_data).ok()) {
|
|
|
|
return Status(1, "Could not read JSON from file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseJSONContent(json_data, tree);
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:37:11 +00:00
|
|
|
Status parseJSONContent(const std::string& content, pt::ptree& tree) {
|
2015-04-10 23:58:11 +00:00
|
|
|
// Read the extensions data into a JSON blob, then property tree.
|
|
|
|
try {
|
2015-05-03 20:37:11 +00:00
|
|
|
std::stringstream json_stream;
|
|
|
|
json_stream << content;
|
2015-04-10 23:58:11 +00:00
|
|
|
pt::read_json(json_stream, tree);
|
|
|
|
} catch (const pt::json_parser::json_parser_error& e) {
|
|
|
|
return Status(1, "Could not parse JSON from file");
|
|
|
|
}
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|