Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows/certificates: Fix bug in environment variable expansion #5697

Merged
merged 4 commits into from
Aug 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion osquery/tables/system/windows/certificates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ void addCertRow(PCCERT_CONTEXT certContext,
}

Status expandEnvironmentVariables(const std::string& src, std::string& dest) {
auto srcW = stringToWstring(src).c_str();
auto srcWstring = stringToWstring(src);
auto srcW = srcWstring.c_str();
auto expandedSize = ExpandEnvironmentStringsW(srcW, nullptr, 0);
if (expandedSize == 0) {
return Status::failure("Unable to get expanded size");
Expand All @@ -541,6 +542,8 @@ Status expandEnvironmentVariables(const std::string& src, std::string& dest) {
auto ret = ExpandEnvironmentStringsW(srcW, buf.data(), expandedSize);
if (ret == 0) {
return Status::failure("Environment variable expansion failed");
} else if (ret != expandedSize) {
return Status::failure("Partial data written");
}

dest = wstringToString(buf.data());
Expand All @@ -557,6 +560,11 @@ void findUserPersonalCertsOnDisk(const std::string& username,

std::string homeDir;
auto homeDirUnexpanded = getUserHomeDir(sid);
if (homeDirUnexpanded.empty()) {
VLOG(1) << "Could not find home dir for account " << username;
return;
}

// System accounts have environment variables in their paths
auto ret = expandEnvironmentVariables(homeDirUnexpanded, homeDir);
if (!ret.ok() || homeDir.empty()) {
Expand Down