Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/common/telemetry/Telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define _GNU_SOURCE
#endif

#include <CommonUtils.h>
#include <Logging.h>
#include <dlfcn.h>
#include <errno.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Logging.h"
#include "Mmi.h"
#include "Result.h"
#include "Telemetry.h"

#include <cerrno>
#include <cstddef>
Expand Down Expand Up @@ -75,6 +76,7 @@ MMI_HANDLE ComplianceEngineMmiOpen(const char* clientName, const unsigned int ma
if (nullptr == context)
{
OsConfigLogError(g_log, "ComplianceEngineMmiOpen(%s, %u): failed to create context", clientName, maxPayloadSizeBytes);
OSConfigTelemetryStatusTrace("ComplianceEngineMmiOpen", ENOMEM);
return nullptr;
}
std::unique_ptr<ComplianceEngine::PayloadFormatter> formatter;
Expand All @@ -99,6 +101,7 @@ MMI_HANDLE ComplianceEngineMmiOpen(const char* clientName, const unsigned int ma
if (error)
{
OsConfigLogError(g_log, "ComplianceEngineMmiOpen(%s, %u): failed to load distribution info: %s", clientName, maxPayloadSizeBytes, error->message.c_str());
OSConfigTelemetryStatusTrace("ComplianceEngineMmiOpen", error->code);
delete engine;
return nullptr;
}
Expand All @@ -118,13 +121,15 @@ int ComplianceEngineMmiGetInfo(const char* clientName, char** payload, int* payl
if ((nullptr == payload) || (nullptr == payloadSizeBytes))
{
OsConfigLogError(g_log, "ComplianceEngineMmiGetInfo(%s, %p, %p) called with invalid arguments", clientName, payload, payloadSizeBytes);
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGetInfo", EINVAL);
return EINVAL;
}

*payload = strdup(Engine::GetModuleInfo());
if (!*payload)
{
OsConfigLogError(g_log, "ComplianceEngineMmiGetInfo: failed to duplicate module info");
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGetInfo", ENOMEM);
return ENOMEM;
}

Expand All @@ -137,18 +142,21 @@ int ComplianceEngineMmiGet(MMI_HANDLE clientSession, const char* componentName,
if ((nullptr == componentName) || (nullptr == objectName) || (nullptr == payload) || (nullptr == payloadSizeBytes))
{
OsConfigLogError(g_log, "ComplianceEngineMmiGet(%s, %s, %p, %p) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
return EINVAL;
}

if (nullptr == clientSession)
{
OsConfigLogError(g_log, "ComplianceEngineMmiGet(%s, %s) called outside of a valid session", componentName, objectName);
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
return EINVAL;
}

if (0 != strcmp(componentName, "ComplianceEngine"))
{
OsConfigLogError(g_log, "ComplianceEngineMmiGet called for an unsupported component name (%s)", componentName);
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
return EINVAL;
}
auto& engine = *reinterpret_cast<Engine*>(clientSession);
Expand Down
9 changes: 9 additions & 0 deletions src/modules/complianceengine/src/lib/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Optional.h"
#include "Procedure.h"
#include "Result.h"
#include "Telemetry.h"

#include <cerrno>
#include <cstring>
Expand Down Expand Up @@ -66,6 +67,7 @@ Optional<Error> Engine::LoadDistributionInfo()
{
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to parse %s: %s", DistributionInfo::cDefaultOverrideFilePath,
overrideInfo.Error().message.c_str());
OSConfigTelemetryStatusTrace("ParseOverrideFile", overrideInfo.Error().code);
return overrideInfo.Error();
}

Expand All @@ -80,6 +82,7 @@ Optional<Error> Engine::LoadDistributionInfo()
{
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to parse %s: %s", DistributionInfo::cDefaultEtcOsReleasePath,
osReleaseInfo.Error().message.c_str());
OSConfigTelemetryStatusTrace("ParseEtcOsRelease", osReleaseInfo.Error().code);
return osReleaseInfo.Error();
}

Expand All @@ -89,6 +92,7 @@ Optional<Error> Engine::LoadDistributionInfo()
{
int status = errno;
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to access %s: %s", DistributionInfo::cDefaultOverrideFilePath, strerror(status));
OSConfigTelemetryStatusTrace("stat", status);
return Error("Failed to access override file", status);
}

Expand Down Expand Up @@ -203,6 +207,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
if (nullptr == procedure.Audit())
{
OsConfigLogError(Log(), "Failed to copy 'audit' object");
OSConfigTelemetryStatusTrace("Audit", ENOMEM);
return Error("Out of memory");
}

Expand All @@ -222,6 +227,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
if (nullptr == procedure.Remediation())
{
OsConfigLogError(Log(), "Failed to copy 'remediate' object");
OSConfigTelemetryStatusTrace("Remediation", ENOMEM);
return Error("Out of memory");
}
}
Expand All @@ -243,6 +249,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
if ((nullptr == key) || (nullptr == val))
{
OsConfigLogError(Log(), "Failed to get parameter name and value");
OSConfigTelemetryStatusTrace("json_object_get_string", EINVAL);
return Error("Failed to get parameter name and value");
}

Expand Down Expand Up @@ -308,6 +315,7 @@ Result<Status> Engine::MmiSet(const char* objectName, const std::string& payload
if (nullptr == objectName)
{
OsConfigLogError(Log(), "Object name is null");
OSConfigTelemetryStatusTrace("objectName", EINVAL);
return Error("Invalid argument", EINVAL);
}

Expand Down Expand Up @@ -345,6 +353,7 @@ Result<Status> Engine::MmiSet(const char* objectName, const std::string& payload
}

OsConfigLogError(Log(), "Invalid object name: Must start with %s, %s or %s prefix", initPrefix, procedurePrefix, remediatePrefix);
OSConfigTelemetryStatusTrace("objectName", EINVAL);
return Error("Invalid object name");
}
} // namespace ComplianceEngine
4 changes: 4 additions & 0 deletions src/modules/complianceengine/src/lib/FileTreeWalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <FileTreeWalk.h>
#include <Telemetry.h>
#include <dirent.h>

namespace ComplianceEngine
Expand All @@ -26,6 +27,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
}

