2015-01-26 08:02:02 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2015-06-04 18:46:19 +00:00
|
|
|
#include <math.h>
|
2015-01-26 08:02:02 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2015-02-09 00:00:43 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2015-02-06 17:42:03 +00:00
|
|
|
#include <osquery/filesystem.h>
|
2015-01-26 08:02:02 +00:00
|
|
|
#include <osquery/logger.h>
|
|
|
|
#include <osquery/sql.h>
|
|
|
|
|
|
|
|
#include "osquery/core/watcher.h"
|
2015-05-04 03:02:01 +00:00
|
|
|
#include "osquery/dispatcher/dispatcher.h"
|
2015-01-26 08:02:02 +00:00
|
|
|
|
2015-02-06 17:42:03 +00:00
|
|
|
extern char** environ;
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2015-01-26 08:02:02 +00:00
|
|
|
namespace osquery {
|
|
|
|
|
2015-02-09 00:00:43 +00:00
|
|
|
const std::map<WatchdogLimitType, std::vector<size_t> > kWatchdogLimits = {
|
2015-02-19 23:19:00 +00:00
|
|
|
// Maximum MB worker can privately allocate.
|
2015-06-05 00:13:28 +00:00
|
|
|
{MEMORY_LIMIT, {80, 50, 30, 1000}},
|
2015-02-19 23:19:00 +00:00
|
|
|
// Percent of user or system CPU worker can utilize for LATENCY_LIMIT
|
|
|
|
// seconds.
|
2015-05-04 03:02:01 +00:00
|
|
|
{UTILIZATION_LIMIT, {90, 80, 60, 1000}},
|
2015-02-09 00:00:43 +00:00
|
|
|
// Number of seconds the worker should run, else consider the exit fatal.
|
2015-02-19 23:19:00 +00:00
|
|
|
{RESPAWN_LIMIT, {20, 20, 20, 5}},
|
2015-02-09 00:00:43 +00:00
|
|
|
// If the worker respawns too quickly, backoff on creating additional.
|
2015-02-19 23:19:00 +00:00
|
|
|
{RESPAWN_DELAY, {5, 5, 5, 1}},
|
|
|
|
// Seconds of tolerable UTILIZATION_LIMIT sustained latency.
|
2015-03-04 01:10:40 +00:00
|
|
|
{LATENCY_LIMIT, {12, 6, 3, 1}},
|
2015-02-09 00:00:43 +00:00
|
|
|
// How often to poll for performance limit violations.
|
2015-05-04 03:02:01 +00:00
|
|
|
{INTERVAL, {3, 3, 3, 1}},
|
|
|
|
};
|
2015-02-09 00:00:43 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
const std::string kExtensionExtension = ".ext";
|
|
|
|
|
2015-03-14 01:18:18 +00:00
|
|
|
CLI_FLAG(int32,
|
|
|
|
watchdog_level,
|
2015-11-02 08:44:33 +00:00
|
|
|
0,
|
2015-03-14 01:18:18 +00:00
|
|
|
"Performance limit level (0=loose, 1=normal, 2=restrictive, 3=debug)");
|
2015-02-17 08:36:20 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
CLI_FLAG(bool, disable_watchdog, false, "Disable userland watchdog process");
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-04 01:10:40 +00:00
|
|
|
/// If the worker exits the watcher will inspect the return code.
|
|
|
|
void childHandler(int signum) {
|
2015-03-04 02:40:24 +00:00
|
|
|
siginfo_t info;
|
2015-03-04 16:45:21 +00:00
|
|
|
// Make sure WNOWAIT is used to the wait information is not removed.
|
|
|
|
// Watcher::watch implements a thread to poll for this information.
|
2015-03-04 02:40:24 +00:00
|
|
|
waitid(P_ALL, 0, &info, WEXITED | WSTOPPED | WNOHANG | WNOWAIT);
|
2015-12-15 00:19:37 +00:00
|
|
|
if (info.si_code == CLD_EXITED) {
|
|
|
|
if (info.si_status == EXIT_CATASTROPHIC) {
|
|
|
|
// A child process had a catastrophic error, abort the watcher.
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
} else if (info.si_status == EXIT_SUCCESS) {
|
|
|
|
// Child process is finished.
|
|
|
|
::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2015-03-04 01:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void Watcher::resetWorkerCounters(size_t respawn_time) {
|
|
|
|
// Reset the monitoring counters for the watcher.
|
|
|
|
auto& state = instance().state_;
|
|
|
|
state.sustained_latency = 0;
|
|
|
|
state.user_time = 0;
|
|
|
|
state.system_time = 0;
|
|
|
|
state.last_respawn_time = respawn_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watcher::resetExtensionCounters(const std::string& extension,
|
|
|
|
size_t respawn_time) {
|
|
|
|
WatcherLocker locker;
|
|
|
|
auto& state = instance().extension_states_[extension];
|
|
|
|
state.sustained_latency = 0;
|
|
|
|
state.user_time = 0;
|
|
|
|
state.system_time = 0;
|
|
|
|
state.last_respawn_time = respawn_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Watcher::getExtensionPath(pid_t child) {
|
|
|
|
for (const auto& extension : extensions()) {
|
|
|
|
if (extension.second == child) {
|
|
|
|
return extension.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watcher::removeExtensionPath(const std::string& extension) {
|
|
|
|
WatcherLocker locker;
|
|
|
|
instance().extensions_.erase(extension);
|
|
|
|
instance().extension_states_.erase(extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
PerformanceState& Watcher::getState(pid_t child) {
|
|
|
|
if (child == instance().worker_) {
|
|
|
|
return instance().state_;
|
|
|
|
} else {
|
|
|
|
return instance().extension_states_[getExtensionPath(child)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PerformanceState& Watcher::getState(const std::string& extension) {
|
|
|
|
return instance().extension_states_[extension];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watcher::setExtension(const std::string& extension, pid_t child) {
|
|
|
|
WatcherLocker locker;
|
|
|
|
instance().extensions_[extension] = child;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watcher::reset(pid_t child) {
|
|
|
|
if (child == instance().worker_) {
|
|
|
|
instance().worker_ = 0;
|
|
|
|
resetWorkerCounters(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it was not the worker pid then find the extension name to reset.
|
|
|
|
for (const auto& extension : extensions()) {
|
|
|
|
if (extension.second == child) {
|
|
|
|
setExtension(extension.first, 0);
|
|
|
|
resetExtensionCounters(extension.first, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watcher::addExtensionPath(const std::string& path) {
|
|
|
|
// Resolve acceptable extension binaries from autoload paths.
|
2015-03-17 16:49:30 +00:00
|
|
|
if (isDirectory(path).ok()) {
|
|
|
|
VLOG(1) << "Cannot autoload extension from directory: " << path;
|
2015-03-13 15:11:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-17 16:49:30 +00:00
|
|
|
// Only autoload extensions which were safe at the time of discovery.
|
|
|
|
// If the extension binary later becomes unsafe (permissions change) then
|
|
|
|
// it will fail to reload if a reload is ever needed.
|
|
|
|
fs::path extension(path);
|
|
|
|
if (safePermissions(extension.parent_path().string(), path, true)) {
|
|
|
|
if (extension.extension().string() == kExtensionExtension) {
|
|
|
|
setExtension(extension.string(), 0);
|
|
|
|
resetExtensionCounters(extension.string(), 0);
|
|
|
|
VLOG(1) << "Found autoloadable extension: " << extension.string();
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:29:23 +00:00
|
|
|
bool Watcher::hasManagedExtensions() {
|
|
|
|
if (instance().extensions_.size() > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A watchdog process may hint to a worker the number of managed extensions.
|
|
|
|
// Setting this counter to 0 will prevent the worker from waiting for missing
|
2015-07-01 22:26:26 +00:00
|
|
|
// dependent config plugins. Otherwise, its existence, will cause a worker to
|
2015-03-17 07:29:23 +00:00
|
|
|
// wait for missing plugins to broadcast from managed extensions.
|
|
|
|
return (getenv("OSQUERY_EXTENSIONS") != nullptr);
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
bool WatcherRunner::ok() {
|
2015-03-14 01:18:18 +00:00
|
|
|
interruptableSleep(getWorkerLimit(INTERVAL) * 1000);
|
2015-03-13 15:11:08 +00:00
|
|
|
// Watcher is OK to run if a worker or at least one extension exists.
|
2015-03-17 07:29:23 +00:00
|
|
|
return (Watcher::getWorker() >= 0 || Watcher::hasManagedExtensions());
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 00:09:07 +00:00
|
|
|
void WatcherRunner::start() {
|
2015-03-13 15:11:08 +00:00
|
|
|
// Set worker performance counters to an initial state.
|
|
|
|
Watcher::resetWorkerCounters(0);
|
|
|
|
signal(SIGCHLD, childHandler);
|
|
|
|
|
|
|
|
// Enter the watch loop.
|
|
|
|
do {
|
|
|
|
if (use_worker_ && !watch(Watcher::getWorker())) {
|
2015-09-02 23:53:29 +00:00
|
|
|
if (Watcher::fatesBound()) {
|
|
|
|
break;
|
|
|
|
}
|
2015-03-13 15:11:08 +00:00
|
|
|
// The watcher failed, create a worker.
|
|
|
|
createWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over every managed extension and check sanity.
|
|
|
|
std::vector<std::string> failing_extensions;
|
|
|
|
for (const auto& extension : Watcher::extensions()) {
|
|
|
|
if (!watch(extension.second)) {
|
|
|
|
if (!createExtension(extension.first)) {
|
|
|
|
failing_extensions.push_back(extension.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If any extension creations failed, stop managing them.
|
|
|
|
for (const auto& failed_extension : failing_extensions) {
|
|
|
|
Watcher::removeExtensionPath(failed_extension);
|
|
|
|
}
|
|
|
|
} while (ok());
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
bool WatcherRunner::watch(pid_t child) {
|
2015-03-04 02:40:24 +00:00
|
|
|
int status;
|
2015-03-13 15:11:08 +00:00
|
|
|
pid_t result = waitpid(child, &status, WNOHANG);
|
|
|
|
if (child == 0 || result == child) {
|
2015-01-26 08:02:02 +00:00
|
|
|
// Worker does not exist or never existed.
|
|
|
|
return false;
|
|
|
|
} else if (result == 0) {
|
|
|
|
// If the inspect finds problems it will stop/restart the worker.
|
2015-03-13 15:11:08 +00:00
|
|
|
if (!isChildSane(child)) {
|
|
|
|
stopChild(child);
|
2015-01-26 08:02:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void WatcherRunner::stopChild(pid_t child) {
|
|
|
|
kill(child, SIGKILL);
|
2015-03-04 01:10:40 +00:00
|
|
|
|
2015-01-26 08:02:02 +00:00
|
|
|
// Clean up the defunct (zombie) process.
|
2015-03-13 15:11:08 +00:00
|
|
|
waitpid(-1, 0, WNOHANG);
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
bool WatcherRunner::isChildSane(pid_t child) {
|
2015-05-21 08:22:00 +00:00
|
|
|
auto rows = SQL::selectAllFrom("processes", "pid", EQUALS, INTEGER(child));
|
2015-01-26 08:02:02 +00:00
|
|
|
if (rows.size() == 0) {
|
|
|
|
// Could not find worker process?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Get the performance state for the worker or extension.
|
|
|
|
size_t sustained_latency = 0;
|
2015-01-26 08:02:02 +00:00
|
|
|
// Compare CPU utilization since last check.
|
2015-11-01 05:20:23 +00:00
|
|
|
size_t footprint = 0;
|
|
|
|
pid_t parent = 0;
|
2015-02-09 00:00:43 +00:00
|
|
|
// IV is the check interval in seconds, and utilization is set per-second.
|
2015-04-11 22:57:12 +00:00
|
|
|
auto iv = std::max(getWorkerLimit(INTERVAL), (size_t)1);
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
{
|
|
|
|
WatcherLocker locker;
|
2015-06-05 00:13:28 +00:00
|
|
|
auto& state = Watcher::getState(child);
|
2015-11-01 05:20:23 +00:00
|
|
|
UNSIGNED_BIGINT_LITERAL user_time = 0, system_time = 0;
|
2015-03-13 15:11:08 +00:00
|
|
|
try {
|
|
|
|
parent = AS_LITERAL(BIGINT_LITERAL, rows[0].at("parent"));
|
|
|
|
user_time = AS_LITERAL(BIGINT_LITERAL, rows[0].at("user_time")) / iv;
|
|
|
|
system_time = AS_LITERAL(BIGINT_LITERAL, rows[0].at("system_time")) / iv;
|
2015-03-26 23:18:28 +00:00
|
|
|
footprint = AS_LITERAL(BIGINT_LITERAL, rows[0].at("resident_size"));
|
2015-03-13 15:11:08 +00:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
state.sustained_latency = 0;
|
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-06-05 00:13:28 +00:00
|
|
|
// Check the difference of CPU time used since last check.
|
|
|
|
if (user_time - state.user_time > getWorkerLimit(UTILIZATION_LIMIT) ||
|
|
|
|
system_time - state.system_time > getWorkerLimit(UTILIZATION_LIMIT)) {
|
2015-03-13 15:11:08 +00:00
|
|
|
state.sustained_latency++;
|
|
|
|
} else {
|
|
|
|
state.sustained_latency = 0;
|
|
|
|
}
|
|
|
|
// Update the current CPU time.
|
|
|
|
state.user_time = user_time;
|
|
|
|
state.system_time = system_time;
|
|
|
|
|
|
|
|
// Check if the sustained difference exceeded the acceptable latency limit.
|
|
|
|
sustained_latency = state.sustained_latency;
|
2015-05-04 18:30:25 +00:00
|
|
|
|
|
|
|
// Set the memory footprint as the amount of resident bytes allocated
|
|
|
|
// since the process image was created (estimate).
|
|
|
|
// A more-meaningful check would limit this to writable regions.
|
|
|
|
if (state.initial_footprint == 0) {
|
|
|
|
state.initial_footprint = footprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the measured/limit-applied footprint to the post-launch allocations.
|
|
|
|
if (footprint < state.initial_footprint) {
|
|
|
|
footprint = 0;
|
|
|
|
} else {
|
|
|
|
footprint = footprint - state.initial_footprint;
|
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Only make a decision about the child sanity if it is still the watcher's
|
|
|
|
// child. It's possible for the child to die, and its pid reused.
|
|
|
|
if (parent != getpid()) {
|
|
|
|
// The child's parent is not the watcher.
|
|
|
|
Watcher::reset(child);
|
|
|
|
// Do not stop or call the child insane, since it is not our child.
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
if (sustained_latency > 0 &&
|
|
|
|
sustained_latency * iv >= getWorkerLimit(LATENCY_LIMIT)) {
|
2015-07-01 22:26:26 +00:00
|
|
|
LOG(WARNING) << "osqueryd worker (" << child
|
|
|
|
<< ") system performance limits exceeded";
|
2015-01-26 08:02:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-13 15:11:08 +00:00
|
|
|
// Check if the private memory exceeds a memory limit.
|
2015-03-04 01:10:40 +00:00
|
|
|
if (footprint > 0 && footprint > getWorkerLimit(MEMORY_LIMIT) * 1024 * 1024) {
|
2015-07-01 22:26:26 +00:00
|
|
|
LOG(WARNING) << "osqueryd worker (" << child
|
|
|
|
<< ") memory limits exceeded: " << footprint;
|
2015-01-26 08:02:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The worker is sane, no action needed.
|
2015-07-01 22:26:26 +00:00
|
|
|
// Attempt to flush status logs to the well-behaved worker.
|
|
|
|
relayStatusLogs();
|
2015-01-26 08:02:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void WatcherRunner::createWorker() {
|
|
|
|
{
|
|
|
|
WatcherLocker locker;
|
|
|
|
if (Watcher::getState(Watcher::getWorker()).last_respawn_time >
|
|
|
|
getUnixTime() - getWorkerLimit(RESPAWN_LIMIT)) {
|
2015-07-01 22:26:26 +00:00
|
|
|
LOG(WARNING) << "osqueryd worker respawning too quickly: "
|
|
|
|
<< Watcher::workerRestartCount() << " times";
|
2015-06-04 18:46:19 +00:00
|
|
|
Watcher::workerRestarted();
|
2015-03-14 01:18:18 +00:00
|
|
|
interruptableSleep(getWorkerLimit(RESPAWN_DELAY) * 1000);
|
2015-06-04 18:46:19 +00:00
|
|
|
// Exponential back off for quickly-respawning clients.
|
|
|
|
interruptableSleep(pow(2, Watcher::workerRestartCount()) * 1000);
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 00:00:12 +00:00
|
|
|
// Get the path of the current process.
|
2015-05-21 08:22:00 +00:00
|
|
|
auto qd = SQL::selectAllFrom("processes", "pid", EQUALS, INTEGER(getpid()));
|
2015-02-20 00:00:12 +00:00
|
|
|
if (qd.size() != 1 || qd[0].count("path") == 0 || qd[0]["path"].size() == 0) {
|
2015-07-01 22:26:26 +00:00
|
|
|
LOG(ERROR) << "osquery watcher cannot determine process path for worker";
|
2015-02-20 00:00:12 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:29:23 +00:00
|
|
|
// Set an environment signaling to potential plugin-dependent workers to wait
|
|
|
|
// for extensions to broadcast.
|
|
|
|
if (Watcher::hasManagedExtensions()) {
|
|
|
|
setenv("OSQUERY_EXTENSIONS", "true", 1);
|
|
|
|
}
|
|
|
|
|
2015-05-07 07:19:10 +00:00
|
|
|
// Get the complete path of the osquery process binary.
|
|
|
|
auto exec_path = fs::system_complete(fs::path(qd[0]["path"]));
|
|
|
|
if (!safePermissions(
|
|
|
|
exec_path.parent_path().string(), exec_path.string(), true)) {
|
|
|
|
// osqueryd binary has become unsafe.
|
2015-07-23 23:05:51 +00:00
|
|
|
LOG(ERROR) << RLOG(1382)
|
|
|
|
<< "osqueryd has unsafe permissions: " << exec_path.string();
|
2015-05-07 07:19:10 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
auto worker_pid = fork();
|
|
|
|
if (worker_pid < 0) {
|
2015-01-26 08:02:02 +00:00
|
|
|
// Unrecoverable error, cannot create a worker process.
|
|
|
|
LOG(ERROR) << "osqueryd could not create a worker process";
|
|
|
|
::exit(EXIT_FAILURE);
|
2015-03-13 15:11:08 +00:00
|
|
|
} else if (worker_pid == 0) {
|
2015-01-26 08:02:02 +00:00
|
|
|
// This is the new worker process, no watching needed.
|
2015-03-17 07:29:23 +00:00
|
|
|
setenv("OSQUERY_WORKER", std::to_string(getpid()).c_str(), 1);
|
2015-02-06 17:42:03 +00:00
|
|
|
execve(exec_path.string().c_str(), argv_, environ);
|
2015-03-13 15:11:08 +00:00
|
|
|
// Code should never reach this point.
|
|
|
|
LOG(ERROR) << "osqueryd could not start worker process";
|
|
|
|
::exit(EXIT_CATASTROPHIC);
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
Watcher::setWorker(worker_pid);
|
|
|
|
Watcher::resetWorkerCounters(getUnixTime());
|
2015-02-06 17:42:03 +00:00
|
|
|
VLOG(1) << "osqueryd watcher (" << getpid() << ") executing worker ("
|
2015-03-13 15:11:08 +00:00
|
|
|
<< worker_pid << ")";
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
bool WatcherRunner::createExtension(const std::string& extension) {
|
|
|
|
{
|
|
|
|
WatcherLocker locker;
|
|
|
|
if (Watcher::getState(extension).last_respawn_time >
|
|
|
|
getUnixTime() - getWorkerLimit(RESPAWN_LIMIT)) {
|
|
|
|
LOG(WARNING) << "Extension respawning too quickly: " << extension;
|
|
|
|
// Unlike a worker, if an extension respawns to quickly we give up.
|
|
|
|
return false;
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Check the path to the previously-discovered extension binary.
|
|
|
|
auto exec_path = fs::system_complete(fs::path(extension));
|
|
|
|
if (!safePermissions(
|
|
|
|
exec_path.parent_path().string(), exec_path.string(), true)) {
|
|
|
|
// Extension binary has become unsafe.
|
2015-07-23 23:05:51 +00:00
|
|
|
LOG(WARNING) << RLOG(1382)
|
|
|
|
<< "Extension binary has unsafe permissions: " << extension;
|
2015-03-13 15:11:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
auto ext_pid = fork();
|
|
|
|
if (ext_pid < 0) {
|
|
|
|
// Unrecoverable error, cannot create an extension process.
|
|
|
|
LOG(ERROR) << "Cannot create extension process: " << extension;
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
} else if (ext_pid == 0) {
|
|
|
|
// Pass the current extension socket and a set timeout to the extension.
|
|
|
|
setenv("OSQUERY_EXTENSION", std::to_string(getpid()).c_str(), 1);
|
|
|
|
// Execute extension with very specific arguments.
|
|
|
|
execle(exec_path.string().c_str(),
|
|
|
|
("osquery extension: " + extension).c_str(),
|
|
|
|
"--socket",
|
|
|
|
Flag::getValue("extensions_socket").c_str(),
|
|
|
|
"--timeout",
|
|
|
|
Flag::getValue("extensions_timeout").c_str(),
|
2015-03-14 01:18:18 +00:00
|
|
|
"--interval",
|
|
|
|
Flag::getValue("extensions_interval").c_str(),
|
2015-03-13 15:11:08 +00:00
|
|
|
(Flag::getValue("verbose") == "true") ? "--verbose" : (char*)nullptr,
|
|
|
|
(char*)nullptr,
|
|
|
|
environ);
|
|
|
|
// Code should never reach this point.
|
|
|
|
VLOG(1) << "Could not start extension process: " << extension;
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Watcher::setExtension(extension, ext_pid);
|
|
|
|
Watcher::resetExtensionCounters(extension, getUnixTime());
|
2015-12-08 07:08:00 +00:00
|
|
|
VLOG(1) << "Created and monitoring extension child (" << ext_pid
|
|
|
|
<< "): " << extension;
|
2015-03-13 15:11:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-05-06 00:09:07 +00:00
|
|
|
void WatcherWatcherRunner::start() {
|
2015-02-06 17:42:03 +00:00
|
|
|
while (true) {
|
|
|
|
if (getppid() != watcher_) {
|
|
|
|
// Watcher died, the worker must follow.
|
|
|
|
VLOG(1) << "osqueryd worker (" << getpid()
|
|
|
|
<< ") detected killed watcher (" << watcher_ << ")";
|
2015-05-06 00:09:07 +00:00
|
|
|
Dispatcher::stopServices();
|
2015-05-24 01:52:42 +00:00
|
|
|
// The watcher watcher is a thread. Do not join services after removing.
|
2015-02-06 17:42:03 +00:00
|
|
|
::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2015-02-09 00:00:43 +00:00
|
|
|
interruptableSleep(getWorkerLimit(INTERVAL) * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getWorkerLimit(WatchdogLimitType name, int level) {
|
|
|
|
if (kWatchdogLimits.count(name) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no level was provided then use the default (config/switch).
|
|
|
|
if (level == -1) {
|
|
|
|
level = FLAGS_watchdog_level;
|
|
|
|
}
|
|
|
|
if (level > 3) {
|
|
|
|
return kWatchdogLimits.at(name).back();
|
2015-02-06 17:42:03 +00:00
|
|
|
}
|
2015-02-09 00:00:43 +00:00
|
|
|
return kWatchdogLimits.at(name).at(level);
|
2015-01-26 08:02:02 +00:00
|
|
|
}
|
|
|
|
}
|