Skip to content

Commit

Permalink
Port MASTG test 0087
Browse files Browse the repository at this point in the history
  • Loading branch information
titze committed Nov 7, 2024
1 parent 24809dc commit a5dc65b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Document/0x06i-Testing-Code-Quality-and-Build-Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Detecting the presence of [binary protection mechanisms](0x04h-Testing-Code-Qual
Although Xcode enables all binary security features by default, it may be relevant to verify this for old applications or to check for compiler flag misconfigurations. The following features are applicable:

- [**PIE (Position Independent Executable)**](0x04h-Testing-Code-Quality.md#position-independent-code):
- PIE applies to executable binaries (Mach-O type `MH_EXECUTE`).
- PIE applies to executable binaries (Mach-O type `MH_EXECUTE`) [source](https://web.archive.org/web/20230328221404/https://opensource.apple.com/source/cctools/cctools-921/include/mach-o/loader.h.auto.html).
- However it's not applicable for libraries (Mach-O type `MH_DYLIB`).
- [**Memory management**](0x04h-Testing-Code-Quality.md#memory-management):
- Both pure Objective-C, Swift and hybrid binaries should have ARC (Automatic Reference Counting) enabled.
Expand Down
28 changes: 28 additions & 0 deletions tests-beta/ios/MASVS-CODE/MASTG-TEST-0x87-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Position Independent Code (PIC) not enabled
platform: ios
id: MASTG-TEST-0x87-1
type: [static]
weakness: MASWE-0116
---

## Overview

[PIE (Position Independent Executables)](../../../Document/0x04h-Testing-Code-Quality/#position-independent-code) are designed to enhance security by allowing executables to be loaded at random memory addresses, mitigating certain types of attacks. In the context Mach-O file format of iOS applications, PIE is applicable to executables with the `MH_EXECUTE` file type, which essentially means the main binary of the application.

Conversely, shared libraries with the `MH_DYLIB` file type (dylibs and frameworks) are inherently position-independent and do not utilize the `MH_PIE` flag.

This test case checks if the main executable is compiled without PIE.

## Steps

1. Extract the application.
2. Run @MASTG-TECH-0115 on the main binary (`App.app/YourApp`) and grep for "pic" or the corresponding keyword used by the selected tool.

## Observation

The output should list if PIC is enabled or disabled.

## Evaluation

The test case fails if PIC is disabled.
27 changes: 27 additions & 0 deletions tests-beta/ios/MASVS-CODE/MASTG-TEST-0x87-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Stack Canaries not enabled
platform: ios
id: MASTG-TEST-0x87-2
type: [static]
weakness: MASWE-0116
---

## Overview

This test case checks if the main binary or any libraries of the app are compiled without stack canaries and therefore lack [stack smashing protection](../../../Document/0x06i-Testing-Code-Quality-and-Build-Settings/#binary-protection-mechanisms), a common mitigation technique against buffer overflow attacks.

This test applies to all binaries and libraries, but is especially important for non-memory safe code like Objective-C or C/C++.

## Steps

1. Extract the application.
2. Run @MASTG-TECH-0113 on the main binary (`App.app/YourApp`) and each shared library (e.g., stored in the Frameworks folder)
3. If the output contains the symbol `__stack_chk_fail` it indicates stack canaries are enabled.

## Observation

The output should list of symbols of the main binary and each shared library.

## Evaluation

The test case fails any binary or library is not purely Swift but does not contain methods indicating stack canaries like `objc_autorelease` or `objc_retainAutorelease`.
35 changes: 35 additions & 0 deletions tests-beta/ios/MASVS-CODE/MASTG-TEST-0x87-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Automatic Reference Counting (ARC) not enabled
platform: ios
id: MASTG-TEST-0x87-3
type: [static]
weakness: MASWE-0116
---

## Overview

[ARC (Automatic Reference Counting)](../../../Document/0x04h-Testing-Code-Quality/#automatic-reference-counting) is a memory management feature of the compiler for the Objective-C and Swift code.

The main binary and all libraries should have ARC enabled if they contain Objective-C or Swift code.
- For libraries written purely in Swift, ARC is enabled by default.
- For libraries containing Objective-C code, it can be enabled via the `-fobjc-arc` of clang.

For libraries written purely in C/C++, ARC is not available.

Once ARC is enabled, symbols such as `objc_autorelease` or `objc_retainAutorelease` will be present in the binaries.

This test case checks if ARC is enabled.

## Steps

1. Extract the application.
2. Run @MASTG-TECH-0113 on the main binary (`App.app/YourApp`) and each shared library (e.g., stored in the Frameworks folder)
3. If the output contains symbols indicating the usage of Objective-C (e.g., `__objc_data...`) or symbols indicating the usage of Swift (e.g., `_swift_allocObject`), check if the symbols `objc_autorelease` or `objc_retainAutorelease` exist in the app.

## Observation

The output should list of symbols of the main binary and each shared library.

## Evaluation

The test case fails any binary or library is not purely C/C++ but does not contain methods indicating ARC is present like `objc_autorelease` or `objc_retainAutorelease`.

0 comments on commit a5dc65b

Please sign in to comment.