Skip to contentSkip to conversation

gcc-plugins/stackleak: Use noinstr in favor of notrace

PATCH Superseded Kees Cook 4 participants 7 messages Feb 2, 2022 – Feb 7, 2022 Gcc plugins

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 gcc-plugins/stackleak: Use noinstr in favor of notrace

While the stackleak plugin was already using notrace, objtool is now a bit more picky. Update the notrace uses to noinstr. Silences these warnings:

vmlinux.o: warning: objtool: do_syscall_64()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_int80_syscall_32()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: exc_general_protection()+0x22: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: fixup_bad_iret()+0x20: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_machine_check()+0x27: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: .text+0x5346e: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x143: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x10eb: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x17f9: call to stackleak_erase() leaves .noinstr.text section

---
Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

---
 kernel/stackleak.c                     | 3 +--
 scripts/gcc-plugins/stackleak_plugin.c | 3 +++
 2 files changed, 4 insertions(+), 2 deletions(-)
kernel/stackleak.c +1 -2
index 66b8af394e58..72d4ebf49480 100644
@@ -70,7 +70,7 @@ late_initcall(stackleak_sysctls_init);
 #define skip_erasing()	false
 #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */
 
-asmlinkage void notrace stackleak_erase(void)
+asmlinkage void noinstr stackleak_erase(void)
 {
 	/* It would be nice not to have 'kstack_ptr' and 'boundary' on stack */
 	unsigned long kstack_ptr = current->lowest_stack;
@@ -124,7 +124,6 @@ asmlinkage void notrace stackleak_erase(void)
 	/* Reset the 'lowest_stack' value for the next syscall */
 	current->lowest_stack = current_top_of_stack() - THREAD_SIZE/64;
 }
-NOKPROBE_SYMBOL(stackleak_erase);
 
 void __used __no_caller_saved_registers notrace stackleak_track_stack(void)
 {
scripts/gcc-plugins/stackleak_plugin.c +3 -0
index e9db7dcb3e5f..e7e51f0eb597 100644
@@ -429,6 +429,7 @@ static unsigned int stackleak_cleanup_execute(void)
 	return 0;
 }
 
+/* Do not instrument anything found in special sections. */
 static bool stackleak_gate(void)
 {
 	tree section;
@@ -446,6 +447,8 @@ static bool stackleak_gate(void)
 			return false;
 		if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
 			return false;
+		if (!strncmp(TREE_STRING_POINTER(section), ".noinstr.text", 13))
+			return false;
 	}
 
 	return track_frame_size >= 0;
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Kees Cook

While the stackleak plugin was already using notrace, objtool is now a bit more picky. Update the notrace uses to noinstr. Silences these warnings:

vmlinux.o: warning: objtool: do_syscall_64()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_int80_syscall_32()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: exc_general_protection()+0x22: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: fixup_bad_iret()+0x20: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_machine_check()+0x27: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: .text+0x5346e: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x143: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x10eb: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x17f9: call to stackleak_erase() leaves .noinstr.text section

Quoted 8 lines

---
Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

Given "noinstr" means "no instrumentation", it seems entirely correct to me that noinstr functions should not be instrumented with stack utilization checks. I am surprised that those *were* instrumented, and arguably this is a fix that should be backported.

For stackleak_erase() itself, using noinstr certianly makes sense to me given the context in which it is called.

FWIW:

Mark.

Quoted 50 lines
---
 kernel/stackleak.c                     | 3 +--
 scripts/gcc-plugins/stackleak_plugin.c | 3 +++
 2 files changed, 4 insertions(+), 2 deletions(-)
kernel/stackleak.c +1 -2
index 66b8af394e58..72d4ebf49480 100644
@@ -70,7 +70,7 @@ late_initcall(stackleak_sysctls_init);
 #define skip_erasing()	false
 #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */
 
-asmlinkage void notrace stackleak_erase(void)
+asmlinkage void noinstr stackleak_erase(void)
 {
 	/* It would be nice not to have 'kstack_ptr' and 'boundary' on stack */
 	unsigned long kstack_ptr = current->lowest_stack;
@@ -124,7 +124,6 @@ asmlinkage void notrace stackleak_erase(void)
 	/* Reset the 'lowest_stack' value for the next syscall */
 	current->lowest_stack = current_top_of_stack() - THREAD_SIZE/64;
 }
-NOKPROBE_SYMBOL(stackleak_erase);
 
 void __used __no_caller_saved_registers notrace stackleak_track_stack(void)
 {
scripts/gcc-plugins/stackleak_plugin.c +3 -0
index e9db7dcb3e5f..e7e51f0eb597 100644
@@ -429,6 +429,7 @@ static unsigned int stackleak_cleanup_execute(void)
 	return 0;
 }
 
+/* Do not instrument anything found in special sections. */
 static bool stackleak_gate(void)
 {
 	tree section;
@@ -446,6 +447,8 @@ static bool stackleak_gate(void)
 			return false;
 		if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
 			return false;
+		if (!strncmp(TREE_STRING_POINTER(section), ".noinstr.text", 13))
+			return false;
 	}
 
 	return track_frame_size >= 0;
Signature · 1 line
2.30.2
#2 lore raw
In reply to Kees Cook

I was going to apply your patch, but then I read your note:

Quoted 4 lines · Kees Cook

Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

... and yes, it seems like stackleak_track_stack() should just be 'noinstr' just like you made stackleak_erase().

So I've dropped the patch to see what happens.

If you decide this is the right patch after all, you can just re-send it.

Linus

#3 lore raw
In reply to Kees Cook
Quoted 14 lines · Kees Cook

While the stackleak plugin was already using notrace, objtool is now a bit more picky. Update the notrace uses to noinstr. Silences these warnings:

vmlinux.o: warning: objtool: do_syscall_64()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_int80_syscall_32()+0x9: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: exc_general_protection()+0x22: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: fixup_bad_iret()+0x20: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: do_machine_check()+0x27: call to stackleak_track_stack() leaves .noinstr.text section vmlinux.o: warning: objtool: .text+0x5346e: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x143: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x10eb: call to stackleak_erase() leaves .noinstr.text section vmlinux.o: warning: objtool: .entry.text+0x17f9: call to stackleak_erase() leaves .noinstr.text section

Quoted 8 lines

---
Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

This patch is right. stackleak_track_stack() cannot be marked noinstr becaues it accesses things that might not be there.

Consider what happens if we pull the PTI page-table swap into the noinstr C part.

scripts/gcc-plugins/stackleak_plugin.c static bool stackleak_gate(void) · line 451

Quoted 6 lines
scripts/gcc-plugins/stackleak_plugin.c +2 -0
@@ -446,6 +447,8 @@ static bool stackleak_gate(void)
 			return false;
 		if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
 			return false;
+		if (!strncmp(TREE_STRING_POINTER(section), ".noinstr.text", 13))
+			return false;

For paranoia's sake I'd like .entry.text added there as well.

Quoted 3 lines
scripts/gcc-plugins/stackleak_plugin.c +0 -0
 	}
 
 	return track_frame_size >= 0;
author
#4 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Kees Cook

Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

This patch is right. stackleak_track_stack() cannot be marked noinstr becaues it accesses things that might not be there.

Hmm, as in "current()" may not be available/sane?

Quoted 2 lines

Consider what happens if we pull the PTI page-table swap into the noinstr C part.

Yeah, I see your point. I suspect the reason this all currently works is because stackleak is supposed to only instrument leaf functions that have sufficiently large (default 100 bytes) stack usage.

What sorts of things may end up in .noinstr.text that are 100+ byte stack leaf functions that would be end up deeper in the call stack? (i.e. what could get missed from stack depth tracking?) Interrupt handling comes to mind, but I'd expect that to make further calls (i.e. not a leaf).

Quoted 8 lines

scripts/gcc-plugins/stackleak_plugin.c static bool stackleak_gate(void) · line 451

Quoted 6 lines
scripts/gcc-plugins/stackleak_plugin.c +2 -0
@@ -446,6 +447,8 @@ static bool stackleak_gate(void)
 			return false;
 		if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
 			return false;
+		if (!strncmp(TREE_STRING_POINTER(section), ".noinstr.text", 13))
+			return false;

For paranoia's sake I'd like .entry.text added there as well.

Yeah, that's reasonable. Again, I think I see why this hasn't been an actual problem before, but given the constraints, I'd agree. It may allow for the paravirt special-case to be removed as well.

I'll send a follow-up patch...

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 10 lines · Kees Cook
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Kees Cook

Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

This patch is right. stackleak_track_stack() cannot be marked noinstr becaues it accesses things that might not be there.

Hmm, as in "current()" may not be available/sane?

Exactly the case; if we lift the PTI address space swizzle, we start with C without having the kernel mapped or even the per-cpu segment offset set. So things like current will explode.

The whole noinstr thing was invented to get back to C as portable Assembler, with the express purpose to lift a bunch of entry code to C.

Quoted 11 lines
Quoted 2 lines

Consider what happens if we pull the PTI page-table swap into the noinstr C part.

Yeah, I see your point. I suspect the reason this all currently works is because stackleak is supposed to only instrument leaf functions that have sufficiently large (default 100 bytes) stack usage.

What sorts of things may end up in .noinstr.text that are 100+ byte stack leaf functions that would be end up deeper in the call stack? (i.e. what could get missed from stack depth tracking?) Interrupt handling comes to mind, but I'd expect that to make further calls (i.e. not a leaf).

All the syscall/exception/interrupt entry stuff is noinstr; I don't think we have huge stackframes, but with all that in C that's much easier to do than with then in asm.

If you worry about this, it should be possible to have objtool warn about excessive stack frames for noinstr code I suppose, it already tracks the stack anyway.

author
#6 lore raw
Quoted 31 lines · Peter Zijlstra
Quoted 10 lines · Kees Cook
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Kees Cook

Is it correct to exclude .noinstr.text here? That means any functions called in there will have their stack utilization untracked. This doesn't seem right to me, though. Shouldn't stackleak_track_stack() just be marked noinstr instead?

This patch is right. stackleak_track_stack() cannot be marked noinstr becaues it accesses things that might not be there.

Hmm, as in "current()" may not be available/sane?

Exactly the case; if we lift the PTI address space swizzle, we start with C without having the kernel mapped or even the per-cpu segment offset set. So things like current will explode.

The whole noinstr thing was invented to get back to C as portable Assembler, with the express purpose to lift a bunch of entry code to C.

Quoted 11 lines
Quoted 2 lines

Consider what happens if we pull the PTI page-table swap into the noinstr C part.

Yeah, I see your point. I suspect the reason this all currently works is because stackleak is supposed to only instrument leaf functions that have sufficiently large (default 100 bytes) stack usage.

What sorts of things may end up in .noinstr.text that are 100+ byte stack leaf functions that would be end up deeper in the call stack? (i.e. what could get missed from stack depth tracking?) Interrupt handling comes to mind, but I'd expect that to make further calls (i.e. not a leaf).

Quoted 7 lines

All the syscall/exception/interrupt entry stuff is noinstr; I don't think we have huge stackframes, but with all that in C that's much easier to do than with then in asm.

If you worry about this, it should be possible to have objtool warn about excessive stack frames for noinstr code I suppose, it already tracks the stack anyway.

Yeah, I think we should be okay at least for now.

Let me know what you think of
https://lore.kernel.org/linux-hardening/[email protected]/
and if you like it I can send a v2 Linus's way...

-Kees

Signature · 1 line
Kees Cook