Search code examples
c++macosiokitkernel-extensionmach-o

Some kext member functions must be redefined, to avoid unresolved symbols


TL;DR
A subclass is reimplementing (redefining) a virtual function of the superclass (base class) in the scope of the superclass, because the dynamic loader requires it to do so. It doesn't make any sense to me.

Example:

class IO80211Controller : public IOEthernetController
{
    virtual IOReturn enablePacketTimestamping(); // Implemented in binary, I can see the disassembly.
};

// .cpp - Redefinition with superclass namespace.
IOReturn IO80211Controller::enablePacketTimestamping()
{
    return kIOReturnUnsupported; // This is from the disassembly of IO80211Controller
}

The above isn't the real header, I hope it's close to what it should be - no header is available.

// .hpp
class AirPortBrcm4331 : public IO80211Controller
{
    // Subclass stuff goes here
};

// .cpp - Redefinition with superclass namespace.
IOReturn IO80211Controller::enablePacketTimestamping()
{
    return kIOReturnUnsupported; // This is from the disassembly of AirPortBrcm4331
}

Background
I'm researching IO80211Family.kext (which there are no headers available for), and IO80211Controller class in particular - I'm in the process of reversing the header so it will be possible to inherit from this class and create custom 802.11 drivers.

Discovering the problem
IO80211Controller defines many virtual member functions, which I need to declare on my reversed header file. I created a header file with all virtual functions (extracted from IO80211Controller's vtable) and used it for my subclass.

When loading my new kext (with the subclass), there were linking errors:

kxld[com.osxkernel.MyWirelessDriver]: The following symbols are unresolved for this kext:
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::enableFeature(IO80211FeatureCode, void*)
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::flowIdSupported()
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::apple80211_ioctl(IO80211Interface*, __ifnet*, unsigned long, void*)
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::enablePacketTimestamping()
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::hardwareOutputQueueDepth(IO80211Interface*)
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::disablePacketTimestamping()
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::performCountryCodeOperation(IO80211Interface*, IO80211CountryCodeOp)
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::requiresExplicitMBufRelease()
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::_RESERVEDIO80211Controllerless7()
kxld[com.osxkernel.MyWirelessDriver]: IO80211Controller::stopDMA()
Link failed (error code 5).

The reversed header of the superclass contains over 50 virtual member functions, so if there were any linking problems, I would assume it would be a all-or-nothing. When adding a simple implementation to these functions (using superclass namespace) the linking errors are gone.

Two questions arise

  1. How can multiple implementations of the same functions co-exist? They both live in the kernel address space.
  2. What makes these specific functions so special, while the other 50 are ok without a weird reimplementing demand?

Hypothesis
I can't answer the first question, but I have started a research about the second.
I looked into the IO80211Family mach-o symbol table, and all the functions with the linking error don't contain the N_EXT bit in their type field - meaning they are not external symbols, while the other functions do contain the N_EXT bit.

I wasn't sure how this affects the loading kext procedure, so I dived into XNU source and looked for the kext loading code. There's a major player in here called vtable patching, which might shed some light on my first question.
Anyway, there's a predicate function called kxld_sym_is_unresolved which checks whether a symbol is unresolved. kxld calls this function on all symbols, to verify they are all ok.

boolean_t
kxld_sym_is_unresolved(const KXLDSym *sym)
{
    return ((kxld_sym_is_undefined(sym) && !kxld_sym_is_replaced(sym)) ||
            kxld_sym_is_indirect(sym) || kxld_sym_is_common(sym));
}

This function result in my case comes down to the return value of kxld_sym_is_replaced, which simply checks if the symbol has been patched (vtable patching), I don't understand well enough what is it and how it affects me...

The Grand Question
Why Apple chose these functions to not be external? are they implying that they should be implemented by others -- and others, why same scope as superclass? I jumped into the source to find answer to this but didn't. This is what most disturbs me - it doesn't follow my logic. I understand that a full comprehensive answer is probably too complicated, so at least help me understand, on a higher level, what's going on here, what's the logic behind not letting the subclass get the implementation of these specific functions, in such a weird way (why not pure abstract)?

Thank you so much for reading this!


Solution

  • The immediate explanation is indeed that the symbols are not exported by the IO80211 kext. The likely reason behind this however is that the functions are implemented inline, like so:

    class IO80211Controller : public IOEthernetController
    {
        //...
    
        virtual IOReturn enablePacketTimestamping()
        {
            return kIOReturnUnsupported;
        }
        //...
    };
    

    For example, if I build this code:

    #include <cstdio>
    
    class MyClass
    {
    public:
            virtual void InlineVirtual() { printf("MyClass::InlineVirtual\n"); }
            virtual void RegularVirtual();
    };
    
    void MyClass::RegularVirtual()
    {
            printf("MyClass::RegularVirtual\n");
    }
    
    int main()
    {
            MyClass a;
            a.InlineVirtual();
            a.RegularVirtual();
    }
    

    using the command

    clang++ -std=gnu++14 inline-virtual.cpp -o inline-virtual
    

    and then inspect the symbols using nm:

    $ nm ./inline-virtual
    0000000100000f10 t __ZN7MyClass13InlineVirtualEv
    0000000100000e90 T __ZN7MyClass14RegularVirtualEv
    0000000100000ef0 t __ZN7MyClassC1Ev
    0000000100000f40 t __ZN7MyClassC2Ev
    0000000100001038 S __ZTI7MyClass
    0000000100000faf S __ZTS7MyClass
    0000000100001018 S __ZTV7MyClass
                     U __ZTVN10__cxxabiv117__class_type_infoE
    0000000100000000 T __mh_execute_header
    0000000100000ec0 T _main
                     U _printf
                     U dyld_stub_binder
    

    You can see that MyClass::InlineVirtual has hidden visibility (t), while MyClass::RegularVirtual is exported (T). The implementation for a function declared as inline (either explicitly with the keyword or implicitly by placing it inside the class definition) must be provided in all compilation units that call it, so it makes sense that they wouldn't have external linkage.