Skip to contentSkip to conversation

stack: replace "o" output with "r" input constraint

PATCH Applied Kees Cook 3 participants 5 messages Apr 19, 2021 – May 11, 2021 Kernel hardening (not covered by other areas)

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 stack: replace "o" output with "r" input constraint

From: Nick Desaulniers <[email protected]>

"o" isn't a common asm() constraint to use; it triggers an assertion in assert-enabled builds of LLVM that it's not recognized when targeting aarch64 (though it appears to fall back to "m"). I've fixed this in LLVM 13 now, but there isn't really a good reason to be using "o" in particular here. To avoid causing build issues for those using assert-enabled builds of earlier LLVM versions, the constraint needs changing.

Instead, if the point is to retain the __builtin_alloca(), we can make ptr appear to "escape" via being an input to an empty inline asm block. This is preferable anyways, since otherwise this looks like a dead store.

While the use of "r" was considered in
https://lore.kernel.org/lkml/202104011447.2E7F543@keescook/
it was only tested as an output (which looks like a dead store, and wasn't sufficient). Use "r" as an input constraint instead, which behaves correctly across compilers and architectures:
https://godbolt.org/z/E9cd411ob

Fixes 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")
---
 include/linux/randomize_kstack.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/randomize_kstack.h +1 -1
index fd80fab663a9..bebc911161b6 100644
@@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
 		u32 offset = raw_cpu_read(kstack_offset);		\
 		u8 *ptr = __builtin_alloca(KSTACK_OFFSET_MAX(offset));	\
 		/* Keep allocation even after "ptr" loses scope. */	\
-		asm volatile("" : "=o"(*ptr) :: "memory");		\
+		asm volatile("" :: "r"(ptr) : "memory");		\
 	}								\
 } while (0)
 
Signature · 1 line
2.25.1
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 20 lines · Kees Cook

From: Nick Desaulniers <[email protected]>

"o" isn't a common asm() constraint to use; it triggers an assertion in assert-enabled builds of LLVM that it's not recognized when targeting aarch64 (though it appears to fall back to "m"). I've fixed this in LLVM 13 now, but there isn't really a good reason to be using "o" in particular here. To avoid causing build issues for those using assert-enabled builds of earlier LLVM versions, the constraint needs changing.

Instead, if the point is to retain the __builtin_alloca(), we can make ptr appear to "escape" via being an input to an empty inline asm block. This is preferable anyways, since otherwise this looks like a dead store.

While the use of "r" was considered in
https://lore.kernel.org/lkml/202104011447.2E7F543@keescook/
it was only tested as an output (which looks like a dead store, and wasn't sufficient). Use "r" as an input constraint instead, which behaves correctly across compilers and architectures:
https://godbolt.org/z/E9cd411ob

Quoted 6 lines
Fixes 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")

I built arm64 defconfig with and without CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT with LLVM 12 (which does not have Nick's LLVM fix) without any issues and did a quick boot test in QEMU, nothing exploded.

Quoted 20 lines
---
 include/linux/randomize_kstack.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/randomize_kstack.h +1 -1
index fd80fab663a9..bebc911161b6 100644
@@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
 		u32 offset = raw_cpu_read(kstack_offset);		\
 		u8 *ptr = __builtin_alloca(KSTACK_OFFSET_MAX(offset));	\
 		/* Keep allocation even after "ptr" loses scope. */	\
-		asm volatile("" : "=o"(*ptr) :: "memory");		\
+		asm volatile("" :: "r"(ptr) : "memory");		\
 	}								\
 } while (0)
 
Signature · 1 line
2.25.1
#2 lore raw
Quoted 21 lines · Nathan Chancellor
Quoted 20 lines · Kees Cook

From: Nick Desaulniers <[email protected]>

"o" isn't a common asm() constraint to use; it triggers an assertion in assert-enabled builds of LLVM that it's not recognized when targeting aarch64 (though it appears to fall back to "m"). I've fixed this in LLVM 13 now, but there isn't really a good reason to be using "o" in particular here. To avoid causing build issues for those using assert-enabled builds of earlier LLVM versions, the constraint needs changing.

Instead, if the point is to retain the __builtin_alloca(), we can make ptr appear to "escape" via being an input to an empty inline asm block. This is preferable anyways, since otherwise this looks like a dead store.

While the use of "r" was considered in
https://lore.kernel.org/lkml/202104011447.2E7F543@keescook/
it was only tested as an output (which looks like a dead store, and wasn't sufficient). Use "r" as an input constraint instead, which behaves correctly across compilers and architectures:
https://godbolt.org/z/E9cd411ob

Quoted 5 lines
Quoted 5 lines
Fixes 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")

Kees, were you planning on taking this to Linus or someone else? It would be nice to have this in for -rc1 (although I understand it might be too late), if not, by -rc2.

Cheers,
Nathan

Quoted 30 lines
Quoted 1 line

I built arm64 defconfig with and without CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT with LLVM 12 (which does not have Nick's LLVM fix) without any issues and did a quick boot test in QEMU, nothing exploded.

Quoted 20 lines
---
 include/linux/randomize_kstack.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/randomize_kstack.h +1 -1
index fd80fab663a9..bebc911161b6 100644
@@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
 		u32 offset = raw_cpu_read(kstack_offset);		\
 		u8 *ptr = __builtin_alloca(KSTACK_OFFSET_MAX(offset));	\
 		/* Keep allocation even after "ptr" loses scope. */	\
-		asm volatile("" : "=o"(*ptr) :: "memory");		\
+		asm volatile("" :: "r"(ptr) : "memory");		\
 	}								\
 } while (0)
 
