ElevatedHelper class

Namespace Runic.Platform.Windows.Elevation in Runic.Platform.Windows.Elevation 0.23.0

public static class ElevatedHelper

The elevated side of an action: the helper that runas started.

Remarks

The helper is the security boundary. Any user can start it with runas and any arguments, so it checks them itself, with ElevatedHelper.Match and its own rules. It must be a NativeAOT build: an elevated process takes its environment from the user's profile (HKCU\Environment), which every process of the user can write, and a JIT runtime would load DOTNET_STARTUP_HOOKS, DOTNET_ROOT or a CORECLR_PROFILER from it. ElevatedHelper.Initialize, which ElevatedHelper.Match calls, replaces that environment and the working directory.

ElevatedHelper.Match connects to the application's pipe, reports the outcome and, for an accepted action, sends this process's standard output and error to the application; tools the helper starts without redirecting their output inherit them. When the application ends or cancels, ElevatedHelper.WatchCancellation is cancelled and later output is dropped.

Methods

GetCallerImagePath

public static string? GetCallerImagePath()

The image path of the application that started this helper, after ElevatedHelper.Match accepted an action; null when it could not be read. A helper may compare it with the expected application, but any process of the user can start the helper, so it is not proof of who asked.

GetCallerProcessId

public static int? GetCallerProcessId()

The process ID of the application that started this helper, after ElevatedHelper.Match accepted an action.

Initialize

public static bool Initialize()

Resets the process state the elevation takes from the user. Call it first in Main; ElevatedHelper.Match calls it.

Returns

False off Windows or when any step fails; do not act then.

Remarks

  • DLLs load only from the helper's directory and System32 (SetDefaultDllDirectories), never from the working directory or PATH.
  • The working directory becomes System32.
  • The environment is replaced: SystemRoot, windir, SystemDrive, Path (System32, the Windows directory and Wbem only), PATHEXT, OS, NUMBER_OF_PROCESSORS, PROCESSOR_ARCHITECTURE and the Program Files and ProgramData variables, from the system rather than the user; every other variable is removed, including ComSpec: start tools by absolute path.
  • TEMP and TMP point to a new directory under the Windows directory that only administrators and SYSTEM can open, removed when the process exits.

The NativeAOT runtime reads its own settings from the environment before Main. A helper built with RunicWindowsElevatedHelper has no standalone GC (DOTNET_GCPath, DOTNET_GCName), no EventPipe or diagnostic server (DOTNET_DiagnosticPorts, DOTNET_EventPipeOutputPath) and invariant globalization compiled in, so ICU (DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU) is never loaded; what is left (server GC, conservative stack scanning, the stress log and heap settings) changes performance but loads no code and writes no files.

Match

public static PrivilegedActionDeclaration? Match(IEnumerable<PrivilegedActionDeclaration> actions, IReadOnlyList<string> arguments, out ImmutableArray<string> actionArguments)

Initializes the process (see ElevatedHelper.Initialize), connects to the application that started this helper and finds the action that arguments, this process's arguments without the program name, select. For an accepted action it starts sending standard output and error to the application.

Parameters

actions

The declared actions, usually the generated PrivilegedActions.All.

arguments

The arguments passed to this process.

actionArguments

The arguments after the verb, when an action matched.

Returns

The matching action, or null; exit without acting when it is null. Only the first call can match.

Remarks

The arguments are those the provider passes: a marker, the pipe name, then the action's verb and arguments. The verb alone selects the action (RUNICPA003), and the remaining arguments must be within it.

Before it reports anything, the helper checks that the pipe's server is the process that started it (its parent, which UAC sets to the requesting process) in the same session. It reports PlatformUnavailableReason.BackendUnavailable to the application when it is not elevated (UAC is off), and refuses on a JIT runtime or when its own image, or a directory above it, can be changed by non-administrators. The application cannot be authenticated beyond that: any process of the user can start the helper and serve the pipe, but the UAC prompt names the helper and its arguments, and the helper accepts only declared actions.

WatchCancellation

public static CancellationToken WatchCancellation()

A token that is cancelled when the application asks the action to stop, closes the pipe or ends. A running action should stop soon after and exit with a code that says so.

Exceptions

InvalidOperationException

ElevatedHelper.Match did not return an action.