Skip to contentSkip to conversation

kcsan: selftest: Ensure that address is at least PAGE_SIZE

PATCH In review Marco Elver 2 participants 4 messages Oct 22, 2020 KCSAN

lore
Share & tools
lore

Marco Elver

author maintainer
#0 lore raw

PATCH v2 1/2 kcsan: selftest: Ensure that address is at least PAGE_SIZE

In preparation of supporting only addresses not within the NULL page, change the selftest to never use addresses that are less than PAGE_SIZE.

---
v2:
* Introduce patch to series.

---
 kernel/kcsan/selftest.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcsan/selftest.c +3 -0
index d98bc208d06d..9014a3a82cf9 100644
@@ -33,6 +33,9 @@ static bool test_encode_decode(void)
 		unsigned long addr;
 
 		prandom_bytes(&addr, sizeof(addr));
+		if (addr < PAGE_SIZE)
+			addr = PAGE_SIZE;
+
 		if (WARN_ON(!check_encodable(addr, size)))
 			return false;
 
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
author maintainer
#1 lore raw

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.

---
 kernel/kcsan/encoding.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/kcsan/encoding.h +5 -1
index f03562aaf2eb..64b3c0f2a685 100644
@@ -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
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
reviewer Reviewed-by on v2 1/2
#3 lore raw
In reply to Marco Elver
Quoted 5 lines · Marco Elver

In preparation of supporting only addresses not within the NULL page, change the selftest to never use addresses that are less than PAGE_SIZE.

Quoted 24 lines

---
v2:
* Introduce patch to series.

---
 kernel/kcsan/selftest.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcsan/selftest.c +3 -1
index d98bc208d06d..9014a3a82cf9 100644
@@ -33,6 +33,9 @@ static bool test_encode_decode(void)
                unsigned long addr;
 
                prandom_bytes(&addr, sizeof(addr));
+               if (addr < PAGE_SIZE)
+                       addr = PAGE_SIZE;
+
                if (WARN_ON(!check_encodable(addr, size)))
                        return false;
 
--

2.29.0.rc1.297.gfa9743e501-goog

reviewer Reviewed-by on v2 2/2
#2 lore raw

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

In reply to Marco Elver
Quoted 8 lines · Marco Elver

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.

Quoted 24 lines
---
 kernel/kcsan/encoding.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/kcsan/encoding.h +5 -2
index f03562aaf2eb..64b3c0f2a685 100644
@@ -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