4 import KernelCollection
7 # The kernel has class OSObject and subclass KernelClass
8 # foo.kext sublclasses KernelClass to get Foo1, and subclasses that to get Foo2
9 # bar.kext sublclasses Foo1 to get Bar1, and subclasses that to get Bar2
11 # In KernelClass the vtable layout is:
12 # [ ..., foo() kernelClassUsed0() ]
14 # In Foo1, the layout is:
15 # [ ..., foo() kernelClass_RESERVED0(), foo1Used0(), foo1Used1() ]
17 # In Foo2, the layout is:
18 # [ ..., foo() kernelClass_RESERVED0(), foo1Used0(), foo1_RESERVED1(), foo1_RESERVED2(), foo1_RESERVED3() ]
20 # In Bar1, the layout is:
21 # [ ..., foo() kernelClass_RESERVED0(), foo1Used0(), foo1_RESERVED1(), foo1_RESERVED2(), foo1_RESERVED3() ]
23 # In Bar2, the layout is:
24 # [ ..., foo() kernelClass_RESERVED0(), foo1Used0(), foo1_RESERVED1(), foo1_RESERVED2(), foo1_RESERVED3() ]
26 # All kext's will end up getting the vtable entry after foo() patched to kernelClassUsed0()
27 # Foo2, Bar1, Bar2, will also get the vtable entry after foo1Used0() patched to foo1Used1()
29 def findSymbolVMAddr(kernel_cache
, dylib_index
, symbol_name
):
30 for symbol_and_addr
in kernel_cache
.dictionary()["dylibs"][dylib_index
]["global-symbols"]:
31 if symbol_and_addr
["name"] == symbol_name
:
32 return symbol_and_addr
["vmAddr"]
35 def findFixupVMAddr(kernel_cache
, fixup_name
):
36 for fixup_vmaddr
, fixup_target
in kernel_cache
.dictionary()["fixups"].iteritems():
37 if fixup_target
== fixup_name
:
41 def findPagableFixupVMAddr(kernel_cache
, dylib_index
, fixup_name
):
42 for fixup_vmaddr
, fixup_target
in kernel_cache
.dictionary()["dylibs"][dylib_index
]["fixups"].iteritems():
43 if fixup_target
== fixup_name
:
47 def findAuxFixupVMAddr(kernel_cache
, dylib_index
, fixup_name
):
48 for fixup_vmaddr
, fixup_target
in kernel_cache
.dictionary()["dylibs"][dylib_index
]["fixups"].iteritems():
49 if fixup_target
== fixup_name
:
53 def offsetVMAddr(vmAddr
, offset
):
54 het_int
= int(vmAddr
, 16)
55 het_int
= het_int
+ offset
56 return ''.join([ '0x', hex(het_int
).upper()[2:] ])
58 def check(kernel_cache
):
60 kernel_cache
.buildKernelCollection("x86_64", "/auxkc-pageablekc-vtable-patching-namespaces/main.kc", "/auxkc-pageablekc-vtable-patching-namespaces/main.kernel", "", [], [])
61 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/main.kc", ["-layout", "-arch", "x86_64"])
63 assert len(kernel_cache
.dictionary()["dylibs"]) == 1
64 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
66 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
67 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/main.kc", ["-symbols", "-arch", "x86_64"])
69 # From kernel, we want to know where the vtable is, and the foo() and kernelClassUsed0() slots in that vtable
71 kernelClassFooVMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X11KernelClass3fooEv")
73 print "kernelClassFooVMAddr: " + kernelClassFooVMAddr
75 # KernelClass::kernelClassUsed0()
76 kernelClassUsed0VMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X11KernelClass16kernelClassUsed0Ev")
78 print "kernelClassUsed0VMAddr: " + kernelClassUsed0VMAddr
82 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/main.kc", ["-fixups", "-arch", "x86_64"])
84 # In vtable for Foo, we match the entry for Foo::foo() by looking for its value on the RHS of the fixup
85 kernelFooFixupAddr
= findFixupVMAddr(kernel_cache
, "kc(0) + " + kernelClassFooVMAddr
+ " : pointer64")
87 print "kernelFooFixupAddr: " + kernelFooFixupAddr
88 # Then the following fixup should be to KernelClass::kernelClassUsed0()
89 kernelFooNextFixupAddr
= offsetVMAddr(kernelFooFixupAddr
, 8)
91 print "kernelFooNextFixupAddr: " + kernelFooNextFixupAddr
92 assert kernel_cache
.dictionary()["fixups"][kernelFooNextFixupAddr
] == "kc(0) + " + kernelClassUsed0VMAddr
+ " : pointer64"
94 # From this point on, the vmAddr for __ZN1X11KernelClass16kernelClassUsed0Ev is an offset in to kc(0)
95 # so we want to turn it from a vmAddr to vmOffset by subtracting the base address of 0x4000 which is on __HIB
96 kernelClassUsed0VMOffset
= offsetVMAddr(kernelClassUsed0VMAddr
, -0x4000)
98 print "kernelClassUsed0VMOffset: " + kernelClassUsed0VMOffset
100 # -----------------------------------------------------------
101 # Now build an pageable cache using the baseline kernel collection
102 kernel_cache
.buildPageableKernelCollection("x86_64", "/auxkc-pageablekc-vtable-patching-namespaces/pageable.kc", "/auxkc-pageablekc-vtable-patching-namespaces/main.kc", "/auxkc-pageablekc-vtable-patching-namespaces/extensions", ["com.apple.foo1", "com.apple.foo2"], [])
103 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/pageable.kc", ["-layout", "-arch", "x86_64"])
105 assert len(kernel_cache
.dictionary()["dylibs"]) == 2
106 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.foo1"
107 assert kernel_cache
.dictionary()["dylibs"][1]["name"] == "com.apple.foo2"
110 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
111 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/pageable.kc", ["-symbols", "-arch", "x86_64"])
113 # From foo1, find the vtable and its override of foo()
115 pageableFoo1FooVMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X4Foo13fooEv")
117 print "pageableFoo1FooVMAddr: " + pageableFoo1FooVMAddr
119 pageableFoo1FooUsed0VMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X4Foo19foo1Used0Ev")
121 print "pageableFoo1FooUsed0VMAddr: " + pageableFoo1FooUsed0VMAddr
123 pageableFoo1FooUsed1VMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X4Foo19foo1Used1Ev")
125 print "pageableFoo1FooUsed1VMAddr: " + pageableFoo1FooUsed1VMAddr
127 # From foo2, find the vtable and its override of foo()
129 pageableFoo2FooVMAddr
= findSymbolVMAddr(kernel_cache
, 1, "__ZN1X4Foo23fooEv")
131 print "pageableFoo2FooVMAddr: " + pageableFoo2FooVMAddr
132 # Also find Foo2::foo1Used0() as it overrides foo1Used0 from the superclass
133 pageableFoo2FooUsed0VMAddr
= findSymbolVMAddr(kernel_cache
, 1, "__ZN1X4Foo29foo1Used0Ev")
135 print "pageableFoo2FooUsed0VMAddr: " + pageableFoo2FooUsed0VMAddr
139 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/pageable.kc", ["-fixups", "-arch", "x86_64"])
140 kernel_cache
.dictionary()["fixups"] == "none"
143 # The vtable we have is [ ..., foo(), kernelClass_RESERVED0(), foo1Used0(), foo1Used1() ]
144 # and we want [ ..., foo(), kernelClassUsed0(), foo1Used0(), foo1Used1() ]
146 # In vtable for Foo1, we match the entry for Foo1::foo() by looking for its value on the RHS of the fixup
147 pageableFoo1FooFixupAddr
= findPagableFixupVMAddr(kernel_cache
, 0, "kc(1) + " + pageableFoo1FooVMAddr
)
149 print "pageableFoo1FooFixupAddr: " + pageableFoo1FooFixupAddr
151 # Then the following fixup should be to KernelClass::kernelClassUsed0()
152 pageableFoo1FooNextFixupAddr
= offsetVMAddr(pageableFoo1FooFixupAddr
, 8)
154 print "pageableFoo1FooNextFixupAddr: " + pageableFoo1FooNextFixupAddr
155 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][pageableFoo1FooNextFixupAddr
] == "kc(0) + " + kernelClassUsed0VMOffset
157 # Then we should have foo1Used0()
158 pageableFoo1FooNextFixupAddr
= offsetVMAddr(pageableFoo1FooFixupAddr
, 16)
160 print "pageableFoo1FooNextFixupAddr: " + pageableFoo1FooNextFixupAddr
161 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][pageableFoo1FooNextFixupAddr
] == "kc(1) + " + pageableFoo1FooUsed0VMAddr
163 # And then foo1Used1()
164 pageableFoo1FooNextFixupAddr
= offsetVMAddr(pageableFoo1FooFixupAddr
, 24)
166 print "pageableFoo1FooNextFixupAddr: " + pageableFoo1FooNextFixupAddr
167 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][pageableFoo1FooNextFixupAddr
] == "kc(1) + " + pageableFoo1FooUsed1VMAddr
170 # The vtable we have is [ ..., foo(), kernelClass_RESERVED0(), foo1Used0(), foo1_RESERVED1() ]
171 # and we want [ ..., foo(), kernelClassUsed0(), foo1Used0(), foo1Used1() ]
173 # In vtable for Foo2, we match the entry for Foo2::foo() by looking for its value on the RHS of the fixup
174 pageableFoo2FooFixupAddr
= findPagableFixupVMAddr(kernel_cache
, 1, "kc(1) + " + pageableFoo2FooVMAddr
)
176 print "pageableFoo2FooFixupAddr: " + pageableFoo2FooFixupAddr
178 # Then the following fixup should be to KernelClass::kernelClassUsed0()
179 pageableFoo2FooNextFixupAddr
= offsetVMAddr(pageableFoo2FooFixupAddr
, 8)
181 print "pageableFoo2FooNextFixupAddr: " + pageableFoo2FooNextFixupAddr
182 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][pageableFoo2FooNextFixupAddr
] == "kc(0) + " + kernelClassUsed0VMOffset
184 # Then we should have foo1Used0(), but Foo2 overrides that, so it should be the Foo2 implementation, not the Foo1 implementation
185 pageableFoo2FooNextFixupAddr
= offsetVMAddr(pageableFoo2FooFixupAddr
, 16)
187 print "pageableFoo2FooNextFixupAddr: " + pageableFoo2FooNextFixupAddr
188 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][pageableFoo2FooNextFixupAddr
] == "kc(1) + " + pageableFoo2FooUsed0VMAddr
190 # And then foo1Used1()
191 pageableFoo2FooNextFixupAddr
= offsetVMAddr(pageableFoo2FooFixupAddr
, 24)
193 print "pageableFoo2FooNextFixupAddr: " + pageableFoo2FooNextFixupAddr
194 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][pageableFoo2FooNextFixupAddr
] == "kc(1) + " + pageableFoo1FooUsed1VMAddr
197 # -----------------------------------------------------------
198 # Now build an aux cache using the baseline kernel collection
199 kernel_cache
.buildAuxKernelCollection("x86_64", "/auxkc-pageablekc-vtable-patching-namespaces/aux.kc", "/auxkc-pageablekc-vtable-patching-namespaces/main.kc", "/auxkc-pageablekc-vtable-patching-namespaces/pageable.kc", "/auxkc-pageablekc-vtable-patching-namespaces/extensions", ["com.apple.bar1", "com.apple.bar2"], [])
200 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/aux.kc", ["-layout", "-arch", "x86_64"])
202 assert len(kernel_cache
.dictionary()["dylibs"]) == 2
203 assert kernel_cache
.dictionary()["dylibs"][0]["name"] == "com.apple.bar1"
204 assert kernel_cache
.dictionary()["dylibs"][1]["name"] == "com.apple.bar2"
207 # Get the addresses for the symbols we are looking at. This will make it easier to work out the fixup slots
208 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/aux.kc", ["-symbols", "-arch", "x86_64"])
210 # From bar1, find the vtable and its override of foo()
212 auxBar1FooVMAddr
= findSymbolVMAddr(kernel_cache
, 0, "__ZN1X4Bar13fooEv")
214 print "auxBar1FooVMAddr: " + auxBar1FooVMAddr
216 # From bar2, find the vtable and its override of foo()
218 auxBar2FooVMAddr
= findSymbolVMAddr(kernel_cache
, 1, "__ZN4Bar23fooEv")
220 print "auxBar2FooVMAddr: " + auxBar2FooVMAddr
224 kernel_cache
.analyze("/auxkc-pageablekc-vtable-patching-namespaces/aux.kc", ["-fixups", "-arch", "x86_64"])
227 # The vtable we have is [ ..., foo(), kernelClass_RESERVED0(), foo1Used0(), foo1Used1() ]
228 # and we want [ ..., foo(), kernelClassUsed0(), foo1Used0(), foo1Used1() ]
230 # In vtable for Bar1, we match the entry for Bar1::foo() by looking for its value on the RHS of the fixup
231 auxBar1FooFixupAddr
= findAuxFixupVMAddr(kernel_cache
, 0, "kc(3) + " + auxBar1FooVMAddr
)
233 print "auxBar1FooFixupAddr: " + auxBar1FooFixupAddr
235 # Then the following fixup should be to KernelClass::kernelClassUsed0()
236 auxBar1FooNextFixupAddr
= offsetVMAddr(auxBar1FooFixupAddr
, 8)
238 print "auxBar1FooNextFixupAddr: " + auxBar1FooNextFixupAddr
239 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][auxBar1FooNextFixupAddr
] == "kc(0) + " + kernelClassUsed0VMOffset
241 # Then we should have foo1Used0() from Foo2 as it overrides it from Foo1
242 auxBar1FooNextFixupAddr
= offsetVMAddr(auxBar1FooFixupAddr
, 16)
244 print "auxBar1FooNextFixupAddr: " + auxBar1FooNextFixupAddr
245 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][auxBar1FooNextFixupAddr
] == "kc(1) + " + pageableFoo2FooUsed0VMAddr
247 # And then foo1Used1()
248 auxBar1FooNextFixupAddr
= offsetVMAddr(auxBar1FooFixupAddr
, 24)
250 print "auxBar1FooNextFixupAddr: " + auxBar1FooNextFixupAddr
251 assert kernel_cache
.dictionary()["dylibs"][0]["fixups"][auxBar1FooNextFixupAddr
] == "kc(1) + " + pageableFoo1FooUsed1VMAddr
254 # The vtable we have is [ ..., foo(), kernelClass_RESERVED0(), foo1Used0(), foo1_RESERVED1() ]
255 # and we want [ ..., foo(), kernelClassUsed0(), foo1Used0(), foo1Used1() ]
257 # In vtable for Foo2, we match the entry for Foo2::foo() by looking for its value on the RHS of the fixup
258 auxBar2FooFixupAddr
= findAuxFixupVMAddr(kernel_cache
, 1, "kc(3) + " + auxBar2FooVMAddr
)
260 print "auxBar2FooFixupAddr: " + auxBar2FooFixupAddr
262 # Then the following fixup should be to KernelClass::kernelClassUsed0()
263 auxBar2FooNextFixupAddr
= offsetVMAddr(auxBar2FooFixupAddr
, 8)
265 print "auxBar2FooNextFixupAddr: " + auxBar2FooNextFixupAddr
266 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][auxBar2FooNextFixupAddr
] == "kc(0) + " + kernelClassUsed0VMOffset
268 # Then we should have foo1Used0() from Foo2 as it overrides it from Foo1
269 auxBar2FooNextFixupAddr
= offsetVMAddr(auxBar2FooFixupAddr
, 16)
271 print "auxBar2FooNextFixupAddr: " + auxBar2FooNextFixupAddr
272 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][auxBar2FooNextFixupAddr
] == "kc(1) + " + pageableFoo2FooUsed0VMAddr
274 # And then foo1Used1()
275 auxBar2FooNextFixupAddr
= offsetVMAddr(auxBar2FooFixupAddr
, 24)
277 print "auxBar2FooNextFixupAddr: " + auxBar2FooNextFixupAddr
278 assert kernel_cache
.dictionary()["dylibs"][1]["fixups"][auxBar2FooNextFixupAddr
] == "kc(1) + " + pageableFoo1FooUsed1VMAddr
280 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-static -mkernel -nostdlib -Wl,-e,__start -Wl,-pie main.cpp kernel.cpp -Wl,-pagezero_size,0x0 -o main.kernel -Wl,-image_base,0x10000 -Wl,-segaddr,__HIB,0x4000 -Wl,-add_split_seg_info -Wl,-install_name,/usr/lib/swift/split.seg.v2.hack -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -Wl,-sectcreate,__LINKINFO,__symbolsets,SymbolSets.plist -Wl,-segprot,__LINKINFO,r--,r-- -std=c++11 -DKERNEL_USED=1
281 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-no_data_const foo1.cpp -o extensions/foo1.kext/foo1 -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -std=c++11 -DFOO1_USED0=1 -DFOO1_USED1=1
282 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-no_data_const foo2.cpp -o extensions/foo2.kext/foo2 -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -std=c++11 -DFOO1_USED0=1
283 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-data_const bar1.cpp -o extensions/bar1.kext/bar1 -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -std=c++11 -DFOO1_USED0=1
284 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-data_const bar2.cpp -o extensions/bar2.kext/bar2 -iwithsysroot /System/Library/Frameworks/Kernel.framework/Headers -std=c++11 -DFOO1_USED0=1