mxc icon indicating copy to clipboard operation
mxc copied to clipboard

ProcessContainer leaks a SeLs lowbox-number entry per environment

Open bluecmd opened this issue 1 month ago • 3 comments

Relevant area(s): Windows

Brief description

Mostly AI-written; I've verified the findings.

Each ProcessContainer (PSEC environment) leaks one SEP_LOWBOX_NUMBER_ENTRY (kernel paged-pool tag SeLs, ~112 B), freed only by reboot. A long-lived host that creates ProcessContainers accumulates them; near ~64K leaked entries, CreateProcessSecurityEnvironment fails with 0x800705AA (CreateProcessW error 1450) until reboot. Microsoft's own fix, gated by feature flag Feature_LowBoxNumberEntryReferenceIssue, is present but disabled on this build. Windows 11 26H2 26300.9550, ntoskrnl 10.0.26100.9549.

Steps to reproduce

Repro using the MXC SDK (Rust) — a crate in the src/ workspace with mxc-sdk = { path = "../mxc-sdk" }. It creates N ProcessContainers and reads outstanding SeLs via NtQuerySystemInformation(SystemPoolTagInformation) before and after:

use std::ffi::c_void;

use mxc_sdk::v1::{self, ContainerRequest, Containment};

#[repr(C)]
struct SystemPooltag {
    tag: [u8; 4],
    paged_allocs: u32,
    paged_frees: u32,
    paged_used: usize,
    nonpaged_allocs: u32,
    nonpaged_frees: u32,
    nonpaged_used: usize,
}

#[link(name = "ntdll")]
extern "system" {
    fn NtQuerySystemInformation(class: u32, buf: *mut c_void, len: u32, ret: *mut u32) -> i32;
}

fn outstanding(tag: &[u8; 4]) -> i64 {
    const CLASS: u32 = 22; // SystemPoolTagInformation
    const MISMATCH: i32 = 0xC000_0004u32 as i32;
    let mut cap: usize = 1 << 20;
    let buf = loop {
        let mut b = vec![0u8; cap];
        let mut ret = 0u32;
        let st = unsafe {
            NtQuerySystemInformation(CLASS, b.as_mut_ptr() as *mut c_void, cap as u32, &mut ret)
        };
        if st >= 0 {
            break b;
        }
        if st == MISMATCH && cap < (64 << 20) {
            cap *= 2;
            continue;
        }
        return -1;
    };
    let count = unsafe { *(buf.as_ptr() as *const u32) } as usize;
    (0..count)
        .map(|i| unsafe {
            &*(buf.as_ptr().add(8 + i * std::mem::size_of::<SystemPooltag>()) as *const SystemPooltag)
        })
        .filter(|e| &e.tag == tag)
        .map(|e| (e.paged_allocs as i64 - e.paged_frees as i64)
            + (e.nonpaged_allocs as i64 - e.nonpaged_frees as i64))
        .sum()
}

fn main() {
    let n: u32 = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(20);
    println!("before: SeLs={}", outstanding(b"SeLs"));
    for _ in 0..n {
        let request = ContainerRequest {
            containment: Containment::Process,
            timeout_ms: Some(30_000),
            ..ContainerRequest::new("cmd.exe /d /s /c \"exit /b 0\"")
        };
        let _ = v1::run(request, Default::default()); // env created before the workload runs
    }
    println!("after {n} envs: SeLs={}", outstanding(b"SeLs"));
}

Example run, Windows 11 26H2 26300.9550 (leakrepro.exe 20, run twice back to back):

before: SeLs=392
after 20 envs: SeLs=412
before: SeLs=412
after 20 envs: SeLs=432

The second run's before equals the first run's after: process exit releases nothing, and each run adds +20. Restarting the AppInfo service does not release them either; only a reboot does.

Expected behavior

Closing the environment and deleting its AppContainer token frees the lowbox-number entry allocated for that environment.

Actual behavior

The entry survives until reboot (112 B each — a separate 20-cycle harness run showed 37088 → 39328 B, +2240).

The SeLs object is a SEP_LOWBOX_NUMBER_ENTRY (per-session AppContainer lowbox record), allocated by SepGetLowBoxNumberEntry on AppInfo's token-create path:

SepGetLowBoxNumberEntry        ; RVA 0xa66110, size (4*SubAuthorityCount+67)&~3
SepSetTokenLowboxNumber+0xc1
SepCreateAppContainerToken+0x73c
NtCreateLowBoxToken+0x2a7

