|
| 1 | +import CoreFoundation |
1 | 2 | import CryptoKit |
2 | 3 | import Darwin |
3 | 4 | import Foundation |
@@ -42,21 +43,65 @@ enum AgentPeerSigning { |
42 | 43 | identifier: identifier, leaf: leaf, expectedHash: expectedHash) |
43 | 44 | } |
44 | 45 |
|
45 | | - /// "identifier:team" of a validly signed, team-identified running program. |
46 | | - /// Looked up by PID, so it narrows who may reuse an approved key but is not |
47 | | - /// by itself proof of identity; the key signature is. |
48 | | - static func teamSignedIdentity(pid: pid_t) -> String? { |
| 46 | + /// Only a directly launching app whose signed bundle explicitly declares ownership |
| 47 | + /// of the MCP transport can have durable approval. An unregistered launcher can |
| 48 | + /// still ask for approval for one live helper. |
| 49 | + static func registeredHost(pid: pid_t) -> String? { |
| 50 | + guard let child = process(pid), child.pbi_ppid > 1 else { return nil } |
| 51 | + let parentPID = pid_t(child.pbi_ppid) |
| 52 | + guard let parent = process(parentPID), let runningPath = executablePath(parentPID) else { return nil } |
| 53 | + |
49 | 54 | var guest: SecCode? |
50 | 55 | var code: SecStaticCode? |
51 | 56 | var info: CFDictionary? |
52 | | - guard SecCodeCopyGuestWithAttributes(nil, [kSecGuestAttributePid as String: pid] as CFDictionary, [], &guest) == errSecSuccess, |
53 | | - let guest, SecCodeCheckValidity(guest, [], nil) == errSecSuccess, |
| 57 | + guard SecCodeCopyGuestWithAttributes(nil, [kSecGuestAttributePid as String: parentPID] as CFDictionary, |
| 58 | + [], &guest) == errSecSuccess, let guest, |
| 59 | + SecCodeCheckValidity(guest, [], nil) == errSecSuccess, |
54 | 60 | SecCodeCopyStaticCode(guest, [], &code) == errSecSuccess, let code, |
55 | 61 | SecCodeCopySigningInformation(code, SecCSFlags(rawValue: kSecCSSigningInformation), &info) == errSecSuccess, |
56 | | - let details = info as? [String: Any], |
| 62 | + let details = info as? [String: Any], hardened(details), |
57 | 63 | let identifier = details[kSecCodeInfoIdentifier as String] as? String, |
58 | | - let team = details[kSecCodeInfoTeamIdentifier as String] as? String, !team.isEmpty else { return nil } |
59 | | - return identifier + ":" + team |
| 64 | + !identifier.isEmpty, identifier.utf8.count <= 255, |
| 65 | + let plist = details[kSecCodeInfoPList as String] as? [String: Any], |
| 66 | + plist["CFBundlePackageType"] as? String == "APPL", |
| 67 | + plist["CFBundleIdentifier"] as? String == identifier, |
| 68 | + let optedIn = plist["CodebasicTTSDirectMCPHost"] as? NSNumber, |
| 69 | + CFGetTypeID(optedIn) == CFBooleanGetTypeID(), optedIn.boolValue, |
| 70 | + let main = (details[kSecCodeInfoMainExecutable as String] as? URL)?.resolvingSymlinksInPath(), |
| 71 | + plist["CFBundleExecutable"] as? String == main.lastPathComponent, |
| 72 | + main.path == runningPath, |
| 73 | + main.deletingLastPathComponent().lastPathComponent == "MacOS", |
| 74 | + main.deletingLastPathComponent().deletingLastPathComponent().lastPathComponent == "Contents", |
| 75 | + main.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().pathExtension == "app", |
| 76 | + let certificates = details[kSecCodeInfoCertificates as String] as? [SecCertificate], |
| 77 | + let certificate = certificates.first, |
| 78 | + let currentChild = process(pid), currentChild.pbi_ppid == child.pbi_ppid, |
| 79 | + sameProcess(child, currentChild), |
| 80 | + let currentParent = process(parentPID), sameProcess(parent, currentParent), |
| 81 | + executablePath(parentPID) == runningPath, |
| 82 | + SecCodeCheckValidity(guest, [], nil) == errSecSuccess else { return nil } |
| 83 | + let signer = AgentBytes.hash(SecCertificateCopyData(certificate) as Data) |
| 84 | + return AgentPairing.registeredHostPrefix + "\(signer):\(identifier)" |
| 85 | + } |
| 86 | + |
| 87 | + private static func process(_ pid: pid_t) -> proc_bsdinfo? { |
| 88 | + var info = proc_bsdinfo() |
| 89 | + let size = Int32(MemoryLayout<proc_bsdinfo>.size) |
| 90 | + guard proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size) == size, |
| 91 | + info.pbi_uid == geteuid(), info.pbi_ruid == getuid(), info.pbi_start_tvsec > 0 else { return nil } |
| 92 | + return info |
| 93 | + } |
| 94 | + |
| 95 | + private static func sameProcess(_ lhs: proc_bsdinfo, _ rhs: proc_bsdinfo) -> Bool { |
| 96 | + lhs.pbi_pid == rhs.pbi_pid && lhs.pbi_start_tvsec == rhs.pbi_start_tvsec |
| 97 | + && lhs.pbi_start_tvusec == rhs.pbi_start_tvusec |
| 98 | + } |
| 99 | + |
| 100 | + private static func executablePath(_ pid: pid_t) -> String? { |
| 101 | + var path = [CChar](repeating: 0, count: Int(MAXPATHLEN) * 4) |
| 102 | + guard proc_pidpath(pid, &path, UInt32(path.count)) > 0, |
| 103 | + let value = String(validatingUTF8: path) else { return nil } |
| 104 | + return URL(fileURLWithPath: value).resolvingSymlinksInPath().path |
60 | 105 | } |
61 | 106 |
|
62 | 107 | private static func signedHash(_ code: SecStaticCode) -> Data? { |
|
0 commit comments