Skip to content

Commit 8519ff1

Browse files
committed
Apply protection against ROP/JOP attacks for aarch64 on asm_trampoline.S
The BTI flag must be applied in assembler sources for this class of attacks to be mitigated on newer aarch64 processors. See also: https://sourceware.org/annobin/annobin.html/Test-branch-protection.html and https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/enabling-pac-and-bti-on-aarch64
1 parent 431d0e5 commit 8519ff1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Python/aarch64.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef _AARCH_64_H_
2+
#define _AARCH_64_H_
3+
4+
/*
5+
* References:
6+
* - https://developer.arm.com/documentation/101028/0012/5--Feature-test-macros
7+
* - https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst
8+
*/
9+
10+
#if defined(__ARM_FEATURE_BTI_DEFAULT) && __ARM_FEATURE_BTI_DEFAULT == 1
11+
#define BTI_J hint 36 /* bti j: for jumps, IE br instructions */
12+
#define BTI_C hint 34 /* bti c: for calls, IE bl instructions */
13+
#define GNU_PROPERTY_AARCH64_BTI 1 /* bit 0 GNU Notes is for BTI support */
14+
#else
15+
#define BTI_J
16+
#define BTI_C
17+
#define GNU_PROPERTY_AARCH64_BTI 0
18+
#endif
19+
20+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
21+
#if __ARM_FEATURE_PAC_DEFAULT & 1
22+
#define SIGN_LR hint 25 /* paciasp: sign with the A key */
23+
#define VERIFY_LR hint 29 /* autiasp: verify with the A key */
24+
#elif __ARM_FEATURE_PAC_DEFAULT & 2
25+
#define SIGN_LR hint 27 /* pacibsp: sign with the b key */
26+
#define VERIFY_LR hint 31 /* autibsp: verify with the b key */
27+
#endif
28+
#define GNU_PROPERTY_AARCH64_POINTER_AUTH 2 /* bit 1 GNU Notes is for PAC support */
29+
#else
30+
#define SIGN_LR BTI_C
31+
#define VERIFY_LR
32+
#define GNU_PROPERTY_AARCH64_POINTER_AUTH 0
33+
#endif
34+
35+
/* Add the BTI support to GNU Notes section */
36+
#if GNU_PROPERTY_AARCH64_BTI != 0 || GNU_PROPERTY_AARCH64_POINTER_AUTH != 0
37+
.pushsection .note.gnu.property, "a"; /* Start a new allocatable section */
38+
.balign 8; /* align it on a byte boundry */
39+
.long 4; /* size of "GNU\0" */
40+
.long 0x10; /* size of descriptor */
41+
.long 0x5; /* NT_GNU_PROPERTY_TYPE_0 */
42+
.asciz "GNU";
43+
.long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
44+
.long 4; /* Four bytes of data */
45+
.long (GNU_PROPERTY_AARCH64_BTI|GNU_PROPERTY_AARCH64_POINTER_AUTH); /* BTI or PAC is enabled */
46+
.long 0; /* padding for 8 byte alignment */
47+
.popsection; /* end the section */
48+
#endif
49+
50+
#endif

Python/asm_trampoline.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "aarch64.h"
2+
13
.text
24
.globl _Py_trampoline_func_start
35
# The following assembly is equivalent to:
@@ -20,10 +22,12 @@ _Py_trampoline_func_start:
2022
#if defined(__aarch64__) && defined(__AARCH64EL__) && !defined(__ILP32__)
2123
// ARM64 little endian, 64bit ABI
2224
// generate with aarch64-linux-gnu-gcc 12.1
25+
SIGN_LR
2326
stp x29, x30, [sp, -16]!
2427
mov x29, sp
2528
blr x3
2629
ldp x29, x30, [sp], 16
30+
VERIFY_LR
2731
ret
2832
#endif
2933
#ifdef __riscv

0 commit comments

Comments
 (0)