Skip to content

Commit bae2734

Browse files
committed
[macOS export] Detect embedded helper executables using MachO header.
1 parent dd926b9 commit bae2734

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

platform/macos/export/export_plugin.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ Error EditorExportPlatformMacOS::_notarize(const Ref<EditorExportPreset> &p_pres
903903
return OK;
904904
}
905905

906-
Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_warn) {
906+
Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_warn, bool p_set_id) {
907907
int codesign_tool = p_preset->get("codesign/codesign");
908908
switch (codesign_tool) {
909909
case 1: { // built-in ad-hoc
@@ -947,6 +947,12 @@ Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_pre
947947
args.push_back("--code-signature-flags");
948948
args.push_back("runtime");
949949

950+
if (p_set_id) {
951+
String app_id = p_preset->get("application/bundle_identifier");
952+
args.push_back("--binary-identifier");
953+
args.push_back(app_id);
954+
}
955+
950956
args.push_back("-v"); /* provide some more feedback */
951957

952958
args.push_back(p_path);
@@ -1006,6 +1012,12 @@ Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_pre
10061012
args.push_back(p_preset->get("codesign/identity"));
10071013
}
10081014

1015+
if (p_set_id) {
1016+
String app_id = p_preset->get("application/bundle_identifier");
1017+
args.push_back("-i");
1018+
args.push_back(app_id);
1019+
}
1020+
10091021
args.push_back("-v"); /* provide some more feedback */
10101022
args.push_back("-f");
10111023

@@ -1037,7 +1049,7 @@ Error EditorExportPlatformMacOS::_code_sign(const Ref<EditorExportPreset> &p_pre
10371049
}
10381050

10391051
Error EditorExportPlatformMacOS::_code_sign_directory(const Ref<EditorExportPreset> &p_preset, const String &p_path,
1040-
const String &p_ent_path, bool p_should_error_on_non_code) {
1052+
const String &p_ent_path, const String &p_helper_ent_path, bool p_should_error_on_non_code) {
10411053
static Vector<String> extensions_to_sign;
10421054

10431055
if (extensions_to_sign.is_empty()) {
@@ -1064,7 +1076,8 @@ Error EditorExportPlatformMacOS::_code_sign_directory(const Ref<EditorExportPres
10641076
}
10651077

10661078
if (extensions_to_sign.find(current_file.get_extension()) > -1) {
1067-
Error code_sign_error{ _code_sign(p_preset, current_file_path, p_ent_path, false) };
1079+
int ftype = MachO::get_filetype(current_file_path);
1080+
Error code_sign_error{ _code_sign(p_preset, current_file_path, (ftype == 2 || ftype == 5) ? p_helper_ent_path : p_ent_path, false, (ftype == 2 || ftype == 5)) };
10681081
if (code_sign_error != OK) {
10691082
return code_sign_error;
10701083
}
@@ -1073,7 +1086,7 @@ Error EditorExportPlatformMacOS::_code_sign_directory(const Ref<EditorExportPres
10731086
FileAccess::set_unix_permissions(current_file_path, 0755);
10741087
}
10751088
} else if (dir_access->current_is_dir()) {
1076-
Error code_sign_error{ _code_sign_directory(p_preset, current_file_path, p_ent_path, p_should_error_on_non_code) };
1089+
Error code_sign_error{ _code_sign_directory(p_preset, current_file_path, p_ent_path, p_helper_ent_path, p_should_error_on_non_code) };
10771090
if (code_sign_error != OK) {
10781091
return code_sign_error;
10791092
}
@@ -1091,6 +1104,7 @@ Error EditorExportPlatformMacOS::_code_sign_directory(const Ref<EditorExportPres
10911104
Error EditorExportPlatformMacOS::_copy_and_sign_files(Ref<DirAccess> &dir_access, const String &p_src_path,
10921105
const String &p_in_app_path, bool p_sign_enabled,
10931106
const Ref<EditorExportPreset> &p_preset, const String &p_ent_path,
1107+
const String &p_helper_ent_path,
10941108
bool p_should_error_on_non_code_sign) {
10951109
static Vector<String> extensions_to_sign;
10961110

@@ -1180,10 +1194,11 @@ Error EditorExportPlatformMacOS::_copy_and_sign_files(Ref<DirAccess> &dir_access
11801194
if (err == OK && p_sign_enabled) {
11811195
if (dir_access->dir_exists(p_src_path) && p_src_path.get_extension().is_empty()) {
11821196
// If it is a directory, find and sign all dynamic libraries.
1183-
err = _code_sign_directory(p_preset, p_in_app_path, p_ent_path, p_should_error_on_non_code_sign);
1197+
err = _code_sign_directory(p_preset, p_in_app_path, p_ent_path, p_helper_ent_path, p_should_error_on_non_code_sign);
11841198
} else {
11851199
if (extensions_to_sign.find(p_in_app_path.get_extension()) > -1) {
1186-
err = _code_sign(p_preset, p_in_app_path, p_ent_path, false);
1200+
int ftype = MachO::get_filetype(p_in_app_path);
1201+
err = _code_sign(p_preset, p_in_app_path, (ftype == 2 || ftype == 5) ? p_helper_ent_path : p_ent_path, false, (ftype == 2 || ftype == 5));
11871202
}
11881203
if (dir_access->file_exists(p_in_app_path) && is_executable(p_in_app_path)) {
11891204
// chmod with 0755 if the file is executable.
@@ -1197,13 +1212,13 @@ Error EditorExportPlatformMacOS::_copy_and_sign_files(Ref<DirAccess> &dir_access
11971212
Error EditorExportPlatformMacOS::_export_macos_plugins_for(Ref<EditorExportPlugin> p_editor_export_plugin,
11981213
const String &p_app_path_name, Ref<DirAccess> &dir_access,
11991214
bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset,
1200-
const String &p_ent_path) {
1215+
const String &p_ent_path, const String &p_helper_ent_path) {
12011216
Error error{ OK };
12021217
const Vector<String> &macos_plugins{ p_editor_export_plugin->get_macos_plugin_files() };
12031218
for (int i = 0; i < macos_plugins.size(); ++i) {
12041219
String src_path{ ProjectSettings::get_singleton()->globalize_path(macos_plugins[i]) };
12051220
String path_in_app{ p_app_path_name + "/Contents/PlugIns/" + src_path.get_file() };
1206-
error = _copy_and_sign_files(dir_access, src_path, path_in_app, p_sign_enabled, p_preset, p_ent_path, false);
1221+
error = _copy_and_sign_files(dir_access, src_path, path_in_app, p_sign_enabled, p_preset, p_ent_path, p_helper_ent_path, false);
12071222
if (error != OK) {
12081223
break;
12091224
}
@@ -1780,8 +1795,9 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
17801795
add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("'rcodesign' doesn't support signing applications with embedded dynamic libraries."));
17811796
}
17821797

1798+
bool sandbox = p_preset->get("codesign/entitlements/app_sandbox/enabled");
17831799
String ent_path = p_preset->get("codesign/entitlements/custom_file");
1784-
String hlp_ent_path = EditorPaths::get_singleton()->get_cache_dir().path_join(pkg_name + "_helper.entitlements");
1800+
String hlp_ent_path = sandbox ? EditorPaths::get_singleton()->get_cache_dir().path_join(pkg_name + "_helper.entitlements") : ent_path;
17851801
if (sign_enabled && (ent_path.is_empty())) {
17861802
ent_path = EditorPaths::get_singleton()->get_cache_dir().path_join(pkg_name + ".entitlements");
17871803

@@ -1933,7 +1949,7 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
19331949
err = ERR_CANT_CREATE;
19341950
}
19351951

1936-
if ((err == OK) && helpers.size() > 0) {
1952+
if ((err == OK) && sandbox && (helpers.size() > 0 || shared_objects.size() > 0)) {
19371953
ent_f = FileAccess::open(hlp_ent_path, FileAccess::WRITE);
19381954
if (ent_f.is_valid()) {
19391955
ent_f->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
@@ -1959,7 +1975,7 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
19591975
String hlp_path = helpers[i];
19601976
err = da->copy(hlp_path, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file());
19611977
if (err == OK && sign_enabled) {
1962-
err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path, false);
1978+
err = _code_sign(p_preset, tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), hlp_ent_path, false, true);
19631979
}
19641980
FileAccess::set_unix_permissions(tmp_app_path_name + "/Contents/Helpers/" + hlp_path.get_file(), 0755);
19651981
}
@@ -1971,11 +1987,11 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
19711987
String src_path = ProjectSettings::get_singleton()->globalize_path(shared_objects[i].path);
19721988
if (shared_objects[i].target.is_empty()) {
19731989
String path_in_app = tmp_app_path_name + "/Contents/Frameworks/" + src_path.get_file();
1974-
err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, true);
1990+
err = _copy_and_sign_files(da, src_path, path_in_app, sign_enabled, p_preset, ent_path, hlp_ent_path, true);
19751991
} else {
19761992
String path_in_app = tmp_app_path_name.path_join(shared_objects[i].target);
19771993
tmp_app_dir->make_dir_recursive(path_in_app);
1978-
err = _copy_and_sign_files(da, src_path, path_in_app.path_join(src_path.get_file()), sign_enabled, p_preset, ent_path, false);
1994+
err = _copy_and_sign_files(da, src_path, path_in_app.path_join(src_path.get_file()), sign_enabled, p_preset, ent_path, hlp_ent_path, false);
19791995
}
19801996
if (err != OK) {
19811997
break;
@@ -1984,7 +2000,7 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
19842000

19852001
Vector<Ref<EditorExportPlugin>> export_plugins{ EditorExport::get_singleton()->get_export_plugins() };
19862002
for (int i = 0; i < export_plugins.size(); ++i) {
1987-
err = _export_macos_plugins_for(export_plugins[i], tmp_app_path_name, da, sign_enabled, p_preset, ent_path);
2003+
err = _export_macos_plugins_for(export_plugins[i], tmp_app_path_name, da, sign_enabled, p_preset, ent_path, hlp_ent_path);
19882004
if (err != OK) {
19892005
break;
19902006
}
@@ -2004,7 +2020,7 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
20042020
if (ep.step(TTR("Code signing bundle"), 2)) {
20052021
return ERR_SKIP;
20062022
}
2007-
err = _code_sign(p_preset, tmp_app_path_name, ent_path);
2023+
err = _code_sign(p_preset, tmp_app_path_name, ent_path, true, false);
20082024
}
20092025

20102026
String noto_path = p_path;
@@ -2022,7 +2038,7 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p
20222038
if (ep.step(TTR("Code signing DMG"), 3)) {
20232039
return ERR_SKIP;
20242040
}
2025-
err = _code_sign(p_preset, p_path, ent_path, false);
2041+
err = _code_sign(p_preset, p_path, ent_path, false, false);
20262042
}
20272043
} else if (export_format == "pkg") {
20282044
// Create a Installer.

platform/macos/export/export_plugin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ class EditorExportPlatformMacOS : public EditorExportPlatform {
8989
void _make_icon(const Ref<EditorExportPreset> &p_preset, const Ref<Image> &p_icon, Vector<uint8_t> &p_data);
9090

9191
Error _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);
92-
Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_warn = true);
93-
Error _code_sign_directory(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_should_error_on_non_code = true);
92+
Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, bool p_warn = true, bool p_set_id = false);
93+
Error _code_sign_directory(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_ent_path, const String &p_helper_ent_path, bool p_should_error_on_non_code = true);
9494
Error _copy_and_sign_files(Ref<DirAccess> &dir_access, const String &p_src_path, const String &p_in_app_path,
95-
bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset, const String &p_ent_path,
95+
bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset, const String &p_ent_path, const String &p_helper_ent_path,
9696
bool p_should_error_on_non_code_sign);
9797
Error _export_macos_plugins_for(Ref<EditorExportPlugin> p_editor_export_plugin, const String &p_app_path_name,
9898
Ref<DirAccess> &dir_access, bool p_sign_enabled, const Ref<EditorExportPreset> &p_preset,
99-
const String &p_ent_path);
99+
const String &p_ent_path, const String &p_helper_ent_path);
100100
Error _create_dmg(const String &p_dmg_path, const String &p_pkg_name, const String &p_app_path_name);
101101
Error _create_pkg(const Ref<EditorExportPreset> &p_preset, const String &p_pkg_path, const String &p_app_path_name);
102102
Error _export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path);

platform/macos/export/macho.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ bool MachO::is_macho(const String &p_path) {
105105
return (magic == 0xcefaedfe || magic == 0xfeedface || magic == 0xcffaedfe || magic == 0xfeedfacf);
106106
}
107107

108+
uint32_t MachO::get_filetype(const String &p_path) {
109+
Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::READ);
110+
ERR_FAIL_COND_V_MSG(fa.is_null(), 0, vformat("MachO: Can't open file: \"%s\".", p_path));
111+
uint32_t magic = fa->get_32();
112+
MachHeader mach_header;
113+
114+
// Read MachO header.
115+
if (magic == 0xcefaedfe || magic == 0xfeedface) {
116+
// Thin 32-bit binary.
117+
fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
118+
} else if (magic == 0xcffaedfe || magic == 0xfeedfacf) {
119+
// Thin 64-bit binary.
120+
fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
121+
fa->get_32(); // Skip extra reserved field.
122+
} else {
123+
ERR_FAIL_V_MSG(0, vformat("MachO: File is not a valid MachO binary: \"%s\".", p_path));
124+
}
125+
return mach_header.filetype;
126+
}
127+
108128
bool MachO::open_file(const String &p_path) {
109129
fa = FileAccess::open(p_path, FileAccess::READ_WRITE);
110130
ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("MachO: Can't open file: \"%s\".", p_path));

platform/macos/export/macho.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class MachO : public RefCounted {
181181

182182
public:
183183
static bool is_macho(const String &p_path);
184+
static uint32_t get_filetype(const String &p_path);
184185

185186
bool open_file(const String &p_path);
186187

0 commit comments

Comments
 (0)