linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kcsan: Fix 0-sized checks
@ 2020-02-05 10:14 Marco Elver
  2020-02-05 15:37 ` Paul E. McKenney
  0 siblings, 1 reply; 2+ messages in thread
From: Marco Elver @ 2020-02-05 10:14 UTC (permalink / raw)
  To: elver; +Cc: paulmck, andreyknvl, glider, dvyukov, kasan-dev, linux-kernel

Instrumentation of arbitrary memory-copy functions, such as user-copies,
may be called with size of 0, which could lead to false positives.

To avoid this, add a comparison in check_access() for size==0, which
will be optimized out for constant sized instrumentation
(__tsan_{read,write}N), and therefore not affect the common-case
fast-path.

Signed-off-by: Marco Elver <elver@google.com>
---
 kernel/kcsan/core.c |  7 +++++++
 kernel/kcsan/test.c | 10 ++++++++++
 2 files changed, 17 insertions(+)

diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index e3c7d8f34f2ff..82c2bef827d42 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -455,6 +455,13 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
 	atomic_long_t *watchpoint;
 	long encoded_watchpoint;
 
+	/*
+	 * Do nothing for 0 sized check; this comparison will be optimized out
+	 * for constant sized instrumentation (__tsan_{read,write}N).
+	 */
+	if (unlikely(size == 0))
+		return;
+
 	/*
 	 * Avoid user_access_save in fast-path: find_watchpoint is safe without
 	 * user_access_save, as the address that ptr points to is only used to
diff --git a/kernel/kcsan/test.c b/kernel/kcsan/test.c
index cc6000239dc01..d26a052d33838 100644
--- a/kernel/kcsan/test.c
+++ b/kernel/kcsan/test.c
@@ -92,6 +92,16 @@ static bool test_matching_access(void)
 		return false;
 	if (WARN_ON(matching_access(9, 1, 10, 1)))
 		return false;
+
+	/*
+	 * An access of size 0 could match another access, as demonstrated here.
+	 * Rather than add more comparisons to 'matching_access()', which would
+	 * end up in the fast-path for *all* checks, check_access() simply
+	 * returns for all accesses of size 0.
+	 */
+	if (WARN_ON(!matching_access(8, 8, 12, 0)))
+		return false;
+
 	return true;
 }
 
-- 
2.25.0.341.g760bfbb309-goog


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

* Re: [PATCH] kcsan: Fix 0-sized checks
  2020-02-05 10:14 [PATCH] kcsan: Fix 0-sized checks Marco Elver
@ 2020-02-05 15:37 ` Paul E. McKenney
  0 siblings, 0 replies; 2+ messages in thread
From: Paul E. McKenney @ 2020-02-05 15:37 UTC (permalink / raw)
  To: Marco Elver; +Cc: andreyknvl, glider, dvyukov, kasan-dev, linux-kernel

On Wed, Feb 05, 2020 at 11:14:19AM +0100, Marco Elver wrote:
> Instrumentation of arbitrary memory-copy functions, such as user-copies,
> may be called with size of 0, which could lead to false positives.
> 
> To avoid this, add a comparison in check_access() for size==0, which
> will be optimized out for constant sized instrumentation
> (__tsan_{read,write}N), and therefore not affect the common-case
> fast-path.
> 
> Signed-off-by: Marco Elver <elver@google.com>

Queued, thank you!

							Thanx, Paul

> ---
>  kernel/kcsan/core.c |  7 +++++++
>  kernel/kcsan/test.c | 10 ++++++++++
>  2 files changed, 17 insertions(+)
> 
> diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
> index e3c7d8f34f2ff..82c2bef827d42 100644
> --- a/kernel/kcsan/core.c
> +++ b/kernel/kcsan/core.c
> @@ -455,6 +455,13 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
>  	atomic_long_t *watchpoint;
>  	long encoded_watchpoint;
>  
> +	/*
> +	 * Do nothing for 0 sized check; this comparison will be optimized out
> +	 * for constant sized instrumentation (__tsan_{read,write}N).
> +	 */
> +	if (unlikely(size == 0))
> +		return;
> +
>  	/*
>  	 * Avoid user_access_save in fast-path: find_watchpoint is safe without
>  	 * user_access_save, as the address that ptr points to is only used to
> diff --git a/kernel/kcsan/test.c b/kernel/kcsan/test.c
> index cc6000239dc01..d26a052d33838 100644
> --- a/kernel/kcsan/test.c
> +++ b/kernel/kcsan/test.c
> @@ -92,6 +92,16 @@ static bool test_matching_access(void)
>  		return false;
>  	if (WARN_ON(matching_access(9, 1, 10, 1)))
>  		return false;
> +
> +	/*
> +	 * An access of size 0 could match another access, as demonstrated here.
> +	 * Rather than add more comparisons to 'matching_access()', which would
> +	 * end up in the fast-path for *all* checks, check_access() simply
> +	 * returns for all accesses of size 0.
> +	 */
> +	if (WARN_ON(!matching_access(8, 8, 12, 0)))
> +		return false;
> +
>  	return true;
>  }
>  
> -- 
> 2.25.0.341.g760bfbb309-goog
> 

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

end of thread, other threads:[~2020-02-05 15:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-05 10:14 [PATCH] kcsan: Fix 0-sized checks Marco Elver
2020-02-05 15:37 ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).