Catch Supersign analysis exceptions and improve Superdump ipc protocol

This commit is contained in:
kabiroberai
2020-04-04 21:08:08 +05:30
committed by Kabir Oberai
Unverified
parent 9cba2f0055
commit bd18ec9f3e
3 changed files with 28 additions and 24 deletions
@@ -87,7 +87,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
var entitlements: Entitlements
// if the executable doesn't already have entitlements, that's
// okay. We don't have to throw an error.
if let entitlementsData = context.signerImpl.analyze(executable: executableURL),
if let entitlementsData = try? context.signerImpl.analyze(executable: executableURL),
let decodedEntitlements = try? PropertyListDecoder().decode(Entitlements.self, from: entitlementsData) {
entitlements = decodedEntitlements
@@ -17,14 +17,10 @@ public struct EntitlementContainer: ProtoCodableContainer {
// allows access to a parsable entitlements list while not losing unparsed entitlements when it's modified
public struct Entitlements {
public enum Error: Swift.Error {
case dataCorrupted
}
private let decoder = PropertyListDecoder()
private let encoder = PropertyListEncoder()
private var dict: [String: PlistItem]
private var dict: [String: AnyEncodable]
public init(entitlements: [Entitlement]) throws {
dict = [:]
@@ -49,9 +45,7 @@ public struct Entitlements {
let newEnts = Dictionary(uniqueKeysWithValues: new.map { (type(of: $0).identifier, $0) })
oldEnts.keys.filter { newEnts[$0] == nil }.forEach { dict[$0] = nil }
try newEnts.forEach {
dict[$0] = try PlistItem(value: $1.plistValue(withEncoder: encoder))
}
newEnts.forEach { dict[$0] = AnyEncodable($1) }
}
public mutating func setEntitlements(_ new: [Entitlement]) throws {
@@ -64,7 +58,8 @@ extension Entitlements: Codable {
public init(from decoder: Decoder) throws {
self.dict = try decoder.singleValueContainer()
.decode([String: PlistItem].self)
.decode([String: CodableElement].self)
.mapValues(AnyEncodable.init)
}
public func encode(to encoder: Encoder) throws {
+23 -14
View File
@@ -20,6 +20,7 @@ public struct SignerImpl {
public enum Error: LocalizedError {
case notFound
case badFilePath
case signer(String?)
public var errorDescription: String? {
@@ -28,6 +29,10 @@ public struct SignerImpl {
return NSLocalizedString(
"signer_impl.error.not_found", value: "No signer implementation found", comment: ""
)
case .badFilePath:
return NSLocalizedString(
"signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: ""
)
case .signer(let error?):
return error
case .signer(nil):
@@ -48,13 +53,12 @@ public struct SignerImpl {
analyze = signer.analyze
}
public static func all() -> [SignerImpl] {
CLinkedList(start: signer_list).map(SignerImpl.init)
private static func all() -> AnySequence<SignerImpl> {
.init(CLinkedList(first: signer_list).lazy.map(SignerImpl.init))
}
public static func first() throws -> SignerImpl {
guard let impl = all().first else { throw Error.notFound }
return impl
try all().makeIterator().next().orThrow(Error.notFound)
}
private func _sign(
@@ -68,10 +72,11 @@ public struct SignerImpl {
encoder.outputFormat = .xml
let entsArray: [entitlements_data_t] = try entitlementMapping.map { url, ents in
try url.withUnsafeFileSystemRepresentation { bundlePath in
try encoder.encode(ents).withUnsafeBytes { bytes in
guard let bundlePath = bundlePath else { throw Error.badFilePath }
return try encoder.encode(ents).withUnsafeBytes { bytes in
let bound = bytes.bindMemory(to: Int8.self)
return entitlements_data_t(
bundle_path: bundlePath, data: bound.baseAddress, len: bound.count
bundle_path: bundlePath, data: bound.baseAddress!, len: bound.count
)
}
}
@@ -87,11 +92,11 @@ public struct SignerImpl {
guard sign(
app.path,
certBound.baseAddress,
certBound.baseAddress!,
certBound.count,
privBound.baseAddress,
privBound.baseAddress!,
privBound.count,
ents.baseAddress,
ents.baseAddress!,
ents.count,
progress,
&exception
@@ -127,13 +132,17 @@ public struct SignerImpl {
}
}
public func analyze(executable: URL) -> Data? {
executable.withUnsafeFileSystemRepresentation({ (path: UnsafePointer<Int8>?) -> Data? in
public func analyze(executable: URL) throws -> Data {
try executable.withUnsafeFileSystemRepresentation { (path: UnsafePointer<Int8>?) -> Data in
var exception: UnsafeMutablePointer<Int8>?
defer { exception.map { free($0) } }
var len = 0
guard let out = analyze(path, &len) else { return nil }
guard let out = analyze(path!, &len, &exception) else {
throw Error.signer(exception.map { String(cString: $0) })
}
defer { free(out) }
return Data(bytes: UnsafeRawPointer(out), count: len)
})
return Data(bytes: out, count: len)
}
}
}