Skip to content

Commit

Permalink
Merge branch 'MariusNi/OsConfig_ASB_Nov07_2024' of https://github.com…
Browse files Browse the repository at this point in the history
…/Azure/azure-osconfig into MariusNi/OsConfig_ASB_Nov07_2024
  • Loading branch information
MariusNi committed Nov 12, 2024
2 parents 229f608 + fa6cae4 commit e8bca27
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/adapters/pnp/PnpAgent.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ int main(int argc, char *argv[])
snprintf(g_productInfo, sizeof(g_productInfo), g_productInfoTemplate, g_modelVersion, OSCONFIG_VERSION, osName, osVersion,
cpuType, cpuVendor, cpuModel, totalMemory, freeMemory, kernelName, kernelRelease, kernelVersion, productVendor, productName);

if (NULL != (encodedProductInfo = UrlEncode(g_productInfo)))
if (NULL != (encodedProductInfo = UrlEncode(g_productInfo, sizeof(g_productInfo))))
{
if (strlen(encodedProductInfo) >= sizeof(g_productInfo))
{
Expand Down
4 changes: 2 additions & 2 deletions src/common/asb/Asb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1897,11 +1897,11 @@ static char* AuditEnsureDisabledInstallationOfJffs2FileSystem(void* log)
static char* AuditEnsureVirtualMemoryRandomizationIsEnabled(void* log)
{
char* reason = NULL;
if (0 == CheckFileContents("/proc/sys/kernel/randomize_va_space", "2", &reason, log))
if (0 == CheckSmallFileContainsText("/proc/sys/kernel/randomize_va_space", "2", &reason, log))
{
return reason;
}
CheckFileContents("/proc/sys/kernel/randomize_va_space", "1", &reason, log);
CheckSmallFileContainsText("/proc/sys/kernel/randomize_va_space", "1", &reason, log);
return reason;
}

Expand Down
6 changes: 3 additions & 3 deletions src/common/commonutils/CommonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ int CheckTextIsFoundInFile(const char* fileName, const char* text, char** reason
int CheckTextIsNotFoundInFile(const char* fileName, const char* text, char** reason, void* log);
int CheckMarkedTextNotFoundInFile(const char* fileName, const char* text, const char* marker, char commentCharacter, char** reason, void* log);
int CheckTextNotFoundInEnvironmentVariable(const char* variableName, const char* text, bool strictComparison, char** reason, void* log);
int CheckFileContents(const char* fileName, const char* text, char** reason, void* log);
int CheckSmallFileContainsText(const char* fileName, const char* text, char** reason, void* log);
int FindTextInFolder(const char* directory, const char* text, void* log);
int CheckTextNotFoundInFolder(const char* directory, const char* text, char** reason, void* log);
int CheckTextFoundInFolder(const char* directory, const char* text, char** reason, void* log);
Expand Down Expand Up @@ -179,8 +179,8 @@ void RemovePrefixUpToString(char* target, const char* marker);
void RemoveTrailingBlanks(char* target);
void TruncateAtFirst(char* target, char marker);

char* UrlEncode(const char* target);
char* UrlDecode(const char* target);
char* UrlEncode(const char* target, size_t targetSize);
char* UrlDecode(const char* target, size_t targetSize);

bool LockFile(FILE* file, void* log);
bool UnlockFile(FILE* file, void* log);
Expand Down
2 changes: 1 addition & 1 deletion src/common/commonutils/DeviceInfoUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ int EnableVirtualMemoryRandomization(void* log)
const char* fullRandomization = "2";
int status = 0;

if (0 == CheckFileContents(procSysKernelRandomizeVaSpace, fullRandomization, NULL, log))
if (0 == CheckSmallFileContainsText(procSysKernelRandomizeVaSpace, fullRandomization, NULL, log))
{
OsConfigLogInfo(log, "EnableVirtualMemoryRandomization: full virtual memory randomization '%s' is already enabled in '%s'", fullRandomization, procSysKernelRandomizeVaSpace);
}
Expand Down
17 changes: 11 additions & 6 deletions src/common/commonutils/FileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1354,31 +1354,36 @@ int CheckTextNotFoundInEnvironmentVariable(const char* variableName, const char*
return status;
}

int CheckFileContents(const char* fileName, const char* text, char** reason, void* log)
int CheckSmallFileContainsText(const char* fileName, const char* text, char** reason, void* log)
{
struct stat statStruct = {0};
char* contents = NULL;
size_t textLength = 0, contentsLength = 0;
int status = 0;

if ((NULL == fileName) || (NULL == text) || (0 == strlen(fileName)) || (0 == strlen(text)))
if ((NULL == fileName) || (NULL == text) || (0 == strlen(fileName)) || (0 == (textLength = strlen(text))))
{
OsConfigLogError(log, "CheckSmallFileContainsText called with invalid arguments");
return EINVAL;
}
else if ((0 == stat(fileName, &statStruct)) && ((statStruct.st_size > MAX_STRING_LENGTH)))
{
OsConfigLogError(log, "CheckFileContents called with invalid arguments");
OsConfigLogError(log, "CheckSmallFileContainsText: file is too large (%lu bytes, maximum supported: %d bytes)", statStruct.st_size, MAX_STRING_LENGTH);
return EINVAL;
}

if (NULL != (contents = LoadStringFromFile(fileName, false, log)))
{
textLength = strlen(text);
contentsLength = strlen(text);

if (0 == strncmp(contents, text, (textLength <= contentsLength) ? textLength : contentsLength))
{
OsConfigLogInfo(log, "CheckFileContents: '%s' matches contents of '%s'", text, fileName);
OsConfigLogInfo(log, "CheckSmallFileContainsText: '%s' matches contents of '%s'", text, fileName);
OsConfigCaptureSuccessReason(reason, "'%s' matches contents of '%s'", text, fileName);
}
else
{
OsConfigLogInfo(log, "CheckFileContents: '%s' does not match contents of '%s' ('%s')", text, fileName, contents);
OsConfigLogInfo(log, "CheckSmallFileContainsText: '%s' does not match contents of '%s' ('%s')", text, fileName, contents);
OsConfigCaptureReason(reason, "'%s' does not match contents of '%s'", text, fileName);
status = ENOENT;
}
Expand Down
26 changes: 13 additions & 13 deletions src/common/commonutils/UrlUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

#include "Internal.h"

char* UrlEncode(const char* target)
char* UrlEncode(const char* target, size_t targetSize)
{
size_t targetLength = 0, i = 0, j = 0;
size_t i = 0, j = 0;
int encodedLength = 0;
char* encodedTarget = NULL;

if ((NULL == target) || (0 == (targetLength = strlen(target))))
if ((NULL == target) || (0 == targetSize))
{
return NULL;
}

encodedLength = (3 * targetLength) + 3;
encodedLength = (3 * targetSize) + 3;

if (NULL != (encodedTarget = (char*)malloc(encodedLength)))
{
memset(encodedTarget, 0, encodedLength);

for (i = 0; i < targetLength; i++)
for (i = 0; i < targetSize; i++)
{
if (isalnum(target[i]) || ('-' == target[i]) || ('_' == target[i]) || ('.' == target[i]) || ('~' == target[i]))
{
Expand All @@ -43,23 +43,23 @@ char* UrlEncode(const char* target)
return encodedTarget;
}

char* UrlDecode(const char* target)
char* UrlDecode(const char* target, size_t targetSize)
{
size_t targetLength = 0, i = 0, j = 0;
size_t i = 0, j = 0;
char buffer[3] = {0};
unsigned int value = 0;
char* decodedTarget = NULL;

if ((NULL == target) || (0 == (targetLength = strlen(target))))
if ((NULL == target) || (0 == targetSize))
{
return NULL;
}

if (NULL != (decodedTarget = (char*)malloc(targetLength + 3)))
if (NULL != (decodedTarget = (char*)malloc(targetSize + 3)))
{
memset(decodedTarget, 0, targetLength + 3);
memset(decodedTarget, 0, targetSize + 3);

for (i = 0, j = 0; (i < targetLength) && (j < targetLength); i++)
for (i = 0, j = 0; (i < targetSize) && (j < targetSize); i++)
{
if ((isalnum(target[j])) || ('-' == target[j]) || ('_' == target[j]) || ('.' == target[j]) || ('~' == target[j]))
{
Expand All @@ -68,7 +68,7 @@ char* UrlDecode(const char* target)
}
else if ('%' == target[j])
{
if (((j + 2) < targetLength) && ('0' == target[j + 1]) && ('A' == toupper(target[j + 2])))
if (((j + 2) < targetSize) && ('0' == target[j + 1]) && ('A' == toupper(target[j + 2])))
{
decodedTarget[i] = '\n';
}
Expand All @@ -78,7 +78,7 @@ char* UrlDecode(const char* target)
buffer[2] = 0;

sscanf(buffer, "%x", &value);
snprintf(&decodedTarget[i], targetLength - i, "%c", value);
snprintf(&decodedTarget[i], targetSize - i, "%c", value);
}

j += sizeof(buffer);
Expand Down
33 changes: 21 additions & 12 deletions src/common/tests/CommonUtilsUT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TEST_F(CommonUtilsTest, LoadStringFromSingleByteFile)
EXPECT_EQ(data[0], contents[0]);
EXPECT_EQ(0, contents[1]);
FREE_MEMORY(contents);
EXPECT_EQ(0, CheckFileContents(m_path, data, nullptr, nullptr));
EXPECT_EQ(0, CheckSmallFileContainsText(m_path, data, nullptr, nullptr));
EXPECT_TRUE(Cleanup(m_path));
}

Expand Down Expand Up @@ -1094,17 +1094,19 @@ TEST_F(CommonUtilsTest, UrlEncodeDecode)

for (int i = 0; i < testUrlsSize; i++)
{
EXPECT_NE(nullptr, url = UrlEncode((char*)testUrls[i].decoded));
EXPECT_NE(nullptr, url = UrlEncode((char*)testUrls[i].decoded, strlen(testUrls[i].decoded)));
EXPECT_STREQ(url, testUrls[i].encoded);
FREE_MEMORY(url);

EXPECT_NE(nullptr, url = UrlDecode((char*)testUrls[i].encoded));
EXPECT_NE(nullptr, url = UrlDecode((char*)testUrls[i].encoded, strlen(testUrls[i].encoded)));
EXPECT_STREQ(url, testUrls[i].decoded);
FREE_MEMORY(url);
}

EXPECT_EQ(nullptr, url = UrlEncode(nullptr));
EXPECT_EQ(nullptr, url = UrlDecode(nullptr));
EXPECT_EQ(nullptr, url = UrlEncode(nullptr, 1));
EXPECT_EQ(nullptr, url = UrlDecode(nullptr, 1));
EXPECT_EQ(nullptr, url = UrlEncode(nullptr, 0));
EXPECT_EQ(nullptr, url = UrlDecode(nullptr, 0));
}

TEST_F(CommonUtilsTest, LockUnlockFile)
Expand Down Expand Up @@ -1675,23 +1677,30 @@ TEST_F(CommonUtilsTest, CheckTextNotFoundInEnvironmentVariable)
EXPECT_EQ(0, unsetenv("TESTVAR"));
}

TEST_F(CommonUtilsTest, CheckFileContents)
TEST_F(CommonUtilsTest, CheckSmallFileContainsText)
{
EXPECT_EQ(EINVAL, CheckFileContents(nullptr, "2", nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckFileContents(nullptr, nullptr, nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckFileContents(m_path, nullptr, nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckFileContents(m_path, "", nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckFileContents("", "2", nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(nullptr, "2", nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(nullptr, nullptr, nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(m_path, nullptr, nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(m_path, "", nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText("", "2", nullptr, nullptr));

const char* test[] = {"2", "ABC", "~1", "This is a test", "One line\nAnd another\n"};
size_t sizeOfTest = ARRAY_SIZE(test);

for (size_t i = 0; i < sizeOfTest; i++)
{
EXPECT_TRUE(CreateTestFile(m_path, test[i]));
EXPECT_EQ(0, CheckFileContents(m_path, test[i], nullptr, nullptr));
EXPECT_EQ(0, CheckSmallFileContainsText(m_path, test[i], nullptr, nullptr));
EXPECT_TRUE(Cleanup(m_path));
}

char bigBuffer[1024] = {0};
memset(bigBuffer, 1, sizeof(bigBuffer) - 1);
EXPECT_TRUE(CreateTestFile(m_path, bigBuffer));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(m_path, bigBuffer, nullptr, nullptr));
EXPECT_EQ(EINVAL, CheckSmallFileContainsText(m_path, "11", nullptr, nullptr));
EXPECT_TRUE(Cleanup(m_path));
}

TEST_F(CommonUtilsTest, OtherOptionalTests)
Expand Down

0 comments on commit e8bca27

Please sign in to comment.