Skip to content

Commit 609ee0f

Browse files
committed
apple-codesign: do not sign additional Mach-O binaries in shallow mode
See inline comments for more details. Closes #148.
1 parent c0e9479 commit 609ee0f

File tree

3 files changed

+34
-55
lines changed

3 files changed

+34
-55
lines changed

apple-codesign/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
Released on ReleaseDate.
88

9+
* When signing a bundle in `--shallow` mode, we no longer sign Mach-O binaries
10+
that aren't the *main* bundle binary. The new behavior is compatible with the
11+
behavior of Apple's `codesign`. (#148)
912
* MSRV 1.78 -> 1.81.
1013
* `aws-sdk-s3` 1.24 -> 1.59.
1114
* `clap` 4.4 -> 4.5.

apple-codesign/src/code_resources.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,20 +1362,26 @@ impl CodeResourcesBuilder {
13621362
// Unlike Apple's tooling, we recognize Mach-O binaries when the nested
13631363
// flag isn't set and we automatically sign.
13641364
//
1365-
// When we seal the file, we treat it as a regular file since the
1366-
// nested flag isn't set. Note that we need to read the signed/installed
1367-
// version of the file since signing will change its content.
1368-
1369-
let read_path = if crate::reader::path_is_macho(full_path)?
1365+
// Unless the path is marked for exclusion or shallow signing mode is
1366+
// active.
1367+
//
1368+
// The reason we exclude in shallow mode is that shallow mode is supposed
1369+
// to behave like Apple's `codesign` and that tool only signs the bundle's
1370+
// "main" Mach-O binary, not other binaries.
1371+
let sign_macho = crate::reader::path_is_macho(full_path)?
13701372
&& !context
13711373
.settings
13721374
.path_exclusion_pattern_matches(root_rel_path)
1373-
{
1375+
&& !context.settings.shallow();
1376+
1377+
let read_path = if sign_macho {
13741378
info!(
13751379
"non-nested file is a Mach-O binary; signing accordingly {}",
13761380
rel_path.display()
13771381
);
13781382
need_install = false;
1383+
// We need to read the signed/installed version of the file since
1384+
// signing will change its content.
13791385
context.sign_and_install_macho(full_path, rel_path)?.0
13801386
} else {
13811387
info!("sealing regular file {}", rel_path_normalized);
@@ -1390,6 +1396,8 @@ impl CodeResourcesBuilder {
13901396
FilesFlavor::Rules2
13911397
};
13921398

1399+
// When we seal the file, we treat it as a regular file since the
1400+
// nested flag isn't set.
13931401
self.resources
13941402
.seal_regular_file(flavor, rel_path_normalized, digests, optional)?;
13951403
}

apple-codesign/tests/cmd/sign-bundle-multiple-macho.trycmd

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ signing bundle at MyApp.app
423423
signing bundle at MyApp.app into MyApp.app.signed-shallow
424424
signing Mach-O file Contents/MacOS/bin
425425
signing Mach-O file Contents/MacOS/lib.dylib
426-
signing Mach-O file Contents/Resources/non-nested-bin
427426
signing main executable Contents/MacOS/MyApp
428427

429428
$ rcodesign print-signature-info MyApp.app.signed-shallow
@@ -433,7 +432,7 @@ $ rcodesign print-signature-info MyApp.app.signed-shallow
433432
entity: other
434433
- path: Contents/MacOS/MyApp
435434
file_size: 22544
436-
file_sha256: ed9b322079f477b956269151b7f05966fe4c2522116e2c23b5ab40f518887fe3
435+
file_sha256: b7c94c6c236fbf011c51b6a98221e648470a8bdb1e79179c5bed2a4229d9f33f
437436
entity:
438437
mach_o:
439438
macho_linkedit_start_offset: 16384 / 0x4000
@@ -451,8 +450,8 @@ $ rcodesign print-signature-info MyApp.app.signed-shallow
451450
- slot: CodeDirectory (0)
452451
magic: fade0c02
453452
length: 493
454-
sha1: f342b48c4632318b35759a678a90f606463d44aa
455-
sha256: 776f6ff24cb7986e98b41600c6f34ad9ef197b9d28c84531ae2cbdf7b965ac36
453+
sha1: f5f97459503ca59e782bc7e09b0a0e84fbe2c469
454+
sha256: eceb671be47c4515ac795ee6b4369f2c3e1ce3c92cd3b31826e3317afe78c8ae
456455
- slot: RequirementSet (2)
457456
magic: fade0c01
458457
length: 12
@@ -485,7 +484,7 @@ $ rcodesign print-signature-info MyApp.app.signed-shallow
485484
slot_digests:
486485
- 'Info (1): 0a5902dc8e47f490d03889d3593d17bddbf79e6c1f79494e20dd28f9459effa5'
487486
- 'RequirementSet (2): 987920904eab650e75788c054aa0b0524e6a80bfc71aa32df8d237a61743f986'
488-
- 'Resources (3): e9faf2afbb4ab5548d3531c4b40abdfd59a2d6c2b5834e1980993957ba5bec83'
487+
- 'Resources (3): 29ca54092b8a1ee42bd378889eae9382dd64f94f6ac99e093d5aff76af6ea2bf'
489488
- 'Application (4): 0000000000000000000000000000000000000000000000000000000000000000'
490489
- 'Entitlements (5): adea2675562421d85cc35e2c909ae27f33846eeb3b2b7c68017abd1b4b02f624'
491490
- 'Rep Specific (6): 0000000000000000000000000000000000000000000000000000000000000000'
@@ -599,53 +598,22 @@ $ rcodesign print-signature-info MyApp.app.signed-shallow
599598
- 'RequirementSet (2): 987920904eab650e75788c054aa0b0524e6a80bfc71aa32df8d237a61743f986'
600599
cms: null
601600
- path: Contents/Resources/non-nested-bin
602-
file_size: 22544
603-
file_sha256: 17ee48591c2b454766b3d38e00ba5b342b3695c635c9114aad839117f45e3b38
601+
file_size: 16386
602+
file_sha256: 4cfaf70bc9fb6827fcf7751deaf65f8b54d46fecb6f39cb2ba8fbcf36912430c
604603
entity:
605604
mach_o:
606-
macho_linkedit_start_offset: 16384 / 0x4000
607-
macho_signature_start_offset: 16400 / 0x4010
608-
macho_signature_end_offset: 16783 / 0x418f
609-
macho_linkedit_end_offset: 22544 / 0x5810
610-
macho_end_offset: 22544 / 0x5810
611-
linkedit_signature_start_offset: 16 / 0x10
612-
linkedit_signature_end_offset: 399 / 0x18f
613-
linkedit_bytes_after_signature: 5761 / 0x1681
614-
signature:
615-
superblob_length: 383 / 0x17f
616-
blob_count: 3
617-
blobs:
618-
- slot: CodeDirectory (0)
619-
magic: fade0c02
620-
length: 327
621-
sha1: c26826707603fb84e28487b5f936799d4edf6377
622-
sha256: 7b46bdc9c357e9a5ce1b15cd255623667b42772b0f42db78c8b630740caecc86
623-
- slot: RequirementSet (2)
624-
magic: fade0c01
625-
length: 12
626-
sha1: 3a75f6db058529148e14dd7ea1b4729cc09ec973
627-
sha256: 987920904eab650e75788c054aa0b0524e6a80bfc71aa32df8d237a61743f986
628-
- slot: CMS Signature (65536)
629-
magic: fade0b01
630-
length: 8
631-
sha1: 2a7254313aa41796079bb0e9d0f044345f69f98b
632-
sha256: e6c83bc98a10348492c7d4d2378a54572ef29e1a5692ccd02b5e29f4b762d6a0
633-
code_directory:
634-
version: '0x20400'
635-
flags: CodeSignatureFlags(ADHOC)
636-
identifier: non-nested-bin
637-
digest_type: sha256
638-
platform: 0
639-
signed_entity_size: 16400
640-
executable_segment_flags: ExecutableSegmentFlags(MAIN_BINARY)
641-
code_digests_count: 5
642-
slot_digests:
643-
- 'Info (1): 0000000000000000000000000000000000000000000000000000000000000000'
644-
- 'RequirementSet (2): 987920904eab650e75788c054aa0b0524e6a80bfc71aa32df8d237a61743f986'
645-
cms: null
605+
macho_linkedit_start_offset: null
606+
macho_signature_start_offset: null
607+
macho_signature_end_offset: null
608+
macho_linkedit_end_offset: null
609+
macho_end_offset: 16386 / 0x4002
610+
linkedit_signature_start_offset: null
611+
linkedit_signature_end_offset: null
612+
linkedit_bytes_after_signature: null
613+
signature: null
646614
- path: Contents/_CodeSignature/CodeResources
647615
file_size: 2882
648-
file_sha256: e9faf2afbb4ab5548d3531c4b40abdfd59a2d6c2b5834e1980993957ba5bec83
616+
file_sha256: 29ca54092b8a1ee42bd378889eae9382dd64f94f6ac99e093d5aff76af6ea2bf
649617
entity:
650618
bundle_code_signature_file: !ResourcesXml
651619
- <?xml version="1.0" encoding="UTF-8"?>
@@ -683,7 +651,7 @@ $ rcodesign print-signature-info MyApp.app.signed-shallow
683651
- ' <dict>'
684652
- ' <key>hash2</key>'
685653
- ' <data>'
686-
- ' F+5IWRwrRUdms9OOALpbNCs2lcY1yRFKrYORF/ReOzg='
654+
- ' TPr3C8n7aCf893Ud6vZfi1TUb+y285yyuo+882kSQww='
687655
- ' </data>'
688656
- ' </dict>'
689657
- ' </dict>'

0 commit comments

Comments
 (0)