Skip to content

Commit 1134c39

Browse files
committed
test(prototypes): compare typed ACL bindings with static CRT
1 parent 1964289 commit 1134c39

6 files changed

Lines changed: 271 additions & 9 deletions

File tree

‎.github/workflows/windows-acl-prototypes.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ jobs:
5555
cargo +1.98.1 clippy --workspace --all-features --all-targets --target ${{ matrix.target }} --manifest-path prototypes/windows-acl/Cargo.toml --locked -- -D warnings
5656
- name: Test native ACL core
5757
run: cargo +1.98.1 test -p acl-prototype-core -p acl-prototype-core-windows --target ${{ matrix.target }} --manifest-path prototypes/windows-acl/Cargo.toml --locked
58+
- name: Test typed ACL core with static CRT
59+
run: cargo +1.98.1 test -p acl-prototype-core-windows --release --target ${{ matrix.target }} --manifest-path prototypes/windows-acl/Cargo.toml --locked
60+
env:
61+
RUSTFLAGS: "-C target-feature=+crt-static"
62+
CARGO_PROFILE_RELEASE_OPT_LEVEL: "s"
63+
CARGO_TARGET_DIR: "prototypes/windows-acl/target/static-core-tests"
5864
- name: Resolve Electron 37 executable
5965
run: pnpm dlx electron@37.10.3 -e 'require("node:fs").appendFileSync(process.env.GITHUB_ENV, "ACL_ELECTRON37_PATH=" + process.execPath + "\n")'
6066
env:

‎prototypes/windows-acl/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ the host process. Both passed the same Windows x64/ARM64 tests under Node 22 and
178178
37/42. Neither has been proven operationally superior on end-user machines. The macOS keyring history argues for strict platform gating and
179179
package-level regression tests, not a claim that shipping binaries is risk-free.
180180

181+
## Static CRT follow-up
182+
183+
The user approved accepting a larger package to reduce deployment dependencies.
184+
`projection-s-static` compares typed bindings with the existing dynamic
185+
`projection-s` cell at identical optimization settings. The existing addon
186+
comparison remains intact; the deployment assessment prioritizes the helper.
187+
Static cells fail if the inspected PE import table contains VC/UCRT runtime DLLs.
188+
This checks direct imports, not transitive or dynamically loaded dependencies,
189+
and is not a substitute for running on a clean Windows installation.
190+
191+
Independent typed-core tests check exact file/directory ACEs, real child
192+
inheritance, path rejection, and the owner allow-list. These follow-up changes
193+
require native runner validation; the results above describe the earlier six-cell
194+
experiment, not this seven-cell run.
195+
181196
## Decision gate
182197

183198
For discussion, the typed-binding helper is the strongest measured candidate for

‎prototypes/windows-acl/core-windows/src/lib.rs‎

Lines changed: 172 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,28 +353,191 @@ mod windows {
353353
#[cfg(test)]
354354
mod tests {
355355
use super::*;
356+
use std::ffi::OsString;
356357
use std::fs;
358+
use std::mem::size_of;
359+
use std::os::windows::ffi::OsStringExt;
360+
use std::path::{Path, PathBuf};
361+
use windows::Win32::Security::{
362+
ACCESS_ALLOWED_ACE, CreateWellKnownSid, GetAce, GetSecurityDescriptorControl,
363+
SE_DACL_PROTECTED, WELL_KNOWN_SID_TYPE, WinWorldSid,
364+
};
365+
use windows::Win32::Storage::FileSystem::FILE_ALL_ACCESS;
357366

358-
#[test]
359-
fn secure_path_applies_an_idempotent_protected_dacl() {
360-
let path = std::env::temp_dir().join(format!(
361-
"acl-prototype-windows-{}-{}.conf",
367+
const ACCESS_ALLOWED_ACE_TYPE: u8 = 0;
368+
const OBJECT_INHERIT_ACE: u8 = 0x01;
369+
const CONTAINER_INHERIT_ACE: u8 = 0x02;
370+
const INHERITED_ACE: u8 = 0x10;
371+
372+
fn temporary_path(name: &str) -> PathBuf {
373+
std::env::temp_dir().join(format!(
374+
"acl-prototype-windows-{name}-{}-{}",
362375
std::process::id(),
363376
std::time::SystemTime::now()
364377
.duration_since(std::time::UNIX_EPOCH)
365378
.unwrap()
366379
.as_nanos(),
367-
));
368-
fs::write(&path, "Host acl-prototype\n").unwrap();
380+
))
381+
}
382+
383+
fn well_known_sid(kind: WELL_KNOWN_SID_TYPE) -> Sid {
384+
let mut length = 0;
385+
let _ = unsafe { CreateWellKnownSid(kind, None, None, &mut length) };
386+
assert_ne!(length, 0, "Windows did not report the well-known SID size");
387+
388+
let mut storage = vec![0usize; (length as usize).div_ceil(size_of::<usize>())];
389+
unsafe {
390+
CreateWellKnownSid(
391+
kind,
392+
None,
393+
Some(PSID(storage.as_mut_ptr().cast())),
394+
&mut length,
395+
)
396+
.unwrap();
397+
}
398+
Sid(storage)
399+
}
400+
401+
fn trusted_sids() -> (Sid, Sid, Sid) {
402+
(
403+
current_user_sid().unwrap(),
404+
well_known_sid(WinLocalSystemSid),
405+
well_known_sid(WinBuiltinAdministratorsSid),
406+
)
407+
}
408+
409+
fn assert_acl(path: &Path, expected_sids: [&Sid; 3], expected_flags: u8, protected: bool) {
410+
let file = open(path, READ_CONTROL.0).unwrap();
411+
let descriptor = SecurityDescriptor::get(&file).unwrap();
412+
let mut control = 0;
413+
let mut revision = 0;
414+
unsafe {
415+
GetSecurityDescriptorControl(descriptor.0, &mut control, &mut revision).unwrap();
416+
}
417+
assert_eq!(
418+
control & SE_DACL_PROTECTED.0 != 0,
419+
protected,
420+
"protected DACL state was unexpected for {}",
421+
path.display()
422+
);
423+
424+
let dacl = descriptor.dacl().unwrap();
425+
assert_eq!(dacl.AceCount, 3, "DACL must contain three ACEs");
426+
427+
for (index, expected_sid) in expected_sids.into_iter().enumerate() {
428+
let mut ace = std::ptr::null_mut();
429+
unsafe {
430+
GetAce(dacl, index as u32, &mut ace).unwrap();
431+
let ace = ace.cast::<ACCESS_ALLOWED_ACE>();
432+
assert_eq!(
433+
(*ace).Header.AceType,
434+
ACCESS_ALLOWED_ACE_TYPE,
435+
"ACE {index} must allow access",
436+
);
437+
assert_eq!(
438+
(*ace).Header.AceFlags,
439+
expected_flags,
440+
"ACE {index} inheritance flags were unexpected",
441+
);
442+
assert_eq!(
443+
(*ace).Mask,
444+
FILE_ALL_ACCESS.0,
445+
"ACE {index} must grant full control",
446+
);
447+
let sid = PSID(std::ptr::addr_of!((*ace).SidStart).cast_mut().cast());
448+
assert!(
449+
EqualSid(sid, expected_sid.as_psid()).is_ok(),
450+
"ACE {index} SID did not match",
451+
);
452+
}
453+
}
454+
}
455+
456+
#[test]
457+
fn validate_owner_accepts_trusted_sids_and_rejects_world() {
458+
let (current_user, system, administrators) = trusted_sids();
459+
for owner in [&current_user, &system, &administrators] {
460+
validate_owner(owner.as_psid(), current_user.as_psid()).unwrap();
461+
}
462+
463+
let world = well_known_sid(WinWorldSid);
464+
assert_eq!(
465+
validate_owner(world.as_psid(), current_user.as_psid())
466+
.unwrap_err()
467+
.kind(),
468+
io::ErrorKind::PermissionDenied
469+
);
470+
}
471+
472+
#[test]
473+
fn open_rejects_relative_and_nul_paths() {
474+
assert_eq!(
475+
open(Path::new("relative"), READ_CONTROL.0)
476+
.unwrap_err()
477+
.kind(),
478+
io::ErrorKind::InvalidInput
479+
);
480+
let nul_path = PathBuf::from(OsString::from_wide(&[
481+
b'C' as u16,
482+
b':' as u16,
483+
b'\\' as u16,
484+
0,
485+
b'x' as u16,
486+
]));
487+
assert_eq!(
488+
open(&nul_path, READ_CONTROL.0).unwrap_err().kind(),
489+
io::ErrorKind::InvalidInput
490+
);
491+
}
492+
493+
#[test]
494+
fn secure_path_applies_an_exact_protected_file_dacl_idempotently() {
495+
let (current_user, system, administrators) = trusted_sids();
496+
let path = temporary_path("file");
497+
fs::write(&path, "test").unwrap();
369498

370499
secure_path(&path).unwrap();
371500
let first = inspect_path(&path).unwrap();
372501
secure_path(&path).unwrap();
373-
let second = inspect_path(&path).unwrap();
502+
assert_eq!(
503+
inspect_path(&path).unwrap(),
504+
first,
505+
"file DACL must be idempotent"
506+
);
507+
assert!(
508+
!first.ends_with('\0'),
509+
"SDDL must not retain the API terminator"
510+
);
511+
assert_acl(&path, [&current_user, &system, &administrators], 0, true);
374512

375-
assert!(first.contains("D:P"), "DACL must be protected: {first}");
376-
assert_eq!(second, first, "secure_path must be idempotent");
377513
fs::remove_file(path).unwrap();
378514
}
515+
516+
#[test]
517+
fn secure_path_applies_inheritable_protected_directory_aces() {
518+
let (current_user, system, administrators) = trusted_sids();
519+
let directory = temporary_path("directory");
520+
fs::create_dir(&directory).unwrap();
521+
522+
secure_path(&directory).unwrap();
523+
assert_acl(
524+
&directory,
525+
[&current_user, &system, &administrators],
526+
OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE,
527+
true,
528+
);
529+
530+
let child = directory.join("child.txt");
531+
fs::write(&child, "test").unwrap();
532+
assert_acl(
533+
&child,
534+
[&current_user, &system, &administrators],
535+
INHERITED_ACE,
536+
false,
537+
);
538+
539+
fs::remove_file(child).unwrap();
540+
fs::remove_dir(directory).unwrap();
541+
}
379542
}
380543
}

‎prototypes/windows-acl/experiment.cjs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ const CELLS = [
3838
"acl-prototype-addon/projection",
3939
],
4040
},
41+
{
42+
name: "projection-s-static",
43+
source: "current",
44+
optLevel: "s",
45+
crtStatic: true,
46+
features: [
47+
"acl-prototype-helper/projection",
48+
"acl-prototype-addon/projection",
49+
],
50+
},
4151
{
4252
name: "simplified-z",
4353
source: "current",
@@ -202,6 +212,19 @@ function parsePortableExecutable(buffer) {
202212
return { architecture, imports: imports.sort() };
203213
}
204214

215+
function verifyStaticRuntime(binaries) {
216+
for (const binary of binaries) {
217+
const runtimeImports = binary.pe.imports.filter((name) =>
218+
/^(?:vcruntime|msvcp|msvcr|ucrtbase|api-ms-win-crt-)/i.test(name),
219+
);
220+
if (runtimeImports.length > 0) {
221+
throw new Error(
222+
`Static CRT payload ${binary.name} imports runtime DLLs: ${runtimeImports.join(", ")}`,
223+
);
224+
}
225+
}
226+
}
227+
205228
function copyBuildOutputs(targetDirectory, target, artifactDirectory) {
206229
const releaseDirectory = path.join(targetDirectory, target, "release");
207230
const outputs = [
@@ -381,6 +404,7 @@ function runExperiment(target, options = {}) {
381404
targetDirectory,
382405
binaries: copyBuildOutputs(targetDirectory, target, artifactDirectory),
383406
};
407+
if (cell.crtStatic) verifyStaticRuntime(report.build.binaries);
384408
copyBuildOutputs(
385409
targetDirectory,
386410
target,
@@ -434,4 +458,5 @@ module.exports = {
434458
sourceRootFor,
435459
targetEnvironmentName,
436460
vitestEntrypoint,
461+
verifyStaticRuntime,
437462
};

‎prototypes/windows-acl/experiment.d.cts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export function buildEnvironment(
3131
targetDirectory: string,
3232
): NodeJS.ProcessEnv;
3333
export function parsePortableExecutable(buffer: Buffer): PortableExecutable;
34+
export function verifyStaticRuntime(
35+
binaries: Array<{ name: string; pe: PortableExecutable }>,
36+
): void;
3437
export function reportFilename(arch: "x64" | "arm64"): string;
3538
export function electron37Path(value?: string): string;
3639
export function vitestEntrypoint(): string;

‎prototypes/windows-acl/test/experiment.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
parsePortableExecutable,
1414
targetEnvironmentName,
1515
vitestEntrypoint,
16+
verifyStaticRuntime,
1617
} = experimentModule;
1718
const temporaryDirectories: string[] = [];
1819

@@ -80,6 +81,55 @@ describe("experiment", () => {
8081
});
8182
});
8283

84+
it("compares typed bindings with identical settings except CRT linkage", () => {
85+
const dynamic = CELLS.find((cell) => cell.name === "projection-s");
86+
const staticCell = CELLS.find(
87+
(cell) => cell.name === "projection-s-static",
88+
);
89+
expect(dynamic).toBeDefined();
90+
expect(staticCell).toEqual({
91+
...dynamic,
92+
name: "projection-s-static",
93+
crtStatic: true,
94+
});
95+
});
96+
97+
it.each([
98+
"VCRUNTIME140.dll",
99+
"vcruntime140_1.dll",
100+
"MSVCP140.dll",
101+
"msvcrt.dll",
102+
"ucrtbase.dll",
103+
"api-ms-win-crt-runtime-l1-1-0.dll",
104+
])("rejects a static payload importing %s", (name) => {
105+
expect(() =>
106+
verifyStaticRuntime([
107+
{
108+
name: "acl-helper.exe",
109+
pe: { architecture: "x64", imports: ["KERNEL32.dll", name] },
110+
},
111+
]),
112+
).toThrow(`imports runtime DLLs: ${name}`);
113+
});
114+
115+
it("allows Windows OS imports in both static payloads", () => {
116+
expect(() =>
117+
verifyStaticRuntime([
118+
{
119+
name: "acl-helper.exe",
120+
pe: {
121+
architecture: "x64",
122+
imports: ["KERNEL32.dll", "advapi32.dll"],
123+
},
124+
},
125+
{
126+
name: "acl.node",
127+
pe: { architecture: "arm64", imports: ["ntdll.dll"] },
128+
},
129+
]),
130+
).not.toThrow();
131+
});
132+
83133
it("uses distinct target directories and Cargo profile overrides", () => {
84134
const directory = temporaryDirectory();
85135
const previousRustFlags = process.env.RUSTFLAGS;

0 commit comments

Comments
 (0)