Bitops instrumentation for KASAN
Subsystems (5)
v2 cover Bitops instrumentation for KASAN
The previous version of this patch series and discussion can be found
here: https://lkml.org/lkml/2019/5/28/769
The most significant change is the change of the instrumented access size to cover the entire word of a bit.
Marco Elver (3):
lib/test_kasan: Add bitops tests
x86: Move CPU feature test out of uaccess region asm-generic, x86: Add bitops instrumentation for KASAN
Documentation/core-api/kernel-api.rst | 2 +- arch/x86/ia32/ia32_signal.c | 9 +- arch/x86/include/asm/bitops.h | 210 ++++---------- include/asm-generic/bitops-instrumented.h | 317 ++++++++++++++++++++++ lib/test_kasan.c | 75 ++++- 5 files changed, 450 insertions(+), 163 deletions(-) create mode 100644 include/asm-generic/bitops-instrumented.h
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
PATCH v2 1/3 lib/test_kasan: Add bitops tests
This adds bitops tests to the test_kasan module. In a follow-up patch, support for bitops instrumentation will be added.
---
Changes in v2:
* Use BITS_PER_LONG.
* Use heap allocated memory for test, as newer compilers (correctly) warn on OOB stack access.
--- lib/test_kasan.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 3 deletions(-)
lib/test_kasan.c +72 -3
@@ -11,16 +11,17 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__ +#include <linux/bitops.h> #include <linux/delay.h> +#include <linux/kasan.h> #include <linux/kernel.h> -#include <linux/mman.h> #include <linux/mm.h> +#include <linux/mman.h> +#include <linux/module.h> #include <linux/printk.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> -#include <linux/module.h> -#include <linux/kasan.h> /* * Note: test functions are marked noinline so that their names appear in
@@ -623,6 +624,73 @@ static noinline void __init kasan_strings(void)strnlen (ptr,1 ); } +static noinline void __init kasan_bitops(void) +{ +long *bits =kmalloc (sizeof (long ),GFP_KERNEL | __GFP_ZERO); +if (!bits) +return ; + +pr_info ("within-bounds in set_bit" ); +set_bit (0 , bits); + +pr_info ("within-bounds in set_bit" ); +set_bit (BITS_PER_LONG -1 , bits); + +pr_info ("out-of-bounds in set_bit\n" ); +set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __set_bit\n" ); +__set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in clear_bit\n" ); +clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __clear_bit\n" ); +__clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in clear_bit_unlock\n" ); +clear_bit_unlock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __clear_bit_unlock\n" ); +__clear_bit_unlock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in change_bit\n" ); +change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __change_bit\n" ); +__change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_set_bit\n" ); +test_and_set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_set_bit\n" ); +__test_and_set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_set_bit_lock\n" ); +test_and_set_bit_lock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_clear_bit\n" ); +test_and_clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_clear_bit\n" ); +__test_and_clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_change_bit\n" ); +test_and_change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_change_bit\n" ); +__test_and_change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_bit\n" ); + (void )test_bit (BITS_PER_LONG , bits); + +#if defined(clear_bit_unlock_is_negative_byte) +pr_info ("out-of-bounds in clear_bit_unlock_is_negative_byte\n" ); +clear_bit_unlock_is_negative_byte (BITS_PER_LONG , bits); +#endif +kfree (bits); +} +static int __init kmalloc_tests_init (void ) {/*
@@ -664,6 +732,7 @@ static int __init kmalloc_tests_init(void)kasan_memchr ();kasan_memcmp ();kasan_strings (); +kasan_bitops ();kasan_restore_multi_shot (multishot);
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Re: [PATCH v2 1/3] lib/test_kasan: Add bitops tests
Quoted 39 lines · Marco Elver
This adds bitops tests to the
test_kasanmodule. In a follow-up patch, support for bitops instrumentation will be added.Signed-off-by Marco Elver <[email protected]>---
Changes in v2:
* UseBITS_PER_LONG.
* Use heap allocated memory for test, as newer compilers (correctly) warn on OOB stack access.--- lib/test_kasan.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 3 deletions(-)lib/test_kasan.c +4
-3@@ -11,16 +11,17 @@#define pr_fmt (fmt) "kasan test: %s " fmt, __func__ +#include <linux/bitops.h> #include <linux/delay.h> +#include <linux/kasan.h> #include <linux/kernel.h> -#include <linux/mman.h> #include <linux/mm.h> +#include <linux/mman.h> +#include <linux/module.h> #include <linux/printk.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/uaccess.h> -#include <linux/module.h> -#include <linux/kasan.h> /* * Note: test functions are marked noinline so that their names appear in
lib/test_kasan.c static noinline void __init kasan_strings(void) · line 629
Quoted 6 lines
lib/test_kasan.c +3
-0@@ -623,6 +624,73 @@ static noinline void __init kasan_strings(void)strnlen (ptr,1 ); } +static noinline void __init kasan_bitops(void) +{ +long *bits =kmalloc (sizeof (long ),GFP_KERNEL | __GFP_ZERO);
Trivial nit, but this can/should be:
long *bits = kzalloc(sizeof(*bits), GFP_KERNEL);
... which is the usual style for sizeof() to keep the LHS and RHS types the same, and using kzalloc avoids the need to explicitly pass __GFP_ZERO.
Otherwise, this looks good to me.
Quoted 78 lines
lib/test_kasan.c +65
-0+if (!bits) +return ; + +pr_info ("within-bounds in set_bit" ); +set_bit (0 , bits); + +pr_info ("within-bounds in set_bit" ); +set_bit (BITS_PER_LONG -1 , bits); + +pr_info ("out-of-bounds in set_bit\n" ); +set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __set_bit\n" ); +__set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in clear_bit\n" ); +clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __clear_bit\n" ); +__clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in clear_bit_unlock\n" ); +clear_bit_unlock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __clear_bit_unlock\n" ); +__clear_bit_unlock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in change_bit\n" ); +change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __change_bit\n" ); +__change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_set_bit\n" ); +test_and_set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_set_bit\n" ); +__test_and_set_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_set_bit_lock\n" ); +test_and_set_bit_lock (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_clear_bit\n" ); +test_and_clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_clear_bit\n" ); +__test_and_clear_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_and_change_bit\n" ); +test_and_change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in __test_and_change_bit\n" ); +__test_and_change_bit (BITS_PER_LONG , bits); + +pr_info ("out-of-bounds in test_bit\n" ); + (void )test_bit (BITS_PER_LONG , bits); + +#if defined(clear_bit_unlock_is_negative_byte) +pr_info ("out-of-bounds in clear_bit_unlock_is_negative_byte\n" ); +clear_bit_unlock_is_negative_byte (BITS_PER_LONG , bits); +#endif +kfree (bits); +} +static int __init kmalloc_tests_init (void ) {/* @@ -664,6 +732,7 @@ static int __init kmalloc_tests_init(void)kasan_memchr ();kasan_memcmp ();kasan_strings (); +kasan_bitops ();kasan_restore_multi_shot (multishot);Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
PATCH v2 2/3 x86: Move CPU feature test out of uaccess region
In reply to Marco ElverThis patch is a pre-requisite for enabling KASAN bitops instrumentation:
moves boot_cpu_has feature test out of the uaccess region, as boot_cpu_has uses test_bit. With instrumentation, the KASAN check would otherwise be flagged by objtool.
This approach is preferred over adding the explicit kasan_check_* functions to the uaccess whitelist of objtool, as the case here appears to be the only one.
---
v1:
* This patch replaces patch: 'tools/objtool: add kasan_check_* to uaccess whitelist'
--- arch/x86/ia32/ia32_signal.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/ia32/ia32_signal.c +8 -1
@@ -333,6 +333,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,void __user *restorer;int err =0 ;void __user *fpstate =NULL ; +bool has_xsave;/* __copy_to_user optimizes that into a single 8 byte store */ static const struct {
@@ -352,13 +353,19 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,if (!access_ok (frame,sizeof (*frame)))return -EFAULT ; +/* +* Move non-uaccess accesses out of uaccess region if not strictly +* required; this also helps avoid objtool flagging these accesses with +* instrumentation enabled. +*/ + has_xsave =boot_cpu_has (X86_FEATURE_XSAVE ); put_user_try {put_user_ex (sig, &frame->sig);put_user_ex (ptr_to_compat (&frame->info), &frame->pinfo);put_user_ex (ptr_to_compat (&frame->uc), &frame->puc);/* Create the ucontext. */ -if (boot_cpu_has (X86_FEATURE_XSAVE )) +if (has_xsave)put_user_ex (UC_FP_XSTATE , &frame->uc.uc_flags);else put_user_ex (0 , &frame->uc.uc_flags);
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
[email protected]
reviewerRe: [PATCH 2/3] x86: Move CPU feature test out of uaccess region
Quoted 52 lines · Marco Elver
This patch is a pre-requisite for enabling KASAN bitops instrumentation:
movesboot_cpu_hasfeature test out of the uaccess region, asboot_cpu_hasusestest_bit. With instrumentation, the KASAN check would otherwise be flagged by objtool.This approach is preferred over adding the explicit kasan_check_* functions to the uaccess whitelist of objtool, as the case here appears to be the only one.
Signed-off-by Marco Elver <[email protected]>---
v1:
* This patch replaces patch: 'tools/objtool: add kasan_check_* to uaccess whitelist'
---arch/x86/ia32/ia32_signal.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/ia32/ia32_signal.c +1
-0@@ -333,6 +333,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, void__user *restorer;int err =0 ;void __user *fpstate =NULL ; +bool has_xsave;/* __copy_to_user optimizes that into a single 8 byte store */ static const struct {arch/x86/ia32/ia32_signal.c +3
-0@@ -352,13 +353,19 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, if (!access_ok(frame, sizeof(*frame))) return -EFAULT; +/* +* Move non-uaccess accesses out of uaccess region if not strictly +* required; this also helps avoid objtool flagging these accesses with
arch/x86/ia32/ia32_signal.c +3
-0+* instrumentation enabled. +*/ + has_xsave =boot_cpu_has (X86_FEATURE_XSAVE );put_user_try { put_user_ex(sig, &frame->sig); put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo); put_user_ex(ptr_to_compat(&frame->uc), &frame->puc); /* Create the ucontext. */arch/x86/ia32/ia32_signal.c +1
-1-if (boot_cpu_has (X86_FEATURE_XSAVE )) +if (has_xsave)
Quoted 3 lines
put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags); else put_user_ex(0, &frame->uc.uc_flags);
This was meant to use static_cpu_has(). Why did that get dropped?
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 2/3] x86: Move CPU feature test out of uaccess region
Quoted 51 lines · On Wed, 29 May 2019 at 16:29, <[email protected]> wrote:
Quoted 49 lines · Marco Elver
This patch is a pre-requisite for enabling KASAN bitops instrumentation:
movesboot_cpu_hasfeature test out of the uaccess region, asboot_cpu_hasusestest_bit. With instrumentation, the KASAN check would otherwise be flagged by objtool.This approach is preferred over adding the explicit kasan_check_* functions to the uaccess whitelist of objtool, as the case here appears to be the only one.
Signed-off-by Marco Elver <[email protected]>---
v1:
* This patch replaces patch: 'tools/objtool: add kasan_check_* to uaccess whitelist'
---arch/x86/ia32/ia32_signal.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)arch/x86/ia32/ia32_signal.c +1
-0@@ -333,6 +333,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, void__user *restorer;int err =0 ;void __user *fpstate =NULL ; +bool has_xsave;/* __copy_to_user optimizes that into a single 8 byte store */ static const struct {arch/x86/ia32/ia32_signal.c +3
-0@@ -352,13 +353,19 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, if (!access_ok(frame, sizeof(*frame))) return -EFAULT; +/* +* Move non-uaccess accesses out of uaccess region if not strictly +* required; this also helps avoid objtool flagging these accesses with
arch/x86/ia32/ia32_signal.c +3
-0+* instrumentation enabled. +*/ + has_xsave =boot_cpu_has (X86_FEATURE_XSAVE ); put_user_try {put_user_ex (sig, &frame->sig);put_user_ex (ptr_to_compat (&frame->info), &frame->pinfo);put_user_ex (ptr_to_compat (&frame->uc), &frame->puc);
Quoted 8 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig, · line 371
Quoted 6 lines
/* Create the ucontext. */arch/x86/ia32/ia32_signal.c +1
-1-if (boot_cpu_has (X86_FEATURE_XSAVE )) +if (has_xsave)put_user_ex (UC_FP_XSTATE , &frame->uc.uc_flags);else put_user_ex (0 , &frame->uc.uc_flags);This was meant to use
static_cpu_has(). Why did that get dropped?
I couldn't find any mailing list thread referring to why this doesn't use static_cpu_has, do you have any background?
static_cpu_has also solves the UACCESS warning.
If you confirm it is safe to change to static_cpu_has(), I will change this patch. Note that I should then also change arch/x86/kernel/signal.c to mirror the change for 32bit (although KASAN is not supported for 32bit x86).
Thanks,
-- Marco
[email protected]
reviewerRe: [PATCH 2/3] x86: Move CPU feature test out of uaccess region
Quoted 75 lines · Marco Elver
Quoted 6 lines · On Wed, 29 May 2019 at 16:29, <[email protected]> wrote:
Quoted 4 lines · Marco Elver
This patch is a pre-requisite for enabling KASAN bitops instrumentation:
movesboot_cpu_hasfeature test out of the uaccess region, asboot_cpu_hasusestest_bit. With instrumentation, the KASAN checkwould
Quoted 4 lines
Quoted 4 lines
otherwise be flagged by objtool.
This approach is preferred over adding the explicit kasan_check_* functions to the uaccess whitelist of objtool, as the case here
appears
Quoted 11 lines
Quoted 11 lines
to be the only one.
Signed-off-by Marco Elver <[email protected]>---
v1:
* This patch replaces patch: 'tools/objtool: add kasan_check_* to uaccess whitelist'
---arch/x86/ia32/ia32_signal.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/x86/ia32/ia32_signal.cQuoted 11 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal · line 339
Quoted 11 lines
index
629d1ee05599..12264e3c9c43100644arch/x86/ia32/ia32_signal.c +1
-0@@ -333,6 +333,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, void__user *restorer;int err =0 ;void __user *fpstate =NULL ; +bool has_xsave;/* __copy_to_user optimizes that into a single 8 byte store */
Quoted 2 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct
Quoted 2 lines
static const struct {arch/x86/ia32/ia32_signal.c +0
-0@@ -352,13 +353,19 @@ int ia32_setup_rt_frame(int sig, structksignal
Quoted 6 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 6 lines
arch/x86/ia32/ia32_signal.c +2
-0*ksig, if (!access_ok(frame, sizeof(*frame))) return -EFAULT; +/* +* Move non-uaccess accesses out of uaccess region if not strictly
Quoted 1 line
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct · line 359
Quoted 1 line
arch/x86/ia32/ia32_signal.c +1
-0+* required; this also helps avoid objtool flagging these accesses
Quoted 7 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 7 lines
with
arch/x86/ia32/ia32_signal.c +3
-0+* instrumentation enabled. +*/ + has_xsave =boot_cpu_has (X86_FEATURE_XSAVE ); put_user_try {put_user_ex (sig, &frame->sig); put_user_ex(ptr_to_compat(&frame->info),
&frame->pinfo);Quoted 6 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 6 lines
put_user_ex(ptr_to_compat(&frame->uc), &frame->puc); /* Create the ucontext. */arch/x86/ia32/ia32_signal.c +1
-1-if (boot_cpu_has (X86_FEATURE_XSAVE )) +if (has_xsave) put_user_ex(UC_FP_XSTATE,
&frame->uc.uc_flags);Quoted 4 lines
Quoted 2 lines
else put_user_ex(0, &frame->uc.uc_flags);This was meant to use
static_cpu_has(). Why did that get dropped?I couldn't find any mailing list thread referring to why this doesn't use
static_cpu_has, do you have any background?
static_cpu_hasalso solves the UACCESS warning.
Quoted 7 lines
If you confirm it is safe to change to
static_cpu_has(), I will change this patch. Note that I should then also changearch/x86/kernel/signal.cto mirror the change for 32bit (although KASAN is not supported for 32bit x86).Thanks,
-- Marco
I believe at some point the intent was that boot_cpu_has() was safer and could be used everywhere.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 2/3] x86: Move CPU feature test out of uaccess region
Thanks for the clarification.
I found that static_cpu_has was replaced by static_cpu_has_safe:
https://lkml.org/lkml/2016/1/24/29 -- so is it fair to assume that both are equally safe at this point?
I have sent a follow-up patch which uses static_cpu_has:
http://lkml.kernel.org/r/[email protected]
Many thanks!
-- Marco
Quoted 88 lines · On Sat, 1 Jun 2019 at 03:13, <[email protected]> wrote:
Quoted 75 lines · Marco Elver
Quoted 6 lines · On Wed, 29 May 2019 at 16:29, <[email protected]> wrote:
Quoted 4 lines · Marco Elver
This patch is a pre-requisite for enabling KASAN bitops instrumentation:
movesboot_cpu_hasfeature test out of the uaccess region, asboot_cpu_hasusestest_bit. With instrumentation, the KASAN checkwould
Quoted 4 lines
Quoted 4 lines
otherwise be flagged by objtool.
This approach is preferred over adding the explicit kasan_check_* functions to the uaccess whitelist of objtool, as the case here
appears
Quoted 11 lines
Quoted 11 lines
to be the only one.
Signed-off-by Marco Elver <[email protected]>---
v1:
* This patch replaces patch: 'tools/objtool: add kasan_check_* to uaccess whitelist'
---arch/x86/ia32/ia32_signal.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/x86/ia32/ia32_signal.cQuoted 11 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal · line 339
Quoted 11 lines
index
629d1ee05599..12264e3c9c43100644arch/x86/ia32/ia32_signal.c +1
-0@@ -333,6 +333,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, void__user *restorer;int err =0 ;void __user *fpstate =NULL ; +bool has_xsave;/* __copy_to_user optimizes that into a single 8 byte store */
Quoted 2 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct
Quoted 2 lines
static const struct {arch/x86/ia32/ia32_signal.c +0
-0@@ -352,13 +353,19 @@ int ia32_setup_rt_frame(int sig, structksignal
Quoted 6 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 6 lines
arch/x86/ia32/ia32_signal.c +2
-0*ksig, if (!access_ok(frame, sizeof(*frame))) return -EFAULT; +/* +* Move non-uaccess accesses out of uaccess region if not strictly
Quoted 1 line
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct · line 359
Quoted 1 line
arch/x86/ia32/ia32_signal.c +1
-0+* required; this also helps avoid objtool flagging these accesses
Quoted 7 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 7 lines
with
arch/x86/ia32/ia32_signal.c +3
-0+* instrumentation enabled. +*/ + has_xsave =boot_cpu_has (X86_FEATURE_XSAVE ); put_user_try {put_user_ex (sig, &frame->sig); put_user_ex(ptr_to_compat(&frame->info),
&frame->pinfo);Quoted 6 lines
arch/x86/ia32/ia32_signal.c int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Quoted 6 lines
put_user_ex(ptr_to_compat(&frame->uc), &frame->puc); /* Create the ucontext. */arch/x86/ia32/ia32_signal.c +1
-1-if (boot_cpu_has (X86_FEATURE_XSAVE )) +if (has_xsave) put_user_ex(UC_FP_XSTATE,
&frame->uc.uc_flags);Quoted 4 lines
Quoted 2 lines
else put_user_ex(0, &frame->uc.uc_flags);This was meant to use
static_cpu_has(). Why did that get dropped?I couldn't find any mailing list thread referring to why this doesn't use
static_cpu_has, do you have any background?
static_cpu_hasalso solves the UACCESS warning.Quoted 7 lines
If you confirm it is safe to change to
static_cpu_has(), I will change this patch. Note that I should then also changearch/x86/kernel/signal.cto mirror the change for 32bit (although KASAN is not supported for 32bit x86).Thanks,
-- MarcoI believe at some point the intent was that
boot_cpu_has()was safer and could be used everywhere. --
Sent from my Android device with K-9 Mail. Please excuse my brevity.
PATCH v2 3/3 asm-generic, x86: Add bitops instrumentation for KASAN
In reply to Marco ElverThis adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.
This change includes the required change for x86 as reference and changes the kernel API doc to point to bitops-instrumented.h instead.
Rationale: the functions in x86's bitops.h are no longer the kernel API functions, but instead the arch_ prefixed functions, which are then instrumented via bitops-instrumented.h.
Other architectures can similarly add support for asm implementations of bitops.
The documentation text has been copied/moved, and *no* changes to it have been made in this patch.
Tested: using lib/test_kasan with bitops tests (pre-requisite patch).
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439
---
Changes in v2:
* Instrument word-sized accesses, as specified by the interface.
--- Documentation/core-api/kernel-api.rst | 2 +- arch/x86/include/asm/bitops.h | 210 ++++---------- include/asm-generic/bitops-instrumented.h | 317 ++++++++++++++++++++++ 3 files changed, 370 insertions(+), 159 deletions(-) create mode 100644 include/asm-generic/bitops-instrumented.h
Documentation/core-api/kernel-api.rst +1 -1
@@ -51,7 +51,7 @@ The Linux kernel provides more basic utility functions. Bit Operations -------------- -.. kernel-doc:: arch/x86/include/asm/bitops.h +.. kernel-doc:: include/asm-generic/bitops-instrumented.h :internal: Bitmap Operations
arch/x86/include/asm/bitops.h +52 -158
313 lines not on this page · raw message
include/asm-generic/bitops-instrumented.h new file +317 -0
317 lines not on this page · raw message
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Re: [PATCH v2 3/3] asm-generic, x86: Add bitops instrumentation for KASAN
Quoted 28 lines · Marco Elver
This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.
This change includes the required change for x86 as reference and changes the kernel API doc to point to
bitops-instrumented.hinstead.
Rationale: the functions in x86'sbitops.hare no longer the kernel API functions, but instead thearch_prefixed functions, which are then instrumented viabitops-instrumented.h.Other architectures can similarly add support for asm implementations of bitops.
The documentation text has been copied/moved, and *no* changes to it have been made in this patch.
Tested: using
lib/test_kasanwith bitops tests (pre-requisite patch).Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439
Signed-off-by Marco Elver <[email protected]>---
Changes in v2:
* Instrument word-sized accesses, as specified by the interface.--- Documentation/core-api/kernel-api.rst | 2 +- arch/x86/include/asm/bitops.h | 210 ++++---------- include/asm-generic/bitops-instrumented.h | 317 ++++++++++++++++++++++ 3 files changed, 370 insertions(+), 159 deletions(-) create mode 100644 include/asm-generic/bitops-instrumented.h
[...]
Quoted 9 lines
include/asm-generic/bitops-instrumented.h new file +3
-0@@ -0,0 +1,317 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/*
include/asm-generic/bitops-instrumented.h line 13
Quoted 10 lines
include/asm-generic/bitops-instrumented.h new file +10
-0+* This file provides wrappers with sanitizer instrumentation for bit +* operations. +* +* To use this functionality, an arch's bitops.h file needs to define each of +* the below bit operations with an arch_ prefix (e.g. arch_set_bit(), +* arch___set_bit(), etc.), #define each provided arch_ function, and include +* this file after their definitions. For undefined arch_ functions, it is +* assumed that they are provided via asm-generic/bitops, which are implicitly +* instrumented. +*/
If using the asm-generic/bitops.h, all of the below will be defined unconditionally, so I don't believe we need the ifdeffery for each function.
Quoted 7 lines
include/asm-generic/bitops-instrumented.h new file +7
-0+#ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_H +#define _ASM_GENERIC_BITOPS_INSTRUMENTED_H + +#include <linux/kasan-checks.h> + +#if defined(arch_set_bit) +/**
include/asm-generic/bitops-instrumented.h line 30
Quoted 10 lines
include/asm-generic/bitops-instrumented.h new file +10
-0+* set_bit - Atomically set a bit in memory +* @nr: the bit to set +* @addr: the address to start counting from +* +* This function is atomic and may not be reordered. See __set_bit() +* if you do not require the atomic guarantees. +* +* Note: there are no guarantees that this function will not be reordered +* on non x86 architectures, so if you are writing portable code, +* make sure not to rely on its reordering guarantees.
These two paragraphs are contradictory.
Since this is not under arch/x86, please fix this to describe the generic semantics; any x86-specific behaviour should be commented under arch/x86.
AFAICT per include/asm-generic/bitops/atomic.h, generically this provides no ordering guarantees. So I think this can be:
/**
* set_bit - Atomically set a bit in memory
* @nr: the bit to set
* @addr: the address to start counting from
*
* This function is atomic and may be reordered.
*
* Note that @nr may be almost arbitrarily large; this function is not
* restricted to acting on a single-word quantity.
*/
... with the x86 ordering beahviour commented in x86's arch_set_bit.
Peter, do you have a better wording for the above?
[...]
Quoted 7 lines
include/asm-generic/bitops-instrumented.h new file +7
-0+#if defined(arch___test_and_clear_bit) +/** +* __test_and_clear_bit - Clear a bit and return its old value +* @nr: Bit to clear +* @addr: Address to count from +* +* This operation is non-atomic and can be reordered.
include/asm-generic/bitops-instrumented.h line 243
Quoted 10 lines
include/asm-generic/bitops-instrumented.h new file +10
-0+* If two examples of this operation race, one can appear to succeed +* but actually fail. You must protect multiple accesses with a lock. +* +* Note: the operation is performed atomically with respect to +* the local CPU, but not other CPUs. Portable code should not +* rely on this behaviour. +* KVM relies on this behaviour on x86 for modifying memory that is also +* accessed from a hypervisor on the same CPU if running in a VM: don't change +* this without also updating arch/x86/kernel/kvm.c +*/
Likewise, please only specify the generic semantics in this header, and leave the x86-specific behaviour commented under arch/x86.
Otherwise this looks sound to me.
Thanks,
Mark.
Re: [PATCH v2 3/3] asm-generic, x86: Add bitops instrumentation for KASAN
Quoted 113 lines · Mark Rutland
Quoted 28 lines · Marco Elver
This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.
This change includes the required change for x86 as reference and changes the kernel API doc to point to
bitops-instrumented.hinstead.
Rationale: the functions in x86'sbitops.hare no longer the kernel API functions, but instead thearch_prefixed functions, which are then instrumented viabitops-instrumented.h.Other architectures can similarly add support for asm implementations of bitops.
The documentation text has been copied/moved, and *no* changes to it have been made in this patch.
Tested: using
lib/test_kasanwith bitops tests (pre-requisite patch).Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439
Signed-off-by Marco Elver <[email protected]>---
Changes in v2:
* Instrument word-sized accesses, as specified by the interface.--- Documentation/core-api/kernel-api.rst | 2 +- arch/x86/include/asm/bitops.h | 210 ++++---------- include/asm-generic/bitops-instrumented.h | 317 ++++++++++++++++++++++ 3 files changed, 370 insertions(+), 159 deletions(-) create mode 100644 include/asm-generic/bitops-instrumented.h[...]
Quoted 9 lines
include/asm-generic/bitops-instrumented.h new file +3
-0@@ -0,0 +1,317 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* include/asm-generic/bitops-instrumented.h line 13
Quoted 10 lines
include/asm-generic/bitops-instrumented.h new file +10
-0+* This file provides wrappers with sanitizer instrumentation for bit +* operations. +* +* To use this functionality, an arch's bitops.h file needs to define each of +* the below bit operations with an arch_ prefix (e.g. arch_set_bit(), +* arch___set_bit(), etc.), #define each provided arch_ function, and include +* this file after their definitions. For undefined arch_ functions, it is +* assumed that they are provided via asm-generic/bitops, which are implicitly +* instrumented. +*/ If using the
asm-generic/bitops.h, all of the below will be defined unconditionally, so I don't believe we need the ifdeffery for each function.Quoted 7 lines
include/asm-generic/bitops-instrumented.h new file +7
-0+#ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_H +#define _ASM_GENERIC_BITOPS_INSTRUMENTED_H + +#include <linux/kasan-checks.h> + +#if defined(arch_set_bit) +/** include/asm-generic/bitops-instrumented.h line 30
Quoted 10 lines
include/asm-generic/bitops-instrumented.h new file +10
-0+* set_bit - Atomically set a bit in memory +* @nr: the bit to set +* @addr: the address to start counting from +* +* This function is atomic and may not be reordered. See __set_bit() +* if you do not require the atomic guarantees. +* +* Note: there are no guarantees that this function will not be reordered +* on non x86 architectures, so if you are writing portable code, +* make sure not to rely on its reordering guarantees. These two paragraphs are contradictory.
Since this is not under
arch/x86, please fix this to describe the generic semantics; any x86-specific behaviour should be commented underarch/x86.AFAICT per
include/asm-generic/bitops/atomic.h, generically this provides no ordering guarantees. So I think this can be:/**
*set_bit- Atomically set a bit in memory
* @nr: the bit to set
* @addr: the address to start counting from
*
* This function is atomic and may be reordered.
*
* Note that @nr may be almost arbitrarily large; this function is not
* restricted to acting on a single-word quantity.
*/... with the x86 ordering beahviour commented in x86's
arch_set_bit.Peter, do you have a better wording for the above?
[...]
Quoted 12 lines
include/asm-generic/bitops-instrumented.h new file +12
-0+#if defined(arch___test_and_clear_bit) +/** +* __test_and_clear_bit - Clear a bit and return its old value +* @nr: Bit to clear +* @addr: Address to count from +* +* This operation is non-atomic and can be reordered. +* If two examples of this operation race, one can appear to succeed +* but actually fail. You must protect multiple accesses with a lock. +* +* Note: the operation is performed atomically with respect to +* the local CPU, but not other CPUs. Portable code should not
Quoted 8 lines
include/asm-generic/bitops-instrumented.h line 243
Quoted 5 lines
include/asm-generic/bitops-instrumented.h new file +5
-0+* rely on this behaviour. +* KVM relies on this behaviour on x86 for modifying memory that is also +* accessed from a hypervisor on the same CPU if running in a VM: don't change +* this without also updating arch/x86/kernel/kvm.c +*/ Likewise, please only specify the generic semantics in this header, and leave the x86-specific behaviour commented under
arch/x86.
The current official API documentation refers to x86 bitops.h (also see the Documentation/core-api/kernel-api.rst change):
https://www.kernel.org/doc/htmldocs/kernel-api/API-set-bit.html
I'm happy to change in this patch, but note that this would change the official API documentation. Alternatively it could be done in a separate patch.
Let me know what you prefer.
Thanks,
-- Marco
Addressed comments, and sent v3:
http://lkml.kernel.org/r/[email protected]
Many thanks!
-- Marco
Quoted 23 lines · Marco Elver
The previous version of this patch series and discussion can be found
here: https://lkml.org/lkml/2019/5/28/769The most significant change is the change of the instrumented access size to cover the entire word of a bit.
Marco Elver (3):
lib/test_kasan: Add bitops tests
x86: Move CPU feature test out of uaccess region asm-generic, x86: Add bitops instrumentation for KASANDocumentation/core-api/kernel-api.rst | 2 +- arch/x86/ia32/ia32_signal.c | 9 +- arch/x86/include/asm/bitops.h | 210 ++++---------- include/asm-generic/bitops-instrumented.h | 317 ++++++++++++++++++++++ lib/test_kasan.c | 75 ++++- 5 files changed, 450 insertions(+), 163 deletions(-) create mode 100644 include/asm-generic/bitops-instrumented.h--
2.22.0.rc1.257.g3120a18244-goog