Skip to contentSkip to conversation

arm64: Optimize ptrauth by enabling it for non-leaf functions

PATCH Superseded Amit Daniel Kachhap 3 participants 11 messages Apr 14, 2020 – Apr 29, 2020 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: Optimize ptrauth by enabling it for non-leaf functions

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

This patch changes compiler option to -mbranch-protection=pac-ret and -msign-return-address=non-leaf.

---
 arch/arm64/Kconfig  | 4 ++--
 arch/arm64/Makefile | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/arm64/Kconfig +2 -2
index 40fb05d..29cfe05 100644
@@ -1541,11 +1541,11 @@ config ARM64_PTR_AUTH
 
 config CC_HAS_BRANCH_PROT_PAC_RET
 	# GCC 9 or later, clang 8 or later
-	def_bool $(cc-option,-mbranch-protection=pac-ret+leaf)
+	def_bool $(cc-option,-mbranch-protection=pac-ret)
 
 config CC_HAS_SIGN_RETURN_ADDRESS
 	# GCC 7, 8
-	def_bool $(cc-option,-msign-return-address=all)
+	def_bool $(cc-option,-msign-return-address=non-leaf)
 
 config AS_HAS_PAC
 	def_bool $(as-option,-Wa$(comma)-march=armv8.3-a)
arch/arm64/Makefile +2 -2
index 85e4149..895f506 100644
@@ -70,8 +70,8 @@ endif
 branch-prot-flags-y += $(call cc-option,-mbranch-protection=none)
 
 ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
-branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
-branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf
+branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=non-leaf
+branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret
 # -march=armv8.3-a enables the non-nops instructions for PAC, to avoid the
 # compiler to generate them and consequently to break the single image contract
 # we pass it only to the assembler. This option is utilized only in case of non
Signature · 7 lines
2.7.4


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#1 lore raw
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer
#2 lore raw
Quoted 10 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

Amit, if you create a leaf function like:

void leaf_function(void)

{
	asm volatile("" : : : "x30");
}

... what assembly does the compiler generate when passed -msign-return-address=non-leaf ?

* Does the compiler create a stack-frame for this function?
* Where does the compiler spill x30?
* Does the compiler sign the LR?

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#10 lore raw
In reply to Will Deacon

Hi,

Quoted 10 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

Since lr is not stored on the stack so this function cannot be used for ROP gadget attack. Sure this also provides performance benefit. I will provide the percentage of code size reduction.

Cheers,
Amit

Quoted 3 lines

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw
In reply to Mark Rutland

Hi,

Quoted 18 lines · Mark Rutland
Quoted 10 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

Amit, if you create a leaf function like:

Quoted 7 lines

void leaf_function(void)

{
	asm volatile("" : : : "x30");
}

... what assembly does the compiler generate when passed -msign-return-address=non-leaf ?

This is the assembly generated in this case,

      80:       d503233f        paciasp
      84:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
      88:       910003fd        mov     x29, sp
        asm volatile("" : : : "x30");
}
      8c:       a8c17bfd        ldp     x29, x30, [sp], #16
      90:       d50323bf        autiasp
      94:       d65f03c0        ret
Quoted 4 lines

* Does the compiler create a stack-frame for this function?
* Where does the compiler spill x30?
* Does the compiler sign the LR?

Yes here the compiler creates stack frame and pushes signed lr in the stack. Probably this situation with x30 as clobber registers breaks this patch.

Cheers,
Amit

Quoted 4 lines

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#5 lore raw
In reply to Mark Rutland
Quoted 8 lines · Mark Rutland
Quoted 7 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Quoted 7 lines
Quoted 3 lines

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

That makes some sense, but doesn't it also mean you can jump into the middle of a leaf function and it will happily return to whatever sits in LR? Perhaps it would make sense to relax to the 'non-leaf' version only if stack protector is enabled?

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer
#6 lore raw
Quoted 13 lines · Will Deacon
Quoted 12 lines · Mark Rutland
Quoted 10 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

Quoted 6 lines
Quoted 3 lines

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

That makes some sense, but doesn't it also mean you can jump into the middle of a leaf function and it will happily return to whatever sits in LR?

If you can do that, you've already subverted control flow, and can probably do the same for a regular function, since for:

| AUTIASP
| RET

... you can just jump to the RET instead.

I agree that with RETAA/RETAB this would be different.

Quoted 2 lines

Perhaps it would make sense to relax to the 'non-leaf' version only if stack protector is enabled?

I'm not sure I follow the rationale for that? What does stack protector help with for leaf functions?

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer
#4 lore raw
In reply to Amit Kachhap
Quoted 42 lines · Amit Kachhap

Hi,

Quoted 18 lines · Mark Rutland
Quoted 10 lines · Will Deacon
Quoted 3 lines · Amit Daniel Kachhap

Compilers are optimized to not store the stack frame record for the leaf function in the stack so applying pointer authentication in the leaf function is not useful from security point of view.

I'm missing the reasoning here -- why don't we care about leaf functions?

