text
stringlengths
0
1.99k
IOUSBHostInterface. All of them have the same Class IOUSBHostInterface:
````````````````````````````````````````````````````````````````````````````
IOUSBHostInterface  IOUSBHostInterface  AppleUSBHostFrameworkInterfaceClient
IOUSBHostInterface  HID I2C             AppleUSBHostFrameworkInterfaceClient
IOUSBHostInterface  IOUSBHostInterface  AppleUSBHostFrameworkInterfaceClient
````````````````````````````````````````````````````````````````````````````
However, we only see AppleUSBHostFrameworkInterfaceClient if we can
successfully establish a connection. The Spawn column shows the result of
calling selector 0 using IOServiceOpen. If the call fails due to an invalid
argument, the service remains accessible but requires different input
sizes. If it returns an unsupported function, selector 0 is unimplemented,
though higher selectors may still be valid. In both cases, we do not get
the User Client name. How to get it? In the first column, we have a Class
name. We can use it to find NewUserClient logic and, inside, the User
Client name. For our example, this is "IOUSBHostInterface::newUserClient":
````````````````````````````````````````````````````````````````````````````
IOReturn IOUSBHostInterface::newUserClient()
...
if (type == 0) {
    // Used when type == 0
    AppleUSBHostFrameworkInterfaceClient *client =
        new AppleUSBHostFrameworkInterfaceClient(...);
}
else if (type == 1 || type == 2) {
    // Used when type == 1 or 2
    AppleUSBHostInterfaceUserClient *client =
        new AppleUSBHostInterfaceUserClient(...);
}
````````````````````````````````````````````````````````````````````````````
In most cases, we will have only the Class name and no UC, so we have to
perform this short RE step manually. Additionally, we can benefit from this
by obtaining the Types quickly. As shown in the above example, there are
two UC classes that this driver supports: 0 and 1||2.
Another way of listing is IORegistry reader command:
````````````````````````````````````````````````````````````````````````````
ioreg -l | grep "IOUserClientClass"
ioreg -l | grep -i "IOClass"
ioreg -l | grep "com.apple.driver.AppleH11ANEInterface" -A 5
````````````````````````````````````````````````````````````````````````````
We can use it to list all registered UC classes, services, and essentially
anything we have learned here or found in the IOKit documentation. 
==============================
--[ 2.2.2 - Driver Hooking in Kernel Space
============================================================================
The complete way to analyze how drivers handle user-space interactions
— such as spawning user clients (newUserClient) or handling external method
calls — is to debug these Kernel functions directly. However, on Apple
Silicon, kernel debugging is effectively impossible, and writing a custom
KEXT to trace or hook into these paths would be a research project on its
own. However, we can use DTrace to list all instrumented IOKit functions in
the Kernel. It shows us some places where kernel methods are being used:
````````````````````````````````````````````````````````````````````````````
sudo dtrace -l | grep IOService
sudo dtrace -l | grep IOUserClient
sudo dtrace -l | grep NewUserClient
sudo dtrace -l | grep externalMethod
sudo dtrace -l | grep getTargetAndMethodForIndex
sudo dtrace -l | grep dispatchExternalMethod
````````````````````````````````````````````````````````````````````````````
It is handy as an additional source of information about services, but this
does not cover all places, as it only shows instrumented functions.
However, as we learned in the first part of this paper, ultimately,
everything is an IOService, so we can still use DTrace to trace system-wide
calls. For that, we need to disable SIP (System Integrity Protection),
which is required to use DTrace on macOS:
````````````````````````````````````````````````````````````````````````````
csrutil disable
# csrutil enable --without dtrace
````````````````````````````````````````````````````````````````````````````
Using the information below, we can trace the processes that spawn
UserClients, which, in fact, call IOServiceOpen in user space:
````````````````````````````````````````````````````````````````````````````
sudo dtrace -n '
fbt::*NewUserClient*:entry,
fbt::*newUserClient*:entry {
    printf("%s: PID=%d CMD=%s\n", probefunc, pid, execname);
}'
````````````````````````````````````````````````````````````````````````````
Example output:
````````````````````````````````````````````````````````````````````````````
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:entry
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:
PID=367 CMD=opendirectoryd
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:entry
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:
PID=609 CMD=Finder
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:entry
_ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient:
PID=339 CMD=configd
_ZN5IOGPU13newUserClientEP4taskPvjPP12IOUserClient:entry
_ZN5IOGPU13newUserClientEP4taskPvjPP12IOUserClient: PID=13910
CMD=com.apple.WebKit.GPU
````````````````````````````````````````````````````````````````````````````
Ultimately, we can create a script that dumps names and demangles
functions, allowing us to see which process calls which UserClients.
I prepared dtrace_NewUserClient.py[34], which example output is below:
````````````````````````````````````````````````````````````````````````````