dropdb
dropdb

Reputation: 656

Running kextstat inside Swift app doesn't work

I'm trying to run the following shell script that check if a kext is loaded in a Swift app.

kextstat -l

When I run it from xcrun swift, it works fine. However it doesn't work inside an app.

It returns an internal error:

Error Domain=KMErrorDomain Code=71 "Kernel request failed: (libkern/kext) internal error (-603947007)" UserInfo={NSLocalizedDescription=Kernel request failed: (libkern/kext) internal error (-603947007)}

My app is not sandboxed. How can I do this?

CODE

import Foundation
func getKextStatus(_ kextID: String) -> Bool {
    do {
        return try shell("/usr/sbin/kextstat", ["-l"]).contains(kextID)
    } catch {
        return false
    }
}
func shell(_ file: String, _ args: [String]) throws -> String {
    let task = Process()
    let pipe = Pipe()
    
    task.standardOutput = pipe
    task.standardError = pipe
    task.arguments = args
    task.executableURL = URL(fileURLWithPath: file)

    do {
        try task.run()
    }
    catch { throw error }
    
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8)!
    
    return output
}

// example
getKextStatus("com.apple.Dont_Steal_Mac_OS_X")

I'm running macOS Monterey 12.1, Xcode 13

Upvotes: 1

Views: 103

Answers (1)

pmdj
pmdj

Reputation: 23428

My best guess is your app is sandboxed, and you're running afoul of the sandboxing restrictions. Try without sandboxing and see if that changes things.

In any case, shelling out like this is rather fragile. The KextManager API is intended for getting information about kexts from user code.

Upvotes: 0

Related Questions