VFS: MergedFind TOCTOU use-after-free and missing pointer/dwSize checks in vfs_hooks #1378

Open
opened 2026-11-24 15:31:20 +00:00 by microwave805 · 0 comments
microwave805 commented 2026-11-24 15:31:20 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:31:20Z.


Checked on main (930e8b4).

Problem

  1. TOCTOU use-after-free & aliased &mut UB on MergedFind (crates/xcl/src/vfs_hooks.rs:952-961, 1128-1178):
    MERGED tracks only raw pointer addresses (HashSet<usize>). hk_find_next_w and hk_find_next_a check is_merged(h) (which locks and immediately unlocks MERGED), then call merged_next(h) unlocked, creating &mut *(h as *mut MergedFind) while a concurrent hk_find_close can drop Box::from_raw(h as *mut MergedFind):

    unsafe fn merged_next(h: HANDLE) -> Option<WIN32_FIND_DATAW> {
        let m = &mut *(h as *mut MergedFind);
        let r = m.items.get(m.pos).copied();
        m.pos += 1;
        r
    }
    ...
    if is_merged(h) {
        return match merged_next(h) {
            Some(d) => { *data = d; 1 }
    
  2. Unchecked caller pointers & CREATEFILE2_EXTENDED_PARAMETERS.dwSize:

    • find_first_vfs (vfs_hooks.rs:1051: *out = items[0]), hk_find_next_w (1139: *data = d), and w_to_a (1095: &mut *a) dereference caller output pointers without null checks.
    • hk_create_file_2 (vfs_hooks.rs:141) reads *((params as *const u8).add(8) as *const u32) without verifying dwSize >= 12 (unlike copy_file_2_fail_if_exists at lines 860-864).

Minimal trigger

  • Call FindNextFileW/A(h, ...) concurrently with FindClose(h) (or two concurrent FindNextFile* calls) on a merged overlay find handle → UAF / aliased &mut UB.
  • Pass a CREATEFILE2_EXTENDED_PARAMETERS struct with dwSize < 12 to CreateFile2, or a null WIN32_FIND_DATA* to FindFirstFile*/FindNextFile*.

Suggested fix

Store HashMap<usize, MergedFind> keyed by synthetic opaque handle IDs inside the MERGED mutex so FindNext* and FindClose hold the lock across lookup/mutation/removal, add null checks on out/data, and check dwSize >= 12 in hk_create_file_2.


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

<!-- greyfinch-sync:ivall/xcl:issue:6585383387 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1388). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:31:20Z. --- Checked on `main` (`930e8b4`). ### Problem 1. **TOCTOU use-after-free & aliased `&mut` UB on `MergedFind` (`crates/xcl/src/vfs_hooks.rs:952-961, 1128-1178`):** `MERGED` tracks only raw pointer addresses (`HashSet<usize>`). `hk_find_next_w` and `hk_find_next_a` check `is_merged(h)` (which locks and immediately unlocks `MERGED`), then call `merged_next(h)` unlocked, creating `&mut *(h as *mut MergedFind)` while a concurrent `hk_find_close` can drop `Box::from_raw(h as *mut MergedFind)`: ```rust unsafe fn merged_next(h: HANDLE) -> Option<WIN32_FIND_DATAW> { let m = &mut *(h as *mut MergedFind); let r = m.items.get(m.pos).copied(); m.pos += 1; r } ... if is_merged(h) { return match merged_next(h) { Some(d) => { *data = d; 1 } ``` 2. **Unchecked caller pointers & `CREATEFILE2_EXTENDED_PARAMETERS.dwSize`:** - `find_first_vfs` (`vfs_hooks.rs:1051`: `*out = items[0]`), `hk_find_next_w` (`1139`: `*data = d`), and `w_to_a` (`1095`: `&mut *a`) dereference caller output pointers without null checks. - `hk_create_file_2` (`vfs_hooks.rs:141`) reads `*((params as *const u8).add(8) as *const u32)` without verifying `dwSize >= 12` (unlike `copy_file_2_fail_if_exists` at lines `860-864`). ### Minimal trigger - Call `FindNextFileW`/`A(h, ...)` concurrently with `FindClose(h)` (or two concurrent `FindNextFile*` calls) on a merged overlay find handle → UAF / aliased `&mut` UB. - Pass a `CREATEFILE2_EXTENDED_PARAMETERS` struct with `dwSize < 12` to `CreateFile2`, or a null `WIN32_FIND_DATA*` to `FindFirstFile*`/`FindNextFile*`. ### Suggested fix Store `HashMap<usize, MergedFind>` keyed by synthetic opaque handle IDs inside the `MERGED` mutex so `FindNext*` and `FindClose` hold the lock across lookup/mutation/removal, add null checks on `out`/`data`, and check `dwSize >= 12` in `hk_create_file_2`. --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#1378
No description provided.