2020-10-22 16:41:06

by Marco Elver

[permalink] [raw]
Subject: [PATCH v2 2/2] kcsan: Never set up watchpoints on NULL pointers

Avoid setting up watchpoints on NULL pointers, as otherwise we would
crash inside the KCSAN runtime (when checking for value changes) instead
of the instrumented code.

Because that may be confusing, skip any address less than PAGE_SIZE.

Signed-off-by: Marco Elver <[email protected]>
---
kernel/kcsan/encoding.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/kcsan/encoding.h b/kernel/kcsan/encoding.h
index f03562aaf2eb..64b3c0f2a685 100644
--- a/kernel/kcsan/encoding.h
+++ b/kernel/kcsan/encoding.h
@@ -48,7 +48,11 @@

static inline bool check_encodable(unsigned long addr, size_t size)
{
- return size <= MAX_ENCODABLE_SIZE;
+ /*
+ * While we can encode addrs<PAGE_SIZE, avoid crashing with a NULL
+ * pointer deref inside KCSAN.
+ */
+ return addr >= PAGE_SIZE && size <= MAX_ENCODABLE_SIZE;
}

static inline long
--
2.29.0.rc1.297.gfa9743e501-goog


2020-10-22 17:54:18

by Dmitry Vyukov

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] kcsan: Never set up watchpoints on NULL pointers

On Thu, Oct 22, 2020 at 1:45 PM Marco Elver <[email protected]> wrote:
>
> Avoid setting up watchpoints on NULL pointers, as otherwise we would
> crash inside the KCSAN runtime (when checking for value changes) instead
> of the instrumented code.
>
> Because that may be confusing, skip any address less than PAGE_SIZE.
>
> Signed-off-by: Marco Elver <[email protected]>

Reviewed-by: Dmitry Vyukov <[email protected]>

> ---
> kernel/kcsan/encoding.h | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/kcsan/encoding.h b/kernel/kcsan/encoding.h
> index f03562aaf2eb..64b3c0f2a685 100644
> --- a/kernel/kcsan/encoding.h
> +++ b/kernel/kcsan/encoding.h
> @@ -48,7 +48,11 @@
>
> static inline bool check_encodable(unsigned long addr, size_t size)
> {
> - return size <= MAX_ENCODABLE_SIZE;
> + /*
> + * While we can encode addrs<PAGE_SIZE, avoid crashing with a NULL
> + * pointer deref inside KCSAN.
> + */
> + return addr >= PAGE_SIZE && size <= MAX_ENCODABLE_SIZE;
> }
>
> static inline long
> --
> 2.29.0.rc1.297.gfa9743e501-goog
>