text
stringlengths
0
1.99k
private:
IOUSBDevice *fDevice;
};
````````````````````````````````````````````````````````````````````````````
The key methods (init, start, stop) are lifecycle hooks invoked by IOKit as
the driver is loaded, initialized, and terminated. Drivers on macOS are
Kernel Extensions (KEXTS[5]). The driver is activated using
OSKext::start()[6]. In real life, USB drivers are often a subclass of
IOUSBDevice or IOUSBInterface, not directly an IOService. The provider
parameter in start() is a pointer to the parent object in the IORegistry
tree (often a device nub such as IOUSBDevice). The fDevice is a reference
to the hardware device or logical service the driver manages.
========
--[ 1.1.4 - Matching
============================================================================
Matching in IOKit refers to the process of finding and loading the
appropriate driver for a detected device or service. When a new device,
such as a USB device, is detected, IOKit creates a nub (for example,
an IOUSBDevice object) in the IORegistry. IOKit then searches for drivers
whose matching dictionaries (IOKitPersonalities[7]) specify compatibility
with the nub, using a three-phase process: 
Class matching : eliminates drivers whose IOProviderClass[8] does not
match the nub's class
Passive matching : checks the remaining drivers' personalities for
properties in KEXT Info.plist (e.g., vendor, product ID) to further narrow
the candidates.
Active matching : for each remaining candidate, it calls the driver's 
probe()[9] method to verify compatibility and assign a score actively. 
````````````````````````````````````````````````````````````````````````````
[IORegistryEntry] (base class)
   |
   +-- [IOService] (abstract service/driver class)
         |
         +-- [IOUSBDevice] (nub created for the USB device)
               |
               +-- [MyUSBDriver] (driver matched and attached to the device)
````````````````````````````````````````````````````````````````````````````
The driver with the highest score is started and attached to the nub,
forming the provider-client relationship in the IORegistry tree.
===================
--[ 1.1.5 - IOKit Personalities
============================================================================
The most important for us is that we can use the matching APIs to find
the services we want to enumerate or fuzz — more on that in "1.3.1". Yet,
to do that, we need to know the name under which the service is registered
in the IORegistry. These can be found in IOKitPersonalities. Each key is
a potential service name, for instance, under IOClass or IOProviderClass:
````````````````````````````````````````````````````````````````````````````
<key>IOKitPersonalities</key>
<dict>
    <key>MyUSBDriver</key>
    <dict>
        <key>CFBundleIdentifier</key>
        <string>com.example.driver.MyUSBDriver</string>
        <key>IOClass</key>
        <string>MyUSBDriver</string>
        <key>IOProviderClass</key>
        <string>IOUSBDevice</string>
        <key>idVendor</key>
        <integer>0x05AC</integer>
        <key>idProduct</key>
        <integer>0x1234</integer>
    </dict>
</dict>
````````````````````````````````````````````````````````````````````````````
A single driver can have multiple personalities, enabling support for
different device types or hardware variants without needing separate
drivers.
=================
--[ 1.1.6 - Service Instances
============================================================================
Not all IOKit personality entries result in instantiated services.
A personality defined in a driver's Info.plist will only be matched and
loaded if the hardware or software conditions are met. For example, on
a Mac mini without an external monitor, any display-related personalities
will not be matched, and the corresponding services will not appear in the
IORegistry. It's also common for a single driver to have multiple
instantiated services when the associated hardware appears more than once,
such as with multiple monitors or input devices. Tools like ioscan[10] can
be used to list all instantiated services and often reveal multiple entries
with the same service name, such as IOThunderboltPort or IONetworkStack:
````````````````````````````````````````````````````````````````````````````
IOThunderboltPort
IOThunderboltSwitchType5
IOThunderboltPort
IONetworkStack
````````````````````````````````````````````````````````````````````````````
To interact with or fuzz a driver, at least one instance of the
corresponding service must be active in the IORegistry, allowing access to
the driver's code. It is discussed further in section "2.1.2".
============
--[ 1.2 - User Clients
============================================================================