From bcd9b27df37a740c85b60704da2b1f048fefc392 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sun, 7 Jan 2024 10:23:37 +0100 Subject: [PATCH] Address feedback from #1009 Add `has_nx_stack` and `has_nx_heap` --- api/python/src/MachO/objects/pyBinary.cpp | 12 ++++++++++++ include/LIEF/MachO/Binary.hpp | 6 ++++++ src/MachO/Binary.cpp | 8 +++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api/python/src/MachO/objects/pyBinary.cpp b/api/python/src/MachO/objects/pyBinary.cpp index 839e2893..286f698f 100644 --- a/api/python/src/MachO/objects/pyBinary.cpp +++ b/api/python/src/MachO/objects/pyBinary.cpp @@ -644,6 +644,18 @@ void create(nb::module_& m) { &Binary::page_size, "Return the binary's page size"_doc) + .def_prop_ro("has_nx_heap", &Binary::has_nx_heap, + R"doc( + Return True if the **heap** is flagged as non-executable. False + otherwise. + )doc"_doc) + + .def_prop_ro("has_nx_stack", &Binary::has_nx_stack, + R"doc( + Return True if the **stack** is flagged as non-executable. False + otherwise. + )doc"_doc) + .def("__getitem__", nb::overload_cast(&Binary::operator[]), nb::rv_policy::reference_internal) diff --git a/include/LIEF/MachO/Binary.hpp b/include/LIEF/MachO/Binary.hpp index 145534fb..67d5f637 100644 --- a/include/LIEF/MachO/Binary.hpp +++ b/include/LIEF/MachO/Binary.hpp @@ -452,6 +452,12 @@ class LIEF_API Binary : public LIEF::Binary { //! Check if the binary uses ``NX`` protection bool has_nx() const override; + /// Return True if the **heap** is flagged as non-executable. False otherwise + bool has_nx_stack() const; + + /// Return True if the **stack** is flagged as non-executable. False otherwise + bool has_nx_heap() const; + //! ``true`` if the binary has an entrypoint. //! //! Basically for libraries it will return ``false`` diff --git a/src/MachO/Binary.cpp b/src/MachO/Binary.cpp index 48e4f4da..341dde3a 100644 --- a/src/MachO/Binary.cpp +++ b/src/MachO/Binary.cpp @@ -207,7 +207,6 @@ bool Binary::is_pie() const { return header().has(HEADER_FLAGS::MH_PIE); } - bool Binary::has_nx() const { if (!header().has(HEADER_FLAGS::MH_NO_HEAP_EXECUTION)) { LIEF_INFO("Heap could be executable"); @@ -215,6 +214,13 @@ bool Binary::has_nx() const { return !header().has(HEADER_FLAGS::MH_ALLOW_STACK_EXECUTION); } +bool Binary::has_nx_stack() const { + return !header().has(HEADER_FLAGS::MH_ALLOW_STACK_EXECUTION); +} + +bool Binary::has_nx_heap() const { + return header().has(HEADER_FLAGS::MH_NO_HEAP_EXECUTION); +} bool Binary::has_entrypoint() const { return has_main_command() || has_thread_command();