Skip to content
Merged
13 changes: 13 additions & 0 deletions dag/extdeps/sudo/elevation.dag
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ fn sudo_elevate(command: List<String>) -> ElevatedInvocation {
}
}

// -u USER: run the command as USER rather than root (sudo(8): "-u user, --user=user"). A SEPARATE
// row from sudo_elevate for the same reason -E is: it makes a different claim -- the command runs
// under another principal's identity and permissions -- and a caller that means root must not get
// a user by default. Still non-interactive: the same missing-grant refusal applies.
data sudo_run_as_user_flag: NonEmptyStr = "-u"

fn sudo_elevate_as(user: NonEmptyStr, command: List<String>) -> ElevatedInvocation {
ElevatedInvocation {
bin_path: sudo_binary_path as String,
args: list_append(left: [sudo_non_interactive_flag as String, sudo_run_as_user_flag as String, user as String], right: command),
}
}

// THE FLAT ARGV OF AN ELEVATED INVOCATION. ElevatedInvocation splits binary from tail because the
// host-effect surface takes it that way; a shell transport wants one word list, and three callers
// had each written the same [bin_path] ++ args append to get it. One row, so a future change to
Expand Down
8 changes: 7 additions & 1 deletion dag/extdeps/tools/diffutils.dag
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module extdeps.tools.diffutils

import std.types { FilePath }
import std.types { FilePath, NonEmptyStr }
import extdeps.external_authority { ExternalAuthority }
import extdeps.uri { Uri, Https }

Expand Down Expand Up @@ -29,3 +29,9 @@ service shell.Diff {
}
}
}

// cmp(1), also shipped in diffutils. -s prints nothing and answers by exit status: 0 identical,
// 1 different, 2 trouble. A readback consumes the status as the readback's own status, so any
// nonzero result fails the step.
data diffutils_cmp_program: NonEmptyStr = "cmp"
data diffutils_cmp_silent_flag: NonEmptyStr = "-s"
363 changes: 363 additions & 0 deletions dag/gunbc/auth/approval_ntfy_runtime_observe.dag

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions dag/gunbc/executor_privileged_apply.dag
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import std.types { String, NonEmptyStr, Bool, List }
import std.process { ProcessExit, ExitSuccess, exit_failure }
import std.disposition { Disposition, SingleAuthority }
import extdeps.exec.command { ArgvCommand, argv_command }
import extdeps.sudo.elevation { sudo_elevate, ElevatedInvocation }
import gunbc.executor_privileged_operation {
ExecutorPrivilegedOperation, executor_privileged_operation_argv,
ExecutorPrivilegedOperation, executor_privileged_operation_elevated,
}
import gunbc.command_runner { run_shell_commands, LocalExec }

Expand Down Expand Up @@ -43,7 +42,7 @@ data executor_privileged_apply_disposition: Disposition = SingleAuthority
// environment ACROSS the privilege boundary, a security claim this applier has no reason to make
// when every argv it runs is already absolute; and it deliberately omits -n, so it can prompt.
fn executor_privileged_operation_command(op: ExecutorPrivilegedOperation) -> ArgvCommand {
let elevated = sudo_elevate(command: map(executor_privileged_operation_argv(op: op), w => w as String))
let elevated = executor_privileged_operation_elevated(op: op)
argv_command(program: elevated.bin_path as NonEmptyStr, arguments: elevated.args)
}

