osquery-1/osquery/extensions/extensions.cpp

540 lines
17 KiB
C++
Raw Normal View History

2015-02-04 03:55:16 +00:00
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2015-02-12 06:07:25 +00:00
#include <csignal>
2015-02-10 05:57:45 +00:00
2015-03-04 16:45:21 +00:00
#include <boost/algorithm/string/trim.hpp>
2015-02-19 23:19:00 +00:00
#include <osquery/events.h>
2015-02-04 03:55:16 +00:00
#include <osquery/filesystem.h>
#include <osquery/logger.h>
2015-02-19 23:19:00 +00:00
#include <osquery/registry.h>
#include <osquery/sql.h>
2015-02-04 03:55:16 +00:00
2015-02-19 23:19:00 +00:00
#include "osquery/extensions/interface.h"
#include "osquery/core/watcher.h"
2015-02-04 03:55:16 +00:00
using namespace osquery::extensions;
namespace fs = boost::filesystem;
2015-02-04 03:55:16 +00:00
namespace osquery {
// Millisecond latency between initalizing manager pings.
const int kExtensionInitializeMLatency = 200;
2015-02-19 01:19:45 +00:00
#ifdef __APPLE__
const std::string kModuleExtension = ".dylib";
#else
const std::string kModuleExtension = ".so";
#endif
2015-03-03 23:03:14 +00:00
CLI_FLAG(bool, disable_extensions, false, "Disable extension API");
2015-03-03 23:03:14 +00:00
CLI_FLAG(string,
extensions_socket,
"/var/osquery/osquery.em",
"Path to the extensions UNIX domain socket")
CLI_FLAG(string,
extensions_autoload,
"/etc/osquery/extensions.load",
"Optional path to a list of autoloaded & managed extensions")
CLI_FLAG(string,
extensions_timeout,
2015-03-14 01:18:18 +00:00
"3",
"Seconds to wait for autoloaded extensions");
2015-03-14 01:18:18 +00:00
CLI_FLAG(string,
extensions_interval,
"3",
"Seconds delay between connectivity checks")
2015-03-04 16:45:21 +00:00
CLI_FLAG(string,
modules_autoload,
"/etc/osquery/modules.load",
"Optional path to a list of autoloaded registry modules")
2015-03-04 16:45:21 +00:00
2015-05-04 03:02:01 +00:00
/**
* @brief Alias the extensions_socket (used by core) to a simple 'socket'.
*
* Extension binaries will more commonly set the path to an extension manager
* socket. Alias the long switch name to 'socket' for an easier UX.
*
* We include timeout and interval, where the 'extensions_' prefix is removed
* in the alias since we are already within the context of an extension.
*/
EXTENSION_FLAG_ALIAS(socket, extensions_socket);
EXTENSION_FLAG_ALIAS(timeout, extensions_timeout);
EXTENSION_FLAG_ALIAS(interval, extensions_interval);
2015-05-04 03:02:01 +00:00
2015-05-06 00:09:07 +00:00
void ExtensionWatcher::start() {
2015-02-04 03:55:16 +00:00
// Watch the manager, if the socket is removed then the extension will die.
2015-02-19 01:19:45 +00:00
while (true) {
watch();
interruptableSleep(interval_);
}
}
2015-02-04 03:55:16 +00:00
2015-02-19 01:19:45 +00:00
void ExtensionWatcher::exitFatal(int return_code) {
// Exit the extension.
::exit(return_code);
}
2015-02-04 03:55:16 +00:00
2015-02-19 01:19:45 +00:00
void ExtensionWatcher::watch() {
2015-02-04 03:55:16 +00:00
ExtensionStatus status;
2015-02-19 01:19:45 +00:00
try {
auto client = EXManagerClient(path_);
2015-02-04 03:55:16 +00:00
// Ping the extension manager until it goes down.
2015-02-19 01:19:45 +00:00
client.get()->ping(status);
} catch (const std::exception& e) {
LOG(WARNING) << "Extension watcher ending: osquery core has gone away";
exitFatal(0);
}
if (status.code != ExtensionCode::EXT_SUCCESS && fatal_) {
exitFatal();
}
}
void ExtensionManagerWatcher::watch() {
// Watch the set of extensions, if the socket is removed then the extension
// will be deregistered.
const auto uuids = Registry::routeUUIDs();
ExtensionStatus status;
for (const auto& uuid : uuids) {
try {
auto client = EXClient(getExtensionSocket(uuid));
// Ping the extension until it goes down.
client.get()->ping(status);
} catch (const std::exception& e) {
LOG(INFO) << "Extension UUID " << uuid << " has gone away";
Registry::removeBroadcast(uuid);
continue;
2015-02-19 01:19:45 +00:00
}
2015-02-04 03:55:16 +00:00
if (status.code != ExtensionCode::EXT_SUCCESS && fatal_) {
2015-02-19 01:19:45 +00:00
Registry::removeBroadcast(uuid);
2015-02-04 03:55:16 +00:00
}
}
}
inline Status socketWritable(const fs::path& path) {
if (pathExists(path).ok()) {
if (!isWritable(path).ok()) {
return Status(1, "Cannot write extension socket: " + path.string());
}
if (!remove(path).ok()) {
return Status(1, "Cannot remove extension socket: " + path.string());
}
} else {
if (!pathExists(path.parent_path()).ok()) {
return Status(1, "Extension socket directory missing: " + path.string());
}
if (!isWritable(path.parent_path()).ok()) {
return Status(1, "Cannot write extension socket: " + path.string());
}
}
return Status(0, "OK");
}
2015-03-04 16:45:21 +00:00
void loadExtensions() {
// Disabling extensions will disable autoloading.
if (FLAGS_disable_extensions) {
return;
}
2015-03-03 23:03:14 +00:00
// Optionally autoload extensions
auto status = loadExtensions(FLAGS_extensions_autoload);
if (!status.ok()) {
2015-03-19 03:47:35 +00:00
VLOG(1) << "Could not autoload extensions: " << status.what();
2015-03-03 23:03:14 +00:00
}
}
2015-03-04 16:45:21 +00:00
void loadModules() {
auto status = loadModules(FLAGS_modules_autoload);
if (!status.ok()) {
2015-03-19 03:47:35 +00:00
VLOG(1) << "Modules autoload contains invalid paths: "
<< FLAGS_modules_autoload;
2015-03-04 16:45:21 +00:00
}
}
Status loadExtensions(const std::string& loadfile) {
std::string autoload_paths;
if (readFile(loadfile, autoload_paths).ok()) {
for (auto& path : osquery::split(autoload_paths, "\n")) {
boost::trim(path);
if (path.size() > 0 && path[0] != '#' && path[0] != ';') {
Watcher::addExtensionPath(path);
}
2015-03-04 16:45:21 +00:00
}
return Status(0, "OK");
2015-03-04 16:45:21 +00:00
}
return Status(1, "Cannot read extensions autoload file");
2015-03-04 16:45:21 +00:00
}
Status loadModuleFile(const std::string& path) {
fs::path module(path);
if (safePermissions(module.parent_path().string(), path)) {
if (module.extension().string() == kModuleExtension) {
// Silently allow module load failures to drop.
RegistryModuleLoader loader(module.string());
loader.init();
return Status(0, "OK");
}
}
return Status(1, "Module check failed");
}
2015-03-04 16:45:21 +00:00
Status loadModules(const std::string& loadfile) {
2015-03-04 16:45:21 +00:00
// Split the search path for modules using a ':' delimiter.
std::string autoload_paths;
if (readFile(loadfile, autoload_paths).ok()) {
auto status = Status(0, "OK");
for (auto& module_path : osquery::split(autoload_paths, "\n")) {
boost::trim(module_path);
auto path_status = loadModuleFile(module_path);
if (!path_status.ok()) {
status = path_status;
}
2015-03-04 16:45:21 +00:00
}
// Return an aggregate failure if any load fails (invalid search path).
return status;
2015-03-04 16:45:21 +00:00
}
return Status(1, "Cannot read modules autoload file");
2015-03-04 16:45:21 +00:00
}
Status extensionPathActive(const std::string& path, bool use_timeout = false) {
// Make sure the extension manager path exists, and is writable.
size_t delay = 0;
size_t timeout = atoi(FLAGS_extensions_timeout.c_str());
do {
if (pathExists(path) && isWritable(path)) {
try {
auto client = EXManagerClient(path);
return Status(0, "OK");
} catch (const std::exception& e) {
// Path might exist without a connected extension or extension manager.
}
}
// Only check active once if this check does not allow a timeout.
if (!use_timeout || timeout == 0) {
break;
}
// Increase the total wait detail.
delay += kExtensionInitializeMLatency;
::usleep(kExtensionInitializeMLatency * 1000);
} while (delay < timeout * 1000);
return Status(1, "Extension socket not available: " + path);
}
2015-02-19 01:19:45 +00:00
Status startExtension(const std::string& name, const std::string& version) {
return startExtension(name, version, "0.0.0");
2015-02-04 03:55:16 +00:00
}
2015-02-19 01:19:45 +00:00
Status startExtension(const std::string& name,
const std::string& version,
const std::string& min_sdk_version) {
Registry::setExternal();
2015-03-14 01:18:18 +00:00
auto latency = atoi(FLAGS_extensions_interval.c_str()) * 1000;
auto status = startExtensionWatcher(FLAGS_extensions_socket, latency, true);
2015-02-19 01:19:45 +00:00
if (!status.ok()) {
// If the threaded watcher fails to start, fail the extension.
return status;
}
status = startExtension(
FLAGS_extensions_socket, name, version, min_sdk_version, kSDKVersion);
if (!status.ok()) {
// If the extension failed to start then the EM is most likely unavailable.
return status;
}
try {
// The extension does nothing but serve the thrift API.
// Join on both the thrift and extension manager watcher services.
2015-02-19 01:19:45 +00:00
Dispatcher::joinServices();
} catch (const std::exception& e) {
// The extension manager may shutdown without notifying the extension.
return Status(0, e.what());
2015-02-04 03:55:16 +00:00
}
2015-02-19 01:19:45 +00:00
// An extension will only return on failure.
return Status(0, "Extension was shutdown");
2015-02-04 03:55:16 +00:00
}
Status startExtension(const std::string& manager_path,
const std::string& name,
const std::string& version,
2015-02-19 01:19:45 +00:00
const std::string& min_sdk_version,
2015-02-04 03:55:16 +00:00
const std::string& sdk_version) {
// Make sure the extension manager path exists, and is writable.
auto status = extensionPathActive(manager_path, true);
if (!status.ok()) {
return status;
2015-02-04 03:55:16 +00:00
}
// The Registry broadcast is used as the ExtensionRegistry.
auto broadcast = Registry::getBroadcast();
// The extension will register and provide name, version, sdk details.
2015-02-04 03:55:16 +00:00
InternalExtensionInfo info;
info.name = name;
info.version = version;
info.sdk_version = sdk_version;
2015-02-19 01:19:45 +00:00
info.min_sdk_version = min_sdk_version;
2015-02-04 03:55:16 +00:00
// If registration is successful, we will also request the manager's options.
InternalOptionList options;
// Register the extension's registry broadcast with the manager.
ExtensionStatus ext_status;
2015-02-04 03:55:16 +00:00
try {
2015-02-19 01:19:45 +00:00
auto client = EXManagerClient(manager_path);
client.get()->registerExtension(ext_status, info, broadcast);
// The main reason for a failed registry is a duplicate extension name
// (the extension process is already running), or the extension broadcasts
// a duplicate registry item.
if (ext_status.code != ExtensionCode::EXT_SUCCESS) {
return Status(ext_status.code, ext_status.message);
}
// Request the core options, mainly to set the active registry plugins for
// logger and config.
client.get()->options(options);
} catch (const std::exception& e) {
2015-02-04 03:55:16 +00:00
return Status(1, "Extension register failed: " + std::string(e.what()));
}
2015-02-19 01:19:45 +00:00
// Now that the uuid is known, try to clean up stale socket paths.
auto extension_path = getExtensionSocket(ext_status.uuid, manager_path);
status = socketWritable(extension_path);
if (!status) {
return status;
2015-02-19 01:19:45 +00:00
}
// Set the active config and logger plugins. The core will arbitrate if the
// plugins are not available in the extension's local registry.
Registry::setActive("config", options["config_plugin"].value);
Registry::setActive("logger", options["logger_plugin"].value);
// Set up all lazy registry plugins and the active config/logger plugin.
Registry::setUp();
2015-02-04 03:55:16 +00:00
// Start the extension's Thrift server
2015-05-04 03:02:01 +00:00
Dispatcher::addService(
std::make_shared<ExtensionRunner>(manager_path, ext_status.uuid));
VLOG(1) << "Extension (" << name << ", " << ext_status.uuid << ", " << version
2015-02-19 01:19:45 +00:00
<< ", " << sdk_version << ") registered";
return Status(0, std::to_string(ext_status.uuid));
2015-02-04 03:55:16 +00:00
}
2015-02-19 01:19:45 +00:00
Status queryExternal(const std::string& manager_path,
const std::string& query,
QueryData& results) {
// Make sure the extension path exists, and is writable.
auto status = extensionPathActive(manager_path);
if (!status.ok()) {
return status;
2015-02-19 01:19:45 +00:00
}
ExtensionResponse response;
try {
auto client = EXManagerClient(manager_path);
client.get()->query(response, query);
} catch (const std::exception& e) {
return Status(1, "Extension call failed: " + std::string(e.what()));
}
for (const auto& row : response.response) {
results.push_back(row);
}
return Status(response.status.code, response.status.message);
}
Status queryExternal(const std::string& query, QueryData& results) {
return queryExternal(FLAGS_extensions_socket, query, results);
}
Status getQueryColumnsExternal(const std::string& manager_path,
const std::string& query,
tables::TableColumns& columns) {
// Make sure the extension path exists, and is writable.
auto status = extensionPathActive(manager_path);
if (!status.ok()) {
return status;
2015-02-19 01:19:45 +00:00
}
ExtensionResponse response;
try {
auto client = EXManagerClient(manager_path);
client.get()->getQueryColumns(response, query);
} catch (const std::exception& e) {
return Status(1, "Extension call failed: " + std::string(e.what()));
}
// Translate response map: {string: string} to a vector: pair(name, type).
for (const auto& column : response.response) {
for (const auto& column_detail : column) {
columns.push_back(make_pair(column_detail.first, column_detail.second));
}
}
return Status(response.status.code, response.status.message);
}
Status getQueryColumnsExternal(const std::string& query,
tables::TableColumns& columns) {
return getQueryColumnsExternal(FLAGS_extensions_socket, query, columns);
}
2015-02-10 20:00:03 +00:00
Status pingExtension(const std::string& path) {
if (FLAGS_disable_extensions) {
return Status(1, "Extensions disabled");
}
// Make sure the extension path exists, and is writable.
auto status = extensionPathActive(path);
if (!status.ok()) {
return status;
2015-02-10 20:00:03 +00:00
}
ExtensionStatus ext_status;
try {
2015-02-19 01:19:45 +00:00
auto client = EXClient(path);
client.get()->ping(ext_status);
2015-02-10 20:00:03 +00:00
} catch (const std::exception& e) {
return Status(1, "Extension call failed: " + std::string(e.what()));
}
return Status(ext_status.code, ext_status.message);
}
2015-02-10 20:50:07 +00:00
Status getExtensions(ExtensionList& extensions) {
if (FLAGS_disable_extensions) {
return Status(1, "Extensions disabled");
}
return getExtensions(FLAGS_extensions_socket, extensions);
}
Status getExtensions(const std::string& manager_path,
ExtensionList& extensions) {
// Make sure the extension path exists, and is writable.
auto status = extensionPathActive(manager_path);
if (!status.ok()) {
return status;
2015-02-10 20:50:07 +00:00
}
InternalExtensionList ext_list;
try {
2015-02-19 01:19:45 +00:00
auto client = EXManagerClient(manager_path);
client.get()->extensions(ext_list);
2015-02-10 20:50:07 +00:00
} catch (const std::exception& e) {
return Status(1, "Extension call failed: " + std::string(e.what()));
}
// Add the extension manager to the list called (core).
2015-02-19 23:19:00 +00:00
extensions[0] = {"core", OSQUERY_VERSION, "0.0.0", OSQUERY_SDK_VERSION};
2015-02-10 20:50:07 +00:00
// Convert from Thrift-internal list type to RouteUUID/ExtenionInfo type.
2015-02-19 23:19:00 +00:00
for (const auto& ext : ext_list) {
extensions[ext.first] = {ext.second.name,
ext.second.version,
ext.second.min_sdk_version,
ext.second.sdk_version};
2015-02-10 20:50:07 +00:00
}
return Status(0, "OK");
}
2015-02-04 03:55:16 +00:00
Status callExtension(const RouteUUID uuid,
const std::string& registry,
const std::string& item,
const PluginRequest& request,
PluginResponse& response) {
2015-02-10 20:00:03 +00:00
if (FLAGS_disable_extensions) {
return Status(1, "Extensions disabled");
}
2015-02-10 20:50:07 +00:00
return callExtension(
getExtensionSocket(uuid), registry, item, request, response);
2015-02-04 03:55:16 +00:00
}
Status callExtension(const std::string& extension_path,
const std::string& registry,
const std::string& item,
const PluginRequest& request,
PluginResponse& response) {
// Make sure the extension manager path exists, and is writable.
auto status = extensionPathActive(extension_path);
if (!status.ok()) {
return status;
2015-02-04 03:55:16 +00:00
}
ExtensionResponse ext_response;
try {
2015-02-19 01:19:45 +00:00
auto client = EXClient(extension_path);
client.get()->call(ext_response, registry, item, request);
2015-02-04 03:55:16 +00:00
}
catch (const std::exception& e) {
return Status(1, "Extension call failed: " + std::string(e.what()));
}
2015-02-10 20:50:07 +00:00
// Convert from Thrift-internal list type to PluginResponse type.
2015-02-04 03:55:16 +00:00
if (ext_response.status.code == ExtensionCode::EXT_SUCCESS) {
for (const auto& item : ext_response.response) {
response.push_back(item);
}
}
return Status(ext_response.status.code, ext_response.status.message);
}
Status startExtensionWatcher(const std::string& manager_path,
size_t interval,
bool fatal) {
// Make sure the extension manager path exists, and is writable.
auto status = extensionPathActive(manager_path, true);
if (!status.ok()) {
return status;
2015-02-04 03:55:16 +00:00
}
// Start a extension manager watcher, if the manager dies, so should we.
2015-05-04 03:02:01 +00:00
Dispatcher::addService(
2015-02-04 03:55:16 +00:00
std::make_shared<ExtensionWatcher>(manager_path, interval, fatal));
return Status(0, "OK");
}
Status startExtensionManager() {
2015-02-10 20:00:03 +00:00
if (FLAGS_disable_extensions) {
return Status(1, "Extensions disabled");
}
return startExtensionManager(FLAGS_extensions_socket);
2015-02-04 03:55:16 +00:00
}
Status startExtensionManager(const std::string& manager_path) {
// Check if the socket location exists.
auto status = socketWritable(manager_path);
if (!status.ok()) {
return status;
2015-02-04 03:55:16 +00:00
}
2015-03-14 01:18:18 +00:00
auto latency = atoi(FLAGS_extensions_interval.c_str()) * 1000;
2015-02-19 01:19:45 +00:00
// Start a extension manager watcher, if the manager dies, so should we.
2015-05-04 03:02:01 +00:00
Dispatcher::addService(
2015-03-14 01:18:18 +00:00
std::make_shared<ExtensionManagerWatcher>(manager_path, latency));
2015-02-19 01:19:45 +00:00
2015-02-04 03:55:16 +00:00
// Start the extension manager thread.
2015-05-04 03:02:01 +00:00
Dispatcher::addService(
2015-02-04 03:55:16 +00:00
std::make_shared<ExtensionManagerRunner>(manager_path));
return Status(0, "OK");
}
}