Skip to content

Commit 53dc47e

Browse files
authored
fix: properly parse dylib trie for mach-o (VirusTotal#517)
There was an inconsistency in how we were parsing the export trie when it came to weak definitions vs normal definitions. The diff shows where the parsing logic went wrong. - added a new test file: `a7f31b44e44700104f1780aca996f094e91f3669fc6f99c8b7047e26ec44c168` - verified we parse the exports properly with [`ipsw`](https://github.com/blacktop/ipsw) From `ipsw`: ``` Symtab ------ 0x00000c000: (__TEXT,__const) non-external (was a private external) _objc_trampolinesVersionString 0x00000c038: (__TEXT,__const) non-external (was a private external) _objc_trampolinesVersionNumber 0x000004000: (__TEXT,__text) external __objc_blockTrampolineImpl 0x000008000: (__TEXT,__text) external __objc_blockTrampolineImpl_stret 0x000007ff8: (__TEXT,__text) external __objc_blockTrampolineLast 0x00000bff8: (__TEXT,__text) external __objc_blockTrampolineLast_stret 0x000004020: (__TEXT,__text) external __objc_blockTrampolineStart 0x000008020: (__TEXT,__text) external __objc_blockTrampolineStart_stret Dyld Exports Trie ----------------- 0x000004000: regular __objc_blockTrampolineImpl 0x000008000: regular __objc_blockTrampolineImpl_stret 0x000007ff8: regular __objc_blockTrampolineLast 0x00000bff8: regular __objc_blockTrampolineLast_stret 0x000004020: regular __objc_blockTrampolineStart 0x000008020: regular __objc_blockTrampolineStart_stret ``` From our parsing: ``` symtab: symoff: 53424 nsyms: 8 stroff: 53552 strsize: 248 entries: - "_objc_trampolinesVersionString" - "_objc_trampolinesVersionNumber" - "__objc_blockTrampolineImpl" - "__objc_blockTrampolineImpl_stret" - "__objc_blockTrampolineLast" - "__objc_blockTrampolineLast_stret" - "__objc_blockTrampolineStart" - "__objc_blockTrampolineStart_stret" exports: - "__objc_blockTrampolineStart" - "__objc_blockTrampolineStart_stret" - "__objc_blockTrampolineLast" - "__objc_blockTrampolineLast_stret" - "__objc_blockTrampolineImpl" - "__objc_blockTrampolineImpl_stret" ``` Closes VirusTotal#512
1 parent e098445 commit 53dc47e

4 files changed

Lines changed: 180 additions & 3 deletions

lib/src/modules/macho/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ const _N_PBUD: u8 = 0xc; /* prebound undefined (defined in a dylib) */
6363
const N_INDR: u8 = 0xa; /* indirect */
6464

6565
/// Mach-O export flag constants
66-
const EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u64 = 0x00000004;
66+
const _EXPORT_SYMBOL_FLAGS_KIND_REGULAR: u64 = 0x00000000;
67+
const _EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u64 = 0x00000004;
6768
const EXPORT_SYMBOL_FLAGS_REEXPORT: u64 = 0x00000008;
6869
const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: u64 = 0x00000010;
6970

@@ -1213,11 +1214,10 @@ impl<'a> MachOFile<'a> {
12131214

12141215
remaining_data = remainder;
12151216
}
1216-
EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION => {
1217+
_ => {
12171218
let (remainder, _offset) = uleb128(remainder)?;
12181219
remaining_data = remainder;
12191220
}
1220-
_ => {}
12211221
}
12221222
}
12231223

lib/src/modules/macho/tests/testdata/3c7879d0b6419b39f9a3ea6372576c25152d9bbc9edafe4953e3eb8ee3a89bad.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,24 +2752,37 @@ exports:
27522752
- "_MsoSmtpAddressCopyContactList"
27532753
- "_MsoSearchContactList"
27542754
- "_MsoReleaseContact"
2755+
- "_MsoReleaseContactList"
2756+
- "_MsoReleaseContactCategory"
27552757
- "_MsoReleaseAddressBookResources"
27562758
- "_MsoNameCopyContactList"
27572759
- "_MsoMeContactCopy"
27582760
- "_MsoLaunchAddressBook"
2761+
- "_MsoLaunchAddressBookEmail"
27592762
- "_MsoFCloseAddressBook"
27602763
- "_MsoEmailAddressCopyContactList"
27612764
- "_MsoContactListCreateAddressBookOPF"
27622765
- "_MsoContactListCreateAddressBookMac"
27632766
- "_MsoContactListCreateAddressBookAll"
27642767
- "_MsoContactListCopy"
2768+
- "_MsoContactListCopyID"
2769+
- "_MsoContactListCopyContactList"
27652770
- "_MsoContactFieldValueInt"
27662771
- "_MsoContactFieldValueDataCopy"
27672772
- "_MsoContactFieldValueCopy"
27682773
- "_MsoContactCopy"
2774+
- "_MsoContactCopyID"
2775+
- "_MsoContactCopyFromID"
2776+
- "_MsoContactCopyContact"
2777+
- "_MsoContactCopyCategoryList"
27692778
- "_MsoContactCategoryNameCopy"
27702779
- "_MsoContactCategoryCopy"
2780+
- "_MsoContactCategoryCopyID"
2781+
- "_MsoContactCategoryCopyFromID"
27712782
- "_MsoCapabilitiesContactList"
27722783
- "_MsoCContact"
2784+
- "_MsoCContactList"
2785+
- "_MsoCContactCategory"
27732786
- "_MsoABTerm"
27742787
imports:
27752788
- "_ABCopyArrayOfAllGroups"
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
magic: 0xcffaedfe
2+
cputype: 0x1000007
3+
cpusubtype: 0x3
4+
filetype: 6
5+
ncmds: 15
6+
sizeofcmds: 712
7+
flags: 0x2100085
8+
reserved: 0
9+
number_of_segments: 2
10+
source_version: "951.1.0.0.0"
11+
symtab:
12+
symoff: 53424
13+
nsyms: 8
14+
stroff: 53552
15+
strsize: 248
16+
entries:
17+
- "_objc_trampolinesVersionString"
18+
- "_objc_trampolinesVersionNumber"
19+
- "__objc_blockTrampolineImpl"
20+
- "__objc_blockTrampolineImpl_stret"
21+
- "__objc_blockTrampolineLast"
22+
- "__objc_blockTrampolineLast_stret"
23+
- "__objc_blockTrampolineStart"
24+
- "__objc_blockTrampolineStart_stret"
25+
nlists:
26+
- n_strx: 186
27+
n_type: 30
28+
n_sect: 2
29+
n_desc: 0
30+
n_value: 49152
31+
- n_strx: 217
32+
n_type: 30
33+
n_sect: 2
34+
n_desc: 0
35+
n_value: 49208
36+
- n_strx: 4
37+
n_type: 15
38+
n_sect: 1
39+
n_desc: 0
40+
n_value: 16384
41+
- n_strx: 31
42+
n_type: 15
43+
n_sect: 1
44+
n_desc: 0
45+
n_value: 32768
46+
- n_strx: 64
47+
n_type: 15
48+
n_sect: 1
49+
n_desc: 0
50+
n_value: 32760
51+
- n_strx: 91
52+
n_type: 15
53+
n_sect: 1
54+
n_desc: 0
55+
n_value: 49144
56+
- n_strx: 124
57+
n_type: 15
58+
n_sect: 1
59+
n_desc: 0
60+
n_value: 16416
61+
- n_strx: 152
62+
n_type: 15
63+
n_sect: 1
64+
n_desc: 0
65+
n_value: 32800
66+
dysymtab:
67+
ilocalsym: 0
68+
nlocalsym: 2
69+
iextdefsym: 2
70+
nextdefsym: 6
71+
tocoff: 8
72+
ntoc: 0
73+
modtaboff: 0
74+
nmodtab: 0
75+
extrefsymoff: 0
76+
nextrefsyms: 0
77+
indirectsymoff: 0
78+
nindirectsyms: 0
79+
extreloff: 0
80+
nextrel: 0
81+
locreloff: 0
82+
nlocrel: 0
83+
code_signature_data:
84+
dataoff: 53808
85+
datasize: 18768
86+
segments:
87+
- segname: "__TEXT"
88+
vmaddr: 0x0
89+
vmsize: 0xd000
90+
fileoff: 0
91+
filesize: 53248
92+
maxprot: 0x5
93+
initprot: 0x5
94+
nsects: 2
95+
flags: 0x0
96+
sections:
97+
- segname: "__TEXT"
98+
sectname: "__text"
99+
addr: 0x1000
100+
size: 0xb000
101+
offset: 4096
102+
align: 12
103+
reloff: 0
104+
nreloc: 0
105+
flags: 0x80000400
106+
reserved1: 0
107+
reserved2: 0
108+
reserved3: 0
109+
- segname: "__TEXT"
110+
sectname: "__const"
111+
addr: 0xc000
112+
size: 0x40
113+
offset: 49152
114+
align: 4
115+
reloff: 0
116+
nreloc: 0
117+
flags: 0x0
118+
reserved1: 0
119+
reserved2: 0
120+
reserved3: 0
121+
- segname: "__LINKEDIT"
122+
vmaddr: 0xd000
123+
vmsize: 0x8000
124+
fileoff: 53248
125+
filesize: 19328
126+
maxprot: 0x1
127+
initprot: 0x1
128+
nsects: 0
129+
flags: 0x0
130+
dylibs:
131+
- name: "/usr/lib/libobjc-trampolines.dylib"
132+
timestamp: 1 # 1970-01-01 00:00:01 UTC
133+
compatibility_version: "1.0.0"
134+
current_version: "228.0.0"
135+
- name: "/usr/lib/libSystem.B.dylib"
136+
timestamp: 2 # 1970-01-01 00:00:02 UTC
137+
compatibility_version: "1.0.0"
138+
current_version: "1356.0.0"
139+
certificates:
140+
- issuer: "C=US, O=Apple Inc., OU=Apple Certification Authority, CN=Apple Root CA"
141+
subject: "C=US, O=Apple Inc., OU=Apple Certification Authority, CN=Apple Code Signing Certification Authority"
142+
is_self_signed: false
143+
- issuer: "C=US, O=Apple Inc., OU=Apple Certification Authority, CN=Apple Code Signing Certification Authority"
144+
subject: "C=US, O=Apple Inc., OU=Apple Software, CN=Software Signing"
145+
is_self_signed: false
146+
- issuer: "C=US, O=Apple Inc., OU=Apple Certification Authority, CN=Apple Root CA"
147+
subject: "C=US, O=Apple Inc., OU=Apple Certification Authority, CN=Apple Root CA"
148+
is_self_signed: true
149+
uuid: "32DA9B89-2C83-393D-B006-8257D61FD9D9"
150+
build_version:
151+
platform: 6
152+
minos: "26.2.0"
153+
sdk: "26.2.0"
154+
ntools: 1
155+
tools:
156+
- tool: 3
157+
version: "1230.3"
158+
exports:
159+
- "__objc_blockTrampolineStart"
160+
- "__objc_blockTrampolineStart_stret"
161+
- "__objc_blockTrampolineLast"
162+
- "__objc_blockTrampolineLast_stret"
163+
- "__objc_blockTrampolineImpl"
164+
- "__objc_blockTrampolineImpl_stret"

0 commit comments

Comments
 (0)