Skip to content

Commit ed95f95

Browse files
melveringomolnar
authored andcommitted
kcsan: Fix 0-sized checks
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> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 8cfbb04 commit ed95f95

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎kernel/kcsan/core.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
455455
atomic_long_t *watchpoint;
456456
long encoded_watchpoint;
457457

458+
/*
459+
* Do nothing for 0 sized check; this comparison will be optimized out
460+
* for constant sized instrumentation (__tsan_{read,write}N).
461+
*/
462+
if (unlikely(size == 0))
463+
return;
464+
458465
/*
459466
* Avoid user_access_save in fast-path: find_watchpoint is safe without
460467
* user_access_save, as the address that ptr points to is only used to

‎kernel/kcsan/test.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ static bool test_matching_access(void)
9292
return false;
9393
if (WARN_ON(matching_access(9, 1, 10, 1)))
9494
return false;
95+
96+
/*
97+
* An access of size 0 could match another access, as demonstrated here.
98+
* Rather than add more comparisons to 'matching_access()', which would
99+
* end up in the fast-path for *all* checks, check_access() simply
100+
* returns for all accesses of size 0.
101+
*/
102+
if (WARN_ON(!matching_access(8, 8, 12, 0)))
103+
return false;
104+
95105
return true;
96106
}
97107

0 commit comments

Comments
 (0)