diff --git a/CHANGELOG.md b/CHANGELOG.md index 473e4e6f..068c94c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -92,6 +92,14 @@ past roughly six lines it belongs in the PR the entry links. against +12, and **no true positive lost on any corpus**. The 140 C/C++ ELF cells, 72 AArch64 ELF cells and 11 ARM64 Mach-O cells are bit-identical. *Not reproducible from the bundled fixtures, which do not move.* (#338) +- **(common)** Bound the LSDA reads that lead nowhere, and remember a pointer that reads back empty. The + call-site table budget is charged only once a table's length field is parsed, so an LSDA failing before that + point -- an unsupported LPStart mode, a short buffer, a TType offset that will not read -- cost a read of up to + `MAX_LSDA_BYTES` and charged nothing: a section naming `MAX_RECORDS` such pointers read 12.2 GB over 199,999 + reads. `MAX_LSDA_FAILED_READ_BYTES` holds that to 256 MB over 4,096, leaving 8.6x headroom over the heaviest + real image measured, which spends 29.75 MB. Separately, an address the reader hands nothing back for returned + before the memo was written, so a section naming one dead pointer from every record read it once per record; + remembered by address, that is now one read. *All 447 cells across the six corpora are bit-identical.* (#335) ### Security diff --git a/src/smda/common/EhFrameDecoder.py b/src/smda/common/EhFrameDecoder.py index a6177285..0e58c563 100644 --- a/src/smda/common/EhFrameDecoder.py +++ b/src/smda/common/EhFrameDecoder.py @@ -260,6 +260,27 @@ def decodeEhFrameFdeRanges(data, section_va, pointer_size=8, max_records=MAX_REC #: two orders of magnitude of headroom while capping a section built to be hostile at about a #: second. MAX_LSDA_TABLE_BYTES = 8 * 1024 * 1024 +#: bytes read for LSDAs that then decode to nothing, across one section. The table budget above +#: cannot bound these: it is charged where a table's length field is parsed, so an LSDA failing +#: before that point - an unsupported LPStart mode, a short buffer, a TType offset that will not +#: read - costs a read of up to MAX_LSDA_BYTES and charges nothing. A section naming MAX_RECORDS +#: distinct pointers that all fail early therefore reads about 12 GB while spending none of it. +#: Charging those reads to the table budget instead is not the fix: read_va is asked for +#: MAX_LSDA_BYTES and returns whatever the section holds, so a real image asks for far more than +#: it decodes - 92 MB against 26 KB of tables on the heaviest cell measured - and the 8 MB above +#: would be gone within about 128 reads of the 1,473 that cell makes. Only the failures need a +#: bound, and they separate the two cases: the heaviest real image spends 29.75 MB on them +#: (476 reads), the corpora's Rust cells spend none, and a section naming MAX_RECORDS distinct +#: pointers that fail this early spends 12.2 GB over 199,999 reads. This bound holds that to +#: 256 MB over 4,096, leaving 8.6x headroom over the heaviest real image measured. Note the +#: failure has to come before the table's length field to reach here at all: one that fails +#: after it is already charged, which is why the same shape built with a too-long length runs +#: 512 reads rather than 200,000. What is charged is broader than that failure, though: the +#: condition is that the read yielded no pads, which also catches a table that parsed cleanly +#: and declares none. That case is real and negligible - across the bundled fixtures it is one +#: LSDA of 11,656 bytes, on elf_cxx_landing_pads_x64_xored, and none on the other four - and +#: the 29.75 MB above already counts it, being the same condition measured. +MAX_LSDA_FAILED_READ_BYTES = 256 * 1024 * 1024 def _isSupportedApplication(encoding): @@ -343,7 +364,13 @@ def _decodeLsdaLandingPads(data, lsda_va, function_start, pointer_size, budget=N def decodeEhFrameLandingPads( - data, section_va, read_va, pointer_size=8, max_records=MAX_RECORDS, max_table_bytes=MAX_LSDA_TABLE_BYTES + data, + section_va, + read_va, + pointer_size=8, + max_records=MAX_RECORDS, + max_table_bytes=MAX_LSDA_TABLE_BYTES, + max_failed_read_bytes=MAX_LSDA_FAILED_READ_BYTES, ): """Every exception landing pad the image's own LSDAs declare. @@ -356,23 +383,36 @@ def decodeEhFrameLandingPads( string carries 'L'. The LSDA lives in another section, so ``read_va(va, length)`` supplies the bytes; it may return fewer than asked for, or nothing. - ``max_table_bytes`` bounds the call-site table bytes decoded across the whole section, and - with them how often ``read_va`` is called at all. It is a parameter rather than only a - constant so a test can exhaust it without building a section large enough to. + ``max_table_bytes`` bounds the call-site table bytes decoded across the whole section. + ``max_failed_read_bytes`` bounds the bytes read for LSDAs that then decode to nothing, which + the first cannot reach because it is charged only once a table's length field is parsed. + Both are parameters rather than only constants so a test can exhaust them without building a + section large enough to. """ pads = set() # keyed by LSDA address: one LSDA legitimately serves one function, but nothing stops a # corrupt or hostile image pointing every FDE at the same table, and decoding it once per # FDE is the difference between milliseconds and minutes decoded = {} + # keyed by address alone: a pointer that reads back empty does so whatever function named + # it, and that result never reaches `decoded`, so without this a section naming one + # unreadable address from every record calls the reader once per record. Bounded by + # max_records like `decoded` is; its worst case is the one it cannot help, a distinct + # unreadable address per record, where it holds about 10 MB and saves no read + unreadable = set() budget = [max_table_bytes] + read_budget = [max_failed_read_bytes] for cie, pos, record_end in _walkEhFrameFdes(data, pointer_size, max_records): if cie.lsda_encoding != DW_EH_PE_omit: - pads |= _fdeLandingPads(data, pos, record_end, section_va, cie, read_va, pointer_size, decoded, budget) + pads |= _fdeLandingPads( + data, pos, record_end, section_va, cie, read_va, pointer_size, decoded, unreadable, budget, read_budget + ) return pads -def _fdeLandingPads(data, pos, record_end, section_va, cie, read_va, pointer_size, decoded, budget): +def _fdeLandingPads( + data, pos, record_end, section_va, cie, read_va, pointer_size, decoded, unreadable, budget, read_budget +): field_pos = pos initial_location, pos = _read_encoded_value(data, pos, record_end, cie.fde_encoding, pointer_size) address_range, pos = _read_encoded_value(data, pos, record_end, cie.fde_encoding & 0x0F, pointer_size) @@ -394,17 +434,25 @@ def _fdeLandingPads(data, pos, record_end, section_va, cie, read_va, pointer_siz # memo is keyed by both rather than by the table alone key = (lsda_va, initial_location) if key not in decoded: - # the budget gates the read as well as the decode: the reader is asked for - # MAX_LSDA_BYTES and a section naming a distinct LSDA per record would otherwise copy - # that much per record before the decode declines it - if budget[0] <= 0: + if lsda_va in unreadable: + return set() + # either budget gates the read: the table one once enough tables have been decoded, the + # failed-read one once enough reads have led nowhere. Without the second, an LSDA that + # fails before its length field is reached costs MAX_LSDA_BYTES and charges nothing + if budget[0] <= 0 or read_budget[0] <= 0: return set() lsda_bytes = read_va(lsda_va, MAX_LSDA_BYTES) if not lsda_bytes: + unreadable.add(lsda_va) return set() # an exhausted budget reads as "declares nothing" from here on, which costs recall on # a section built to exhaust it and never invents a pad - decoded[key] = _decodeLsdaLandingPads(lsda_bytes, lsda_va, initial_location, pointer_size, budget) or set() + pads = _decodeLsdaLandingPads(lsda_bytes, lsda_va, initial_location, pointer_size, budget) or set() + # charged on yielding no pads rather than on failing, which is broader than the name; + # see MAX_LSDA_FAILED_READ_BYTES for what that costs a real image + if not pads: + read_budget[0] -= len(lsda_bytes) + decoded[key] = pads # A landing pad is interior to the function its own FDE names; the format says so, and over # 43,881 pads across four corpora and three system libraries not one falls outside. That # makes this the check separating a real table from an LSDA pointer that led into arbitrary diff --git a/tests/testLsdaLandingPads.py b/tests/testLsdaLandingPads.py index f330fd4e..12f8235d 100644 --- a/tests/testLsdaLandingPads.py +++ b/tests/testLsdaLandingPads.py @@ -6,6 +6,7 @@ import lief from smda.common.EhFrameDecoder import ( + MAX_LSDA_FAILED_READ_BYTES, MAX_LSDA_TABLE_BYTES, _decodeLsdaLandingPads, decodeEhFrameFdeRanges, @@ -433,6 +434,106 @@ def testTheCallSiteTableBudgetBoundsOneSection(self): self.assertEqual(_decodeLsdaLandingPads(blob, 0x1000, 0x2000, 8, budget), {0x2020}) self.assertEqual(budget[0], 10 - (len(table) - 2)) + def testAnLsdaThatDecodesIsNotChargedToTheFailedReadBudget(self): + """The condition that keeps this bound off working images. + + A real image asks for far more than it decodes -- the heaviest cell measured reads + 92 MB to decode 26 KB of tables -- so a bound charged for every read would have to be + enormous to be safe. Charging only the reads that lead nowhere is what lets it be + small enough to matter. + """ + section = self.sectionWith(*[self.fdeBody(self.LSDA_VA + step, 0x2000 + step) for step in (0, 0x40, 0x80)]) + + def read_va(addr, length): + return self.LSDA_BYTES + + # a budget smaller than a single read still reaches every table, because none fails + pads = decodeEhFrameLandingPads(section, 0x1000, read_va, max_failed_read_bytes=1) + self.assertEqual(pads, {0x2020, 0x2060, 0x20A0}) + + def testAnExhaustedFailedReadBudgetStopsCallingTheReader(self): + # the shape the bound exists for: distinct pointers that each cost a read of up to + # MAX_LSDA_BYTES and decode to nothing, which the table budget never sees + reads = [] + #: an LSDA whose call-site table length runs past the bytes it arrived in, so the + #: decode declines before the table budget is charged anything + undecodable = bytes([0xFF, 0xFF, 0x01, 0xFF, 0x7F]) + + def counting_read(addr, length): + reads.append(addr) + return undecodable + + section = self.sectionWith(*[self.fdeBody(self.LSDA_VA + step, 0x2000 + step) for step in (0, 0x40, 0x80)]) + # control: with room, every distinct pointer is read and none of them declares a pad + self.assertEqual(decodeEhFrameLandingPads(section, 0x1000, counting_read), set()) + self.assertEqual(len(reads), 3) + + reads.clear() + self.assertEqual(decodeEhFrameLandingPads(section, 0x1000, counting_read, max_failed_read_bytes=0), set()) + self.assertEqual(reads, [], "the reader was called with the failed-read budget already spent") + + reads.clear() + # one read's worth admits exactly one, then the bound stops the walk + self.assertEqual( + decodeEhFrameLandingPads(section, 0x1000, counting_read, max_failed_read_bytes=len(undecodable)), + set(), + ) + self.assertEqual(len(reads), 1) + + def testAnUnreadableLsdaPointerIsReadOnceHoweverManyRecordsNameIt(self): + # an address that reads back empty never reaches the per-(table, function) memo, so + # without one by address a section naming the same dead pointer from every record + # calls the reader once per record -- 199,999 times at max_records + reads = [] + + def counting_read(addr, length): + reads.append(addr) + return b"" + + section = self.sectionWith(*[self.fdeBody(self.LSDA_VA, 0x2000 + 0x40 * step) for step in range(16)]) + self.assertEqual(decodeEhFrameLandingPads(section, 0x1000, counting_read), set()) + self.assertEqual(reads, [self.LSDA_VA]) + + def testEachDistinctUnreadablePointerIsStillRead(self): + # the memo is by address rather than a latch: one dead pointer does not stop the next + # address being tried + reads = [] + + def counting_read(addr, length): + reads.append(addr) + return b"" + + section = self.sectionWith( + *[self.fdeBody(self.LSDA_VA + 0x100 * step, 0x2000 + 0x40 * step) for step in range(4)] + ) + self.assertEqual(decodeEhFrameLandingPads(section, 0x1000, counting_read), set()) + self.assertEqual(reads, [self.LSDA_VA + 0x100 * step for step in range(4)]) + + def testATableThatDecodesAndDeclaresNoPadIsChargedToo(self): + # the charge condition is that the read yielded no pads, which is broader than failing + # before the table length: this table parses cleanly, charges the table budget and + # still declares nothing. Documented rather than separated because it is negligible -- + # one LSDA of 11,656 bytes across the bundled fixtures and none on four of the five + declares_no_pad = bytes([0xFF, 0xFF, 0x01, 0x04, 0x00, 0x10, 0x00, 0x00]) + reads = [] + + def counting_read(addr, length): + reads.append(addr) + return declares_no_pad + + section = self.sectionWith(*[self.fdeBody(self.LSDA_VA + step, 0x2000 + step) for step in (0, 0x40)]) + self.assertEqual( + decodeEhFrameLandingPads(section, 0x1000, counting_read, max_failed_read_bytes=len(declares_no_pad)), + set(), + ) + self.assertEqual(len(reads), 1, "a clean decode that declares no pad was not charged") + + def testTheFailedReadBudgetClearsTheHeaviestRealImageMeasured(self): + # a bound sized below real input would drop pads on the images this rule is for; the + # worst measured across the built ELF corpora spends 29.75 MB on reads that decode to + # nothing, on a cell making 476 of them + self.assertGreater(MAX_LSDA_FAILED_READ_BYTES, 8 * 29_753_856) + def testTheBudgetClearsTheHeaviestShapeThisDecoderIsBuiltFor(self): # a bound sized below real input would silently drop pads, which is how the first # version of it lost 197 on a statically linked C++ binary