Skip to content

Commit

Permalink
Merge commit 'refs/pull/822/head' of https://github.com/azure/azure-o…
Browse files Browse the repository at this point in the history
  • Loading branch information
jepio committed Nov 26, 2024
2 parents ee693f6 + cfcf637 commit 22700c1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/asb/Asb.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,11 @@ void AsbInitialize(void* log)
FREE_MEMORY(prettyName);
FREE_MEMORY(kernelVersion);

if (DetectSelinux(log))
{
OsConfigLogInfo(log, "AsbInitialize: SELinux present; keeping file contexts");
}

if (IsCommodore(log))
{
OsConfigLogInfo(log, "AsbInitialize: running on product '%s'", PRODUCT_NAME_AZURE_COMMODORE);
Expand Down
3 changes: 2 additions & 1 deletion src/common/commonutils/CommonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ int SetPassWarnAge(long days, void* log);
bool IsCurrentOs(const char* name, void* log);
bool IsRedHatBased(void* log);
bool IsCommodore(void* log);
bool DetectSelinux(void* log);

void RemovePrefix(char* target, char marker);
void RemovePrefixBlanks(char* target);
Expand Down Expand Up @@ -238,4 +239,4 @@ char* GetGitBranchFromJsonConfig(const char* jsonString, void* log);
}
#endif

#endif // COMMONUTILS_H
#endif // COMMONUTILS_H
20 changes: 19 additions & 1 deletion src/common/commonutils/DeviceInfoUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,22 @@ bool IsCommodore(void* log)
FREE_MEMORY(textResult);

return status;
}
}

enum SelinuxState {
SelinuxUnknown = 0,
SelinuxFound,
SelinuxNotFound,
};
static enum SelinuxState g_selinuxState = SelinuxUnknown;

bool DetectSelinux(void* log)
{
if (g_selinuxState != SelinuxUnknown)
{
return g_selinuxState == SelinuxFound;
}

g_selinuxState = (0 == CheckTextIsFoundInFile("/sys/kernel/security/lsm", "selinux", NULL, log)) ? SelinuxFound : SelinuxNotFound;
return g_selinuxState == SelinuxFound;
}
33 changes: 33 additions & 0 deletions src/common/commonutils/FileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,29 @@ int GetDirectoryAccess(const char* name, unsigned int* ownerId, unsigned int* gr
return GetAccess(true, name, ownerId, groupId, mode, log);
}

static int RestoreSelinuxContext(const char* target, void* log)
{
char* restoreCommand = NULL;
char* textResult = NULL;
int status = 0;

if (NULL == (restoreCommand = FormatAllocateString("restorecon -F '%s'", target)))
{
OsConfigLogError(log, "RestoreSelinuxContext: out of memory");
return ENOMEM;
}

if (0 != (status = ExecuteCommand(NULL, restoreCommand, false, false, 0, 0, &textResult, NULL, log)))
{
OsConfigLogError(log, "RestoreSelinuxContext: restorecon failed %d: %s", status, textResult);
}

FREE_MEMORY(textResult);
FREE_MEMORY(restoreCommand);

return status;
}

int RenameFile(const char* original, const char* target, void* log)
{
int status = 0;
Expand All @@ -893,6 +916,11 @@ int RenameFile(const char* original, const char* target, void* log)
status = (0 == errno) ? ENOENT : errno;
}

if (DetectSelinux(log))
{
RestoreSelinuxContext(target, log);
}

return status;
}

Expand Down Expand Up @@ -946,6 +974,11 @@ int RenameFileWithOwnerAndAccess(const char* original, const char* target, void*
status = (0 == errno) ? ENOENT : errno;
}

if (DetectSelinux(log))
{
RestoreSelinuxContext(target, log);
}

return status;
}

Expand Down

0 comments on commit 22700c1

Please sign in to comment.