Reject Objective-C method records shorter than their declared layout (R3.12) (#1387)

A method list whose record size was smaller than a method record, or whose last record was only partly in the file, was still read, so methods came from whatever bytes followed. A list now needs records of at least 24 bytes (12 for relative ones), each wholly in the file, and longer extended records still read.
This commit is contained in:
Afloat authored and GitHub committed 2026-11-09 23:05:42 +07:00
1 parent 4824e31d5f
commit f7b985cd3f
4 files changed
+72 -2

No files matched your search

+2 -2
View File
@@ -526,13 +526,13 @@ fn method_list(data: &[u8], b: &Binary, list: u64, kind: &'static str, out: &mut
let (Some(ent_flags), Some(count)) = (r.u32_le(0), r.u32_le(4)) else { return };
let relative = ent_flags & 0x8000_0000 != 0;
let entsize = u64::from(ent_flags & 0xfffc);
if entsize == 0 {
if entsize < if relative { 12 } else { 24 } {
return;
}
for i in 0..u64::from(count).min(MAX_METHODS as u64) {
let Some(e) = i.checked_mul(entsize).and_then(|offset| list.checked_add(8)?.checked_add(offset)) else { break };
let Some(raw) = b.read(data, e, entsize as usize).filter(|bytes| bytes.len() as u64 == entsize) else { break };
if relative {
let Some(raw) = b.read(data, e, 12) else { break };
let rr = Reader::new(raw);
let rel = |o: usize| rr.u32_le(o).and_then(|v| e.checked_add(o as u64).map(|a| a.wrapping_add(i64::from(v as i32) as u64)));
let name_field = rel(0);
@@ -0,0 +1,67 @@
use craft_anything_formats::binary::{Binary, parse_binary};
use craft_anything_formats::macho;
use craft_anything_testkit::{MACHO_BASE, OBJC_SELECTOR, macho64};
const LIST: usize = 0x1088;
const ENTRY: usize = LIST + 8;
fn fixture(relative: bool, stride: u32) -> (Vec<u8>, Binary) {
let mut data = macho64();
let flags = if relative { 0xc000_0000 } else { 0 };
data[LIST..LIST + 4].copy_from_slice(&(flags | stride).to_le_bytes());
if relative {
for (field, target) in [(0, 0x6f0i32), (4, 0x700), (8, 0x660)] {
let at = ENTRY + field;
data[at..at + 4].copy_from_slice(&(target - at as i32).to_le_bytes());
}
}
let binary = parse_binary(&data).unwrap();
(data, binary)
}
fn methods(data: &[u8], binary: &Binary) -> Vec<macho::ObjcMethod> {
let metadata = macho::objc_metadata(data, binary);
assert_eq!(metadata.classes.len(), 1);
metadata.classes[0].methods.clone()
}
#[test]
fn absolute_records_cannot_borrow_fields_after_their_stride() {
for stride in [4, 8, 12, 16, 20] {
let (data, binary) = fixture(false, stride);
assert!(methods(&data, &binary).is_empty(), "stride {stride}");
}
}
#[test]
fn relative_records_cannot_borrow_fields_after_their_stride() {
for stride in [4, 8] {
let (data, binary) = fixture(true, stride);
assert!(methods(&data, &binary).is_empty(), "stride {stride}");
}
}
#[test]
fn incomplete_records_are_not_published_from_the_readable_prefix() {
for relative in [false, true] {
let stride = if relative { 12 } else { 24 };
let (data, mut binary) = fixture(relative, stride);
let section = binary.sections.iter_mut().find(|section| section.name == "__objc_const").unwrap();
section.file_size = ENTRY as u64 - section.file_offset.unwrap() + if relative { 4 } else { 8 };
assert!(methods(&data, &binary).is_empty());
}
let (data, binary) = fixture(false, 128);
assert!(methods(&data, &binary).is_empty(), "declared padding also belongs to the record");
}
#[test]
fn complete_records_and_extended_strides_preserve_method_facts() {
for (relative, stride) in [(false, 24), (false, 32), (true, 12), (true, 16)] {
let (data, binary) = fixture(relative, stride);
let methods = methods(&data, &binary);
assert_eq!(methods.len(), 1);
assert_eq!(methods[0].selector, OBJC_SELECTOR);
assert_eq!(methods[0].types.as_deref(), Some("v24@0:8@16"));
assert_eq!(methods[0].imp, Some(MACHO_BASE + 0x660));
}
}
+2
View File
@@ -1538,6 +1538,8 @@ Owner: open · Status: in progress
— claimed: Afloat16, branch bugfix/r3.12-macho-zerofill-section-data
- [x] Keep native address translations and reads inside both the mapped range and its file-backed bytes.
— claimed: Afloat16, branch bugfix/r3.12-binary-padding
- [x] Require complete Objective-C method record strides before reading absolute or relative method fields.
— claimed: Afloat16, branch bugfix/r3.12-objc-method-record-stride-20261109
- [x] Keep Mach-O function starts within their declared payload.
— claimed: Afloat16, branch bugfix/r3.12-macho-function-starts-payload-boundary-20261105
- [x] Apply one indirect-import budget across all Mach-O sections and report when repeated table references exhaust it.
+1
View File
@@ -108,6 +108,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
| npm agents without Node on Windows | Programs are also looked for in nvm for Windows' version folders (`NVM_HOME`, else `%APPDATA%\nvm`, newest first, after `PATH`), so an npm shim such as `claude.cmd` finds Node when no `nvm use` link exists. When a shim's interpreter is still missing, starting it fails with `EngineMissing`, reason `interpreter_missing`, naming `node` and `nvm use`, instead of Rust's "batch file arguments are invalid" | On Windows 10 (Rust 1.99.1): unit tests for version-folder listing with nvm's own files beside the versions, the missing-interpreter check (no Node, Node on the search path, Node beside the shim) and the error's fields; the existing shim tests, including a real multi-line prompt through Node, and the platform crate's 70 tests pass. The failure was seen with Craft Anything 0.4.0 (Claude Code 1.0.96 npm shim, nvm with no version in use). Not checked: the fixed desktop app or CLI on that machine, and a build through a shim found only in nvm's folders |
| Mach-O Objective-C pointers | Chained offsets mapped only into file-empty `__PAGEZERO` reach the image-base fallback; pointer candidates must be backed by file bytes | Synthetic thin and fat Mach-O regression, plain-pointer and authenticated-rebase controls, null and unmapped-pointer checks. Real binaries were not checked for this correction |
| Swift existentials (`apple_swift_symbols_demangle`) | A protocol in a protocol list (`…_p`) is read as the mangling writes it, a context and a name with no `P` kind letter, so an existential such as `Swift.Error`, `__C.NSLocking` or a composition `A & B` decodes. The protocol takes no substitution slot. Class-bound lists (`Xl`) and existential metatypes (`Xp`) are still reported invalid, and an optional composition prints without parentheses (`A & B?` for `(A & B)?`) | Integration test on seven symbols from macOS system binaries. 133,643 distinct Swift symbols from the system apps, frameworks and `/usr/libexec` of macOS 15.7 compared with `swift-demangle`: 6,275 of the 8,899 reported invalid as "expected a type" now decode (4,460 match exactly; 1,761 differ only in rendering this change leaves alone, such as the `(extension in …)` prefix; 34 also contain `SQ`, read as `ImplicitlyUnwrappedOptional` until the `Equatable` fix; 20 are optional compositions without parentheses), and no symbol that matched before changed. Giving the protocol a slot would change 119 symbols that match |
| Objective-C method record layouts | Absolute and relative method records require their complete 24-byte or 12-byte layout and the full declared stride to be backed before facts are published. Complete extended strides remain supported. | Undersized absolute/relative strides, incomplete backed records, and standard/extended controls preserving selectors, types and implementations. |
| Mach-O entry points | The `LC_MAIN` file offset maps through the segment whose file bytes hold it, so a segment with no file bytes (`__PAGEZERO`, or a `__DATA` of only zero-fill sections, which also records file offset 0) never takes the entry; an offset outside every segment's file bytes gives no entry | Synthetic regression. All 3,205 executable slices of 1,794 Mach-O files on macOS 15.7 (`/bin`, `/sbin`, `/usr/bin`, `/usr/sbin`, `/usr/libexec`, app bundles) match `otool -l` (`__TEXT` address plus `entryoff`, or the `LC_UNIXTHREAD` program counter); 270 slices in 153 files read the wrong address before |
| Swift symbol substitutions (`apple_swift_symbols_demangle`) | A later `A…` reference names the type the mangling means: an `Sg` optional and a `Qz`/`Qy` associated type take a substitution slot and an extension doesn't. Other rendering (no `(extension in …)` prefix, unsupported manglings reported invalid) is unchanged | Unit test on eight symbols from macOS system binaries. 133,643 distinct Swift symbols from the system apps, frameworks and `/usr/libexec` of macOS 15.7 compared with `swift-demangle`: 199 that named a wrong type now name the same types (35 match exactly; the rest differ only in rendering this change leaves alone, such as the `(extension in …)` prefix), 948 reported invalid now decode, and no symbol that matched before changed |
| Swift `Equatable` (`apple_swift_symbols_demangle`) | The standard type `SQ` reads as the protocol `Swift.Equatable`, as `swift-demangle` reads it in the `$s`, `$S` and `_T0` manglings, so `Equatable` conformance descriptors and witness tables decode and a requirement names `Swift.Equatable`. A spelled-out `ImplicitlyUnwrappedOptional` still prints as `T!` | Integration test on six symbols from macOS system binaries and four constructed ones checked with `swift-demangle`. 133,643 distinct Swift symbols from the system apps, frameworks and `/usr/libexec` of macOS 15.7 compared with `swift-demangle`: 1,773 reported invalid ("expected a protocol") now decode (1,759 match exactly; 14 differ only in the `(extension in …)` prefix), 3 that named `ImplicitlyUnwrappedOptional` now name `Equatable` (1 matches exactly; 2 differ only in that prefix and an `_:` label), and no symbol that matched before changed |