From 82b7cb3eb85843afd79c3961014e21dd70a7c6e5 Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:38:48 +0000 Subject: [PATCH] D3D12.X: clamp root argument writes to the target parameter's size bind::put_in and console_list::close destructured Param::Constants without checking `count`, only bounds-checking against the entire root argument table. Writing constants with offset + n > count (or writing past a 4-byte table / 8-byte descriptor slot) silently overwrote the adjacent root parameter, and large offsets could overflow in bind.rs. Clamp writes to the parameter's own byte span using checked arithmetic in bind::put_in, and stop at `off + k >= count` in console_list::close, with a unit test verifying adjacent parameters are preserved. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/bind.rs | 57 +++++++++++++++++++++-- crates/xcl/src/hle/d3d12x/console_list.rs | 6 ++- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index e0c1518fa4..bbd8a5f03e 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -155,14 +155,25 @@ pub(crate) fn remove_state_for_test(list: *mut c_void) { fn put_in(list: *mut c_void, gfx: bool, i: u32, offset: u32, bytes: &[u8]) { with(list, |s| { let (layout, args) = if gfx { (&s.layout_gfx, &mut s.args_gfx) } else { (&s.layout, &mut s.args) }; - let Some(at) = layout.as_ref().and_then(|l| l.params.get(i as usize)).map(|p| match *p { - Param::Constants { at, .. } => at as usize + 4 * offset as usize, - Param::Descriptor { at } | Param::Table { at } => at as usize, + let Some((at, limit)) = layout.as_ref().and_then(|l| l.params.get(i as usize)).and_then(|p| match *p { + Param::Constants { at, count } => { + let end = (at as usize).checked_add((count as usize).checked_mul(4)?)?; + let start = (at as usize).checked_add((offset as usize).checked_mul(4)?)?; + Some((start, end)) + } + Param::Descriptor { at } => Some((at as usize, (at as usize).checked_add(8)?)), + Param::Table { at } => Some((at as usize, (at as usize).checked_add(4)?)), }) else { return; }; - if let Some(t) = args.get_mut(at..at + bytes.len()) { - t.copy_from_slice(bytes); + let Some(end) = at.checked_add(bytes.len()) else { + return; + }; + let write_end = end.min(limit); + if write_end > at { + if let Some(t) = args.get_mut(at..write_end) { + t.copy_from_slice(&bytes[..write_end - at]); + } } }) } @@ -970,6 +981,42 @@ mod tests { words.iter().flat_map(|w| w.to_le_bytes()).collect() } + #[test] + fn out_of_bounds_root_constants_do_not_clobber_adjacent_parameter() { + // Param 0: Constants(count=2) -> bytes 0..8; Param 1: Descriptor (CBV=2) -> bytes 8..16. + let words: [u32; 18] = [ + 2, 2, 24, 0, 0, 0, + 1, 0, 48, // param 0: Constants + 2, 0, 60, // param 1: CBV Descriptor + 0, 0, 2, // param 0 payload: reg=0, space=0, count=2 + 0, 0, 0, // param 1 payload + ]; + let blob: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let list = 0x7100_00F1usize as *mut c_void; + let rs = 0x7100_00F2usize; + console_list::note_root_signature(rs, &blob); + unsafe { + set_compute_root_signature(list, rs as *mut c_void); + set_compute_root_descriptor(list, 1, 0x1122_3344_5566_7788); + // Offset 2 is past count=2: must not overwrite param 1 at bytes 8..12. + set_compute_root_constant(list, 0, 0xDEAD_BEEF, 2); + // Multi-constant write starting at offset 1 with 3 dwords: only offset 1 fits. + let vals = [0xAAAA_BBBBu32, 0xCCCC_DDDDu32, 0xEEEE_FFFFu32]; + set_compute_root_constants(list, 0, vals.len() as u32, vals.as_ptr(), 1); + // Overflowing offset must not panic. + set_compute_root_constant(list, 0, 0x9999_9999, u32::MAX); + } + let (_, _, args, _, _) = reset_state_for_test(list); + remove_state_for_test(list); + assert_eq!(&args[0..4], &0u32.to_le_bytes()); + assert_eq!(&args[4..8], &0xAAAA_BBBBu32.to_le_bytes()); + assert_eq!( + &args[8..16], + &0x1122_3344_5566_7788u64.to_le_bytes(), + "out-of-bounds root constant overwrote adjacent root descriptor" + ); + } + #[test] fn reset_and_clear_state_track_initial_pso_and_clear_root_args() { let list = 0x7100_0001usize as *mut c_void; diff --git a/crates/xcl/src/hle/d3d12x/console_list.rs b/crates/xcl/src/hle/d3d12x/console_list.rs index 8619941f17..2ed5bb3428 100644 --- a/crates/xcl/src/hle/d3d12x/console_list.rs +++ b/crates/xcl/src/hle/d3d12x/console_list.rs @@ -848,8 +848,12 @@ pub unsafe fn close(obj: *mut c_void) -> Result { let off = ((w0 & 0x3FF) >> 2) as usize; let n = ((w0 >> 16) & 0x3FFF) as usize + 1; match layout.as_ref().and_then(|l| l.params.get(i).copied()) { - Some(Param::Constants { at: o, .. }) => { + Some(Param::Constants { at: o, count }) => { + let limit = count as usize; for k in 0..n { + if off + k >= limit { + break; + } let d = (o as usize) + 4 * (off + k); if let Some(t) = table.get_mut(d..d + 4) { t.copy_from_slice(&get(1 + k).to_le_bytes());