Skip to content

Commit 7ea0dd9

Browse files
authored
Verify the libarchive payload (#181)
The libarchive that is used to make a swiftly release for Linux is downloaded directly from the libarchive project on GitHub from a release artifact. In theory, this release could become tampered in the future. Typically package managers get around this problem by both getting a specific version of the package, and also keep a hash/git commit to verify the contents. Add a content check using an expected SHA-256 sum of the release source tarball of libarchive to help protect against any tampering of the release in the future.
1 parent 94ec29c commit 7ea0dd9

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Tools/build-swiftly-release/BuildSwiftlyRelease.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,19 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
298298
let make = try await self.assertTool("make", message: "Please install make with `yum install make`")
299299
let git = try await self.assertTool("git", message: "Please install git with `yum install git`")
300300
let strip = try await self.assertTool("strip", message: "Please install strip with `yum install binutils`")
301+
let sha256sum = try await self.assertTool("sha256sum", message: "Please install sha256sum with `yum install coreutils`")
301302

302303
let swift = try await self.checkSwiftRequirement()
303304

304305
try await self.checkGitRepoStatus(git)
305306

306-
// Build a specific version of libarchive
307+
// Start with a fresh SwiftPM package
308+
try runProgram(swift, "package", "reset")
309+
310+
// Build a specific version of libarchive with a check on the tarball's SHA256
307311
let libArchiveVersion = "3.7.4"
312+
let libArchiveTarSha = "7875d49596286055b52439ed42f044bd8ad426aa4cc5aabd96bfe7abb971d5e8"
313+
308314
let buildCheckoutsDir = FileManager.default.currentDirectoryPath + "/.build/checkouts"
309315
let libArchivePath = buildCheckoutsDir + "/libarchive-\(libArchiveVersion)"
310316
let pkgConfigPath = libArchivePath + "/pkgconfig"
@@ -314,6 +320,11 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
314320

315321
try? FileManager.default.removeItem(atPath: libArchivePath)
316322
try runProgram(curl, "-o", "\(buildCheckoutsDir + "/libarchive-\(libArchiveVersion).tar.gz")", "--remote-name", "--location", "https://github.com/libarchive/libarchive/releases/download/v\(libArchiveVersion)/libarchive-\(libArchiveVersion).tar.gz")
323+
let libArchiveTarShaActual = try await runProgramOutput(sha256sum, "\(buildCheckoutsDir)/libarchive-\(libArchiveVersion).tar.gz")
324+
guard let libArchiveTarShaActual, libArchiveTarShaActual.starts(with: libArchiveTarSha) else {
325+
let shaActual = libArchiveTarShaActual ?? "none"
326+
throw Error(message: "The libarchive tar.gz file sha256sum is \(shaActual), but expected \(libArchiveTarSha)")
327+
}
317328
try runProgram(tar, "--directory=\(buildCheckoutsDir)", "-xzf", "\(buildCheckoutsDir)/libarchive-\(libArchiveVersion).tar.gz")
318329

319330
let cwd = FileManager.default.currentDirectoryPath
@@ -350,8 +361,6 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
350361

351362
FileManager.default.changeCurrentDirectoryPath(cwd)
352363

353-
try runProgram(swift, "package", "clean")
354-
355364
// Statically link standard libraries for maximum portability of the swiftly binary
356365
try runProgram(swift, "build", "--product=swiftly", "--pkg-config-path=\(pkgConfigPath)/lib/pkgconfig", "--static-swift-stdlib", "--configuration=release")
357366

0 commit comments

Comments
 (0)