ProcessContainer: a sandbox whose program starts another program is never released (signed-in session)
Relevant area(s): Windows
Brief description
In a signed-in (interactive) session, a process sandbox (CreateProcessSecurityEnvironment) whose program starts another program is kept by the kernel after everything in it has ended and the environment is closed: its \Sessions\N\AppContainerNamedObjects\<SID> directory stays (with Local, Session, Global, RPC Control), along with its tokens (Toke) and some pool. No process of it remains, and no user-mode handle names the directory; the System process's handle count grows by 3 directories, 2 symbolic links and a file per kept sandbox. Only a reboot frees it. A sandbox whose program just exits is released normally.
On a host that runs many sandboxed commands, these accumulate until CreateProcessSecurityEnvironment fails with 0x800705AA (ERROR_NO_SYSTEM_RESOURCES). We hit that after about 3,300 kept sandboxes (537k live tokens, 5.7 GB nonpaged pool). Related but distinct: #1486 (an SeLs entry leaked for every environment) and #1487 (bfs.sys grant records). This one is per environment too, but only when its program creates a process, and it keeps far more per environment.
Windows 11 26H2, build 26300.
Steps to reproduce
The attached leak.ps1 uses no other software. It makes N process sandboxes (a policy that only reads C:\Windows, no network), runs a program in each to its end and closes the environment. Before and after, it counts the session's per-sandbox object directories, the account's AppContainer profiles and live Toke allocations (SystemPoolTagInformation).
powershell -NoProfile -ExecutionPolicy Bypass -File leak.ps1 -N 5
Run in a signed-in session:
| Program in the sandbox | Kept |
|---|---|
cmd /c exit, powershell -Command exit |
0 of 5–24 |
cmd /c cmd /c exit |
0 of 5 |
cmd /c start /wait /min cmd /c exit |
5 of 5 |
powershell -Command C:\Windows\System32\cmd.exe /c exit |
5 of 5 |
PowerShell Start-Process (hidden or -NoNewWindow), .NET Process.Start (CreateNoWindow either way), CreateProcessW by P/Invoke (any handle inheritance, CREATE_NO_WINDOW or not) |
every one |
Made no difference: closing the environment or not (or killing its creator), the program in a kill-on-close job or not, its own console or a shared one, its own non-interactive window station and desktop instead of WinSta0\Default, the UI limits in the policy, many sandboxes at once or one at a time, deleting the profile, the network policy. Kept sandboxes stay for hours. The same programs under a service in session 0 keep none (not looked into further).
Expected behavior
Once every process in the sandbox has ended and the environment is closed, its object directory, tokens and pool are released.
Actual behavior
The directory and tokens stay until reboot whenever the sandbox's program created a process.
leak.ps1
# Does a sandbox Windows makes leave something behind once it's ended?
# No other software in it: N
# process sandboxes (CreateProcessSecurityEnvironment, a policy that only
# reads C:\Windows, no network), each running `cmd /c exit` to its end and
# closed, then N app containers made the documented way
# (CreateAppContainerProfile, the same program, the profile deleted).
# Before and after: this session's per-sandbox object directories
# (\Sessions\N\AppContainerNamedObjects, one per sandbox something holds a
# token of), the account's profiles, and the kernel's live tokens (pool
# tag Toke). Nothing should be left: the directories and tokens back where
# they started. Run in a signed-in session, then in a service's (session 0).
# Then N sandboxes whose program starts another (PowerShell running cmd),
# which Windows keeps after all of it has ended, and N whose program is
# cmd running cmd, which it doesn't (2026-11-21, session 1).
#
# powershell -NoProfile -ExecutionPolicy Bypass -File leak.ps1 -N 20
param([int]$N = 20)
Add-Type -TypeDefinition @"
using System; using System.Runtime.InteropServices; using System.Collections.Generic;
public static class L {
[DllImport("processmodel.dll")] public static extern int CreateProcessSecurityEnvironment(byte[] spec, uint size, uint flags, out IntPtr env);
[DllImport("processmodel.dll")] public static extern void CloseProcessSecurityEnvironment(IntPtr env);
[DllImport("userenv.dll", CharSet=CharSet.Unicode)] public static extern int CreateAppContainerProfile(string n, string d, string s, IntPtr c, int k, out IntPtr sid);
[DllImport("userenv.dll", CharSet=CharSet.Unicode)] public static extern int DeleteAppContainerProfile(string n);
[StructLayout(LayoutKind.Sequential)] struct SC { public IntPtr Sid; public IntPtr Caps; public int Count; public int Reserved; }
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] struct SI { public int cb; public IntPtr r; public IntPtr d; public IntPtr t; public int x, y, xs, ys, xc, yc, fa, fl; public short sw, cb2; public IntPtr r2, i, o, e; }
[StructLayout(LayoutKind.Sequential)] struct SIX { public SI s; public IntPtr list; }
[StructLayout(LayoutKind.Sequential)] struct PI { public IntPtr p, t; public int pid, tid; }
[DllImport("kernel32.dll", SetLastError=true)] static extern bool InitializeProcThreadAttributeList(IntPtr l, int n, int f, ref IntPtr s);
[DllImport("kernel32.dll", SetLastError=true)] static extern bool UpdateProcThreadAttribute(IntPtr l, int f, IntPtr a, IntPtr v, IntPtr s, IntPtr p, IntPtr r);
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)] static extern bool CreateProcessW(string app, System.Text.StringBuilder cmd, IntPtr pa, IntPtr ta, bool inh, int flags, IntPtr env, string cwd, ref SIX si, out PI pi);
[DllImport("kernel32.dll")] static extern int WaitForSingleObject(IntPtr h, int ms);
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
[DllImport("ntdll.dll")] static extern int NtQuerySystemInformation(int c, IntPtr b, int l, out int r);
[StructLayout(LayoutKind.Sequential)] struct US { public ushort Length; public ushort Max; public IntPtr Buffer; }
[StructLayout(LayoutKind.Sequential)] struct OA { public int Length; public IntPtr Root; public IntPtr Name; public uint Attr; public IntPtr SD; public IntPtr QOS; }
[DllImport("ntdll.dll")] static extern int NtOpenDirectoryObject(out IntPtr h, uint a, ref OA oa);
[DllImport("ntdll.dll")] static extern int NtQueryDirectoryObject(IntPtr h, IntPtr b, int l, bool one, bool restart, ref uint ctx, out uint r);
// Run `cmd /c exit` with one attribute (the sandbox's, or an app container's capabilities) to its end.
static string Run(int attribute, IntPtr value, int size) { return Run(attribute, value, size, @"C:\Windows\System32\cmd.exe /c exit"); }
static string Run(int attribute, IntPtr value, int size, string line) {
IntPtr n = IntPtr.Zero; InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref n);
IntPtr list = Marshal.AllocHGlobal(n); InitializeProcThreadAttributeList(list, 1, 0, ref n);
if (!UpdateProcThreadAttribute(list, 0, (IntPtr)attribute, value, (IntPtr)size, IntPtr.Zero, IntPtr.Zero)) return "attribute " + Marshal.GetLastWin32Error();
SIX si = new SIX(); si.s.cb = Marshal.SizeOf(si); si.list = list; PI pi;
if (!CreateProcessW(null, new System.Text.StringBuilder(line), IntPtr.Zero, IntPtr.Zero, false, 0x08080000, IntPtr.Zero, @"C:\Windows", ref si, out pi)) return "start " + Marshal.GetLastWin32Error();
WaitForSingleObject(pi.p, 20000); CloseHandle(pi.t); CloseHandle(pi.p); return null;
}
public static string Sandbox(byte[] spec) { return Sandbox(spec, @"C:\Windows\System32\cmd.exe /c exit"); }
public static string Sandbox(byte[] spec, string line) {
IntPtr env; int r = CreateProcessSecurityEnvironment(spec, (uint)spec.Length, 0, out env);
if (r < 0) return "make 0x" + r.ToString("X8");
IntPtr h = Marshal.AllocHGlobal(8); Marshal.WriteIntPtr(h, env);
string e = Run(0x20023, h, 8, line); CloseProcessSecurityEnvironment(env); return e;
}
public static string Container(string name) {
IntPtr sid; int r = CreateAppContainerProfile(name, name, name, IntPtr.Zero, 0, out sid);
if (r < 0) return "profile 0x" + r.ToString("X8");
SC sc = new SC(); sc.Sid = sid; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(sc)); Marshal.StructureToPtr(sc, p, false);
string e = Run(0x20009, p, Marshal.SizeOf(sc)); DeleteAppContainerProfile(name); return e;
}
public static long Tokens() {
int len = 1 << 20; IntPtr b; int ret; int st;
while (true) { b = Marshal.AllocHGlobal(len); st = NtQuerySystemInformation(22, b, len, out ret); if (st == unchecked((int)0xC0000004)) { Marshal.FreeHGlobal(b); len *= 2; continue; } break; }
int count = Marshal.ReadInt32(b); long live = -1;
for (int i = 0; i < count; i++) { IntPtr e = b + 8 + i * 40; if (Marshal.ReadInt32(e) == 0x656B6F54) live = Marshal.ReadInt32(e, 4) - Marshal.ReadInt32(e, 8); }
Marshal.FreeHGlobal(b); return live;
}
public static int Directories(string path) {
US us = new US(); us.Buffer = Marshal.StringToHGlobalUni(path); us.Length = (ushort)(path.Length * 2); us.Max = (ushort)(path.Length * 2 + 2);
IntPtr pus = Marshal.AllocHGlobal(Marshal.SizeOf(us)); Marshal.StructureToPtr(us, pus, false);
OA oa = new OA(); oa.Length = Marshal.SizeOf(typeof(OA)); oa.Name = pus; oa.Attr = 0x40;
IntPtr h; if (NtOpenDirectoryObject(out h, 1, ref oa) != 0) return -1;
uint ctx = 0, r; IntPtr buf = Marshal.AllocHGlobal(1 << 16); int n = 0; bool first = true;
while (true) {
int st = NtQueryDirectoryObject(h, buf, 1 << 16, false, first, ref ctx, out r); first = false;
if (st != 0 && st != 0x105) break;
for (int i = 0; Marshal.ReadInt16(buf + i * 32) != 0; i++) n++;
if (st == 0) break;
}
return n;
}
}
"@
$spec = '20000000505345431400180004000000000008000000100000001400000000001800000001000000ff03000000000000080000002400000001000000040000000a000000433a5c57696e646f77730000080008000000040008000000080000000400040004000000'
$bytes = [byte[]]::new($spec.Length / 2); for ($i = 0; $i -lt $bytes.Length; $i++) { $bytes[$i] = [Convert]::ToByte($spec.Substring(2 * $i, 2), 16) }
$session = (Get-Process -Id $PID).SessionId
$dirs = "\Sessions\$session\AppContainerNamedObjects"
$maps = 'HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Mappings'
function Now($what) { "{0,-26} directories {1,6} profiles {2,6} tokens {3,8}" -f $what, [L]::Directories($dirs), (Get-ChildItem $maps).Count, [L]::Tokens() }
"session $session"
Now "before"
for ($i = 0; $i -lt $N; $i++) { $e = [L]::Sandbox($bytes); if ($e) { "process sandbox: $e"; break } }
Start-Sleep 2; Now "after $N process sandboxes"
for ($i = 0; $i -lt $N; $i++) { $e = [L]::Container("leak-test-$PID-$i"); if ($e) { "app container: $e"; break } }
Start-Sleep 2; Now "after $N app containers"
# A program in the sandbox that starts another: PowerShell running cmd,
# then cmd running cmd. (2026-11-21, session 1: each PowerShell one left
# its sandbox's directory and tokens, 5 of 5; no cmd one did.)
$ps = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command C:\Windows\System32\cmd.exe /c exit'
for ($i = 0; $i -lt $N; $i++) { $e = [L]::Sandbox($bytes, $ps); if ($e) { "powershell starting cmd: $e"; break } }
Start-Sleep 2; Now "after $N, powershell -> cmd"
for ($i = 0; $i -lt $N; $i++) { $e = [L]::Sandbox($bytes, 'C:\Windows\System32\cmd.exe /c C:\Windows\System32\cmd.exe /c exit'); if ($e) { "cmd starting cmd: $e"; break } }
Start-Sleep 2; Now "after $N, cmd -> cmd"