Expand Down
105 changes: 98 additions & 7 deletions dag/gunbc/executor_privileged_operation.dag
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module gunbc.executor_privileged_operation
import std.types { String, NonEmptyStr, List }
import std.disposition { Disposition, Scaffold, SingleAuthority, RealizationDispatch }
import std.decl_ref { DeclarationRef, WholeDeclaration }
import extdeps.sudo.elevation { sudo_elevate_argv }
import extdeps.sudo.elevation { ElevatedInvocation, sudo_elevate, sudo_elevate_as, sudo_elevate_argv }
import gunbc.cli_invoke { gunbc_run_invocation_words_with_functions, invocation_words_bare }
import extdeps.posix.shell_command_language { posix_single_quote }
import v2.std.algebra { fold_list }
import extdeps.github.actions_runner { actions_runner_base_dir, actions_runner_cache_dir }
Expand Down Expand Up @@ -80,6 +81,50 @@ type ExecutorPrivilegedOperation
| ReadConntrackExpectations
| ReadLegacyXtablesRules
| ReadUserspaceConntrackHelpers
| RunGunbcEntryAs {
run_as: NonEmptyStr
binary: NonEmptyStr
source_roots: List<NonEmptyStr>
entry: NonEmptyStr
function: NonEmptyStr
}

// THE PRINCIPAL AN OPERATION RUNS AS IS A FACT OF THE OPERATION, not of the renderer. Every
// operation above needs root. RunGunbcEntryAs is the one that must NOT run as root: it runs a released
// gunbc entry as the service account whose process it reads (the approval broker's ntfy runtime
// observation reads /proc/<MainPID> of gunbc-ntfy's own process, which that uid can read and the
// operator's cannot). Before this, executor_sudoers_run_as was a constant, and the elevation always
// had -n and no -u. Both the sudoers `(run_as)` field and the `sudo -u` word are now read from this
// one match. So a grant cannot name one principal while the command elevates to another.
type OperationPrincipal
= RunsAsRoot
| RunsAsAccount { login: NonEmptyStr }

// A gunbc ENTRY RUN IS ALSO AN OPERATION, and its argv is gunbc.cli_invoke's one `gunbc run` shape.
// It is not re-spelled here. The words are exact and argument-free: no --arg, so the sudoers
// match (which is this argv) leaves no value open for the grantee to choose. The binary and every
// path are absolute and name one release directory, so the grant authorizes one build over one
// tree, and a sudoers wildcard is never needed to name it.
fn gunbc_entry_run_argv(
binary: NonEmptyStr,
source_roots: List<NonEmptyStr>,
entry: NonEmptyStr,
function: NonEmptyStr,
) -> List<NonEmptyStr> {
concat(
[binary],
map(
invocation_words_bare(words: gunbc_run_invocation_words_with_functions(
source_roots: map(source_roots, r => r as String),
entry: entry as String,
functions: [function as String],
claim_run: false,
arguments: [],
)),
w => w as NonEmptyStr,
),
)
}

// THE ONE PROJECTION. Everything else in this module derives from this function, so there is
// exactly one place where an operation becomes words.
Expand Down Expand Up @@ -161,9 +206,56 @@ fn executor_privileged_operation_argv(op: ExecutorPrivilegedOperation) -> List<N
u,
a
]
RunGunbcEntryAs { run_as: _, binary: b, source_roots: rs, entry: e, function: f } =>
gunbc_entry_run_argv(binary: b, source_roots: rs, entry: e, function: f)
}
}

// EXHAUSTIVE, NO WILDCARD: a new operation must say whom it runs as instead of inheriting root.
fn executor_privileged_operation_principal(op: ExecutorPrivilegedOperation) -> OperationPrincipal {
match op {
RunGunbcEntryAs { run_as: u, binary: _, source_roots: _, entry: _, function: _ } => RunsAsAccount { login: u }
EnsureOwnedDirectory { path: _, owner: _, group: _, mode: _ } => RunsAsRoot
RemoveDirectoryTree { path: _ } => RunsAsRoot
SystemdDaemonReload => RunsAsRoot
SystemdEnableUnit { unit: _ } => RunsAsRoot
SystemdStartUnit { unit: _ } => RunsAsRoot
SystemdSetUnitProperty { unit: _, assignment: _ } => RunsAsRoot
EnsureSystemAccount { login: _, shell: _ } => RunsAsRoot
EnsureSupplementaryGroupMembership { login: _, group: _ } => RunsAsRoot
InstallFile { source: _, destination: _, owner: _, group: _, mode: _ } => RunsAsRoot
SystemdReloadUnit { unit: _ } => RunsAsRoot
SystemdRestartUnit { unit: _ } => RunsAsRoot
ReadNftTable { family: _, table: _ } => RunsAsRoot
ReadNftRuleset => RunsAsRoot
ReadConntrackExpectations => RunsAsRoot
ReadLegacyXtablesRules => RunsAsRoot
ReadUserspaceConntrackHelpers => RunsAsRoot
}
}

