public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] objtool: Permit __kasan_check_{read,write} under UACCESS
@ 2020-09-28 22:49 Jann Horn
  2020-09-29  7:05 ` Peter Zijlstra
  0 siblings, 1 reply; 2+ messages in thread
From: Jann Horn @ 2020-09-28 22:49 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra
  Cc: linux-kernel, x86, Andrey Ryabinin, Alexander Potapenko,
	Dmitry Vyukov, kasan-dev, Dan Williams, Tony Luck, Vishal Verma

Building linux-next with JUMP_LABEL=n and KASAN=y, I got this objtool
warning:

arch/x86/lib/copy_mc.o: warning: objtool: copy_mc_to_user()+0x22: call to
__kasan_check_read() with UACCESS enabled

What happens here is that copy_mc_to_user() branches on a static key in a
UACCESS region:

        __uaccess_begin();
        if (static_branch_unlikely(&copy_mc_fragile_key))
                ret = copy_mc_fragile(to, from, len);
        ret = copy_mc_generic(to, from, len);
        __uaccess_end();

and the !CONFIG_JUMP_LABEL version of static_branch_unlikely() uses
static_key_enabled(), which uses static_key_count(), which uses
atomic_read(), which calls instrument_atomic_read(), which uses
kasan_check_read(), which is __kasan_check_read().

Let's permit these KASAN helpers in UACCESS regions - static keys should
probably work under UACCESS, I think.

Signed-off-by: Jann Horn <jannh@google.com>
---
Calling atomic_read() on a global under UACCESS should probably be fine,
right? The alternative to this patch would be to change
copy_mc_to_user()...

Note that copy_mc_to_user() does not exist in the tip tree yet; it
appeared in commit 0a78de3d4b7b1b80e5c1eead24ce11c4b3cc8791 in the
nvdimm tree.

 tools/objtool/check.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index a88fb05242d5..1141a8e26c1e 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -583,6 +583,8 @@ static const char *uaccess_safe_builtin[] = {
 	"__asan_store4_noabort",
 	"__asan_store8_noabort",
 	"__asan_store16_noabort",
+	"__kasan_check_read",
+	"__kasan_check_write",
 	/* KASAN in-line */
 	"__asan_report_load_n_noabort",
 	"__asan_report_load1_noabort",

base-commit: 0248dedd12d43035bf53c326633f0610a49d7134
-- 
2.28.0.709.gb0816b6eb0-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] objtool: Permit __kasan_check_{read,write} under UACCESS
  2020-09-28 22:49 [PATCH] objtool: Permit __kasan_check_{read,write} under UACCESS Jann Horn
@ 2020-09-29  7:05 ` Peter Zijlstra
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Zijlstra @ 2020-09-29  7:05 UTC (permalink / raw)
  To: Jann Horn
  Cc: Josh Poimboeuf, linux-kernel, x86, Andrey Ryabinin,
	Alexander Potapenko, Dmitry Vyukov, kasan-dev, Dan Williams,
	Tony Luck, Vishal Verma

On Tue, Sep 29, 2020 at 12:49:16AM +0200, Jann Horn wrote:
> Building linux-next with JUMP_LABEL=n and KASAN=y, I got this objtool
> warning:
> 
> arch/x86/lib/copy_mc.o: warning: objtool: copy_mc_to_user()+0x22: call to
> __kasan_check_read() with UACCESS enabled
> 
> What happens here is that copy_mc_to_user() branches on a static key in a
> UACCESS region:
> 
>         __uaccess_begin();
>         if (static_branch_unlikely(&copy_mc_fragile_key))
>                 ret = copy_mc_fragile(to, from, len);
>         ret = copy_mc_generic(to, from, len);
>         __uaccess_end();
> 
> and the !CONFIG_JUMP_LABEL version of static_branch_unlikely() uses
> static_key_enabled(), which uses static_key_count(), which uses
> atomic_read(), which calls instrument_atomic_read(), which uses
> kasan_check_read(), which is __kasan_check_read().
> 
> Let's permit these KASAN helpers in UACCESS regions - static keys should
> probably work under UACCESS, I think.

It's not a matter of permitting, it's a matter of being safe and
correct. In this case it is, because it's a thin wrapper around
check_memory_region() which was already marked safe.

check_memory_region() is correct because the only thing it ends up
calling is kasa_report() and that is also marked safe because that is
annotated with user_access_save/restore() before it does anything else.

On top of that, all of KASAN is noinstr, so nothing in here will end up
in tracing and/or call schedule() before the user_access_save().

> Signed-off-by: Jann Horn <jannh@google.com>

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

> ---
> Calling atomic_read() on a global under UACCESS should probably be fine,
> right?

Yes, per the above.

>  tools/objtool/check.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index a88fb05242d5..1141a8e26c1e 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -583,6 +583,8 @@ static const char *uaccess_safe_builtin[] = {
>  	"__asan_store4_noabort",
>  	"__asan_store8_noabort",
>  	"__asan_store16_noabort",
> +	"__kasan_check_read",
> +	"__kasan_check_write",
>  	/* KASAN in-line */
>  	"__asan_report_load_n_noabort",
>  	"__asan_report_load1_noabort",
> 
> base-commit: 0248dedd12d43035bf53c326633f0610a49d7134
> -- 
> 2.28.0.709.gb0816b6eb0-goog
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-29  7:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-28 22:49 [PATCH] objtool: Permit __kasan_check_{read,write} under UACCESS Jann Horn
2020-09-29  7:05 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox