Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

fix(prefix): check Winetricks against its pin right before it runs

raymondchinsbf6d69576c11
diff --git a/app/Sources/BridgeplayApp/Model/PrefixTools.swift b/app/Sources/BridgeplayApp/Model/PrefixTools.swift
index 0feee8e..c258045 100644
--- a/app/Sources/BridgeplayApp/Model/PrefixTools.swift
+++ b/app/Sources/BridgeplayApp/Model/PrefixTools.swift
@@ -304,13 +304,16 @@ enum PrefixTools {
)
return try winetricks(
verbs, in: prefix, script: script,
- runner: SupportPaths.clonedRoot(forBuild: chosen.build), flavor: chosen.tool.flavor
+ runner: SupportPaths.clonedRoot(forBuild: chosen.build), flavor: chosen.tool.flavor,
+ sha256: winetricksSHA256
)
}
+ // sha256: the pin the script must still match when it runs. The cached copy is the
+ // user's to write, so it is read once, checked, and the checked bytes are what runs.
static func winetricks(
_ verbs: [String], in prefix: WinePrefix, script: URL, runner: URL,
- flavor: CompatTool.Flavor = .fex
+ flavor: CompatTool.Flavor = .fex, sha256: String? = nil
) throws -> URL {
let step = String(localized: "Run Winetricks")
guard !verbs.isEmpty else {
@@ -341,9 +344,12 @@ enum PrefixTools {
// Winetricks reads the architecture from WINE_BIN, which would otherwise be the wrapper.
environment["WINE_BIN"] = loader.path(percentEncoded: false)
+ let checked = try sha256.map { try verifiedCopy(of: script, sha256: $0, step: step) }
+ defer { if let checked { try? FileManager.default.removeItem(at: checked.deletingLastPathComponent()) } }
+
let log = prefix.root.appending(path: winetricksLogName)
let status = try Shell.logged(
- "/bin/sh", [script.path(percentEncoded: false), "-q"] + verbs,
+ "/bin/sh", [(checked ?? script).path(percentEncoded: false), "-q"] + verbs,
environment: environment, to: log
)
// Until the wineserver exits, the prefix still counts as in use.
@@ -357,6 +363,33 @@ enum PrefixTools {
return log
}
+ // Reads the script once, checks those bytes against the pin and writes them to a new
+ // private folder, so what runs is what was checked.
+ static func verifiedCopy(of script: URL, sha256: String, step: String) throws -> URL {
+ let data: Data
+ do {
+ data = try Data(contentsOf: script)
+ } catch {
+ throw StepFailure(step: step, detail: error.localizedDescription)
+ }
+ guard Digest.sha256(of: data) == sha256 else {
+ AppLog.note("winetricks: \(script.path(percentEncoded: false)) no longer matches its pin")
+ try? FileManager.default.removeItem(at: script)
+ throw StepFailure(step: step, detail: String(localized: "The downloaded Winetricks changed after it was checked, so it was not run. Try again to download it afresh."))
+ }
+ let folder = FileManager.default.temporaryDirectory.appending(path: "bridgeplay-winetricks-\(UUID().uuidString)")
+ do {
+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true,
+ attributes: [.posixPermissions: 0o700])
+ let copy = folder.appending(path: "winetricks")
+ try data.write(to: copy, options: .withoutOverwriting)
+ return copy
+ } catch {
+ try? FileManager.default.removeItem(at: folder)
+ throw StepFailure(step: step, detail: error.localizedDescription)
+ }
+ }
+
static func arguments(for executable: URL) -> [String]? {
let path = executable.path(percentEncoded: false)
switch executable.pathExtension.lowercased() {
diff --git a/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift b/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift
index 0549dcc..54cfbbd 100644
--- a/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift
+++ b/app/Tests/BridgeplayAppTests/PrefixToolsTests.swift
@@ -292,6 +292,38 @@ struct PrefixToolsTests {
#expect(output.contains("bin \(unix.appending(path: "wine").path(percentEncoded: false))"))
}
+ // The script is hashed when downloaded but runs later from a cache the user can write to.
+ @Test("Winetricks runs only the bytes that match its pin")
+ func winetricksRunsOnlyThePinnedScript() throws {
+ let dir = FileManager.default.temporaryDirectory.appending(path: "np-winetricks-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let library = SteamLibrary(root: dir.appending(path: "library"))
+ let prefix = WinePrefix(appID: "2075800", name: "Game", library: library, lastUsed: nil)
+ let runner = dir.appending(path: "runner")
+ let unix = runner.appending(path: "lib/wine/x86_64-unix")
+ for folder in [prefix.pfx, unix, dir.appending(path: "cache")] {
+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
+ }
+ try Data("#!/bin/sh\nexit 0\n".utf8).write(to: unix.appending(path: "wine"))
+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: unix.appending(path: "wine").path(percentEncoded: false))
+ let script = dir.appending(path: "cache/winetricks")
+ let pinned = Data("#!/bin/sh\necho pinned\n".utf8)
+ try pinned.write(to: script)
+ let pin = Digest.sha256(of: pinned)
+
+ let log = try PrefixTools.winetricks(["vcrun2022"], in: prefix, script: script, runner: runner, sha256: pin)
+ #expect(try String(contentsOf: log, encoding: .utf8).contains("pinned"))
+ try FileManager.default.removeItem(at: log)
+
+ try Data("#!/bin/sh\necho swapped\n".utf8).write(to: script)
+ let failure = try #require(throws: StepFailure.self) {
+ try PrefixTools.winetricks(["vcrun2022"], in: prefix, script: script, runner: runner, sha256: pin)
+ }
+ #expect(failure.detail.contains("changed after it was checked"))
+ #expect(!FileManager.default.fileExists(atPath: log.path(percentEncoded: false)), "the swapped script ran")
+ #expect(!FileManager.default.fileExists(atPath: script.path(percentEncoded: false)), "the swapped script stays cached")
+ }
+
@Test("Every kind offered by the file picker is a kind that can be run")
func pickerTypesMatchWhatRuns() {
let extensions = PrefixTools.runnableTypes.compactMap(\.preferredFilenameExtension)