Skip to content

Commit 866bab3

Browse files
shanselmanCopilot
andcommitted
fix: pin execution transport on every approved exec path
Follow-up to ce0a3cb from the same adversarial review, which found two more paths that still executed an image other than the one the operator approved. 1. The pre-approved branch chose the reusable transport only for an allowlist hit, so a security=full/ask=off run reached process launch with the carrier unpinned: argv[0] resolved through PATH and a bare payload token stayed bare for cmd to resolve again at launch. Full policy already permits arbitrary execution, so this is not an authorization escalation, but it left the "every approved run is pinned" invariant untrue and the two allow branches inconsistent. 2. A direct command is resolved twice, once by the normalizer for the execution identity and once by the binder for the identity that is displayed and stored. Execution used the normalizer's, so the prompt could name one image while a separate lookup of the same name selected another. Both collapse into one rule, so transport selection now lives in a single helper used by both decision points: whenever the binder produced a reusable command, that command's argv is what runs. This is safe for direct commands because BindDirect already applies the same env-wrapper unwrapping and modifier rejection as BuildApprovedExecution and adds strictly more gates, so only the source of the resolved executable changes. Durability is unchanged and still gated separately, so no allow persists anything it did not persist before. PreApprovedFullPolicy_CanonicalCarrier_ExecutesThePinnedCarrier fails without this change with the payload token still bare. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1223ff2c-11b6-48d9-937b-a56408e99b93
1 parent ce0a3cb commit 866bab3

3 files changed

Lines changed: 110 additions & 32 deletions

File tree

‎docs/WINDOWS_NODE_ARCHITECTURE.md‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,20 @@ text is untouched. The tail's arity is preserved as well, so a pre-joined tail s
621621
one element and a tokenized tail keeps its elements and no new process-creation
622622
quoting is introduced.
623623

624-
Both pins apply to **every** approved run of a recognized canonical carrier,
625-
including a one-time Allow Once, not only to durable approvals. Approval identity
626-
and durability are separate from execution transport: the prompt names the inner
627-
executable the binder resolved through a trusted system `cmd.exe`, so executing the
628-
request's own argv instead would reopen both launch-time lookups after the operator
629-
had already decided, and a `cmd.exe` planted earlier on PATH than the system
630-
directory would run in place of the image that was shown. Pinning a one-time allow
631-
persists nothing; durability is still gated separately on an Allow Always decision
632-
with a bound reusable command.
624+
Both pins apply to **every** approved run of a recognized canonical carrier: a durable
625+
Allow Always, a one-time Allow Once, an allowlist hit that never prompts, and a
626+
pre-approved `security=full` run. Approval identity and durability are separate from
627+
execution transport. The prompt names the inner executable the binder resolved through
628+
a trusted system `cmd.exe`, so executing the request's own argv instead would reopen
629+
both launch-time lookups after the decision had been made, and a `cmd.exe` planted
630+
earlier on PATH than the system directory would run in place of the image that was
631+
shown. Choosing this transport persists nothing; durability is gated separately on an
632+
Allow Always decision with a bound reusable command.
633+
634+
The same rule covers a directly invoked executable, which is resolved twice: once by
635+
the normalizer for the execution identity and once by the binder for the identity that
636+
is displayed and stored. Execution uses the binder's resolution, so the two lookups can
637+
never disagree about which image the operator approved.
633638

634639
Pinning is refused rather than approximated. The pinned path must be writable into
635640
the payload as a single token that `cmd` reads back byte for byte: no whitespace, no

