Skip to contentSkip to conversation

x86/shstk: Enable shadow stack for x32

PATCH Applied H.J. Lu 5 participants 16 messages Mar 15, 2024 – Mar 22, 2024

Subsystems (2)
lore
Share & tools
lore

H.J. Lu

author
#0 lore raw

PATCH 1/1 x86/shstk: Enable shadow stack for x32

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

---
 arch/x86/entry/syscalls/syscall_64.tbl | 2 +-
 arch/x86/kernel/shstk.c                | 4 ++--
 arch/x86/kernel/signal_64.c            | 6 ++++++
 3 files changed, 9 insertions(+), 3 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -1
index 7e8d46f4147f..cc78226ffc35 100644
@@ -374,7 +374,7 @@
 450	common	set_mempolicy_home_node	sys_set_mempolicy_home_node
 451	common	cachestat		sys_cachestat
 452	common	fchmodat2		sys_fchmodat2
-453	64	map_shadow_stack	sys_map_shadow_stack
+453	common	map_shadow_stack	sys_map_shadow_stack
 454	common	futex_wake		sys_futex_wake
 455	common	futex_wait		sys_futex_wait
 456	common	futex_requeue		sys_futex_requeue
arch/x86/kernel/shstk.c +2 -2
index 59e15dd8d0f8..6f1e9883f074 100644
@@ -163,8 +163,8 @@ static int shstk_setup(void)
 	if (features_enabled(ARCH_SHSTK_SHSTK))
 		return 0;
 
-	/* Also not supported for 32 bit and x32 */
-	if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_32bit_syscall())
+	/* Also not supported for 32 bit */
+	if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_ia32_syscall())
 		return -EOPNOTSUPP;
 
 	size = adjust_shstk_size(0);
arch/x86/kernel/signal_64.c +6 -0
index 23d8aaf8d9fd..8a94053c5444 100644
@@ -315,6 +315,9 @@ int x32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
 
 	uc_flags = frame_uc_flags(regs);
 
+	if (setup_signal_shadow_stack(ksig))
+		return -EFAULT;
+
 	if (!user_access_begin(frame, sizeof(*frame)))
 		return -EFAULT;
 
@@ -377,6 +380,9 @@ COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
 	if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
 		goto badframe;
 
+	if (restore_signal_shadow_stack())
+		goto badframe;
+
 	if (compat_restore_altstack(&frame->uc.uc_stack))
 		goto badframe;
 
Signature · 1 line
2.44.0
#1 lore raw
Quoted 9 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

How many people do you think will use this?

I would have thought it would require more changes for basic x32 operation. What was the testing exactly?

author
#2 lore raw
Quoted 8 lines · Rick P
Quoted 6 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

Quoted 7 lines
Quoted 3 lines

How many people do you think will use this?

I would have thought it would require more changes for basic x32

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled. There are no regressions. I verified that shadow stack is enabled via /proc/pid/status.

Signature · 1 line
H.J.
#3 lore raw
Quoted 1 line · H.J. Lu
Quoted 1 line

How many people do you think will use this?

I'm concerned that the only use of this will ever be exercise via the glibc unit tests, but will still require work to support.

Quoted 11 lines
Quoted 2 lines

I would have thought it would require more changes for basic x32

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled.  There are no regressions.  I verified that shadow stack is enabled via /proc/pid/status.

The shadow stack is supposed to be mapped above 4G, so how is this supposed to work for x32?

#4 lore raw
Quoted 5 lines · Rick Edgecombe
Quoted 1 line · H.J. Lu
Quoted 1 line

How many people do you think will use this?

I'm concerned that the only use of this will ever be exercise via the glibc unit tests, but will still require work to support.

To elaborate more on this... The main usage of shadow stack is security, and comes with some overhead. IIUC the main usage of x32 is performance benchmarking type stuff. Why would someone want to use shadow stack and x32 together?

Quoted 15 lines
Quoted 4 lines
Quoted 3 lines

I would have thought it would require more changes for basic x32

Quoted 8 lines

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled.  There are no regressions.  I verified that shadow stack is enabled via /proc/pid/status.

