How to access classes/functions that are contained in a kext (Objective-C)

142 Views Asked by At

Kernel extensions are able to execute code at kernel levels but also make kernel level data available to higher level things, right? That's how Intel PowerGadget accesses kernel level info, because it has a kext that it pulls that info from.

My question is, How would I access info from a kext via my own Objective-C code? Can I access a function? Or a class? How would I do such a thing?

1

There are 1 best solutions below

0
pmdj On

Modern macOS kernel extensions (when you absolutely must use them - for many types of driver, there are now alternatives in DriverKit or user space) use the I/O Kit. Using the IOKit user space framework, you can access the I/O Kit services implemented by your kext.

  • Reading properties is not restricted, so this is an easy way to share data from the kernel with user space.
  • Setting properties from user space needs to be explicitly opted into by overriding the setProperties() function in your kext's IOService subclass. (While taking extreme care to only allow setting "safe" properties and validating all inputs.)
  • Your kext can provide a fully-fledged user space API by implementing an IOUserClient subclass - the keys here are that calling IOServiceOpen from user space invokes the newUserClient method in your IOService subclass (which can be overridden or by default returns an instance of the class specified by the IOUserClientClass property in your Info.plist's IOKitPersonality), and that when you call one of the IOConnectCall* functions from user space, this calls the externalMethod function in your IOUserClient subclass.