3

I need to unload a bunch of driver (kext) at the beginning of my Qt Desktop application on Mac OS. I tried with QProcess, but kextunload requires to have admin privileges. Anyone knows a workaround? Or how to start a QProcess with sudo? I need this to be easy for the end user : one only has to enter the admin password when prompted and the application does the rest.

The problem is apple loading their own driver on a device I want to use with specific driver (FTDI232H with FT2Dxx driver).

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
AloyseTech
  • 41
  • 4
  • Possible duplicate of [How to launch a QProcess with root rights?](https://stackoverflow.com/questions/5625870/how-to-launch-a-qprocess-with-root-rights) – Mohammad Kanan Jun 02 '18 at 21:01

2 Answers2

0

I have found something that seems to work for me :

QString password = "yourRootPassword"; //could be asked with QInputDialog::getText(...)
QString cmd = QString("sudo -S kextunload -b %1 > /dev/null").arg(driverName);
FILE *pipe = popen(cmd.toStdString().c_str(), "w");
if(pipe != nullptr)
{
    fprintf(pipe, "%s\n", password.toStdString().c_str());
    if (ferror(pipe))
    {
        qDebug() << "Failed to write to pipe";
    }
    else
    {
        qDebug() << "Written to pipe";
    }
}
else
{
   qDebug() << "Failed to open pipe";
}
qDebug() << "Pipe returned : " << pclose(pipe);
AloyseTech
  • 41
  • 4
0

Unless the kext you are trying to load or unload has specifically been marked as being loadable by unprivileged users, you will definitely need to have root privileges. There's a C API for loading and unloading kexts. It's defined in <libkern/OSKext.h>, and the functions you're after are probably:

  • OSKextCreate()
  • OSKextLoadWithOptions()
  • OSKextUnloadKextWithIdentifier()

The recommended method for calling functions with elevated privileges is to install a privileged helper tool using SMJobBless(), and to launch it and communicate with it via XPC. This way, the user's admin password is only required once, during the SMJobBless call, and it also works for users where sudo doesn't work. (sudo only works if the user is in the wheel group, and only if they have a user password set.)

pmdj
  • 22,018
  • 3
  • 52
  • 103