diff --git a/tests/test_inline_scan.py b/tests/test_inline_scan.py index 5fb30b637..65bcb325b 100644 --- a/tests/test_inline_scan.py +++ b/tests/test_inline_scan.py @@ -121,6 +121,44 @@ class InlineScanTests(unittest.TestCase): self.assertTrue(site["checked"]) self.assertEqual(site["limit"], 0x30) + def test_list_copied_before_the_cursor_load_is_still_unchecked(self): + # The UNGUARDED write with `this` copied to rbx first and the cursor loaded through rcx. The walk back from + # the store-back meets the load before the copy, so it took the load for another structure's +18h and + # called the write unrelated: "UNCHECKED 0" for a write that never checks. The copy after the load was + # already followed. + write = ( + "mov rax,qword ptr [rcx+18h]", + "mov dword ptr [rax],4002C400h", + "mov dword ptr [rax+4],edx", + "add rax,10h", + "mov qword ptr [rbx+18h],rax", + "mov byte ptr [rbx+0000000000000140h],0", + ) + for name, rows in (("copy before the load", ("mov rbx,rcx",) + write), + ("copy after the load", write[:1] + ("mov rbx,rcx",) + write[1:])): + with self.subTest(name): + (site,) = sites(rows) + self.assertFalse(site["checked"]) + self.assertTrue(site["list"]) + self.assertTrue(site["load_traced"]) + self.assertEqual(site["size"], 0x10) + self.assertIn("UNCHECKED 1", inline_scan.summarize([site])) + + def test_cursor_loaded_from_another_structure_is_not_a_list(self): + # rsi is never a copy of the list, so its +18h is not the list's cursor, wherever the copy into rbx is. + rows = ( + "mov rbx,rcx", + "mov rax,qword ptr [rsi+18h]", + "mov dword ptr [rax],4002C400h", + "add rax,10h", + "mov qword ptr [rbx+18h],rax", + "mov byte ptr [rbx+0000000000000140h],0", + ) + (site,) = sites(rows) + self.assertFalse(site["list"]) + self.assertFalse(site["load_traced"]) + self.assertIsNone(site["load"]) + def test_stack_frame_slot_is_not_a_list(self): rows = ( "mov qword ptr [rbp+0000000000000140h],rcx", diff --git a/tools/inline_scan.py b/tools/inline_scan.py index 86fde4201..37e4d51a4 100644 --- a/tools/inline_scan.py +++ b/tools/inline_scan.py @@ -91,7 +91,7 @@ def instructions(lines): def classify(window, base, stored): """Look back from a store-back of `stored` into [base+18h]. Return a site dict or None.""" - chain, size, load, bases, broken = {stored}, 0, None, {base}, False + chain, size, load, load_base, bases, broken = {stored}, 0, None, None, {base}, False touched = {stored} # every register the cursor passed through, for counting stores span = [] for addr, mnem, ops in reversed(window): @@ -101,8 +101,8 @@ def classify(window, base, stored): if mnem == "mov" and (cp := COPY.match(ops)) and cp.group(1) in bases: bases.add(cp.group(2)) # `mov rbx,rcx`: the list is in both if load is None and not broken: - if mnem == "mov" and (lm := LOAD.match(ops)) and lm.group(1) in chain and lm.group(2) in bases: - load = addr + if mnem == "mov" and (lm := LOAD.match(ops)) and lm.group(1) in chain: + load, load_base = addr, lm.group(2) # its base is checked after the walk, below elif mnem == "lea" and (le := LEA.match(ops)) and le.group(1) in chain: chain.discard(le.group(1)) # defined here; older values of it are not the cursor chain.add(le.group(2)) @@ -112,6 +112,10 @@ def classify(window, base, stored): size += int(am.group(2), 16) elif ops.split(",", 1)[0] in chain and mnem not in ("cmp", "test") and not mnem.startswith("j"): broken = True # the stored value was computed from something other than the cursor + # The walk runs backwards, so a copy `mov rbx,rcx` that comes before a load through rcx is only seen after the + # load: check the load's base against every register the list was copied through. + if load is not None and load_base not in bases: + load, broken = None, True # the cursor came from another structure's +18h span.reverse() stores, header, limit, target, evidence = 0, None, None, None, False limit_regs = {} # register -> limit offset, for `mov rdx,[list+30h]` ... `cmp rcx,rdx`