text
stringlengths
0
1.99k
The key permission required for a sandboxed app to use IOConnectCallMethod
is "iokit-open-user-client." Note that unsandboxed malware does not have
such restrictions, and the remainder of the article discusses the context
of an unsandboxed app.
=================
--[ 1.3.1 - Service Discovery
============================================================================
User space applications can use matching dictionaries to find services
based on properties like IOProviderClass, IONameMatch[22], or custom
attributes. IOServiceGetMatchingServices()[23] searches the registry and
returns matching IOService objects:
````````````````````````````````````````````````````````````````````````````
CFDictionaryRef matching = IOServiceMatching("IOService");
result = IOServiceGetMatchingServices(masterPort, matching, &iterator);
````````````````````````````````````````````````````````````````````````````
However, this method iterates only over the root services that directly
inherit from the IOService class. To explore deeper into the hierarchy and
access all services, use a recursive iterator:
````````````````````````````````````````````````````````````````````````````
io_iterator_t iter;
io_service_t service_ref;
// Create an iterator for the IOService plane, recursively
const kern_return_t kr = IORegistryCreateIterator(
kIOMainPortDefault,
kIOServicePlane,
kIORegistryIterateRecursively,
&iter
);
// Iterate over every service in the plane
while ((service_ref = IOIteratorNext(iter)) != MACH_PORT_NULL) {
````````````````````````````````````````````````````````````````````````````
Let's say we want to find driver named "NS_01", we can use
IORegistryEntryGetName[24]:
````````````````````````````````````````````````````````````````````````````
char name_buf[128];
if (IORegistryEntryGetName(service_ref, name_buf) == KERN_SUCCESS) {
if (strcmp(name_buf, "NS_01") == 0) {
// Found target service - use service_ref for IOServiceOpen()
found_service_ref = service_ref;
break;
}
}
IOObjectRelease(service_ref);
}
````````````````````````````````````````````````````````````````````````````
This provides a service handle ready for communication.
====================
--[ 1.3.2 - Spawning User Client
============================================================================
Once a target service is located, we can use IOServiceOpen()[25] to
create an IOUserClient instance for communication:
````````````````````````````````````````````````````````````````````````````
io_connect_t connection;
kern_return_t result = IOServiceOpen(
found_service_ref, // service from discovery
mach_task_self(), // current task
0, // user client type
&connection // returned connection handle
);
````````````````````````````````````````````````````````````````````````````
The service validates the request and instantiates the appropriate
IOUserClient subclass, returning a connection handle for method calls. On
the kernel side, this is handled by SERVICE_NAME::newUserClient functions.
=======================
--[ 1.3.3 - Calling External Method
============================================================================
Finally, we can use IOConnectCallMethod() to invoke the functionality
we want through the established connection. Although there is no direct
kernel memory access, this exposure can still introduce vulnerabilities
that may lead to kernel code execution[26].
````````````````````````````````````````````````````````````````````````````
uint64_t input_scalar = 0x1234;
uint64_t output_scalar = 0;
uint32_t output_count = 1;
result = IOConnectCallMethod(
connection, // connection handle
5, // selector (method index)
&input_scalar, // scalar inputs
1, // scalar input count
NULL, // struct input buffer
0, // struct input size
&output_scalar, // scalar outputs
&output_count, // scalar output count
NULL, // struct output buffer
NULL // struct output size
);
````````````````````````````````````````````````````````````````````````````
The output from the external method, if any, is received through the
structure of the output buffer and scalar outputs, while the status code is
stored in the result. It's important to note that IOConnectCallMethod is
the most commonly used function; however, there are other similar methods,
all of which begin with IOConnectCall*[27].
===========================