Skip to content

Commit 36e4d4d

Browse files
melveringomolnar
authored andcommitted
include/linux: Add instrumented.h infrastructure
This adds instrumented.h, which provides generic wrappers for memory access instrumentation that the compiler cannot emit for various sanitizers. Currently this unifies KASAN and KCSAN instrumentation. In future this will also include KMSAN instrumentation. Note that, copy_{to,from}_user should use special instrumentation, since we should be able to instrument both source and destination memory accesses if both are kernel memory. The current patch only instruments the memory access where the address is always in kernel space, however, both may in fact be kernel addresses when a compat syscall passes an argument allocated in the kernel to a real syscall. In a future change, both KASAN and KCSAN should check both addresses in such cases, as well as KMSAN will make use of both addresses. [It made more sense to provide the completed function signature, rather than updating it and changing all locations again at a later time.] Suggested-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Marco Elver <elver@google.com> Acked-by: Alexander Potapenko <glider@google.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ad4f8ee commit 36e4d4d

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

‎include/linux/instrumented.h‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
/*
4+
* This header provides generic wrappers for memory access instrumentation that
5+
* the compiler cannot emit for: KASAN, KCSAN.
6+
*/
7+
#ifndef _LINUX_INSTRUMENTED_H
8+
#define _LINUX_INSTRUMENTED_H
9+
10+
#include <linux/compiler.h>
11+
#include <linux/kasan-checks.h>
12+
#include <linux/kcsan-checks.h>
13+
#include <linux/types.h>
14+
15+
/**
16+
* instrument_read - instrument regular read access
17+
*
18+
* Instrument a regular read access. The instrumentation should be inserted
19+
* before the actual read happens.
20+
*
21+
* @ptr address of access
22+
* @size size of access
23+
*/
24+
static __always_inline void instrument_read(const volatile void *v, size_t size)
25+
{
26+
kasan_check_read(v, size);
27+
kcsan_check_read(v, size);
28+
}
29+
30+
/**
31+
* instrument_write - instrument regular write access
32+
*
33+
* Instrument a regular write access. The instrumentation should be inserted
34+
* before the actual write happens.
35+
*
36+
* @ptr address of access
37+
* @size size of access
38+
*/
39+
static __always_inline void instrument_write(const volatile void *v, size_t size)
40+
{
41+
kasan_check_write(v, size);
42+
kcsan_check_write(v, size);
43+
}
44+
45+
/**
46+
* instrument_atomic_read - instrument atomic read access
47+
*
48+
* Instrument an atomic read access. The instrumentation should be inserted
49+
* before the actual read happens.
50+
*
51+
* @ptr address of access
52+
* @size size of access
53+
*/
54+
static __always_inline void instrument_atomic_read(const volatile void *v, size_t size)
55+
{
56+
kasan_check_read(v, size);
57+
kcsan_check_atomic_read(v, size);
58+
}
59+
60+
/**
61+
* instrument_atomic_write - instrument atomic write access
62+
*
63+
* Instrument an atomic write access. The instrumentation should be inserted
64+
* before the actual write happens.
65+
*
66+
* @ptr address of access
67+
* @size size of access
68+
*/
69+
static __always_inline void instrument_atomic_write(const volatile void *v, size_t size)
70+
{
71+
kasan_check_write(v, size);
72+
kcsan_check_atomic_write(v, size);
73+
}
74+
75+
/**
76+
* instrument_copy_to_user - instrument reads of copy_to_user
77+
*
78+
* Instrument reads from kernel memory, that are due to copy_to_user (and
79+
* variants). The instrumentation must be inserted before the accesses.
80+
*
81+
* @to destination address
82+
* @from source address
83+
* @n number of bytes to copy
84+
*/
85+
static __always_inline void
86+
instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
87+
{
88+
kasan_check_read(from, n);
89+
kcsan_check_read(from, n);
90+
}
91+
92+
/**
93+
* instrument_copy_from_user - instrument writes of copy_from_user
94+
*
95+
* Instrument writes to kernel memory, that are due to copy_from_user (and
96+
* variants). The instrumentation should be inserted before the accesses.
97+
*
98+
* @to destination address
99+
* @from source address
100+
* @n number of bytes to copy
101+
*/
102+
static __always_inline void
103+
instrument_copy_from_user(const void *to, const void __user *from, unsigned long n)
104+
{
105+
kasan_check_write(to, n);
106+
kcsan_check_write(to, n);
107+
}
108+
109+
#endif /* _LINUX_INSTRUMENTED_H */

0 commit comments

Comments
 (0)