From 1fc3cd833f19ffa534f04caf541e88042124fd91 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Tue, 25 Feb 2025 20:32:24 -0500 Subject: Remove platform registration API footgun. Turns out, you can just pass this thing an architecture that is *completely* different from the one in the platform you are also passing in and it'll just happily do the wrong thing. While that's pretty wild, I'm in favor of not having to make this particular mistake again the next time I copy/paste some code, so we're now deprecating it. --- binaryninjaapi.h | 8 ++++ binaryviewtype.cpp | 11 +++++ platform/decree/platform_decree.cpp | 2 +- platform/freebsd/platform_freebsd.cpp | 8 ++-- platform/linux/platform_linux.cpp | 76 +++++++++++++++++------------------ 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 08e3feb5..4336febb 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -7203,6 +7203,14 @@ namespace BinaryNinja { /*! Register a Platform for a specific view type + \param name Name of the BinaryViewType + \param id ID of the platform + \param platform The Platform to register + */ + static void RegisterPlatform(const std::string& name, uint32_t id, Platform* platform); + + /*! Register a Platform for a specific view type (this form is deprecated as of 4.3, please use the form without architecture as an argument instead) + \param name Name of the BinaryViewType \param id ID of the platform \param arch Architecture to register this platform with diff --git a/binaryviewtype.cpp b/binaryviewtype.cpp index 2ee8e2ac..d0479a11 100644 --- a/binaryviewtype.cpp +++ b/binaryviewtype.cpp @@ -174,6 +174,17 @@ Ref BinaryViewType::GetArchitecture(uint32_t id, BNEndianness endi } +void BinaryViewType::RegisterPlatform(const string& name, uint32_t id, Platform* platform) +{ + Ref type = BinaryViewType::GetByName(name); + if (!type) + return; + Ref arch = platform->GetArchitecture(); + if (!arch) + return; + type->RegisterPlatform(id, arch, platform); +} + void BinaryViewType::RegisterPlatform(const string& name, uint32_t id, Architecture* arch, Platform* platform) { Ref type = BinaryViewType::GetByName(name); diff --git a/platform/decree/platform_decree.cpp b/platform/decree/platform_decree.cpp index 4d26566c..5c69490c 100644 --- a/platform/decree/platform_decree.cpp +++ b/platform/decree/platform_decree.cpp @@ -58,7 +58,7 @@ extern "C" platform = new DecreeX86Platform(x86); Platform::Register("decree", platform); - BinaryViewType::RegisterPlatform("ELF", 'C', x86, platform); + BinaryViewType::RegisterPlatform("ELF", 'C', platform); } return true; diff --git a/platform/freebsd/platform_freebsd.cpp b/platform/freebsd/platform_freebsd.cpp index 8aa28f7f..d25bbc08 100644 --- a/platform/freebsd/platform_freebsd.cpp +++ b/platform/freebsd/platform_freebsd.cpp @@ -113,7 +113,7 @@ extern "C" platform = new FreeBSDX86Platform(x86); Platform::Register("freebsd", platform); - BinaryViewType::RegisterPlatform("ELF", 9, x86, platform); + BinaryViewType::RegisterPlatform("ELF", 9, platform); } Ref x64 = Architecture::GetByName("x86_64"); @@ -123,7 +123,7 @@ extern "C" platform = new FreeBSDX64Platform(x64); Platform::Register("freebsd", platform); - BinaryViewType::RegisterPlatform("ELF", 9, x64, platform); + BinaryViewType::RegisterPlatform("ELF", 9, platform); } Ref armv7 = Architecture::GetByName("armv7"); @@ -138,7 +138,7 @@ extern "C" thumbPlatform->AddRelatedPlatform(armv7, armPlatform); Platform::Register("freebsd", armPlatform); Platform::Register("freebsd", thumbPlatform); - BinaryViewType::RegisterPlatform("ELF", 9, armv7, armPlatform); + BinaryViewType::RegisterPlatform("ELF", 9, armPlatform); } Ref arm64 = Architecture::GetByName("aarch64"); @@ -148,7 +148,7 @@ extern "C" platform = new FreeBSDArm64Platform(arm64); Platform::Register("freebsd", platform); - BinaryViewType::RegisterPlatform("ELF", 9, arm64, platform); + BinaryViewType::RegisterPlatform("ELF", 9, platform); } return true; diff --git a/platform/linux/platform_linux.cpp b/platform/linux/platform_linux.cpp index 2ba346f8..3bbb7248 100644 --- a/platform/linux/platform_linux.cpp +++ b/platform/linux/platform_linux.cpp @@ -307,8 +307,8 @@ extern "C" platform = new LinuxX86Platform(x86); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, x86, platform); - BinaryViewType::RegisterPlatform("ELF", 3, x86, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } Ref x64 = Architecture::GetByName("x86_64"); @@ -319,8 +319,8 @@ extern "C" platform = new LinuxX64Platform(x64); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, x64, platform); - BinaryViewType::RegisterPlatform("ELF", 3, x64, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } Ref armv7 = Architecture::GetByName("armv7"); @@ -344,10 +344,10 @@ extern "C" Platform::Register("linux", armebPlatform); Platform::Register("linux", thumbebPlatform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, armv7, armPlatform); - BinaryViewType::RegisterPlatform("ELF", 3, armv7, armPlatform); - BinaryViewType::RegisterPlatform("ELF", 0, armv7eb, armebPlatform); - BinaryViewType::RegisterPlatform("ELF", 3, armv7eb, armebPlatform); + BinaryViewType::RegisterPlatform("ELF", 0, armPlatform); + BinaryViewType::RegisterPlatform("ELF", 3, armPlatform); + BinaryViewType::RegisterPlatform("ELF", 0, armebPlatform); + BinaryViewType::RegisterPlatform("ELF", 3, armebPlatform); } Ref arm64 = Architecture::GetByName("aarch64"); @@ -358,8 +358,8 @@ extern "C" platform = new LinuxArm64Platform(arm64); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, arm64, platform); - BinaryViewType::RegisterPlatform("ELF", 3, arm64, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } Ref ppc = Architecture::GetByName("ppc"); @@ -374,10 +374,10 @@ extern "C" Platform::Register("linux", platform); Platform::Register("linux", platformle); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, ppc, platform); - BinaryViewType::RegisterPlatform("ELF", 3, ppc, platform); - BinaryViewType::RegisterPlatform("ELF", 0, ppcle, platformle); - BinaryViewType::RegisterPlatform("ELF", 3, ppcle, platformle); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platformle); + BinaryViewType::RegisterPlatform("ELF", 3, platformle); } Ref ppc64 = Architecture::GetByName("ppc64"); @@ -392,10 +392,10 @@ extern "C" Platform::Register("linux", platform); Platform::Register("linux", platformle); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, ppc64, platform); - BinaryViewType::RegisterPlatform("ELF", 3, ppc64, platform); - BinaryViewType::RegisterPlatform("ELF", 0, ppc64le, platformle); - BinaryViewType::RegisterPlatform("ELF", 3, ppc64le, platformle); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platformle); + BinaryViewType::RegisterPlatform("ELF", 3, platformle); } Ref mipsel = Architecture::GetByName("mipsel32"); @@ -421,18 +421,18 @@ extern "C" Platform::Register("linux", platformBE64); Platform::Register("linux", platformBE64cn); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, mipsel, platformLE); - BinaryViewType::RegisterPlatform("ELF", 0, mipseb, platformBE); - BinaryViewType::RegisterPlatform("ELF", 0, mips3el, platform3LE); - BinaryViewType::RegisterPlatform("ELF", 0, mips3eb, platform3BE); - BinaryViewType::RegisterPlatform("ELF", 0, mips64eb, platformBE64); - BinaryViewType::RegisterPlatform("ELF", 0, cnmips64eb, platformBE64cn); - BinaryViewType::RegisterPlatform("ELF", 3, mipsel, platformLE); - BinaryViewType::RegisterPlatform("ELF", 3, mipseb, platformBE); - BinaryViewType::RegisterPlatform("ELF", 3, mips3el, platform3LE); - BinaryViewType::RegisterPlatform("ELF", 3, mips3eb, platform3BE); - BinaryViewType::RegisterPlatform("ELF", 3, mips64eb, platformBE64); - BinaryViewType::RegisterPlatform("ELF", 3, cnmips64eb, platformBE64cn); + BinaryViewType::RegisterPlatform("ELF", 0, platformLE); + BinaryViewType::RegisterPlatform("ELF", 0, platformBE); + BinaryViewType::RegisterPlatform("ELF", 0, platform3LE); + BinaryViewType::RegisterPlatform("ELF", 0, platform3BE); + BinaryViewType::RegisterPlatform("ELF", 0, platformBE64); + BinaryViewType::RegisterPlatform("ELF", 0, platformBE64cn); + BinaryViewType::RegisterPlatform("ELF", 3, platformLE); + BinaryViewType::RegisterPlatform("ELF", 3, platformBE); + BinaryViewType::RegisterPlatform("ELF", 3, platform3LE); + BinaryViewType::RegisterPlatform("ELF", 3, platform3BE); + BinaryViewType::RegisterPlatform("ELF", 3, platformBE64); + BinaryViewType::RegisterPlatform("ELF", 3, platformBE64cn); } Ref rv32 = Architecture::GetByName("rv32gc"); @@ -443,8 +443,8 @@ extern "C" platform = new LinuxRiscVPlatform(rv32, "linux-rv32gc"); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, rv32, platform); - BinaryViewType::RegisterPlatform("ELF", 3, rv32, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } Ref rv64 = Architecture::GetByName("rv64gc"); @@ -455,8 +455,8 @@ extern "C" platform = new LinuxRiscVPlatform(rv64, "linux-rv64gc"); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, rv64, platform); - BinaryViewType::RegisterPlatform("ELF", 3, rv64, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } #ifdef ULTIMATE_EDITION @@ -468,8 +468,8 @@ extern "C" platform = new LinuxCSkyV1Platform(cskyv1, "linux-csky_le_v1"); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, cskyv1, platform); - BinaryViewType::RegisterPlatform("ELF", 3, cskyv1, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } Ref cskyv2 = Architecture::GetByName("csky_le"); @@ -480,8 +480,8 @@ extern "C" platform = new LinuxCSkyV2Platform(cskyv2, "linux-csky_le"); Platform::Register("linux", platform); // Linux binaries sometimes have an OS identifier of zero, even though 3 is the correct one - BinaryViewType::RegisterPlatform("ELF", 0, cskyv2, platform); - BinaryViewType::RegisterPlatform("ELF", 3, cskyv2, platform); + BinaryViewType::RegisterPlatform("ELF", 0, platform); + BinaryViewType::RegisterPlatform("ELF", 3, platform); } #endif -- cgit v1.3.1