diff --git a/CHANGELOG.md b/CHANGELOG.md index 4755165ff..a1029cb9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ _None_ ### New Features -_None_ +- `macos_verify_code_signing` now verifies bare Mach-O binaries, recognized by their content rather than their name. [#782] +- `macos_verify_code_signing` has a new `verify_hardened_runtime` option to check that app bundles and bare binaries are signed with the hardened runtime. [#782] ### Bug Fixes diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/macos/macos_verify_code_signing.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/macos/macos_verify_code_signing.rb index e134e979c..ffbaf9d54 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/macos/macos_verify_code_signing.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/macos/macos_verify_code_signing.rb @@ -15,22 +15,34 @@ def self.run(params) UI.message("Verifying #{path}") - case File.extname(path).downcase - when '.app' - verify_app_bundle(path: path, expected_authority: params[:expected_authority], verify_notarization: params[:verify_notarization]) - when '.dmg' + extension = File.extname(path).downcase + if mach_o?(path) + verify_binary(path: path, expected_authority: params[:expected_authority], verify_hardened_runtime: params[:verify_hardened_runtime], verify_notarization: params[:verify_notarization]) + elsif extension == '.app' + verify_app_bundle(path: path, expected_authority: params[:expected_authority], verify_hardened_runtime: params[:verify_hardened_runtime], verify_notarization: params[:verify_notarization]) + elsif extension == '.dmg' + UI.message("Skipping the hardened runtime check for #{path}: it only applies to code, not to a disk image.") if params[:verify_hardened_runtime] verify_disk_image(path: path, expected_authority: params[:expected_authority], verify_notarization: params[:verify_notarization]) else - UI.user_error!("Don't know how to verify #{path}. Supported artifacts are `.app` bundles and `.dmg` disk images") + UI.user_error!("Don't know how to verify #{path}. Supported artifacts are `.app` bundles, `.dmg` disk images and Mach-O binaries") end end UI.success("Verified #{paths.length} artifact(s)") end - def self.verify_app_bundle(path:, expected_authority:, verify_notarization:) + # Thin 32- and 64-bit, then universal, in both byte orders. + MACH_O_MAGIC_NUMBERS = %w[feedface feedfacf cefaedfe cffaedfe cafebabe cafebabf bebafeca bfbafeca].freeze + + # Recognizes a bare binary by its content, because its name can look like it has an extension, e.g. `tool-v1.2.3`. + # + def self.mach_o?(path) + File.file?(path) && MACH_O_MAGIC_NUMBERS.include?(File.binread(path, 4).to_s.unpack1('H*')) + end + + def self.verify_app_bundle(path:, expected_authority:, verify_hardened_runtime:, verify_notarization:) UI.user_error!("#{path} is not signed at all") unless signed?(path, deep: true) - verify_authority!(path: path, expected_authority: expected_authority) unless expected_authority.nil? + verify_signature_details!(path: path, expected_authority: expected_authority, verify_hardened_runtime: verify_hardened_runtime) return unless verify_notarization @@ -49,7 +61,7 @@ def self.verify_disk_image(path:, expected_authority:, verify_notarization:) UI.message("Checking whether #{path} is signed. Disk images usually aren't, so a `codesign` failure due to the image being unsigned is expected and tolerated (other signature failures will still fail).") if signed?(path) - verify_authority!(path: path, expected_authority: expected_authority) unless expected_authority.nil? + verify_signature_details!(path: path, expected_authority: expected_authority, verify_hardened_runtime: false) verify!("#{path} was rejected by Gatekeeper", 'spctl', '--assess', '--type', 'open', '--context', 'context:primary-signature', '--verbose=2', path) if verify_notarization else UI.important("#{path} is not signed — skipping its signature checks. We expect that the app it contains is the one carrying the signature.") @@ -58,6 +70,14 @@ def self.verify_disk_image(path:, expected_authority:, verify_notarization:) verify!("#{path} has no notarization ticket stapled to it", 'xcrun', 'stapler', 'validate', path) if verify_notarization end + def self.verify_binary(path:, expected_authority:, verify_hardened_runtime:, verify_notarization:) + UI.user_error!("#{path} is not signed at all") unless signed?(path) + verify_signature_details!(path: path, expected_authority: expected_authority, verify_hardened_runtime: verify_hardened_runtime) + + # `--type execute` rejects any bare binary, notarized or not, as `does not seem to be an app`. + verify!("#{path} was rejected by Gatekeeper", 'spctl', '--assess', '--type', 'open', '--context', 'context:primary-signature', '--verbose=2', path) if verify_notarization + end + # Distinguishes an artifact that carries no signature at all from one whose signature is # broken: the former is expected for a disk image, the latter always a failure. # @@ -80,11 +100,33 @@ def self.verify!(error_message, *command) sh(*command, error_callback: ->(_) { UI.user_error!(error_message) }) end - def self.verify_authority!(path:, expected_authority:) + def self.verify_signature_details!(path:, expected_authority:, verify_hardened_runtime:) + return if expected_authority.nil? && !verify_hardened_runtime + + signature_details(path).each do |details| + UI.user_error!("#{path} is not signed by '#{expected_authority}':\n#{details}") unless expected_authority.nil? || details.lines(chomp: true).include?("Authority=#{expected_authority}") + UI.user_error!("#{path} is not signed with the hardened runtime:\n#{details}") if verify_hardened_runtime && !code_directory_flags(details).include?('runtime') + end + end + + # Each architecture of universal code carries its own signature, but `codesign --display` + # only shows the one matching this Mac's, so the others are displayed one at a time. + # + # @return [Array] The `codesign --display` output for each architecture. + # + def self.signature_details(path) details = sh('codesign', '--display', '--verbose=2', path) - return if details.include?("Authority=#{expected_authority}") + architectures = details[/^Format=.*Mach-O universal \(([^)]+)\)/, 1]&.split + return [details] if architectures.nil? + + architectures.map { |architecture| sh('codesign', '--display', '--verbose=2', '--arch', architecture, path) } + end - UI.user_error!("#{path} is not signed by '#{expected_authority}':\n#{details}") + # The flags of the CodeDirectory line, e.g. `%w[adhoc runtime]` for `flags=0x10002(adhoc,runtime)`. + # The `Executable=` line can contain `flags=` too, as part of the path. + # + def self.code_directory_flags(details) + details[/^CodeDirectory\b.*?\bflags=0x\h+\(([^)]*)\)/, 1]&.split(',') || [] end ##################################################### @@ -99,11 +141,16 @@ def self.details <<~DETAILS Verify that the given macOS artifacts are signed, and optionally notarized. - The checks that apply are picked from the artifact's extension: + The checks that apply are picked from the kind of artifact: - `.app` — the signature is valid and satisfies its designated requirement, Gatekeeper accepts the bundle for execution, and a notarization ticket is stapled to it. - `.dmg` — if the image is signed, the signature is valid (and can be checked against the expected authority) and Gatekeeper accepts opening it; when `verify_notarization` is true, a notarization ticket is stapled to the image. + - a bare Mach-O binary, whatever its name — the signature is valid and can be checked against the expected authority, and Gatekeeper accepts it as notarized. A bare binary can't be stapled, so that acceptance relies on an online lookup that can lag a fresh submission. + + `verify_hardened_runtime` checks that app bundles and bare binaries are signed with the hardened runtime. + Notarization requires it, so `verify_notarization` already covers it implicitly. Use this option to catch a missing hardened runtime before submitting for notarization. + It doesn't apply to disk images. DETAILS end @@ -111,7 +158,7 @@ def self.available_options [ FastlaneCore::ConfigItem.new( key: :artifact_path, - description: 'The path, or list of paths, to the `.app` bundle(s) or `.dmg` disk image(s) to verify', + description: 'The path, or list of paths, to the `.app` bundle(s), `.dmg` disk image(s) or bare binaries to verify', type: Array, verify_block: proc do |value| UI.user_error!('`artifact_path` must be a String or an Array of Strings') unless value.all?(String) @@ -125,9 +172,15 @@ def self.available_options optional: true, default_value: nil ), + FastlaneCore::ConfigItem.new( + key: :verify_hardened_runtime, + description: 'Whether to also assert that app bundles and bare binaries are signed with the hardened runtime. Disk images are skipped', + type: Boolean, + default_value: false + ), FastlaneCore::ConfigItem.new( key: :verify_notarization, - description: 'Whether to also assert that the artifact is accepted by Gatekeeper and has a notarization ticket stapled to it', + description: 'Whether to also assert that the artifact is accepted by Gatekeeper and, unless it is a bare binary, has a notarization ticket stapled to it', type: Boolean, default_value: true ), diff --git a/spec/macos_verify_code_signing_spec.rb b/spec/macos_verify_code_signing_spec.rb index 18a9988e8..6650cdf8d 100644 --- a/spec/macos_verify_code_signing_spec.rb +++ b/spec/macos_verify_code_signing_spec.rb @@ -21,6 +21,21 @@ def with_artifacts(*names) end end + # Like `with_artifacts`, but as files starting with the Mach-O magic number, which is what + # identifies a bare binary. + # + def with_binaries(*names) + in_tmp_dir do |tmp_dir| + paths = names.map do |name| + path = File.join(tmp_dir, name) + FileUtils.mkdir_p(File.dirname(path)) + File.binwrite(path, "\xCF\xFA\xED\xFE") + path + end + yield(paths) + end + end + def expect_codesign_verify_deep(path, exitstatus: 0, output: '') expect_shell_command('codesign', '--verify', '--deep', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output) end @@ -29,8 +44,18 @@ def expect_codesign_verify(path, exitstatus: 0, output: '') expect_shell_command('codesign', '--verify', '--strict', '--verbose=2', path, exitstatus: exitstatus, output: output) end - def expect_codesign_display(path, authority:) - expect_shell_command('codesign', '--display', '--verbose=2', path, output: "Executable=#{path}\nAuthority=#{authority}\n") + def expect_codesign_display(path, authority: 'Developer ID Application: Automattic, Inc. (ABCDE12345)', flags: '0x10000(runtime)', format: 'Mach-O thin (arm64)', arch: nil) + arch_args = arch.nil? ? [] : ['--arch', arch] + expect_shell_command('codesign', '--display', '--verbose=2', *arch_args, path, output: "Executable=#{path}\nFormat=#{format}\nCodeDirectory v=20500 size=1234 flags=#{flags} hashes=30+7 location=embedded\nAuthority=#{authority}\n") + end + + # `codesign --display` shows the native slice of universal code, so these are its outputs + # for the universal file and then for each slice. + # + def expect_codesign_display_universal(path, x86_64_flags:, arm64_flags: '0x10000(runtime)', format: 'Mach-O universal (x86_64 arm64)') + expect_codesign_display(path, flags: arm64_flags, format: format) + expect_codesign_display(path, flags: x86_64_flags, format: format.sub('universal (x86_64 arm64)', 'thin (x86_64)'), arch: 'x86_64') + expect_codesign_display(path, flags: arm64_flags, format: format.sub('universal (x86_64 arm64)', 'thin (arm64)'), arch: 'arm64') end def expect_gatekeeper_assess_execute(path, exitstatus: 0) @@ -130,6 +155,37 @@ def expect_stapler_validate(path, exitstatus: 0) .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app has no notarization ticket stapled to it/) end end + + describe '`verify_hardened_runtime`' do + it 'passes when signed with the hardened runtime' do + with_artifacts('Test.app') do |(path)| + expect_codesign_verify_deep(path) + expect_codesign_display(path) + + run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) + end + end + + it 'fails when signed without the hardened runtime' do + with_artifacts('Test.app') do |(path)| + expect_codesign_verify_deep(path) + expect_codesign_display(path, flags: '0x0(none)') + + expect { run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app is not signed with the hardened runtime/) + end + end + + it 'fails when one architecture of a universal app lacks the hardened runtime' do + with_artifacts('Test.app') do |(path)| + expect_codesign_verify_deep(path) + expect_codesign_display_universal(path, x86_64_flags: '0x0(none)', format: 'bundle with Mach-O universal (x86_64 arm64)') + + expect { run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /Test\.app is not signed with the hardened runtime/) + end + end + end end describe 'disk images' do @@ -195,6 +251,202 @@ def expect_stapler_validate(path, exitstatus: 0) run_described_fastlane_action(artifact_path: path, verify_notarization: false) end end + + it 'skips the hardened runtime check' do + with_artifacts('Test.dmg') do |(path)| + expect_codesign_verify(path) + expect(Open3).not_to receive(:popen2e).with('codesign', '--display', any_args) + allow(FastlaneCore::UI).to receive(:message) + expect(FastlaneCore::UI).to receive(:message).with(/Skipping the hardened runtime check for .*Test\.dmg/) + + run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) + end + end + end + + describe 'bare binaries' do + it 'verifies the signature and Gatekeeper acceptance, without expecting a stapled ticket' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_gatekeeper_assess_open(path) + expect(Open3).not_to receive(:popen2e).with('xcrun', any_args) + + run_described_fastlane_action(artifact_path: path) + end + end + + it 'recognizes a binary whose name looks like it has an extension' do + with_binaries('test-cli-v1.2.3') do |(path)| + expect_codesign_verify(path) + expect_gatekeeper_assess_open(path) + + run_described_fastlane_action(artifact_path: path) + end + end + + it 'verifies a binary named like a disk image as a binary' do + with_binaries('test-cli.dmg') do |(path)| + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all") + + expect { run_described_fastlane_action(artifact_path: path, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli\.dmg is not signed at all/) + end + end + + it 'fails on a file without an extension that is not a Mach-O binary' do + in_tmp_dir do |tmp_dir| + path = File.join(tmp_dir, 'test-script') + File.write(path, "#!/bin/sh\n") + + expect { run_described_fastlane_action(artifact_path: path) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /Don't know how to verify .*test-script/) + end + end + + it 'skips Gatekeeper when `verify_notarization` is `false`' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect(Open3).not_to receive(:popen2e).with('spctl', any_args) + + run_described_fastlane_action(artifact_path: path, verify_notarization: false) + end + end + + it 'passes when signed by the expected authority' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, authority: authority) + + run_described_fastlane_action(artifact_path: path, expected_authority: authority, verify_notarization: false) + end + end + + it 'fails when signed by a different authority' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, authority: 'Apple Development: Someone Else (ZZZZZ99999)') + + expect { run_described_fastlane_action(artifact_path: path, expected_authority: authority) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /is not signed by '#{Regexp.escape(authority)}'/) + end + end + + it 'fails when the expected authority is only a prefix of the actual one' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, authority: "#{authority} Evil") + + expect { run_described_fastlane_action(artifact_path: path, expected_authority: authority, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /is not signed by '#{Regexp.escape(authority)}'/) + end + end + + it 'fails when the signature is not valid' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: invalid signature (code or signature have been modified)\n") + + expect { run_described_fastlane_action(artifact_path: path) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /The code signature of .*test-cli is not valid/) + end + end + + # Unlike a disk image, there is no container whose contents could carry the signature instead. + it 'fails when it is not signed at all' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path, exitstatus: 1, output: "#{path}: code object is not signed at all\n") + + expect { run_described_fastlane_action(artifact_path: path) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli is not signed at all/) + end + end + + it 'fails when Gatekeeper rejects it' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_gatekeeper_assess_open(path, exitstatus: 3) + + expect { run_described_fastlane_action(artifact_path: path) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli was rejected by Gatekeeper/) + end + end + + describe '`verify_hardened_runtime`' do + it 'passes when signed with the hardened runtime' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path) + + run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) + end + end + + it 'passes when the hardened runtime is one of several flags' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, flags: '0x10002(adhoc,runtime)') + + run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) + end + end + + it 'fails when signed without the hardened runtime' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, flags: '0x0(none)') + + expect { run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli is not signed with the hardened runtime/) + end + end + + it 'passes when every architecture of a universal binary has the hardened runtime' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display_universal(path, x86_64_flags: '0x10000(runtime)') + + run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) + end + end + + it 'fails when one architecture of a universal binary lacks the hardened runtime' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display_universal(path, x86_64_flags: '0x0(none)') + + expect { run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli is not signed with the hardened runtime:\n.*thin \(x86_64\)/m) + end + end + + it 'ignores a `flags=` that is part of the path' do + with_binaries('flags=0x10000(runtime)/test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, flags: '0x0(none)') + + expect { run_described_fastlane_action(artifact_path: path, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /test-cli is not signed with the hardened runtime/) + end + end + + it 'still checks the authority' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect_codesign_display(path, authority: 'Apple Development: Someone Else (ZZZZZ99999)') + + expect { run_described_fastlane_action(artifact_path: path, expected_authority: authority, verify_hardened_runtime: true, verify_notarization: false) } + .to raise_error(FastlaneCore::Interface::FastlaneError, /is not signed by '#{Regexp.escape(authority)}'/) + end + end + + it 'does not inspect the signature details by default' do + with_binaries('test-cli') do |(path)| + expect_codesign_verify(path) + expect(Open3).not_to receive(:popen2e).with('codesign', '--display', any_args) + + run_described_fastlane_action(artifact_path: path, verify_notarization: false) + end + end + end end it 'verifies every artifact when given a list of paths' do