Signature · 1 line
2.25.1
author
#3 lore raw
Quoted 28 lines · Nathan Chancellor
Quoted 21 lines · Nathan Chancellor
Quoted 20 lines · Kees Cook

From: Nick Desaulniers <[email protected]>

"o" isn't a common asm() constraint to use; it triggers an assertion in assert-enabled builds of LLVM that it's not recognized when targeting aarch64 (though it appears to fall back to "m"). I've fixed this in LLVM 13 now, but there isn't really a good reason to be using "o" in particular here. To avoid causing build issues for those using assert-enabled builds of earlier LLVM versions, the constraint needs changing.

Instead, if the point is to retain the __builtin_alloca(), we can make ptr appear to "escape" via being an input to an empty inline asm block. This is preferable anyways, since otherwise this looks like a dead store.

While the use of "r" was considered in
https://lore.kernel.org/lkml/202104011447.2E7F543@keescook/
it was only tested as an output (which looks like a dead store, and wasn't sufficient). Use "r" as an input constraint instead, which behaves correctly across compilers and architectures:
https://godbolt.org/z/E9cd411ob

Quoted 5 lines
Quoted 5 lines
Fixes 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")
Quoted 3 lines

Kees, were you planning on taking this to Linus or someone else? It would be nice to have this in for -rc1 (although I understand it might be too late), if not, by -rc2.

I assumed Thomas would pick this up. Thomas, shall I send this directly to Linus?

Thanks!

-Kees

Quoted 34 lines

Cheers,
Nathan

Quoted 30 lines
Quoted 1 line

I built arm64 defconfig with and without CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT with LLVM 12 (which does not have Nick's LLVM fix) without any issues and did a quick boot test in QEMU, nothing exploded.

Quoted 20 lines
---
 include/linux/randomize_kstack.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/randomize_kstack.h +1 -1
index fd80fab663a9..bebc911161b6 100644
@@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
 		u32 offset = raw_cpu_read(kstack_offset);		\
 		u8 *ptr = __builtin_alloca(KSTACK_OFFSET_MAX(offset));	\
 		/* Keep allocation even after "ptr" loses scope. */	\
-		asm volatile("" : "=o"(*ptr) :: "memory");		\
+		asm volatile("" :: "r"(ptr) : "memory");		\
 	}								\
 } while (0)
 
Signature · 1 line
2.25.1
Signature · 1 line
Kees Cook
#4 lore raw

[tip: core/urgent] stack: Replace "o" output with "r" input constraint

In reply to Kees Cook
The following commit has been merged into the core/urgent branch of tip:

The following commit has been merged into the core/urgent branch of tip:

Commit-ID:     2515dd6ce8e545b0b2eece84920048ef9ed846c4
Gitweb:        https://git.kernel.org/tip/2515dd6ce8e545b0b2eece84920048ef9ed846c4
Author:        Nick Desaulniers <[email protected]>
AuthorDate:    Mon, 19 Apr 2021 16:17:41 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 11 May 2021 09:56:11 +02:00

stack: Replace "o" output with "r" input constraint

"o" isn't a common asm() constraint to use; it triggers an assertion in assert-enabled builds of LLVM that it's not recognized when targeting aarch64 (though it appears to fall back to "m"). It's fixed in LLVM 13 now, but there isn't really a good reason to use "o" in particular here. To avoid causing build issues for those using assert-enabled builds of earlier LLVM versions, the constraint needs changing.

Instead, if the point is to retain the __builtin_alloca(), make ptr appear to "escape" via being an input to an empty inline asm block. This is preferable anyways, since otherwise this looks like a dead store.

While the use of "r" was considered in

https://lore.kernel.org/lkml/202104011447.2E7F543@keescook/

it was only tested as an output (which looks like a dead store, and wasn't sufficient).

Use "r" as an input constraint instead, which behaves correctly across compilers and architectures.

---
 include/linux/randomize_kstack.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/randomize_kstack.h +1 -1
index fd80fab..bebc911 100644
@@ -38,7 +38,7 @@ void *__builtin_alloca(size_t size);
 		u32 offset = raw_cpu_read(kstack_offset);		\
 		u8 *ptr = __builtin_alloca(KSTACK_OFFSET_MAX(offset));	\
 		/* Keep allocation even after "ptr" loses scope. */	\
-		asm volatile("" : "=o"(*ptr) :: "memory");		\
+		asm volatile("" :: "r"(ptr) : "memory");		\
 	}								\
 } while (0)