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

Detect if the current process is elevated in Dashboard #3833

Merged
merged 1 commit into from
Sep 13, 2024
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
33 changes: 33 additions & 0 deletions common/Helpers/RuntimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// Licensed under the MIT License.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Principal;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Security;
using static DevHome.Common.Helpers.RuntimeHelper;

namespace DevHome.Common.Helpers;

Expand Down Expand Up @@ -35,6 +39,35 @@ public static bool IsCurrentProcessRunningAsAdmin()
return identity.Owner?.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) ?? false;
}

// Determine whether the current process is running elevated in a split token session
// will not return true if UAC is disabled and the user is running as administrator by default
public static unsafe bool IsCurrentProcessRunningElevated()
{
HANDLE tokenHandle;
if (!PInvoke.OpenProcessToken(PInvoke.GetCurrentProcess(), TOKEN_ACCESS_MASK.TOKEN_QUERY, &tokenHandle))
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}

try
{
TOKEN_ELEVATION_TYPE elevationType;
uint elevationTypeSize = (uint)Unsafe.SizeOf<TOKEN_ELEVATION_TYPE>();
uint returnLength;

if (!PInvoke.GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, &elevationType, elevationTypeSize, &returnLength))
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
jaholme marked this conversation as resolved.
Show resolved Hide resolved
}

return elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
}
finally
{
PInvoke.CloseHandle(tokenHandle);
}
}

public static void VerifyCurrentProcessRunningAsAdmin()
{
if (!IsCurrentProcessRunningAsAdmin())
Expand Down
6 changes: 5 additions & 1 deletion common/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ GetWindowLong
WINDOW_EX_STYLE
SHLoadIndirectString
StrFormatByteSizeEx
OpenProcessToken
GetTokenInformation
GetCurrentProcess
SFBS_FLAGS
MAX_PATH
GetDpiForWindow
GetWindowRect
GetMonitorInfo
SetWindowPos
MonitorFromWindow
E_INVALIDARG
E_INVALIDARG
TOKEN_ELEVATION_TYPE
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public Visibility GetNoWidgetMessageVisibility(int widgetCount, bool isLoading)
return (widgetCount == 0 && !isLoading && HasWidgetServiceInitialized) ? Visibility.Visible : Visibility.Collapsed;
}

public bool IsRunningAsAdmin()
public bool IsRunningElevated()
{
return RuntimeHelper.IsCurrentProcessRunningAsAdmin();
return RuntimeHelper.IsCurrentProcessRunningElevated();
}
}
4 changes: 2 additions & 2 deletions tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private void UnsubscribeFromWidgets()

private async Task<bool> ValidateDashboardState()
{
// Ensure we're not running as admin. Display an error and don't allow using the Dashboard if we are.
if (ViewModel.IsRunningAsAdmin())
// Ensure we're not running elevated. Display an error and don't allow using the Dashboard if we are.
if (ViewModel.IsRunningElevated())
jaholme marked this conversation as resolved.
Show resolved Hide resolved
{
_log.Error($"Dev Home is running as admin, can't show Dashboard");
RunningAsAdminMessageStackPanel.Visibility = Visibility.Visible;
Expand Down
Loading