PolkitHelper class

Namespace Runic.Platform.Linux.Polkit in Runic.Platform.Linux.Polkit 0.23.0

public static class PolkitHelper

The privileged side of an action: the program pkexec runs as root.

Remarks

Any local user can run the program through pkexec with any arguments, so the program checks them itself, with PolkitHelper.Match and its own rules. pkexec clears the environment (PATH is a fixed list), but the caller chooses the working directory (pkexec --keep-cwd), the umask, ignored and blocked signals and resource limits. PolkitHelper.Initialize, which PolkitHelper.Match calls, resets them. Call tools by absolute path and read no configuration from the caller's environment or working directory.

When the application ends while the program runs, nothing signals the program: pkexec's parent-death signal is cleared when it switches to root. Standard input reaches its end instead (see PolkitHelper.WatchCancellation), and nothing reads standard output and error any more: .NET's console drops writes silently, while the tools the program starts get write errors (EPIPE) or, when they handle SIGPIPE by default, are killed. A step that must finish should not depend on either: redirect the output of the tools it starts, or run them in their own systemd unit, as nixos-rebuild runs switch-to-configuration.

Methods

GetCallerUserId

public static int? GetCallerUserId()

The user ID of the user who started this process through pkexec, from PKEXEC_UID, when the process runs as root; null otherwise.

Initialize

public static bool Initialize()

Checks that this process runs as root for a pkexec caller and resets the process state pkexec inherits from the caller. Call it first in Main, before starting threads or processes; PolkitHelper.Match calls it.

Returns

False when the effective user is not root (as when pkexec ran with --user and a user other than root), PKEXEC_UID is missing, the process is 32-bit, or any reset fails; do not act then. Some state may already be reset when a later step fails.

Remarks

  • The working directory becomes /, the umask 022 and the nice value 0.
  • Signals the caller ignored are handled by default again, except SIGPIPE, which .NET ignores itself. The signal mask is per thread: the calling thread's becomes empty, and threads and processes it starts later inherit that, but threads the runtime started earlier keep the caller's.
  • Core dumps are off (RLIMIT_CORE 0); the CPU time, address space, data and file size limits are lifted (RLIMIT_CPU, RLIMIT_AS, RLIMIT_DATA, RLIMIT_FSIZE); at least 1024 open files are allowed (RLIMIT_NOFILE); and oom_score_adj is 0.

The scheduling policy, CPU affinity and RLIMIT_STACK stay as the caller set them.

Match

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

Initializes the process (see PolkitHelper.Initialize) and finds the action that arguments, this process's arguments without the program name, select: its verb is the first argument (or it is the only action and has none), and the remaining arguments are within it.

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; reject the invocation when it is null. It is also null when PolkitHelper.Initialize fails.

Remarks

The verb alone decides. pkexec authorized the action by the program's real path and the verb, but one executable can be installed under several paths, such as behind the wrapper script buildDotnetModule installs, and this process cannot see which one pkexec matched. Declarations therefore give every action its own verb (RUNICPA003).

WatchCancellation

public static CancellationToken WatchCancellation()

A token that is cancelled when the application asks the action to stop, or dies: the same contract as ElevatedHelper.WatchCancellation on Windows.

Remarks

The application closes the program's standard input to ask it to stop, because it cannot signal a process that runs as root, so the token is cancelled when standard input reaches its end. The first call starts a background thread that reads and discards standard input.