‎src/OpenClaw.Shared/ExecApprovals/ExecApprovalsCoordinator.cs‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public async Task<ExecApprovalV2Result> HandleAsync(NodeInvokeRequest request, s
117117
{
118118
// Pre-approved path (security=Full, ask=Off or allowlist satisfied): skip prompt.
119119
// Fail closed if the approved executable cannot be pinned to a resolved path.
120-
var reusableAllow = context.Security == ExecSecurity.Allowlist
120+
var requiresReusableAllow = context.Security == ExecSecurity.Allowlist
121121
&& context.AllowlistSatisfied;
122-
var preApprovedExecution = reusableAllow
122+
var preApprovedExecution = UseReusableExecution(identity, requiresReusableAllow)
123123
? BuildReusableApprovedExecution(
124124
identity.ReusableCommand,
125125
identity,
@@ -279,9 +279,7 @@ public async Task<ExecApprovalV2Result> HandleAsync(NodeInvokeRequest request, s
279279
// re-resolve against PATH or cwd. Neither is the image that was shown and approved.
280280
// This is a transport choice only. Durability is still governed by
281281
// requiresReusableExecution, so a one-time allow persists nothing.
282-
var useReusableExecution =
283-
requiresReusableExecution
284-
|| identity.ReusableCommand?.IsCarrierTransport == true;
282+
var useReusableExecution = UseReusableExecution(identity, requiresReusableExecution);
285283

286284
var execution = useReusableExecution
287285
? BuildReusableApprovedExecution(
@@ -374,6 +372,24 @@ public ValueTask<ExecApprovalRevalidationResult> RevalidateAsync(
374372
}
375373
}
376374

375+
// Chooses the execution transport, independently of durability and of which policy
376+
// branch allowed the command. Whenever the binder produced a reusable command, that
377+
// command's argv is the identity the operator was shown and the identity durable
378+
// policy describes, so it is also what has to run. Anything else lets a second
379+
// resolver pick the image after the decision was made: an unpinned carrier re-resolves
380+
// both cmd.exe and its payload at launch, and a direct command would execute the
381+
// normalizer's earlier resolution rather than the binder's, which is a separate lookup
382+
// of the same name and can disagree with it. The two builders agree on shape for a
383+
// direct command (BindDirect applies the same env-wrapper unwrapping and rejects
384+
// wrappers with modifiers), so this only ever changes which resolution supplies the
385+
// executable.
386+
//
387+
// `required` stays the durability gate: an allowlist-satisfied or match-dependent
388+
// allow must have a reusable command or fail closed. Choosing this transport never
389+
// persists anything on its own.
390+
private static bool UseReusableExecution(CanonicalCommandIdentity identity, bool required)
391+
=> required || identity.ReusableCommand is not null;
392+
377393
// Builds the approved execution payload from the RESOLVED executable path, never
378394
// the raw argv[0]. The command must execute with the same canonical identity it
379395
// was evaluated under: a relative argv[0] in the payload would let Windows

‎tests/OpenClaw.Shared.Tests/ExecApprovalsCoordinatorTests.cs‎

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ private static NodeInvokeRequest DefaultReq()
7474
private void WriteStoreFile(string json)
7575
=> File.WriteAllText(Path.Combine(_dir, "exec-approvals.json"), json);
7676

77+
// Coordinator requests cannot carry a custom env (the validator returns
78+
// custom-env-not-supported), so a bare payload name resolves against the process
79+
// PATH. Pick the first candidate this host can actually pin: where a toolchain such
80+
// as coreutils shadows a system name, the resolved path contains a space and the
81+
// binder correctly refuses it, which is a different claim than the ones under test.
82+
private static (ExecReusableCommand? Bound, string Payload) FindBindablePayload()
83+
{
84+
foreach (var candidate in new[] { "where.exe", "whoami.exe", "hostname.exe" })
85+
{
86+
var bound = ExecReusableCommandBinder.TryBind(
87+
["cmd.exe", "/d", "/s", "/c", candidate], cwd: null, env: null);
88+
if (bound is not null && bound.IsCarrierTransport)
89+
return (bound, candidate);
90+
}
91+
92+
return (null, "");
93+
}
94+
7795
private string LegacyPolicyPath => Path.Combine(_dir, "exec-policy.json");
7896

7997
private ExecApprovalsCoordinator MakeCoordinator(
@@ -1223,6 +1241,62 @@ public async Task AllowAlways_CanonicalHostname_PersistsInnerExecutableAndPreser
12231241
Assert.Equal("^\0\0$", entry.ArgPattern);
12241242
}
12251243

1244+
// Transport is chosen independently of which policy branch allowed the command. A
1245+
// pre-approved security=full/ask=off run reaches process launch without a prompt, so
1246+
// an unpinned carrier there would re-resolve both cmd.exe and its payload at launch
1247+
// exactly as an unpinned one-time allow would.
1248+
[Fact]
1249+
public async Task PreApprovedFullPolicy_CanonicalCarrier_ExecutesThePinnedCarrier()
1250+
{
1251+
if (!OperatingSystem.IsWindows()) return;
1252+
1253+
var (bound, payload) = FindBindablePayload();
1254+
Assert.NotNull(bound);
1255+
1256+
WriteStoreFile("""{"version":1,"defaults":{"security":"full","ask":"off"}}""");
1257+
1258+
var result = await MakeCoordinator()
1259+
.HandleAsync(
1260+
Req($$"""{"command":["cmd.exe","/d","/s","/c","{{payload}}"]}"""),
1261+
"full-carrier-preapproved");
1262+
1263+
Assert.True(result.IsAllow);
1264+
Assert.Equal(SystemCmdPath, result.Execution!.Argv[0], StringComparer.OrdinalIgnoreCase);
1265+
Assert.NotEqual(payload, result.Execution.Argv[4]);
1266+
Assert.True(Path.IsPathFullyQualified(result.Execution.Argv[4]));
1267+
Assert.Equal(bound!.ExecutionArgv.ToArray(), result.Execution.Argv.ToArray());
1268+
}
1269+
1270+
// A direct command is resolved twice: once by the normalizer for the execution
1271+
// identity and once by the binder for the identity that is displayed and stored.
1272+
// Execution must use the binder's, so the image the operator approved is the image
1273+
// that runs even if the two lookups of the same name could disagree.
1274+
[Fact]
1275+
public async Task AllowOnce_DirectCommand_ExecutesTheBoundResolution()
1276+
{
1277+
if (!OperatingSystem.IsWindows()) return;
1278+
1279+
var bound = ExecReusableCommandBinder.TryBind(
1280+
[SystemHostnamePath], cwd: null, env: null);
1281+
Assert.NotNull(bound);
1282+
Assert.False(bound!.IsCarrierTransport);
1283+
1284+
WriteStoreFile(
1285+
"""{"version":1,"defaults":{"security":"allowlist","ask":"on-miss"}}""");
1286+
1287+
var result = await MakeCoordinator(
1288+
canPresent: AlwaysCanPresentEvaluator.Instance,
1289+
prompt: new FixedDecisionPromptHandler(ExecApprovalPromptOutcome.AllowOnce))
1290+
.HandleAsync(
1291+
Req($$"""{"command":["{{SystemHostnameJson}}"]}"""),
1292+
"direct-once");
1293+
1294+
Assert.True(result.IsAllow);
1295+
Assert.Equal(bound.Argv.ToArray(), result.Execution!.Argv.ToArray());
1296+
Assert.Equal(bound.Pattern, result.Execution.Argv[0], StringComparer.OrdinalIgnoreCase);
1297+
Assert.Empty(new ExecApprovalsStore(_dir, NullLogger.Instance).ResolveReadOnly("main").Allowlist);
1298+
}
1299+
12261300
// The carrier form survives a one-time allow, but both launch-time lookups are pinned:
12271301
// argv[0] is the resolved system cmd.exe and the payload token is fully qualified. The
12281302
// operator was shown the inner executable the binder resolved, so the request's own argv
@@ -1232,24 +1306,7 @@ public async Task AllowOnce_CanonicalHostname_ExecutesThePinnedCarrier()
12321306
{
12331307
if (!OperatingSystem.IsWindows()) return;
12341308

1235-
// Same binder the coordinator uses. Coordinator requests cannot carry a custom env,
1236-
// so the bare payload resolves against the process PATH: pick the first candidate
1237-
// this host can actually pin. On a machine where coreutils shadows a system name the
1238-
// resolved path contains a space and the binder correctly refuses it, which is a
1239-
// different claim than the one under test.
1240-
ExecReusableCommand? bound = null;
1241-
var payload = "";
1242-
foreach (var candidate in new[] { "where.exe", "whoami.exe", "hostname.exe" })
1243-
{
1244-
bound = ExecReusableCommandBinder.TryBind(
1245-
["cmd.exe", "/d", "/s", "/c", candidate], cwd: null, env: null);
1246-
if (bound is not null && bound.IsCarrierTransport)
1247-
{
1248-
payload = candidate;
1249-
break;
1250-
}
1251-
bound = null;
1252-
}
1309+
var (bound, payload) = FindBindablePayload();
12531310
Assert.NotNull(bound);
12541311

12551312
WriteStoreFile(

0 commit comments

Comments
 (0)