The shadow stack is supposed to be mapped above 4G, so how is this supposed to work for x32?

author
#5 lore raw
In reply to Edgecombe, Rick P
Quoted 6 lines · Rick P
Quoted 1 line · H.J. Lu
Quoted 1 line

How many people do you think will use this?

I'm concerned that the only use of this will ever be exercise via the glibc unit tests, but will still require work to support.

Correct. A small glibc change is needed. Will post it after
my kernel change is merged.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

I would have thought it would require more changes for basic x32

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

Quoted 7 lines
Quoted 4 lines

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled. There are no regressions. I verified that shadow stack is enabled via /proc/pid/status.

The shadow stack is supposed to be mapped above 4G, so how is this supposed to work for x32?

This is not what I see:

(gdb) info reg
...
pl3_ssp 0xf7dcbfe8 0xf7dcbfe8

--
H.J.

#6 lore raw
Quoted 4 lines · H.J. Lu
Quoted 2 lines · Rick P

On Fri, 2024-03-15 at 07:34 -0700, H.J. Lu wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

How many people do you think will use this?

I'm concerned that the only use of this will ever be exercise via the
glibc unit tests, but will still require work to support.

 Correct.  A small glibc change is needed.  Will post it after my kernel change is merged.

I mean it will require kernel work in the future to maintain support. That we will have to think about x32 effects when making other shadow stack changes.

I'll paste my other comment in this thread:

The main usage of shadow stack is security, and comes with some overhead. IIUC the main usage of x32 is performance benchmarking type stuff. Why would someone want to use shadow stack and x32 together?

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 5 lines

I would have thought it would require more changes for basic x32

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled.  There are no regressions.  I verified that shadow stack is enabled via /proc/pid/status.

Quoted 8 lines
Quoted 2 lines

The shadow stack is supposed to be mapped above 4G, so how is this supposed to work for x32?

This is not what I see:

(gdb) info reg
...
pl3_ssp        0xf7dcbfe8          0xf7dcbfe8

