This repository was archived by the owner on Mar 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathBTXPCValidation.swift
More file actions
167 lines (152 loc) · 5.7 KB
/
BTXPCValidation.swift
File metadata and controls
167 lines (152 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Copyright (C) 2022 Marvin Häuser. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
//
import BTPreprocessor
import Foundation
import NSXPCConnectionAuditToken
import os.log
import SecCodeEx
internal enum BTXPCValidation {
static func protectService(connection: NSXPCConnection) {
if #available(macOS 13.0, *) {
connection.setCodeSigningRequirement(
requirementsTextFromId(identifier: BT_SERVICE_ID)
)
}
}
static func protectDaemon(connection: NSXPCConnection) {
if #available(macOS 13.0, *) {
connection.setCodeSigningRequirement(
requirementsTextFromId(identifier: BT_DAEMON_ID)
)
}
}
static func isValidClient(connection: NSXPCConnection) -> Bool {
var token = connection.auditToken
let tokenData = Data(
bytes: &token,
count: MemoryLayout.size(ofValue: token)
)
let attributes = [kSecGuestAttributeAudit: tokenData]
var code: SecCode? = nil
let codeStatus = SecCodeCopyGuestWithAttributes(
nil,
attributes as CFDictionary,
[],
&code
)
guard codeStatus == errSecSuccess, let code else {
return false
}
guard self.verifyCsStatus(code: code) else {
return false
}
let requirementText = self
.requirementsTextFromId(identifier: BT_APP_ID)
if #available(macOS 13.0, *) {
connection.setCodeSigningRequirement(requirementText)
return true
} else {
var requirement: SecRequirement? = nil
let reqStatus = SecRequirementCreateWithString(
requirementText as CFString,
[],
&requirement
)
guard reqStatus == errSecSuccess, let requirement else {
return false
}
let validStatus = SecCodeCheckValidity(
code,
[
.enforceRevocationChecks,
SecCSFlags(rawValue: kSecCSRestrictSidebandData),
SecCSFlags(rawValue: kSecCSStrictValidate),
],
requirement
)
return validStatus == errSecSuccess
}
}
private static func verifyCsStatus(code: SecCode) -> Bool {
var signInfo: CFDictionary? = nil
let infoStatus = SecCodeCopySigningInformationDynamic(
code,
[SecCSFlags(rawValue: kSecCSDynamicInformation)],
&signInfo
)
guard infoStatus == errSecSuccess else {
os_log("Failed to retrieve signing information")
return false
}
guard let signInfo = signInfo as? [String: AnyObject] else {
os_log("Signing information is nil")
return false
}
guard
let signStatus =
signInfo[kSecCodeInfoStatus as String] as? UInt32
else {
os_log("Failed to retrieve signature status")
return false
}
let codeStatus = SecCodeStatus(rawValue: signStatus)
//
// REF: https://blog.obdev.at/what-we-have-learned-from-a-vulnerability/index.html
// valid: Enforce a valid in-memory CS status.
// hard, kill: Disallow late loading of (malicious) code.
// restrict: Disallow unrestricted DTrace.
// enforcement: Enforce code signing for all executable memory.
// libraryValidation: Disallow loading of third-party libraries.
// runtime: Enforce Hardened Runtime.
//
var reqStatus: SecCodeStatus = [
.valid,
.hard,
.kill,
SecCodeStatus(rawValue: SecCodeSignatureFlags.restrict.rawValue),
SecCodeStatus(rawValue: SecCodeSignatureFlags.enforcement.rawValue),
SecCodeStatus(
rawValue: SecCodeSignatureFlags.libraryValidation.rawValue
),
SecCodeStatus(rawValue: SecCodeSignatureFlags.runtime.rawValue),
]
//
// Debugging the app may change its dynamic codesign properties.
//
if codeStatus.contains(.debugged) {
#if !DEBUG
os_log(
"Signature status constraints violated: Code has been debugged"
)
return false
#else
reqStatus.remove([.valid, .hard, .kill])
#endif
}
guard codeStatus.contains(reqStatus) else {
os_log(
"Signature status constraints violated: \(signStatus) vs \(reqStatus.rawValue)"
)
return false
}
return true
}
private static func requirementsTextFromId(identifier: String) -> String {
let debugText = "identifier \"" + identifier + "\"" +
" and anchor apple generic" +
" and certificate leaf[subject.CN] = \"" + BT_CODESIGN_CN + "\"" +
" and certificate 1[field.1.2.840.113635.100.6.2.1] /* exists */" +
" and !(entitlement[\"com.apple.security.cs.allow-dyld-environment-variables\"] /* exists */)" +
" and !(entitlement[\"com.apple.security.cs.disable-library-validation\"] /* exists */)" +
" and !(entitlement[\"com.apple.security.cs.allow-unsigned-executable-memory\"] /* exists */)" +
" and !(entitlement[\"com.apple.security.cs.allow-jit\"] /* exists */)"
#if DEBUG
return debugText
#else
return debugText +
" and !(entitlement[\"com.apple.security.get-task-allow\"] /* exists */)"
#endif
}
}