Skip to content

Commit

Permalink
Copy the parent environment when launching worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Breakwell authored Apr 8, 2020
1 parent 2993321 commit 0e5b048
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
5 changes: 4 additions & 1 deletion osquery/core/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const std::string kBackupDefaultFlagfile{OSQUERY_HOME "osquery.flags.default"};

const size_t kDatabaseMaxRetryCount{25};
const size_t kDatabaseRetryDelay{200};
bool Initializer::isWorker_{false};

namespace {

Expand Down Expand Up @@ -210,6 +211,8 @@ Initializer::Initializer(int& argc,
// The config holds the initialization time for easy access.
Config::setStartTime(getUnixTime());

isWorker_ = hasWorkerVariable();

// osquery can function as the daemon or shell depending on argv[0].
if (tool == ToolType::SHELL_DAEMON) {
if (fs::path(argv[0]).filename().string().find("osqueryd") !=
Expand Down Expand Up @@ -482,7 +485,7 @@ void Initializer::initWorkerWatcher(const std::string& name) const {
}

bool Initializer::isWorker() {
return hasWorkerVariable();
return isWorker_;
}

bool Initializer::isWatcher() {
Expand Down
3 changes: 3 additions & 0 deletions osquery/include/osquery/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ class Initializer : private boost::noncopyable {

/// The deduced program name determined by executing path.
std::string binary_;

/// Is this a worker process
static bool isWorker_;
};

/**
Expand Down
45 changes: 26 additions & 19 deletions osquery/process/windows/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,6 @@ std::shared_ptr<PlatformProcess> PlatformProcess::launchWorker(
handle_stream << hLauncherProcess;
auto handle = handle_stream.str();

// In the POSIX version, the environment variable OSQUERY_WORKER is set to the
// string form of the child process' process ID. However, this is not easily
// doable on Windows. Since the value does not appear to be used by the rest
// of osquery, we currently just set it to '1'.
//
// For the worker case, we also set another environment variable,
// OSQUERY_LAUNCHER. OSQUERY_LAUNCHER stores the string form of a HANDLE to
// the current process. This is mostly used for detecting the death of the
// launcher process in WatcherWatcherRunner::start
if (!setEnvVar("OSQUERY_WORKER", "1") ||
!setEnvVar("OSQUERY_LAUNCHER", handle)) {
::CloseHandle(hLauncherProcess);

return std::shared_ptr<PlatformProcess>();
}

// Since Windows does not accept a char * array for arguments, we have to
// build one as a string. Therefore, we need to make sure that special
// characters are not present that would obstruct the parsing of arguments.
Expand Down Expand Up @@ -219,18 +203,41 @@ std::shared_ptr<PlatformProcess> PlatformProcess::launchWorker(
std::vector<char> mutable_argv(cmdline.begin(), cmdline.end());
mutable_argv.push_back('\0');

LPCH retrievedEnvironment = GetEnvironmentStringsA();
LPTSTR currentEnvironment = (LPTSTR)retrievedEnvironment;
std::stringstream childEnvironment;
while (*currentEnvironment) {
childEnvironment << currentEnvironment;
childEnvironment << '\0';
currentEnvironment += lstrlen(currentEnvironment) + 1;
}

FreeEnvironmentStrings(retrievedEnvironment);

// In the POSIX version, the environment variable OSQUERY_WORKER is set to the
// string form of the child process' process ID. However, this is not easily
// doable on Windows. Since the value does not appear to be used by the rest
// of osquery, we currently just set it to '1'.
//
// For the worker case, we also set another environment variable,
// OSQUERY_LAUNCHER. OSQUERY_LAUNCHER stores the string form of a HANDLE to
// the current process. This is mostly used for detecting the death of the
// launcher process in WatcherWatcherRunner::start
childEnvironment << "OSQUERY_WORKER=1" << '\0';
childEnvironment << "OSQUERY_LAUNCHER=" << handle << '\0' << '\0';

std::string environmentString = childEnvironment.str();

auto status = ::CreateProcessA(exec_path.c_str(),
mutable_argv.data(),
nullptr,
nullptr,
TRUE,
IDLE_PRIORITY_CLASS,
nullptr,
&environmentString[0],
nullptr,
&si,
&pi);
unsetEnvVar("OSQUERY_WORKER");
unsetEnvVar("OSQUERY_LAUNCHER");
::CloseHandle(hLauncherProcess);

if (!status) {
Expand Down

0 comments on commit 0e5b048

Please sign in to comment.