It is keyed by the AppContainer package SID (unique per environment) with a refcount at +0x18. After insertion an extra increment runs whenever Feature_LowBoxNumberEntryReferenceIssue is disabled:

+0x177  call RtlInsertEntryHashTable
+0x180  call Feature_LowBoxNumberEntryReferenceIssue__..._IsEnabled
+0x187  jnz  +0x1a1                 ; enabled  -> skip, refcount stays 1
+0x18c  lock xadd [rsi+18h], rax    ; disabled -> refcount 1 -> 2

On this build the flag is disabled, so a fresh entry with one referencing token carries refcount 2. Token deletion SepTokenDeleteMethod → SepDereferenceLowBoxNumberEntry (RVA 0xa574bc) frees and unlinks only at 0, so the count settles at 1 and the entry stays in the hash table.

WinDbg:

* fix flag disabled: prints 00000016  (IsEnabled = (0x16 & 0x10) ? 0x16 & 1 : .. = 0)
dd nt!Feature_LowBoxNumberEntryReferenceIssue__private_featureState L1

* a new entry for one token is born at refcount 2 (should be 1)
bp nt!SepGetLowBoxNumberEntry "gu; .printf \"entry %p refcount %d\\n\", @rax, dwo(@rax+0x18); g"

* the delete-time deref enters at 2 and is never called again to reach 0
bp nt!SepDereferenceLowBoxNumberEntry ".printf \"deref entry %p refcount-before %d\\n\", @rcx, dwo(@rcx+0x18); g"

Confirmed in KDNET on 26300.9550: the flag reads 0x16; the entry's refcount is 2 right after creation for a single token; after one create+close it remains hash-linked at refcount 1 with its inline SID (+0x38) matching the environment's AppContainer package SID.

Workaround for callers: reuse environments; a shared package SID lets SepFindMatchingLowBoxNumberEntry return the existing entry instead of allocating.

(RVAs relative to the IDA image base 0x140000000.)

bluecmd avatar Oct 13 '26 21:10 bluecmd

Assigning to you @jsidewhite might have to get Salahs input here but I don't know his handle.

bbonaby avatar Oct 23 '26 02:10 bbonaby

Same here on Windows 11 26H2 (build 26300). This took a long-running host down: after about a week of sandboxed commands, every CreateProcessSecurityEnvironment failed with 0x800705AA until we rebooted.

For a sense of the rate under a real workload: 3 hours after a restart, with a day's worth of test runs squeezed in, SeLs was at 6,760 live entries. That puts the ~64K limit about a day or two out on a busy host.

We're taking the workaround you describe: one environment, and so one package SID, per long-lived container instead of one per command. That limits the leak to one entry per container.

While tracking this down we found a separate leak with the same error code. In a signed-in session, a sandbox whose program starts another program keeps its whole AppContainerNamedObjects\<SID> directory and its tokens until reboot. That's in its own issue: #1840.

sanford avatar Nov 22 '26 02:11 sanford

Hi everyone, this first bit was written by me; the rest is LLM generated. I've been experimenting with using Codex to do many, many calls (tens of thousands) for a project evaluating how to improve LLMs' ability to solve coding problems if we give them a little "nudge". I kept running into issues, so I got Claude on to the case, and below is what it found. Hope that it is of help to anyone in a similar position.

Everything below was written by Claude

Seen on Windows 11 25H2 (build 26200) too, through the MXC backend of Codex CLI 0.172.0, with a count at the point of failure:

  • SeLs outstanding was 65,963 while every CreateProcessSecurityEnvironment call was failing with 0x800705AA, and 114 after a reboot.
  • After a reboot, the next failure came 7 h 20 min later, after about 65,400 environments, each with a new AppContainer name (Sandbox.{GUID}). That fits a limit of 65,536 lowbox numbers per session, less those other apps hold.
  • Once creation fails, each failed call leaves its AppContainer profile behind (the Mappings entry and the folder under %LOCALAPPDATA%\Packages). We had about 79,000 within a day. Until then, profiles were removed normally.

We have not tried the shared-SID workaround, since Codex picks a new name for every launch. Our workaround is to start far fewer environments (details in openai/codex#56311).

tirios avatar Nov 27 '26 01:11 tirios