Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions plugins/workflow_objc/src/activities/remove_memory_management.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::activities::util;
use crate::{error::ILLevel, metadata::GlobalState, Error};
use binaryninja::{
architecture::{Architecture as _, CoreRegister, Register as _, RegisterInfo as _},
binary_view::BinaryView,
Expand All @@ -11,12 +13,11 @@ use binaryninja::{
lifting::LowLevelILLabel,
LowLevelILRegisterKind,
},
symbol::Symbol,
variable::PossibleValueSet,
workflow::AnalysisContext,
};

use crate::{error::ILLevel, metadata::GlobalState, Error};

// TODO: We should also handle `objc_retain_x` / `objc_release_x` variants
// that use a custom calling convention.
const IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS: &[&[u8]] = &[
Expand All @@ -29,8 +30,51 @@ const IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS: &[&[u8]] = &[
b"_objc_retainAutoreleasedReturnValue",
b"_objc_retainBlock",
b"_objc_unsafeClaimAutoreleasedReturnValue",
b"_objc_claimAutoreleasedReturnValue",
];

fn is_objc_rt_symbol_dscview(view: &BinaryView, symbol: &Symbol) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what the purpose of this function is? What is it guarding against?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that check just to be as precise as possible and reduce the reliance on only symbol names– because if I'm not mistaken, it's possible in principle for multiple different symbols with the same name to exist in a binary view. So although it's unlikely– if Apple includes a function in one of their shared cache libraries with the same name as a runtime memory management function, it won't be hidden like one.

if view.view_type() != "DSCView" {
return false;
}

let addr = symbol.address();

let Some(view_section_name) = view.sections().iter().find_map(|sect| {
(sect.start()..sect.end())
.contains(&addr)
.then(|| sect.name())
}) else {
return false;
};

let Ok(view_section_name) = view_section_name.to_str() else {
return false;
};

// Ensure that the symbol lies within the text segment and section of libobjc._.dylib by
// parsing the view section name:

let Some((image, mem_fqid)) = view_section_name.split_once("::") else {
return false;
};

if mem_fqid != "__TEXT.__text" {
return false;
}

let mut imgparts = image.split(".");
matches!(
(
imgparts.next(),
imgparts.next(),
imgparts.next(),
imgparts.next()
),
(Some("libobjc"), Some(_), Some("dylib"), None)
)
}

fn is_call_to_ignorable_memory_management_function<'func>(
view: &binaryninja::binary_view::BinaryView,
instr: &'func LowLevelILInstruction<'func, Mutable, NonSSA>,
Expand All @@ -57,7 +101,17 @@ fn is_call_to_ignorable_memory_management_function<'func>(
// Remove any j_ prefix that the shared cache workflow adds to stub functions.
let symbol_name = symbol_name.strip_prefix(b"j_").unwrap_or(symbol_name);

IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS.contains(&symbol_name)
// Normalize the name to also include register-specific functions (e.g. _objc_release_x19).
let symbol_name = util::strip_arc_reg_suffix(symbol_name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in incorrect handling of objc_retain_xN calls. Converting them to a no-op will result in x0 having the incorrect value.

Consider this example:

Image

This change results in:

Image

It should be result->_internal = _initWithCFURLResponse[1]


let name_test = IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS.contains(&symbol_name);

if view.view_type() == "DSCView" {
// verify that the section of the symbol in question lies within the memory of the runtime image
name_test && is_objc_rt_symbol_dscview(view, &symbol)
} else {
name_test
}
}

fn process_instruction(
Expand Down
13 changes: 13 additions & 0 deletions plugins/workflow_objc/src/activities/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,16 @@ pub fn adjust_return_type_of_call(call: &Call<'_>, return_type: &Type, confidenc
None,
);
}

pub fn strip_arc_reg_suffix(fname: &[u8]) -> &[u8] {
let Some(pos) = fname.windows(2).rposition(|p| p == b"_x") else {
return fname;
};

let reg_num = &fname[pos + 2..];
if !reg_num.is_empty() && reg_num.iter().all(u8::is_ascii_digit) {
&fname[..pos]
} else {
fname
}
}