Mobile application security

Compiler hardening for native mobile code

Compiler hardening for native mobile code: phone, chip, code arranged to illustrate compiler hardening for native mobile code.

Native compiler protections reduce the impact of some memory safety failures. Understanding compiler hardening for native mobile code helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

A software dependency becomes part of the application's effective behaviour and maintenance commitment. The important inventory is what the release actually contains, including transitive packages, bundled binaries and code that arrives through a build or update process.

Developers need to ensure that the flags -fstack-protector-strong, or -fstack-protector-all are set in the compiler flags for all native libraries. This is especially important for custom C/C++ libraries that are not part of the NDK.

Sometimes, due to the size of the library and the optimizations applied by the compiler, it might be possible that the library was originally compiled with stack canaries but they were optimized out. For example, this is the case for some react native apps. They are built with -fstack-protector-strong but when attempting to search for stack_chk_fail inside the .so files, it is not found.

Why it matters

Consider a realistic failure: a release binary lacks protections enabled in development builds.

A compromised or vulnerable component may run with the application's privileges or receive information the business did not expect to share. Impact depends on reachability, permissions and actual data flows rather than a package name alone.

Mobile behaviour can change between a fresh installation, an authenticated session, background activity and a device migration. The assessment needs the actual release and representative supported device states.

How penetration testers check for it

Inspect actual shipped binaries and supported build targets rather than assuming project defaults apply.

  • NDK libraries should have stack canaries enabled since the compiler does it by default.
  • Other custom C/C++ libraries might not have stack canaries enabled because they lack the necessary compiler flags (-fstack-protector-strong, or -fstack-protector-all) or the canaries were optimized out by the compiler.
  • Lack of stack buffer calls: Other files such as libreact_utils.so, libreact_config.so, and libreact_debug.so are not empty and contain method calls, but those methods don't contain stack buffer calls, so there are no stack_chk_fail strings inside them.
  • PIE is applicable to executables with the MH_EXECUTE file type, which essentially means the main app binary (e.g. YourApp.app/YourApp).
  • Inspect transitive dependencies and unexpected runtime activity.
  • Confirm whether affected code is reachable under realistic conditions.

How to fix it

Enable applicable mitigations and address the underlying unsafe memory operations.

  • Use maintained and appropriately trusted dependencies.
  • Pin and review relevant changes in the delivery process.
  • Remove unnecessary components and reduce their permissions.
  • Verify remediation in the shipped release and its supporting artefacts.

A vulnerability identifier is a starting point for investigation. Version evidence, affected functionality and deployment conditions determine whether it creates a confirmed application risk.

Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.

Read about our methodology or contact pentested.com to discuss your mobile applications.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo