Skip to content

Commit

Permalink
Address feedback from #1009
Browse files Browse the repository at this point in the history
Add `has_nx_stack` and `has_nx_heap`
  • Loading branch information
romainthomas committed Jan 7, 2024
1 parent c01dc6a commit bcd9b27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions api/python/src/MachO/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,18 @@ void create<Binary>(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<LOAD_COMMAND_TYPES>(&Binary::operator[]),
nb::rv_policy::reference_internal)
Expand Down
6 changes: 6 additions & 0 deletions include/LIEF/MachO/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
8 changes: 7 additions & 1 deletion src/MachO/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ 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");
}
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();
Expand Down

0 comments on commit bcd9b27

Please sign in to comment.