OsConfigLogError(context.GetLogHandle(), "Failed to open directory '%s': %s", path.c_str(), strerror(status));
OSConfigTelemetryStatusTrace("opendir", status);
return Error("Failed to open directory '" + path + "': " + strerror(status), status);
}

Expand Down Expand Up @@ -67,6 +69,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
{
int status = errno;
OsConfigLogError(context.GetLogHandle(), "Failed to lstat '%s': %s", directory.c_str(), strerror(status));
OSConfigTelemetryStatusTrace("lstat", status);
result = Error("Failed to lstat '" + directory + "': " + strerror(status), status);
break;
}
Expand Down Expand Up @@ -100,6 +103,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
if (0 != status)
{
OsConfigLogError(context.GetLogHandle(), "Failed to iterate directory '%s': %s", path.c_str(), strerror(status));
OSConfigTelemetryStatusTrace("readdir", status);
return Error("Failed to iterate directory '" + path + "': " + strerror(status), status);
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/complianceengine/src/lib/GroupsIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <GroupsIterator.h>
#include <Telemetry.h>
#include <cerrno>
#include <pwd.h>

Expand All @@ -25,6 +26,7 @@ Result<GroupsRange> GroupsRange::Make(std::string path, OsConfigLogHandle logHan
{
int status = errno;
OsConfigLogError(logHandle, "Failed to open file '%s': %s", path.c_str(), strerror(status));
OSConfigTelemetryStatusTrace("fopen", status);
return Error("Failed to create GroupsRange: " + std::string(strerror(status)), status);
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/complianceengine/src/lib/KernelModuleTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Evaluator.h>
#include <KernelModuleTools.h>
#include <Regex.h>
#include <Telemetry.h>
#include <dirent.h>
#include <fts.h>
#include <iostream>
Expand Down Expand Up @@ -59,6 +60,7 @@ Result<bool> SearchFilesystemForModuleName(std::string& moduleName, ContextInter
if (!fts)
{
OsConfigLogError(context.GetLogHandle(), "Failed to open %s - errno %d", modulesVersionDir.c_str(), errno);
OSConfigTelemetryStatusTrace("fts_open", errno);
continue;
}
auto ftspDeleter = std::unique_ptr<FTS, int (*)(FTS*)>(fts, fts_close);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/complianceengine/src/lib/ListValidShells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <ListValidShells.h>
#include <Telemetry.h>
#include <fstream>

namespace ComplianceEngine
Expand All @@ -17,6 +18,7 @@ Result<set<string>> ListValidShells(ContextInterface& context)
if (!shellsFile.is_open())
{
OsConfigLogError(context.GetLogHandle(), "Failed to open %s file", etcShellsPath);
OSConfigTelemetryStatusTrace("fopen", EINVAL);
return Error(std::string("Failed to open ") + etcShellsPath + " file", EINVAL);
}

Expand Down
10 changes: 10 additions & 0 deletions src/modules/complianceengine/src/lib/LuaEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <CommonUtils.h>
#include <Result.h>
#include <Telemetry.h>
#include <iostream>
#include <map>
#include <memory>
Expand Down Expand Up @@ -75,6 +76,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
error += lua_tostring(L, -1);
}
OsConfigLogError(log, "%s", error.c_str());
OSConfigTelemetryStatusTrace("luaL_loadstring", -1);
lua_pop(L, 1);
return Error(error);
}
Expand All @@ -86,6 +88,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
if (!upvalueName)
{
OsConfigLogError(log, "Could not set restricted Lua environment");
OSConfigTelemetryStatusTrace("lua_setupvalue", -1);
lua_pop(L, 1);
lua_settop(L, 0);
return Error("Could not set restricted Lua environment");
Expand All @@ -100,6 +103,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
lua_pop(L, 1);
lua_settop(L, 0);
OsConfigLogError(log, "Restricted Lua environment not found");
OSConfigTelemetryStatusTrace("lua_getfield", -1);
return Error("Restricted Lua environment not found");
}

Expand All @@ -112,6 +116,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
error += lua_tostring(L, -1);
}
OsConfigLogError(log, "%s", error.c_str());
OSConfigTelemetryStatusTrace("lua_pcall", result);
luaL_traceback(L, L, NULL, 1);
const char* traceback = lua_tostring(L, -1);
if (traceback)
Expand All @@ -130,6 +135,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
{
lua_settop(L, 0);
OsConfigLogError(log, "Lua script did not return a value");
OSConfigTelemetryStatusTrace("lua_gettop", -1);
return Error("Lua script did not return a value");
}

Expand Down Expand Up @@ -295,6 +301,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
if (!lua_isstring(L, -1))
{
OsConfigLogError(log, "Failed to get procedure name from upvalue");
OSConfigTelemetryStatusTrace("lua_upvalueindex", -1);
lua_pushstring(L, "Failed to get procedure name from upvalue");
lua_error(L);
return 0;
Expand All @@ -305,6 +312,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
if ((callContext->action != ComplianceEngine::Action::Remediate) && (procedureName.substr(0, 9) == "Remediate"))
{
OsConfigLogError(log, "Remediation not allowed in audit mode");
OSConfigTelemetryStatusTrace("action", EPERM);
lua_pushstring(L, "Remediation not allowed in audit mode");
lua_error(L);
return 0;
Expand All @@ -314,6 +322,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
if (!lua_islightuserdata(L, -1))
{
OsConfigLogError(log, "Failed to get function pointer from upvalue");
OSConfigTelemetryStatusTrace("lua_islightuserdata", -1);
lua_pushstring(L, "Failed to get function pointer from upvalue");
lua_error(L);
return 0;
Expand All @@ -324,6 +333,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
if (!actionFunc)
{
OsConfigLogError(log, "No function for procedure %s", procedureName.c_str());
OSConfigTelemetryStatusTrace("actionFunc", ENOENT);
lua_pushstring(L, ("No function for procedure: " + procedureName).c_str());
lua_error(L);
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/complianceengine/src/lib/NetworkTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <NetworkTools.h>
#include <Telemetry.h>
#include <sstream>

namespace ComplianceEngine
Expand Down Expand Up @@ -64,6 +65,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
if (pos == std::string::npos)
{
OsConfigLogError(context.GetLogHandle(), "Invalid local address format: %s", local.c_str());
OSConfigTelemetryStatusTrace("rfind", EINVAL);
continue;
}
try
Expand All @@ -73,6 +75,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
catch (const std::exception& e)
{
OsConfigLogError(context.GetLogHandle(), "Invalid port number: %s", local.substr(pos + 1).c_str());
OSConfigTelemetryStatusTrace("stoi", EINVAL);
continue;
}

Expand Down Expand Up @@ -102,6 +105,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
if (r <= 0)
{
OsConfigLogError(context.GetLogHandle(), "Invalid IP address: %s", ip.c_str());
OSConfigTelemetryStatusTrace("inet_pton", EINVAL);
continue;
}
openPort.family = AF_INET6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Logging.h>
#include <PasswordEntriesIterator.h>
#include <Telemetry.h>
#include <cerrno>
#include <stdexcept>

Expand Down Expand Up @@ -105,6 +106,7 @@ void PasswordEntryIterator::next() // NOLINT(*-identifier-naming)
}

OsConfigLogError(mRange->GetLogHandle(), "Failed to read /etc/shadow entry: %s (%d)", strerror(status), status);
OSConfigTelemetryStatusTrace("fgetspent_r", status);
throw std::runtime_error("Failed to read /etc/shadow entry: " + string(strerror(status)) + ", errno: " + std::to_string(status));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/complianceengine/src/lib/ReentrantIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Logging.h>
#include <MmiResults.h>
#include <Result.h>
#include <Telemetry.h>
#include <vector>

namespace ComplianceEngine
Expand Down Expand Up @@ -102,6 +103,7 @@ class ReentrantIterator
}

OsConfigLogError(mRange->GetLogHandle(), "Failed to read next entry: %s", strerror(status));
OSConfigTelemetryStatusTrace("ReentrantIterator", status);
throw std::runtime_error("Failed to read next entry: " + std::string(strerror(status)));
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/modules/complianceengine/src/lib/UsersIterator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// // Copyright (c) Microsoft Corporation. All rights reserved.
// // Licensed under the MIT License.

#include <Telemetry.h>
#include <UsersIterator.h>

namespace ComplianceEngine
Expand All @@ -20,8 +21,10 @@ Result<UsersRange> UsersRange::Make(std::string path, OsConfigLogHandle logHandl
auto stream = fopen(path.c_str(), "r");
if (nullptr == stream)
{
OsConfigLogError(logHandle, "Failed to open file '%s': %s", path.c_str(), strerror(errno));
return Error("Failed to create UsersRange: " + std::string(strerror(errno)), errno);
int status = errno;
OsConfigLogError(logHandle, "Failed to open file '%s': %s", path.c_str(), strerror(status));
OSConfigTelemetryStatusTrace("fopen", status);
return Error("Failed to create UsersRange: " + std::string(strerror(status)), status);
}

return UsersRange(stream, logHandle);
Expand Down
Loading
Loading