Actually copy the mach-o file when signing
Dadoum

Dadoum commited on 2026-08-01 20:47:53
Showing 1 changed files, with 20 additions and 7 deletions.

... ...
@@ -1,5 +1,4 @@
1 1
 use crate::error::{CodeSignError, Result};
2
-use crate::file_bytes::read_file_bytes;
3 2
 use crate::signature::{
4 3
     CmsSigner, CodeDirectoryParams, ExecutableSegment, SuperblobSizePlan, der_entitlements_blob,
5 4
     empty_requirements_blob, encode_embedded_signature, entitlement_exec_flags, entitlements_blob,
... ...
@@ -64,8 +63,8 @@ impl<'a> MachOSigningConfig<'a> {
64 63
 }
65 64
 
66 65
 pub fn sign_macho_file(path: &Path, config: &MachOSigningConfig<'_>) -> Result<()> {
67
-    let original = read_file_bytes(path)?;
68
-    let signed = sign_macho_data(path, original.as_slice(), config)?;
66
+    let original = fs::read(path).map_err(|source| CodeSignError::io(path, source))?;
67
+    let signed = sign_macho_owned(path, original, config)?;
69 68
     fs::write(path, signed).map_err(|source| CodeSignError::io(path, source))?;
70 69
     Ok(())
71 70
 }
... ...
@@ -75,7 +74,11 @@ pub fn sign_macho_data(
75 74
     data: &[u8],
76 75
     config: &MachOSigningConfig<'_>,
77 76
 ) -> Result<Vec<u8>> {
78
-    sign_macho_owned(path, data.to_vec(), config)
77
+    if let Some(fat_arches) = parse_fat_arches(path, data)? {
78
+        sign_fat_macho(path, data, fat_arches, config)
79
+    } else {
80
+        Ok(sign_thin_macho(path, data.to_vec(), config)?.data)
81
+    }
79 82
 }
80 83
 
81 84
 fn sign_macho_owned(
... ...
@@ -84,6 +87,19 @@ fn sign_macho_owned(
84 87
     config: &MachOSigningConfig<'_>,
85 88
 ) -> Result<Vec<u8>> {
86 89
     if let Some(fat_arches) = parse_fat_arches(path, &data)? {
90
+        sign_fat_macho(path, &data, fat_arches, config)
91
+    } else {
92
+        Ok(sign_thin_macho(path, data, config)?.data)
93
+    }
94
+}
95
+
96
+// copies
97
+fn sign_fat_macho(
98
+    path: &Path,
99
+    data: &[u8],
100
+    fat_arches: Vec<FatArch>,
101
+    config: &MachOSigningConfig<'_>,
102
+) -> Result<Vec<u8>> {
87 103
     let signed_arches = fat_arches
88 104
         .par_iter()
89 105
         .map(|arch| {
... ...
@@ -99,9 +115,6 @@ fn sign_macho_owned(
99 115
         .collect::<Result<Vec<_>>>()?;
100 116
 
101 117
     rebuild_fat(path, &signed_arches)
102
-    } else {
103
-        Ok(sign_thin_macho(path, data, config)?.data)
104
-    }
105 118
 }
106 119
 
107 120
 fn sign_thin_macho(
108 121