[pr-fix] loader: a patch writes only bytes it checked, inside the image #412
Loading…
Reference in a new issue
No description provided.
Delete branch "qnjk/Darner:callhook-patch-bounds"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Contributor role (optional)
What it changes
The problem on main:
patch_bytesreadsexpected.len()bytes atat, compares them, then writesnew.len()bytes at the same address:(
crates/darner-loader/src/callhook.rs:186). Two things are unchecked: a longernewoverwrites title instructions the guard never compared (the shapehook_entryavoids atcallhook.rs:176withjump.resize(prologue.len(), 0x90)), a shorter one leaves a checked instruction half replaced, and neither length is bounded by the title image, so the write can leave it.This change makes the image window a parameter —
patch_bytes(base, size, at, expected, new), asframe_rate::applyandheap_sites::applyalready take it — and refuses, before reading anything, whennew.len() != expected.len()or whenat.checked_sub(base).and_then(|o| o.checked_add(new.len()))leaves[base, base + size)(the arithmeticheap_sites.rs:1349uses). A bare absolute address cannot tell whetheratis title code or one of our thunk pages, which is why the window is passed in rather than guessed; our thunk pages are written byplacewhen they are made, never through here, and the doc now says so.Note for review:
patch_byteshas no caller in this tree today (grep patch_bytesfinds the definition only). So this is hardening of apub unsafeAPI that a fork can call, not a fix for a live path — if you would rather see it deleted, that is a fine answer and this change makes that decision easier to take instead of harder.How it was tested
67fbdd4(one commit onf3840d7).cargo test --locked -p darner-loader --lib— 94 passed, 0 failed.VirtualAlloc, so they add no allocator churn to the exception-handler tests in the same process):callhook::tests::a_patch_that_leaves_the_image_is_refused— a range wholly before the image and one straddling its end are refused with the bytes unchanged, and a range inside is written.callhook::tests::a_patch_of_another_length_is_refused— a 7-byte and a 3-bytenewover the same 5 checked bytes are refused with the bytes unchanged.if false && …) and the tests kept: i.e. the old code returnedOkand wrote the bytes.cargo clippy --locked -p darner-loader --all-targets -- -D warningspasses.cargo clippy --workspace --all-targets --locked -- -D warnings: passes.cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.cargo test --workspace --locked: not green on this machine, and not because of this change. Three pre-existing failures, each covered by an open pull request:darner-gpustands still in parallel on the test deadlock #224 describes, and serially finishes 1772 passed and 1 failed (pipeline::make::tests::a_capped_draw_waits_once_then_drops, which passes on its own) beside the order-dependent flakes of #150;darner-shaderfailsemit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders, whose fixture #227 fixes.darner-loader's own suite is green.Checklist
patch_bytes, so it cannot run during a game session. The evidence is the two tests, which fail onmain.main.docs/asset-policy.md. (cargo xtask policy: 555 files ok.)Checked for overlap
crates/darner-loader/src/callhook.rs(it adds the six missing// SAFETY:comments, this file's tests among them), so a small conflict in the test module is possible; that is why it is listed. Open PR #9 namesdarner_hle::viewhook::installas a caller ofpatch_bytes; that caller is not onmainand does not exist in this tree, so if #9 lands after this one it must pass the newbase, sizearguments. No open or closed issue or pull request mentionspatch_bytesotherwise.patch_byteswrite outside the image or over unchecked bytes 67fbdd4e69View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.