The mapping above 4G was because Peterz raised the possibility that a 64 bit process could far call into a 32 bit segment and start doing signal stuff that would encounter undefined behavior. He wanted it cleanly blocked. So by keeping the shadow stack above 4GB, existing processes that turned on shadow stack would be preventing from transitioning to 32 bit and encountering the missing 32 bit signal support (because the CPU would #GP during the 32 bit transition if SSP is above 4GB).

Probably there is some interplay between the x32 mmap logic and shadow stacks mapping, where it then becomes possible to get below 4GB. Since x32 needs the shadow stack to be below 4GB, it's incompatible with that solution. So this patch is not sufficient to enable x32 without side effects that were previously considered bad.

I see this is in tip now. I don't think it's a good idea to support upstream. The implications need more discussion, and there doesn't seem to be any real end user value.

author
#7 lore raw
Quoted 12 lines · Rick P
Quoted 10 lines · H.J. Lu
Quoted 7 lines · Rick P
Quoted 1 line · H.J. Lu
Quoted 1 line

How many people do you think will use this?

I'm concerned that the only use of this will ever be exercise via the
glibc unit tests, but will still require work to support.

Quoted 6 lines
Quoted 2 lines

Correct. A small glibc change is needed. Will post it after
my kernel change is merged.

I mean it will require kernel work in the future to maintain support. That we will have to think about x32 effects when making other shadow stack changes.

It is way more than kernel SHSTK self tests.

Quoted 5 lines

I'll paste my other comment in this thread:

The main usage of shadow stack is security, and comes with some overhead. IIUC the main usage of x32 is performance benchmarking type stuff. Why would someone want to use shadow stack and x32 together?

Improve x32 security and user space IBT will add more improvement.

Quoted 33 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 5 lines

I would have thought it would require more changes for basic x32

This is all needed.

Quoted 1 line

operation. What was the testing exactly?

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled. There are no regressions. I verified that shadow stack is enabled via /proc/pid/status.

Quoted 8 lines
Quoted 2 lines

The shadow stack is supposed to be mapped above 4G, so how is this supposed to work for x32?

This is not what I see:

(gdb) info reg
...
pl3_ssp 0xf7dcbfe8 0xf7dcbfe8

The mapping above 4G was because Peterz raised the possibility that a 64 bit process could far call into a 32 bit segment and start doing signal stuff that would encounter undefined behavior. He wanted it cleanly blocked. So by keeping the shadow stack above 4GB, existing processes that turned on shadow stack would be preventing from transitioning to 32 bit and encountering the missing 32 bit signal support (because the CPU would #GP during the 32 bit transition if SSP is above 4GB).

Quoted 5 lines

Probably there is some interplay between the x32 mmap logic and shadow stacks mapping, where it then becomes possible to get below 4GB. Since x32 needs the shadow stack to be below 4GB, it's incompatible with that solution. So this patch is not sufficient to enable x32 without side effects that were previously considered bad.

Mapping shadow stack below 4GB on x32 still provides security improvements over no show stack.

Quoted 3 lines

I see this is in tip now. I don't think it's a good idea to support upstream. The implications need more discussion, and there doesn't seem to be any real end user value.

Signature · 1 line
H.J.
#8 lore raw
Quoted 8 lines · H.J. Lu
Quoted 5 lines

I mean it will require kernel work in the future to maintain support.
That we will have to think about x32 effects when making other shadow
stack changes.

It is way more than kernel SHSTK self tests.

Quoted 8 lines
Quoted 6 lines

I'll paste my other comment in this thread:

The main usage of shadow stack is security, and comes with some overhead. IIUC the main usage of x32 is performance benchmarking type
stuff. Why would someone want to use shadow stack and x32 together?

Improve x32 security and user space IBT will add more improvement.

Please elaborate on the users that will use x32 and shadow stack together. How many people are we talking about? They care enough about performance to use x32, but also don't mind overhead to increase security? Perhaps I'm missing something on what x32 is used for today.

[snip]

Quoted 22 lines
Quoted 12 lines

The mapping above 4G was because Peterz raised the possibility that a
64 bit process could far call into a 32 bit segment and start doing signal stuff that would encounter undefined behavior. He wanted it cleanly blocked. So by keeping the shadow stack above 4GB, existing processes that turned on shadow stack would be preventing from transitioning to 32 bit and encountering the missing 32 bit signal support (because the CPU would #GP during the 32 bit transition if SSP
is above 4GB).

Quoted 9 lines

Probably there is some interplay between the x32 mmap logic and shadow
stacks mapping, where it then becomes possible to get below 4GB. Since
x32 needs the shadow stack to be below 4GB, it's incompatible with that
solution. So this patch is not sufficient to enable x32 without side
effects that were previously considered bad.

Quoted 3 lines

Mapping shadow stack below 4GB on x32 still provides security improvements
over no show stack.

Agreed on this point. I don't think x32 shadow stack has to be perfect to improve security of the x32 apps.

But Peterz's concern (I think it could probably be re-opened) was that the ia32 signal stuff should not be just declared unsupported with shadow stack, but blocked from being used somehow. So it was really about being able to not have to think about the implications of the undefined behavior. (was my understanding at least)

This patch is just turning things on and finding that nothing crashes. It needs more analysis.

author
#9 lore raw
Quoted 17 lines · Rick P
Quoted 15 lines · H.J. Lu
Quoted 5 lines

I mean it will require kernel work in the future to maintain support.
That we will have to think about x32 effects when making other shadow
stack changes.

It is way more than kernel SHSTK self tests.

Quoted 6 lines

I'll paste my other comment in this thread:

The main usage of shadow stack is security, and comes with some overhead. IIUC the main usage of x32 is performance benchmarking type
stuff. Why would someone want to use shadow stack and x32 together?

Quoted 6 lines
Quoted 1 line

Improve x32 security and user space IBT will add more improvement.

Please elaborate on the users that will use x32 and shadow stack together. How many people are we talking about? They care enough about performance to use x32, but also don't mind overhead to increase security? Perhaps I'm missing something on what x32 is used for today.

SHSTK is enabled by -fcf-protection which is the default for some OSes where x32 binaries are SHSTK enabled already. Enable SHSTK should have minimum performance overhead.

Quoted 32 lines

[snip]

Quoted 22 lines
Quoted 12 lines

The mapping above 4G was because Peterz raised the possibility that a
64 bit process could far call into a 32 bit segment and start doing signal stuff that would encounter undefined behavior. He wanted it cleanly blocked. So by keeping the shadow stack above 4GB, existing processes that turned on shadow stack would be preventing from transitioning to 32 bit and encountering the missing 32 bit signal support (because the CPU would #GP during the 32 bit transition if SSP
is above 4GB).

Quoted 9 lines

Probably there is some interplay between the x32 mmap logic and shadow
stacks mapping, where it then becomes possible to get below 4GB. Since
x32 needs the shadow stack to be below 4GB, it's incompatible with that
solution. So this patch is not sufficient to enable x32 without side
effects that were previously considered bad.

Quoted 3 lines

Mapping shadow stack below 4GB on x32 still provides security improvements
over no show stack.

Agreed on this point. I don't think x32 shadow stack has to be perfect to improve security of the x32 apps.

Quoted 9 lines

But Peterz's concern (I think it could probably be re-opened) was that the ia32 signal stuff should not be just declared unsupported with shadow stack, but blocked from being used somehow. So it was really about being able to not have to think about the implications of the undefined behavior. (was my understanding at least)

This patch is just turning things on and finding that nothing crashes. It needs more analysis.

It is more than that. There are 3 glibc tests to check if SHSTK triggers
show stack violation when there is an intentional show stack mismatch:

[hjl@gnu-tgl-1 build-x86_64-linux]$ ./elf/tst-shstk-legacy-1b [hjl@gnu-tgl-1 build-x86_64-linux]$ ./elf/tst-shstk-legacy-1b --direct Segmentation fault (core dumped)
[hjl@gnu-tgl-1 build-x86_64-linux]$ GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK ./elf/tst-shstk-legacy-1b --direct
[hjl@gnu-tgl-1 build-x86_64-linux]$

They pass on x32.

Signature · 1 line
H.J.
merge bot Applied
#10 lore raw

[tip: x86/shstk] x86/shstk: Enable shadow stacks for x32

In reply to H.J. Lu
The following commit has been merged into the x86/shstk branch of tip:

The following commit has been merged into the x86/shstk branch of tip:

Commit-ID:     2883f01ec37dd8668e7222dfdb5980c86fdfe277
Gitweb:        https://git.kernel.org/tip/2883f01ec37dd8668e7222dfdb5980c86fdfe277
Author:        H.J. Lu <[email protected]>
AuthorDate:    Fri, 15 Mar 2024 07:04:33 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 22 Mar 2024 10:17:11 +01:00

x86/shstk: Enable shadow stacks for x32

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake:

I configured x32 glibc with --enable-cet, build glibc and run all glibc tests with shadow stack enabled. There are no regressions. I verified that shadow stack is enabled via /proc/pid/status.

---
 arch/x86/entry/syscalls/syscall_64.tbl | 2 +-
 arch/x86/kernel/shstk.c                | 4 ++--
 arch/x86/kernel/signal_64.c            | 6 ++++++
 3 files changed, 9 insertions(+), 3 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -1
index 7e8d46f..cc78226 100644
@@ -374,7 +374,7 @@
 450	common	set_mempolicy_home_node	sys_set_mempolicy_home_node
 451	common	cachestat		sys_cachestat
 452	common	fchmodat2		sys_fchmodat2
-453	64	map_shadow_stack	sys_map_shadow_stack
+453	common	map_shadow_stack	sys_map_shadow_stack
 454	common	futex_wake		sys_futex_wake
 455	common	futex_wait		sys_futex_wait
 456	common	futex_requeue		sys_futex_requeue
arch/x86/kernel/shstk.c +2 -2
index 59e15dd..6f1e988 100644
@@ -163,8 +163,8 @@ static int shstk_setup(void)
 	if (features_enabled(ARCH_SHSTK_SHSTK))
 		return 0;
 
-	/* Also not supported for 32 bit and x32 */
-	if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_32bit_syscall())
+	/* Also not supported for 32 bit */
+	if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_ia32_syscall())
 		return -EOPNOTSUPP;
 
 	size = adjust_shstk_size(0);
arch/x86/kernel/signal_64.c +6 -0
index 23d8aaf..8a94053 100644
@@ -315,6 +315,9 @@ int x32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
 
 	uc_flags = frame_uc_flags(regs);
 
+	if (setup_signal_shadow_stack(ksig))
+		return -EFAULT;
+
 	if (!user_access_begin(frame, sizeof(*frame)))
 		return -EFAULT;
 
@@ -377,6 +380,9 @@ COMPAT_SYSCALL_DEFINE0(x32_rt_sigreturn)
 	if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
 		goto badframe;
 
+	if (restore_signal_shadow_stack())
+		goto badframe;
+
 	if (compat_restore_altstack(&frame->uc.uc_stack))
 		goto badframe;
 
maintainer
#11 lore raw

Re: [tip: x86/shstk] x86/shstk: Enable shadow stacks for x32

Quoted 9 lines · On Fri, Mar 22, 2024 at 10:59:58AM -0000, tip-bot2 for H.J. Lu wrote:

The following commit has been merged into the x86/shstk branch of tip:

Commit-ID:     2883f01ec37dd8668e7222dfdb5980c86fdfe277
Gitweb:        https://git.kernel.org/tip/2883f01ec37dd8668e7222dfdb5980c86fdfe277
Author:        H.J. Lu <[email protected]>
AuthorDate:    Fri, 15 Mar 2024 07:04:33 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 22 Mar 2024 10:17:11 +01:00
Quoted 5 lines

x86/shstk: Enable shadow stacks for x32

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Why are we still diddling with x32 and not patching it out instead?

https://lore.kernel.org/all/CALCETrXoRAibsbWa9nfbDrt0iEuebMnCMhSFg-d9W-J2g8mDjw@mail.gmail.com/

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#12 lore raw
In reply to H.J. Lu
Quoted 5 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

I don't think we should wire up code that's never going to get used in practice. I think I'd want to be sure of the existence of some specific, real-world, long-term users of this before we add a new ABI that we need to support forever.

Are there real users?

author
#13 lore raw
Quoted 6 lines · Dave Hansen
Quoted 4 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Quoted 8 lines
Quoted 1 line

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

I don't think we should wire up code that's never going to get used in practice. I think I'd want to be sure of the existence of some specific, real-world, long-term users of this before we add a new ABI that we need to support forever.

Are there real users?

Were you talking about x32 users or x32 users with shadow stack?

Signature · 1 line
H.J.
#14 lore raw
Quoted 6 lines · H.J. Lu
Quoted 5 lines · Dave Hansen
Quoted 4 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

I don't think we should wire up code that's never going to get used in practice. I think I'd want to be sure of the existence of some specific, real-world, long-term users of this before we add a new ABI that we need to support forever.

Are there real users?

Were you talking about x32 users or x32 users with shadow stack?

x32 users who both want and will use shadow stacks.

It's been a long time since I've seen an x32 bug report.

author
#15 lore raw
Quoted 14 lines · Dave Hansen
Quoted 12 lines · H.J. Lu
Quoted 11 lines · Dave Hansen
Quoted 5 lines · H.J. Lu

1. Add shadow stack support to x32 signal.
2. Use the 64-bit map_shadow_stack syscall for x32.
3. Set up shadow stack for x32.

Tested with shadow stack enabled x32 glibc on Intel Tiger Lake.

I don't think we should wire up code that's never going to get used in practice. I think I'd want to be sure of the existence of some specific, real-world, long-term users of this before we add a new ABI that we need to support forever.

Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Are there real users?

Were you talking about x32 users or x32 users with shadow stack?

x32 users who both want and will use shadow stacks.

Someone can do a survey among x32 users.

Quoted 1 line

It's been a long time since I've seen an x32 bug report.

Because it just works?

Signature · 1 line
H.J.