fn operation_principal_sudoers_run_as(p: OperationPrincipal) -> NonEmptyStr {
match p {
RunsAsRoot => executor_sudoers_run_as
RunsAsAccount { login: l } => l
}
}

// THE ELEVATED INVOCATION, from the same principal the sudoers line reads. The root arm is
// sudo_elevate unchanged (-n, no -u), so no emitted byte for an existing operation moves.
fn executor_privileged_operation_elevated(op: ExecutorPrivilegedOperation) -> ElevatedInvocation {
let argv = map(executor_privileged_operation_argv(op: op), w => w as String)
match executor_privileged_operation_principal(op: op) {
RunsAsRoot => sudo_elevate(command: argv)
RunsAsAccount { login: l } => sudo_elevate_as(user: l, command: argv)
}
}

fn executor_privileged_operation_elevated_argv(op: ExecutorPrivilegedOperation) -> List<String> {
let e = executor_privileged_operation_elevated(op: op)
concat([e.bin_path], e.args)
}

// WHY SystemdSetUnitProperty IS ADMISSIBLE WHERE A PRIVILEGED FILE-INSTALL WAS VETOED (parent
// ruling, warm-seal-35, 2026-08-29). A privileged operation must not copy bytes an unprivileged
// principal can author: a granted `install <staged> /etc/systemd/system/<unit>` has a constant argv
Expand Down Expand Up @@ -195,7 +287,8 @@ fn executor_privileged_operation_sudoers_command(op: ExecutorPrivilegedOperation
// RUN-AS IS root, NOT ALL. The executor previously rendered ALL=(ALL), which authorizes the
// grantee to run the command as ANY user including other service accounts; Spark already rendered
// ALL=(root). These were two different grants wearing one vocabulary. root is the narrower of the
// two and is what every operation here actually needs.
// two, and it is what every root operation here needs. An operation that runs as another account
// names that account through executor_privileged_operation_principal. It is never ALL.
data executor_sudoers_run_as: NonEmptyStr = "root" as NonEmptyStr

fn executor_privileged_operation_sudoers_line(
Expand All @@ -205,7 +298,7 @@ fn executor_privileged_operation_sudoers_line(
join([
grantee as String,
" ALL=(",
executor_sudoers_run_as as String,
operation_principal_sudoers_run_as(p: executor_privileged_operation_principal(op: op)) as String,
") NOPASSWD: ",
executor_privileged_operation_sudoers_command(op: op) as String
], "") as NonEmptyStr
Expand Down Expand Up @@ -342,6 +435,7 @@ fn executor_privileged_operation_precondition(op: ExecutorPrivilegedOperation) -
ReadConntrackExpectations => NoPrecondition
ReadLegacyXtablesRules => NoPrecondition
ReadUserspaceConntrackHelpers => NoPrecondition
RunGunbcEntryAs { run_as: _, binary: _, source_roots: _, entry: _, function: _ } => NoPrecondition
}
}

Expand Down Expand Up @@ -392,10 +486,7 @@ fn executor_privileged_operation_converge_command(op: ExecutorPrivilegedOperatio
fn executor_privileged_operation_shell_command(op: ExecutorPrivilegedOperation) -> String {
join(
map(
sudo_elevate_argv(command: map(
executor_privileged_operation_argv(op: op),
w => w as String,
)),
executor_privileged_operation_elevated_argv(op: op),
w => posix_single_quote(spelling: w),
),
" ",
Expand Down
Loading
Loading