Sounds like there's a performance/security trade-off that needs spelling out and justifying with some numbers, or is it clear-cut and I'm missing something?

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

Amit, if you create a leaf function like:

Quoted 7 lines

void leaf_function(void)

{
	asm volatile("" : : : "x30");
}

... what assembly does the compiler generate when passed -msign-return-address=non-leaf ?

This is the assembly generated in this case,

      80:       d503233f        paciasp
      84:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
      88:       910003fd        mov     x29, sp
        asm volatile("" : : : "x30");
}
      8c:       a8c17bfd        ldp     x29, x30, [sp], #16
      90:       d50323bf        autiasp
      94:       d65f03c0        ret
Quoted 1 line
Quoted 5 lines
Quoted 3 lines

* Does the compiler create a stack-frame for this function?
* Where does the compiler spill x30?
* Does the compiler sign the LR?

Yes here the compiler creates stack frame and pushes signed lr in the stack.

Thanks for testing that!

That looks fine. My concern was that the compiler might spill the register without signing it, but that's evidently not the case.

So 'leaf' is a misnomer here, and this is really about functions without frame records, but that's fine.

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#7 lore raw
In reply to Mark Rutland
Quoted 9 lines · Mark Rutland
Quoted 7 lines · Will Deacon
Quoted 3 lines · Mark Rutland

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

That makes some sense, but doesn't it also mean you can jump into the middle of a leaf function and it will happily return to whatever sits in LR?

Quoted 7 lines

If you can do that, you've already subverted control flow, and can probably do the same for a regular function, since for:

| AUTIASP
| RET

... you can just jump to the RET instead.

Perhaps, but it's not at all clear to me that being able to jump over the AUT instruction is just as easy or useful as being able to jump into the middle of a leaf function, which might act as a form of gadget. The commit message is quite bold in saying "[this] is not useful from security point of view".

How would this interact with BTI? Would we need to have different landing pads for leaf functions?

Quoted 5 lines
Quoted 2 lines

Perhaps it would make sense to relax to the 'non-leaf' version only if stack protector is enabled?

I'm not sure I follow the rationale for that? What does stack protector help with for leaf functions?

Yeah, course it doesn't help because we're not pushing a frame. Ignore me.

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

reviewer
#8 lore raw
Quoted 16 lines · Will Deacon
Quoted 15 lines · Mark Rutland
Quoted 7 lines · Will Deacon
Quoted 3 lines · Mark Rutland

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

That makes some sense, but doesn't it also mean you can jump into the middle of a leaf function and it will happily return to whatever sits in LR?

If you can do that, you've already subverted control flow, and can probably do the same for a regular function, since for:

| AUTIASP
| RET

Quoted 7 lines
Quoted 1 line

... you can just jump to the RET instead.

Perhaps, but it's not at all clear to me that being able to jump over the AUT instruction is just as easy or useful as being able to jump into the middle of a leaf function, which might act as a form of gadget. The commit message is quite bold in saying "[this] is not useful from security point of view".

Ah, I see.

You're right that this would give some number of potentially useful gadgets.

Quoted 2 lines

How would this interact with BTI? Would we need to have different landing pads for leaf functions?

IIRC the compiler would emit a BTI instruction where the PACIASP would have been, unless the function were only ever called directly in which case the BTI can also be omitted.

For functions that can only be called directly, this prevents the whole function (which might not be AAPCS compliant) from being a gadget. For functions that can be called indirectly, the only saving is the omission of AUTIASP, which I suspect is not a significant saving.

The tradeoff isn't clear to me.

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#9 lore raw

Hi Will/Mark,

Quoted 17 lines · Mark Rutland
Quoted 16 lines · Will Deacon
Quoted 15 lines · Mark Rutland
Quoted 7 lines · Will Deacon
Quoted 3 lines · Mark Rutland

I believe this is because leaf functions don't store the LR to the stack (as they don't create a frame record), so it cannot be modified by a stray memory write.

That makes some sense, but doesn't it also mean you can jump into the middle of a leaf function and it will happily return to whatever sits in LR?

If you can do that, you've already subverted control flow, and can probably do the same for a regular function, since for:

| AUTIASP
| RET

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

... you can just jump to the RET instead.

Perhaps, but it's not at all clear to me that being able to jump over the AUT instruction is just as easy or useful as being able to jump into the middle of a leaf function, which might act as a form of gadget. The commit message is quite bold in saying "[this] is not useful from security point of view".

I re-worded the commit log and posted the V2 version.

Cheers,
Amit

Quoted 18 lines

Ah, I see.

You're right that this would give some number of potentially useful gadgets.

Quoted 2 lines

How would this interact with BTI? Would we need to have different landing pads for leaf functions?

IIRC the compiler would emit a BTI instruction where the PACIASP would have been, unless the function were only ever called directly in which case the BTI can also be omitted.

For functions that can only be called directly, this prevents the whole function (which might not be AAPCS compliant) from being a gadget. For functions that can be called indirectly, the only saving is the omission of AUTIASP, which I suspect is not a significant saving.

Quoted 5 lines

The tradeoff isn't clear to me.

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel