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

Add logging SAC state at the start of build #10538

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
69 changes: 69 additions & 0 deletions src/Framework/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ internal static class NativeMethods
private const string WINDOWS_FILE_SYSTEM_REGISTRY_KEY = @"SYSTEM\CurrentControlSet\Control\FileSystem";
private const string WINDOWS_LONG_PATHS_ENABLED_VALUE_NAME = "LongPathsEnabled";

private const string WINDOWS_SAC_REGISTRY_KEY = @"SYSTEM\CurrentControlSet\Control\CI\Policy";
private const string WINDOWS_SAC_VALUE_NAME = "VerifiedAndReputablePolicyState";

internal static DateTime MinFileDate { get; } = DateTime.FromFileTimeUtc(0);

internal static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
Expand Down Expand Up @@ -661,6 +664,72 @@ private static LongPathsStatus IsLongPathsEnabledRegistry()
}
}

internal static SAC_State GetSACState()
{
if (IsWindows)
{
try
{
return GetSACStateRegistry();
}
catch
{
return SAC_State.Missing;
}
}

return SAC_State.NotApplicable;
}

[SupportedOSPlatform("windows")]
private static SAC_State GetSACStateRegistry()
{
SAC_State SACState = SAC_State.Missing;

using (RegistryKey policyKey = Registry.LocalMachine.OpenSubKey(WINDOWS_SAC_REGISTRY_KEY))
{
object sacValue = policyKey?.GetValue(WINDOWS_SAC_VALUE_NAME, 0);
if (policyKey != null)
{
SACState = Convert.ToInt32(sacValue) switch
{
0 => SAC_State.Off,
1 => SAC_State.Enforcement,
2 => SAC_State.Evaluation,
_ => SAC_State.Missing,
};
}
}
return SACState;
}

/// <summary>
/// State of Smart App Control (SAC) on the system.
/// </summary>
internal enum SAC_State
JanProvaznik marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// 0: SAC is off.
/// </summary>
Off,
/// <summary>
/// 1: SAC is on and enforcing.
/// </summary>
Enforcement,
/// <summary>
/// 2: SAC is on and in evaluation mode.
/// </summary>
Evaluation,
/// <summary>
/// The registry key is missing.
/// </summary>
Missing,
/// <summary>
/// Not on Windows.
/// </summary>
NotApplicable
}

/// <summary>
/// Cached value for IsUnixLike (this method is called frequently during evaluation).
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,11 @@ public void MSBuildEngineLogger()
logFileContents.ShouldContain("Current directory = ");
logFileContents.ShouldContain("MSBuild version = ");
logFileContents.ShouldContain("[Hello]");

if (NativeMethodsShared.IsWindows)
{
logFileContents.ShouldContain("Based on the Windows registry key VerifiedAndReputablePolicyState, SAC state = ");
}
}
finally
{
Expand Down
13 changes: 13 additions & 0 deletions src/MSBuild/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,19 @@
<data name="LongPaths_Missing" xml:space="preserve">
<value>not set</value>
</data>
<data name="SAC" xml:space="preserve">
<value>Based on the Windows registry key VerifiedAndReputablePolicyState, SAC state = {0}.</value>
<comment>"Windows" is the OS, SAC is the Smart App Control, "VerifiedAndReputablePolicyState" should not be localized</comment>
</data>
<data name="SAC_Evaluation" xml:space="preserve">
<value>2: in evaluation</value>
</data>
<data name="SAC_Enforcement" xml:space="preserve">
<value>1: in enforcement</value>
</data>
<data name="SAC_Off" xml:space="preserve">
<value>0: turned off</value>
</data>
<!-- **** TerminalLogger strings end **** -->
<!--
The command line message bucket is: MSB1001 - MSB1999
Expand Down
20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading