Skip to contentSkip to conversation

x86: suppress KMSAN reports in arch_within_stack_frames()

PATCH In review Alexander Potapenko 3 participants 9 messages Nov 18, 2022 – Jan 27, 2023 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: suppress KMSAN reports in arch_within_stack_frames()

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/include/asm/thread_info.h +5 -0
index f0cb881c1d690..f1cccba52eb97 100644
@@ -163,7 +163,12 @@ struct thread_info {
  *	GOOD_FRAME	if within a frame
  *	BAD_STACK	if placed across a frame boundary (or outside stack)
  *	NOT_STACK	unable to determine (no frame pointers, etc)
+ *
+ * This function reads pointers from the stack and dereferences them. The
+ * pointers may not have their KMSAN shadow set up properly, which may result
+ * in false positive reports. Disable instrumentation to avoid those.
  */
+__no_kmsan_checks
 static inline int arch_within_stack_frames(const void * const stack,
 					   const void * const stackend,
 					   const void *obj, unsigned long len)
Signature · 1 line
2.38.1.584.g0f3c55d4c2-goog
#1 lore raw
Quoted 21 lines · Alexander Potapenko

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/include/asm/thread_info.h +0 -0
index f0cb881c1d690..f1cccba52eb97 100644
@@ -163,7 +163,12 @@ struct thread_info {
  *	GOOD_FRAME	if within a frame
  *	BAD_STACK	if placed across a frame boundary (or outside stack)

arch/x86/include/asm/thread_info.h struct thread_info { · line 174

Quoted 10 lines
arch/x86/include/asm/thread_info.h +5 -0
  *	NOT_STACK	unable to determine (no frame pointers, etc)
+ *
+ * This function reads pointers from the stack and dereferences them. The
+ * pointers may not have their KMSAN shadow set up properly, which may result
+ * in false positive reports. Disable instrumentation to avoid those.
  */
+__no_kmsan_checks
 static inline int arch_within_stack_frames(const void * const stack,
 					   const void * const stackend,
 					   const void *obj, unsigned long len)

Seems OK; but now I'm confused as to the exact distinction between __no_sanitize_memory and __no_kmsan_checks.

The comments there about seem to suggest __no_sanitize_memory ensures no instrumentation at all, and __no_kmsan_checks some instrumentation but doesn't actually check anything -- so what's left then?

#2 lore raw
Quoted 37 lines · Peter Zijlstra
Quoted 21 lines · Alexander Potapenko

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/include/asm/thread_info.h +0 -0
index f0cb881c1d690..f1cccba52eb97 100644
@@ -163,7 +163,12 @@ struct thread_info {
  *   GOOD_FRAME      if within a frame
  *   BAD_STACK       if placed across a frame boundary (or outside stack)

arch/x86/include/asm/thread_info.h struct thread_info { · line 174

Quoted 10 lines
arch/x86/include/asm/thread_info.h +5 -0
  *   NOT_STACK       unable to determine (no frame pointers, etc)
+ *
+ * This function reads pointers from the stack and dereferences them. The
+ * pointers may not have their KMSAN shadow set up properly, which may result
+ * in false positive reports. Disable instrumentation to avoid those.
  */
+__no_kmsan_checks
 static inline int arch_within_stack_frames(const void * const stack,
                                         const void * const stackend,
                                         const void *obj, unsigned long len)

Seems OK; but now I'm confused as to the exact distinction between __no_sanitize_memory and __no_kmsan_checks.

Quoted 3 lines

The comments there about seem to suggest __no_sanitize_memory ensures no instrumentation at all, and __no_kmsan_checks some instrumentation but doesn't actually check anything -- so what's left then?

__no_sanitize_memory prohibits all instrumentation whatsoever, whereas __no_kmsan_checks adds instrumentation that suppresses potential false positives around this function.

Quoting include/linux/compiler-clang.h:

/*
* The __no_kmsan_checks attribute ensures that a function does not produce
* false positive reports by:

 *  - initializing all local variables and memory stores in this function;
 *  - skipping all shadow checks;
 *  - passing initialized arguments to this function's callees.

*/

Does this answer your question?

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
#3 lore raw
Quoted 21 lines · Alexander Potapenko
Quoted 11 lines
Quoted 4 lines

+__no_kmsan_checks
static inline int arch_within_stack_frames(const void * const stack,
const void * const stackend, const void *obj, unsigned long len)

Seems OK; but now I'm confused as to the exact distinction between __no_sanitize_memory and __no_kmsan_checks.

The comments there about seem to suggest __no_sanitize_memory ensures no instrumentation at all, and __no_kmsan_checks some instrumentation but doesn't actually check anything -- so what's left then?

__no_sanitize_memory prohibits all instrumentation whatsoever, whereas __no_kmsan_checks adds instrumentation that suppresses potential false positives around this function.

Quoting include/linux/compiler-clang.h:

/*
* The __no_kmsan_checks attribute ensures that a function does not produce
* false positive reports by:

Quoted 6 lines
 *  - initializing all local variables and memory stores in this function;
 *  - skipping all shadow checks;
 *  - passing initialized arguments to this function's callees.

*/

Does this answer your question?

So I read that comment; and it didn't click. So you're explicitly initializing variables/arguments and explicitly not checking shadow state vs, not doing explicit initialization and checking shadow state?

That is, it doesn't do the normal checks and adds explicit initialization to avoid triggering discontent in surrounding functions?

#4 lore raw
Quoted 29 lines · Peter Zijlstra
Quoted 26 lines · Alexander Potapenko
Quoted 11 lines
Quoted 4 lines

+__no_kmsan_checks
static inline int arch_within_stack_frames(const void * const stack,
const void * const stackend, const void *obj, unsigned long len)

Seems OK; but now I'm confused as to the exact distinction between __no_sanitize_memory and __no_kmsan_checks.

The comments there about seem to suggest __no_sanitize_memory ensures no instrumentation at all, and __no_kmsan_checks some instrumentation but doesn't actually check anything -- so what's left then?

__no_sanitize_memory prohibits all instrumentation whatsoever, whereas __no_kmsan_checks adds instrumentation that suppresses potential false positives around this function.

Quoting include/linux/compiler-clang.h:

/*
* The __no_kmsan_checks attribute ensures that a function does not produce
* false positive reports by:

 *  - initializing all local variables and memory stores in this function;
 *  - skipping all shadow checks;
 *  - passing initialized arguments to this function's callees.

*/

Quoted 9 lines
Quoted 1 line

Does this answer your question?

So I read that comment; and it didn't click. So you're explicitly initializing variables/arguments and explicitly not checking shadow state vs, not doing explicit initialization and checking shadow state?

That is, it doesn't do the normal checks and adds explicit initialization to avoid triggering discontent in surrounding functions?

Correct

In other words, for normal instrumentation:
- locals are explicitly marked as uninitialized;
- shadow values are calculated for arithmetic operations based on their inputs;
- shadow values are checked for branches, pointer dereferences, and
before passing them as function arguments;
- memory stores update shadow for affected variables.

For __no_kmsan_checks:
- locals are explicitly marked as initialized;
- no instrumentation is added for arithmetic operations, branches,
pointer dereferences;
- all function arguments are marked as initialized;
- stores always mark memory as initialized.

For __no_sanitize_memory:
- no instrumentation for locals (they may end up being initialized or
uninitialized - doesn't matter, because their shadow values are never used);
- no instrumentation for arithmetic operations, branches, pointer dereferences;
- no instrumentation for function calls (an instrumented function
will receive garbage shadow values from a non-instrumented one);
- no instrumentation for stores (initialization done in these
functions is invisible).

In all the cases explicit calls to kmsan_poison_memory()/kmsan_unpoison_memory()/kmsan_check_memory() behave the same way and can be used to tell the tool what is going on in the absence of instrumentation.

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
#5 lore raw
Quoted 14 lines · Alexander Potapenko

In other words, for normal instrumentation:
- locals are explicitly marked as uninitialized;
- shadow values are calculated for arithmetic operations based on their inputs;
- shadow values are checked for branches, pointer dereferences, and
before passing them as function arguments;
- memory stores update shadow for affected variables.

For __no_kmsan_checks:
- locals are explicitly marked as initialized;
- no instrumentation is added for arithmetic operations, branches,
pointer dereferences;
- all function arguments are marked as initialized;
- stores always mark memory as initialized.

Quoted 9 lines

For __no_sanitize_memory:
- no instrumentation for locals (they may end up being initialized or
uninitialized - doesn't matter, because their shadow values are never used);
- no instrumentation for arithmetic operations, branches, pointer dereferences;
- no instrumentation for function calls (an instrumented function
will receive garbage shadow values from a non-instrumented one);
- no instrumentation for stores (initialization done in these
functions is invisible).

Thanks! That is a great summary.

Tested-by on v1 1/1
#6 lore raw
In reply to Alexander Potapenko
Quoted 6 lines · Alexander Potapenko

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

Quoted 7 lines
---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)

- Eric

#7 lore raw
Quoted 15 lines · Eric Biggers
Quoted 6 lines · Alexander Potapenko

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

Quoted 7 lines
---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 2 lines

Can this patch be applied to the x86 tree, please?

- Eric

#8 lore raw
Quoted 16 lines · Eric Biggers
Quoted 15 lines · Eric Biggers
Quoted 13 lines · Alexander Potapenko

arch_within_stack_frames() performs stack walking and may confuse KMSAN by stepping on stale shadow values. To prevent false positive reports, disable KMSAN checks in this function.

This fixes KMSAN's interoperability with CONFIG_HARDENED_USERCOPY.

---
 arch/x86/include/asm/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 6 lines
Quoted 2 lines

Can this patch be applied to the x86 tree, please?

- Eric

Ping.