From 9c8c34f454037414d1d8dbe6705a5ac2bae050bb Mon Sep 17 00:00:00 2001 From: Lam Wei Lun Date: Fri, 13 Nov 2026 17:02:34 +0800 Subject: [PATCH] Pin the update publisher and key the install identity by root The helper accepted any trusted Authenticode signer on Windows and any Developer ID signature on macOS, so once an update installs without asking, nothing said whose build it was. The signer display name and the Apple team ID are compiled in now and come from the CI signing material: the Windows publisher is read from the certificate that signs the installer, so the two cannot drift. An empty value keeps the old behaviour with a warning, which is what a local build and an unsigned PR artifact want. installed_artifact was one value per user, so two copies of the application - a Program Files install and a per-user one, or two channels - overwrote each other's digest and each read the other's identity. It is keyed by a hash of the install root now, and a value recorded before the change moves to the running root on first start. finish_pending_update() only ran when the preset updater existed, so a G-code viewer start never restored a backup or consumed the install record. It runs from every start. The macOS helper mounted the image on a path named after its pid in world-writable /tmp, which another local user could create first. mkdtemp names it, inside the cache. --- .github/workflows/build_orca.yml | 8 +++ src/CMakeLists.txt | 7 ++ src/libslic3r/AppConfig.hpp | 25 +++++-- src/libslic3r/UpdateHandoff.cpp | 22 ++++++ src/libslic3r/UpdateHandoff.hpp | 4 ++ src/slic3r/GUI/GUI_App.cpp | 13 +++- src/slic3r/UpdateHelper/apply_macos.cpp | 37 ++++++++-- src/slic3r/UpdateHelper/apply_windows.cpp | 88 ++++++++++++++++++++++- tests/libslic3r/update_handoff_tests.cpp | 31 ++++++++ version.inc | 17 +++++ 10 files changed, 236 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build_orca.yml b/.github/workflows/build_orca.yml index 87de726d7e..6a1aec6181 100644 --- a/.github/workflows/build_orca.yml +++ b/.github/workflows/build_orca.yml @@ -34,6 +34,8 @@ jobs: ubuntu-ver: '2404' ubuntu-ver-str: '_Ubuntu2404' ORCA_UPDATER_SIG_KEY: ${{ secrets.ORCA_UPDATER_SIG_KEY }} + # The team the updater requires of a downloaded build; the same one that signs it. + ORCA_UPDATER_TEAM_ID: ${{ secrets.TEAM_ID }} NSIS_VERSION: '3.13' NSIS_SHA256: BA63DFFC4410EE89193E1CB5A41989991BD77C61068DA17E3156D136B7B0B3D8 JSIGN_VERSION: '7.6' @@ -505,6 +507,12 @@ jobs: "ORCA_SIGN_JSIGN=$jar" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8 "ORCA_SIGN_CERTFILE=$cert" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8 "ORCA_SIGN_KMS_KEY=$env:KMS_KEY" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8 + # The publisher the updater pins is the leaf certificate's simple name, which is + # what CertGetNameStringW(CERT_NAME_SIMPLE_DISPLAY_TYPE) reports to the helper. + $leaf = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromPemFile($cert) + $publisher = $leaf.GetNameInfo([System.Security.Cryptography.X509Certificates.X509NameType]::SimpleName, $false) + if (-not $publisher) { throw "The signing certificate has no simple name to pin." } + "ORCA_UPDATER_PUBLISHER=$publisher" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8 - name: Set up Java for code signing if: steps.codesign.outcome == 'success' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9be2148dc9..68235cb738 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -272,6 +272,13 @@ if (SLIC3R_GUI AND (WIN32 OR APPLE)) target_compile_definitions(OrcaSlicerUpdater PRIVATE -DwxDEBUG_LEVEL=0) endif () + # The publisher the downloaded artifact must be signed by: the Authenticode signer's + # display name on Windows, the Apple Developer Team ID on macOS. Both empty disable + # the check with a warning, which is what a local build wants. + target_compile_definitions(OrcaSlicerUpdater PRIVATE + "ORCA_UPDATER_PUBLISHER=\"${ORCA_UPDATER_PUBLISHER}\"" + "ORCA_UPDATER_TEAM_ID=\"${ORCA_UPDATER_TEAM_ID}\"") + target_link_libraries(OrcaSlicerUpdater libslic3r cereal::cereal ${wxWidgets_LIBRARIES} libcurl OpenSSL::SSL OpenSSL::Crypto) if (WIN32) # libcurl's peer.c calls if_nametoindex, which lives in iphlpapi. diff --git a/src/libslic3r/AppConfig.hpp b/src/libslic3r/AppConfig.hpp index 6bf194d999..c7acae01da 100644 --- a/src/libslic3r/AppConfig.hpp +++ b/src/libslic3r/AppConfig.hpp @@ -12,6 +12,7 @@ #include "libslic3r/Config.hpp" #include "libslic3r/Semver.hpp" +#include "libslic3r/UpdateHandoff.hpp" #include "calib.hpp" #define ENV_DEV_HOST "0" @@ -380,12 +381,26 @@ public: std::string update_channel() const { return this->get("update_channel"); } void set_update_channel(const std::string& channel) { this->set("update_channel", channel); } - // Identity of the build that is installed: the artifact digest it came from - // and the channel it came from. Empty until the first in-app update. - std::string installed_artifact() const { return this->get("installed_artifact"); } + // Identity of the build that is installed: the artifact digest it came from and + // the channel it came from. Empty until the first in-app update. + std::string installed_artifact(const std::string& install_root) const + { return this->get(install_identity_key(install_root)); } std::string installed_channel() const { return this->get("installed_channel"); } - void set_installed_build(const std::string& channel, const std::string& artifact) - { this->set("installed_channel", channel); this->set("installed_artifact", artifact); } + void set_installed_build(const std::string& install_root, const std::string& channel, const std::string& artifact) + { this->set("installed_channel", channel); this->set(install_identity_key(install_root), artifact); } + // Moves a build recorded before the identity was keyed by install root into this root's slot. + void migrate_installed_artifact(const std::string& install_root) + { + const std::string legacy = this->get("installed_artifact"); + if (legacy.empty()) + return; + const std::string key = install_identity_key(install_root); + if (this->get(key).empty()) + this->set(key, legacy); + // The keyed slot is authoritative from here on, or the unkeyed value + // would be claimed by whichever copy of the application starts next. + this->erase("app", "installed_artifact"); + } // Artifact digest the user asked not to be offered again (nightly channel). std::string skip_artifact() const { return this->get("skip_artifact"); } diff --git a/src/libslic3r/UpdateHandoff.cpp b/src/libslic3r/UpdateHandoff.cpp index c452ba8db2..aa9ea1e454 100644 --- a/src/libslic3r/UpdateHandoff.cpp +++ b/src/libslic3r/UpdateHandoff.cpp @@ -278,6 +278,28 @@ std::string updater_log_path() return (fs::path(cache_root()) / "updater.log").string(); } +std::string install_identity_key(const std::string& install_root) +{ + std::string normalised = install_root; + while (normalised.size() > 1 && (normalised.back() == '/' || normalised.back() == '\\')) + normalised.pop_back(); +#ifdef _WIN32 + for (char& c : normalised) + if (c >= 'A' && c <= 'Z') + c = static_cast(c - 'A' + 'a'); +#endif + uint64_t hash = 14695981039346656037ull; + for (const char c : normalised) { + hash ^= static_cast(static_cast(c)); + hash *= 1099511628211ull; + } + static const char* const hex = "0123456789abcdef"; + std::string key = "installed_artifact:"; + for (int shift = 60; shift >= 0; shift -= 4) + key.push_back(hex[(hash >> shift) & 0xfu]); + return key; +} + DownloadPrep prepare_download_dir(const std::string& dir, const std::string& asset_name, const std::string& digest, diff --git a/src/libslic3r/UpdateHandoff.hpp b/src/libslic3r/UpdateHandoff.hpp index 8749a5ef09..b3dc3856e5 100644 --- a/src/libslic3r/UpdateHandoff.hpp +++ b/src/libslic3r/UpdateHandoff.hpp @@ -76,6 +76,10 @@ std::string updater_pending_path(); std::string updater_lock_path(); std::string updater_log_path(); +// Key of the app config entry recording one install's installed digest. The path is +// hashed because AppConfig keys cannot hold one: they reject whitespace. +std::string install_identity_key(const std::string& install_root); + // The state of a partial download in `dir` for `asset_name`, after the directory has been // reduced to it. The artifact's digest is recorded beside the .part, so a later run only // resumes a download started for that same build: a nightly reuses one asset name and an diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index b595d7fad2..d216b27003 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1129,6 +1129,10 @@ void GUI_App::post_init() BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " sync_user_preset: false"; } + // Restore a backup and consume installed.json before anything else: the G-code viewer + // never runs the block below, so it would otherwise never finish an interrupted install. + CallAfter([this] { this->finish_pending_update(); }); + // The extra CallAfter() is needed because of Mac, where this is the only way // to popup a modal dialog on start without screwing combo boxes. // This is ugly but I honestly found no better way to do it. @@ -1145,7 +1149,6 @@ void GUI_App::post_init() this->preset_updater->sync(http_url, language, network_ver, sys_preset ? preset_bundle : nullptr); } - this->finish_pending_update(); this->check_new_version_sf(); if (!cw_showed) this->check_legacy_user_filament_presets(false); @@ -6548,7 +6551,8 @@ void GUI_App::check_new_version_sf(bool show_tips, int by_user) // Read on the UI thread: the response is resolved on the Http worker thread. const UpdateChannel channel = update_channel_from_key(app_config->update_channel()); - const std::string installed_artifact = app_config->installed_artifact(); + const std::string install_root = app_install_root(); + const std::string installed_artifact = app_config->installed_artifact(install_root); const std::string installed_channel = app_config->installed_channel(); const std::string skip_version = app_config->get("skip_version"); const std::string skip_artifact = app_config->skip_artifact(); @@ -6986,13 +6990,16 @@ void GUI_App::start_in_app_update() void GUI_App::finish_pending_update() { set_updater_cache_dir(update_cache_dir()); + app_config->migrate_installed_artifact(app_install_root()); // The record is shared by every copy of the app in this cache. Only the copy it names // acts on it; the others leave it in place. InstallRecord record; const bool has_record = read_install_record(record) && !record.target.empty() && running_from(record.target); if (has_record) - app_config->set_installed_build(record.channel, record.digest); + // Under the running root, not the recorded spelling: the two name the same install + // on disk but build different keys. + app_config->set_installed_build(app_install_root(), record.channel, record.digest); // A live helper owns the install lock and may still roll back onto the backup. // Holding the lock here also stops a new install from starting while this runs. diff --git a/src/slic3r/UpdateHelper/apply_macos.cpp b/src/slic3r/UpdateHelper/apply_macos.cpp index 44ff697c6c..252e259508 100644 --- a/src/slic3r/UpdateHelper/apply_macos.cpp +++ b/src/slic3r/UpdateHelper/apply_macos.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include +#include #include "libslic3r/UpdateHandoff.hpp" @@ -267,15 +269,23 @@ bool apply_macos_install(const UpdateHandoff& handoff, const std::string& artifa remove_path_quietly(staged); + // A private mount point: a name built from the pid alone in a world-writable + // /tmp could be pre-created by another local user before the update runs. + const std::string mount_parent = handoff.cache_dir.empty() ? std::string("/tmp") : handoff.cache_dir; + std::string mount_template = "/tmp/orca-updater-XXXXXX"; + if (!handoff.cache_dir.empty()) { + boost::system::error_code mkdir_ec; + boost::filesystem::create_directories(boost::filesystem::path(mount_parent), mkdir_ec); + mount_template = (boost::filesystem::path(mount_parent) / "mount.XXXXXX").string(); + } + MountGuard guard; - guard.mount = "/tmp/orca-updater-" + pid; ScratchGuard scratch_guard{parent_writable ? std::string() : scratch}; - boost::system::error_code mkdir_ec; - boost::filesystem::create_directories(boost::filesystem::path(guard.mount), mkdir_ec); - if (!boost::filesystem::is_directory(boost::filesystem::path(guard.mount), mkdir_ec)) { - error = "The update could not be prepared: " + guard.mount + " could not be created."; + if (::mkdtemp(mount_template.data()) == nullptr) { + error = "The update could not be prepared: a mount point in " + mount_parent + " could not be created."; return false; } + guard.mount = mount_template; int status = 0; if (!spawn_and_wait({"/usr/bin/hdiutil", "attach", "-nobrowse", "-readonly", "-mountpoint", guard.mount, artifact_path}, @@ -301,6 +311,23 @@ bool apply_macos_install(const UpdateHandoff& handoff, const std::string& artifa error = "The new version's signature could not be verified, so nothing was changed."; return false; } + // A valid signature alone proves nothing about who signed it: pin the team. +#if defined(ORCA_UPDATER_TEAM_ID) + const std::string team_id = ORCA_UPDATER_TEAM_ID; +#else + const std::string team_id; +#endif + if (team_id.empty()) { + BOOST_LOG_TRIVIAL(warning) << "The updater was built without a signing team, so the new version's developer is not checked."; + } else { + const std::string requirement = "=anchor apple generic and certificate leaf[subject.OU] = \"" + team_id + "\""; + if (!spawn_and_wait({"/usr/bin/codesign", "--verify", "--strict", "--deep", "-R", requirement, mounted_app}, 300, status, error)) + return false; + if (status != 0) { + error = "The new version is signed by a different developer, so nothing was changed."; + return false; + } + } if (!spawn_and_wait({"/usr/sbin/spctl", "-a", "-t", "exec", mounted_app}, 300, status, error)) return false; if (status != 0) { diff --git a/src/slic3r/UpdateHelper/apply_windows.cpp b/src/slic3r/UpdateHelper/apply_windows.cpp index 43944dd40f..faf5f07196 100644 --- a/src/slic3r/UpdateHelper/apply_windows.cpp +++ b/src/slic3r/UpdateHelper/apply_windows.cpp @@ -15,6 +15,7 @@ #include #if defined(_MSC_VER) + #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "wintrust.lib") #endif @@ -23,6 +24,7 @@ #include #include #include + #include #include #include @@ -53,15 +55,63 @@ std::wstring widen_utf8(const std::string& text) return out; } +std::string narrow_utf8(const std::wstring& text) +{ + if (text.empty()) + return std::string(); + const int needed = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, nullptr, 0, nullptr, nullptr); + if (needed <= 0) + return std::string(); + std::string out(static_cast(needed), '\0'); + ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, out.data(), needed, nullptr, nullptr); + out.resize(static_cast(needed) - 1); + return out; +} + // Handoff paths travel as UTF-8; boost::filesystem on Windows takes UTF-16. fs::path to_path(const std::string& utf8) { return fs::path(widen_utf8(utf8)); } -// Checks that the downloaded installer carries a valid Authenticode signature. -// Publisher pinning needs the expected certificate subject from CI and is a -// follow-up; this only proves the file is intact and signed by someone trusted. +// The display name of the certificate that signed the embedded PKCS#7 message. +// Empty when the message or the certificate cannot be read. +std::wstring signer_display_name(HCERTSTORE store, HCRYPTMSG message) +{ + DWORD signer_size = 0; + if (!::CryptMsgGetParam(message, CMSG_SIGNER_INFO_PARAM, 0, nullptr, &signer_size) || signer_size == 0) + return std::wstring(); + + std::vector buffer(signer_size); + if (!::CryptMsgGetParam(message, CMSG_SIGNER_INFO_PARAM, 0, buffer.data(), &signer_size)) + return std::wstring(); + const auto* signer = reinterpret_cast(buffer.data()); + + // CERT_FIND_SUBJECT_CERT keys on the issuer and serial number of the signer. + CERT_INFO cert_info; + ::ZeroMemory(&cert_info, sizeof(cert_info)); + cert_info.Issuer = signer->Issuer; + cert_info.SerialNumber = signer->SerialNumber; + + PCCERT_CONTEXT cert = ::CertFindCertificateInStore(store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, + CERT_FIND_SUBJECT_CERT, &cert_info, nullptr); + if (cert == nullptr) + return std::wstring(); + + const DWORD size = ::CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, nullptr, 0); + std::wstring name; + if (size > 1) { + name.resize(static_cast(size) - 1); + if (::CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, name.data(), size) == 0) + name.clear(); + } + ::CertFreeCertificateContext(cert); + return name; +} + +// Checks that the downloaded installer carries a valid Authenticode signature +// by the pinned publisher. An empty ORCA_UPDATER_PUBLISHER accepts any trusted +// signer with a warning, which is what a local build wants. bool verify_signature(const std::string& artifact_path, std::string& error) { const std::wstring file = widen_utf8(artifact_path); @@ -100,6 +150,38 @@ bool verify_signature(const std::string& artifact_path, std::string& error) "), so it was not run."; return false; } + + // WinVerifyTrust accepts any trusted signer, so the signer identity is read + // from the embedded PKCS#7 message and compared with the pinned publisher. + const std::wstring expected = widen_utf8(ORCA_UPDATER_PUBLISHER); + if (expected.empty()) { + BOOST_LOG_TRIVIAL(warning) << "The update publisher is not pinned in this build; any trusted signer is accepted."; + return true; + } + + HCERTSTORE store = nullptr; + HCRYPTMSG message = nullptr; + DWORD encoding = 0; + DWORD content = 0; + DWORD format = 0; + std::wstring actual; + if (::CryptQueryObject(CERT_QUERY_OBJECT_FILE, file.c_str(), CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, + CERT_QUERY_FORMAT_FLAG_BINARY, 0, &encoding, &content, &format, &store, &message, nullptr)) + actual = signer_display_name(store, message); + if (message != nullptr) + ::CryptMsgClose(message); + if (store != nullptr) + ::CertCloseStore(store, 0); + + if (actual.empty()) { + error = "The installer's signature could not be read, so it was not run."; + return false; + } + if (_wcsicmp(actual.c_str(), expected.c_str()) != 0) { + error = "The update is signed by \"" + narrow_utf8(actual) + "\", not by " + ORCA_UPDATER_PUBLISHER + + ", so it was not run."; + return false; + } return true; } diff --git a/tests/libslic3r/update_handoff_tests.cpp b/tests/libslic3r/update_handoff_tests.cpp index 543e376243..5836d25f7b 100644 --- a/tests/libslic3r/update_handoff_tests.cpp +++ b/tests/libslic3r/update_handoff_tests.cpp @@ -354,3 +354,34 @@ TEST_CASE("Reading the install record leaves it in place and removing it deletes clear_updater_cache_dir(); } + +TEST_CASE("The same install root always gives the same identity key", "[UpdateHandoff]") +{ + const std::string key = install_identity_key("/opt/OrcaSlicer"); + CHECK(install_identity_key("/opt/OrcaSlicer") == key); +} + +TEST_CASE("A trailing separator does not change the identity key", "[UpdateHandoff]") +{ + const std::string key = install_identity_key("/opt/OrcaSlicer"); + CHECK(install_identity_key("/opt/OrcaSlicer/") == key); + CHECK(install_identity_key("/opt/OrcaSlicer//") == key); +} + +TEST_CASE("Copies installed in different roots get different identity keys", "[UpdateHandoff]") +{ + const std::string parent = "/opt/OrcaSlicer"; + const std::string client = "/opt/OrcaSlicer/client"; + CHECK(install_identity_key(parent) != install_identity_key(client)); +} + +TEST_CASE("The identity key is prefixed and free of whitespace", "[UpdateHandoff]") +{ + // AppConfig asserts that a key has no whitespace, so a path such as Program Files + // cannot be the key itself. + const std::string key = install_identity_key("C:\\Program Files\\OrcaSlicer"); + CHECK(key.rfind("installed_artifact:", 0) == 0); + CHECK(key.find_first_of(" \t\r\n\v\f") == std::string::npos); + CHECK(key.size() == std::string("installed_artifact:").size() + 16); + CHECK(key.find_first_not_of("0123456789abcdef", std::string("installed_artifact:").size()) == std::string::npos); +} diff --git a/version.inc b/version.inc index 9ed0297f05..c9ead4dfee 100644 --- a/version.inc +++ b/version.inc @@ -23,3 +23,20 @@ endif() if (NOT DEFINED ORCA_BUILD_TIMESTAMP) set(ORCA_BUILD_TIMESTAMP "" CACHE STRING "Build timestamp (UTC ISO-8601) baked into the binary as a build id") endif() + +# The publisher a downloaded update must be signed by. The helper refuses an artifact +# signed by anyone else, so a release build has to set these. Empty disables the check +# with a warning in the log, which is what a local or CI artifact build wants. +if (NOT DEFINED ORCA_UPDATER_PUBLISHER) + set(ORCA_UPDATER_PUBLISHER "" CACHE STRING "Expected Authenticode signer display name of update artifacts") +endif() +if (NOT ORCA_UPDATER_PUBLISHER AND DEFINED ENV{ORCA_UPDATER_PUBLISHER}) + set(ORCA_UPDATER_PUBLISHER "$ENV{ORCA_UPDATER_PUBLISHER}") +endif() + +if (NOT DEFINED ORCA_UPDATER_TEAM_ID) + set(ORCA_UPDATER_TEAM_ID "" CACHE STRING "Expected Apple Developer Team ID of macOS update artifacts") +endif() +if (NOT ORCA_UPDATER_TEAM_ID AND DEFINED ENV{ORCA_UPDATER_TEAM_ID}) + set(ORCA_UPDATER_TEAM_ID "$ENV{ORCA_UPDATER_TEAM_ID}") +endif()