Skip to contentSkip to conversation

KCSAN updates for 5.11

PATCH In review Paul E. McKenney 3 participants 10 messages Nov 5, 2020 – Nov 6, 2020 KCSAN

lore
Share & tools
lore
#0 lore raw

cover KCSAN updates for 5.11

Hello!

This series provides KCSAN updates:

1. Ensure that selftest address is at least PAGE_SIZE.

2. Never set up watchpoints on NULL pointers.

3. Fix encoding masks and regain address bit.

Thanx, Paul

------------------------------------------------------------------------

 encoding.h |   20 +++++++++++---------
 selftest.c |    3 +++
 2 files changed, 14 insertions(+), 9 deletions(-)
#1 lore raw

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

From: Marco Elver <[email protected]>

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

---
 kernel/kcsan/selftest.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcsan/selftest.c +3 -0
index d98bc20..9014a3a 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.9.5
#2 lore raw

PATCH 2/3 kcsan: Never set up watchpoints on NULL pointers

In reply to Paul E. McKenney

From: Marco Elver <[email protected]>

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 1a6db2f..4f73db6 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.9.5
#3 lore raw

PATCH 3/3 kcsan: Fix encoding masks and regain address bit

In reply to Paul E. McKenney

From: Marco Elver <[email protected]>

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

	[ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

	[ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +6 -8
index 4f73db6..b50bda9 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-	GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK	BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
+#define WATCHPOINT_ADDR_MASK	GENMASK(BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);
+static_assert((WATCHPOINT_WRITE_MASK ^ WATCHPOINT_SIZE_MASK ^ WATCHPOINT_ADDR_MASK) == ~0UL);
 
 static inline bool check_encodable(unsigned long addr, size_t size)
 {
Signature · 1 line
2.9.5
#4 lore raw

Re: [PATCH kcsan 3/3] kcsan: Fix encoding masks and regain address bit

Hi Marco,

Quoted 41 lines · On Thu, Nov 05, 2020 at 02:03:24PM -0800, [email protected] wrote:

From: Marco Elver <[email protected]>

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

	[ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

	[ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +0 -3
index 4f73db6..b50bda9 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */

kernel/kcsan/encoding.h line 44

Quoted 10 lines
kernel/kcsan/encoding.h +5 -5
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-	GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK	BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
+#define WATCHPOINT_ADDR_MASK	GENMASK(BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);

Nit:

Since you use the static_assert(), why not define WATCHPOINT_ADDR_MASK as:

#define WATCHPOINT_ADDR_MASK (BIT(WATCHPOINT_SIZE_BITS) - 1)

Besides, WATCHPOINT_SIZE_MASK can also be defined as:

#define WATCHPOINT_SIZE_MASK GENMASK(BITS_PER_LONG - 2, WATCHPOINT_SIZE_BITS)

Regards,
Boqun

Quoted 7 lines
kernel/kcsan/encoding.h +1 -0
+static_assert((WATCHPOINT_WRITE_MASK ^ WATCHPOINT_SIZE_MASK ^ WATCHPOINT_ADDR_MASK) == ~0UL);
 
 static inline bool check_encodable(unsigned long addr, size_t size)
 {
Signature · 1 line
2.9.5
maintainer
#5 lore raw

Re: [PATCH kcsan 3/3] kcsan: Fix encoding masks and regain address bit

Quoted 57 lines · Boqun Feng

Hi Marco,

Quoted 41 lines · On Thu, Nov 05, 2020 at 02:03:24PM -0800, [email protected] wrote:

From: Marco Elver <[email protected]>

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

      [ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

      [ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +0 -3
index 4f73db6..b50bda9 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */

kernel/kcsan/encoding.h line 44

Quoted 10 lines
kernel/kcsan/encoding.h +5 -5
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-     GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-     GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK        BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
+#define WATCHPOINT_ADDR_MASK GENMASK(BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);

Nit:

Quoted 4 lines

Since you use the static_assert(), why not define WATCHPOINT_ADDR_MASK as:

#define WATCHPOINT_ADDR_MASK (BIT(WATCHPOINT_SIZE_BITS) - 1)

This is incorrect, as the static_assert()s would have indicated. It should probably be (BIT(WATCHPOINT_ADDR_BITS) - 1)?

As an aside, I explicitly did *not* want to use additional arithmetic to generate the masks but purely rely on BIT(), and GENMASK(), as it would be inconsistent otherwise. The static_assert()s then sanity check everything without BIT+GENMASK (because I've grown slightly paranoid about off-by-1s here). So I'd rather not start bikeshedding about which way around things should go.

In general, GENMASK() is safer, because subtracting 1 to get the mask doesn't always work, specifically e.g. (BIT(BITS_PER_LONG) - 1) does not work.

Quoted 1 line

Besides, WATCHPOINT_SIZE_MASK can also be defined as:

No, sorry it cannot.

Quoted 1 line

#define WATCHPOINT_SIZE_MASK GENMASK(BITS_PER_LONG - 2, WATCHPOINT_SIZE_BITS)

GENMASK(BITS_PER_LONG - 2, WATCHPOINT_SIZE_BITS)

is not equivalent to the current

GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)

Did you mean GENMASK(BITS_PER_LONG-2, WATCHPOINT_ADDR_BITS)? I can send a v2 for this one.

Thanks,
-- Marco

#6 lore raw

Re: [PATCH kcsan 3/3] kcsan: Fix encoding masks and regain address bit

Quoted 83 lines · Marco Elver
Quoted 57 lines · Boqun Feng

Hi Marco,

Quoted 41 lines · On Thu, Nov 05, 2020 at 02:03:24PM -0800, [email protected] wrote:

From: Marco Elver <[email protected]>

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

      [ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

      [ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +0 -3
index 4f73db6..b50bda9 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */

kernel/kcsan/encoding.h line 44

Quoted 10 lines
kernel/kcsan/encoding.h +5 -5
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-     GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-     GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK        BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
+#define WATCHPOINT_ADDR_MASK GENMASK(BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);

Nit:

Quoted 4 lines

Since you use the static_assert(), why not define WATCHPOINT_ADDR_MASK as:

#define WATCHPOINT_ADDR_MASK (BIT(WATCHPOINT_SIZE_BITS) - 1)

This is incorrect, as the static_assert()s would have indicated. It should probably be (BIT(WATCHPOINT_ADDR_BITS) - 1)?

As an aside, I explicitly did *not* want to use additional arithmetic to generate the masks but purely rely on BIT(), and GENMASK(), as it would be inconsistent otherwise. The static_assert()s then sanity check everything without BIT+GENMASK (because I've grown slightly paranoid about off-by-1s here). So I'd rather not start bikeshedding about which way around things should go.

In general, GENMASK() is safer, because subtracting 1 to get the mask doesn't always work, specifically e.g. (BIT(BITS_PER_LONG) - 1) does not work.

Quoted 1 line

Besides, WATCHPOINT_SIZE_MASK can also be defined as:

No, sorry it cannot.

Quoted 1 line

#define WATCHPOINT_SIZE_MASK GENMASK(BITS_PER_LONG - 2, WATCHPOINT_SIZE_BITS)

Quoted 7 lines

GENMASK(BITS_PER_LONG - 2, WATCHPOINT_SIZE_BITS)

is not equivalent to the current

GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)

Did you mean GENMASK(BITS_PER_LONG-2, WATCHPOINT_ADDR_BITS)? I can

You're right! Guess I should check first about what vim completes for me ;-) And I agree with you on the preference to GENMASK()

Quoted 1 line

send a v2 for this one.

Let me add an ack for that one, thanks!

Regards,
Boqun

Quoted 3 lines

Thanks,
-- Marco

maintainer
#7 lore raw

Re: [PATCH kcsan 3/3] kcsan: Fix encoding masks and regain address bit

Quoted 3 lines · Boqun Feng
Quoted 1 line

send a v2 for this one.

Let me add an ack for that one, thanks!

Thank you!

-- Marco

maintainer
#8 lore raw

PATCH v2 1/1 kcsan: Fix encoding masks and regain address bit

In reply to [email protected]

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

	[ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

	[ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

---
v2:
* Use WATCHPOINT_ADDR_BITS to avoid duplicating "BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS" per Boqun's suggestion.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +6 -8
index 4f73db6d1407..7ee405524904 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-	GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK	BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK	GENMASK(BITS_PER_LONG-2, WATCHPOINT_ADDR_BITS)
+#define WATCHPOINT_ADDR_MASK	GENMASK(WATCHPOINT_ADDR_BITS-1, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);
+static_assert((WATCHPOINT_WRITE_MASK ^ WATCHPOINT_SIZE_MASK ^ WATCHPOINT_ADDR_MASK) == ~0UL);
 
 static inline bool check_encodable(unsigned long addr, size_t size)
 {
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
Acked-by on v2 1/1
#9 lore raw

Re: [PATCH v2] kcsan: Fix encoding masks and regain address bit

Quoted 17 lines · Marco Elver

The watchpoint encoding masks for size and address were off-by-one bit each, with the size mask using 1 unnecessary bit and the address mask missing 1 bit. However, due to the way the size is shifted into the encoded watchpoint, we were effectively wasting and never using the extra bit.

For example, on x86 with PAGE_SIZE==4K, we have 1 bit for the is-write bit, 14 bits for the size bits, and then 49 bits left for the address. Prior to this fix we would end up with this usage:

	[ write<1> | size<14> | wasted<1> | address<48> ]

Fix it by subtracting 1 bit from the GENMASK() end and start ranges of size and address respectively. The added static_assert()s verify that the masks are as expected. With the fixed version, we get the expected usage:

Quoted 6 lines
	[ write<1> | size<14> |             address<49> ]

Functionally no change is expected, since that extra address bit is insignificant for enabled architectures.

Regards,
Boqun

Quoted 36 lines

---
v2:
* Use WATCHPOINT_ADDR_BITS to avoid duplicating "BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS" per Boqun's suggestion.

---
 kernel/kcsan/encoding.h | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/kcsan/encoding.h +6 -8
index 4f73db6d1407..7ee405524904 100644
@@ -37,14 +37,12 @@
  */
 #define WATCHPOINT_ADDR_BITS (BITS_PER_LONG-1 - WATCHPOINT_SIZE_BITS)
 
-/*
- * Masks to set/retrieve the encoded data.
- */
-#define WATCHPOINT_WRITE_MASK BIT(BITS_PER_LONG-1)
-#define WATCHPOINT_SIZE_MASK                                                   \
-	GENMASK(BITS_PER_LONG-2, BITS_PER_LONG-2 - WATCHPOINT_SIZE_BITS)
-#define WATCHPOINT_ADDR_MASK                                                   \
-	GENMASK(BITS_PER_LONG-3 - WATCHPOINT_SIZE_BITS, 0)
+/* Bitmasks for the encoded watchpoint access information. */
+#define WATCHPOINT_WRITE_MASK	BIT(BITS_PER_LONG-1)
+#define WATCHPOINT_SIZE_MASK	GENMASK(BITS_PER_LONG-2, WATCHPOINT_ADDR_BITS)
+#define WATCHPOINT_ADDR_MASK	GENMASK(WATCHPOINT_ADDR_BITS-1, 0)
+static_assert(WATCHPOINT_ADDR_MASK == (1UL << WATCHPOINT_ADDR_BITS) - 1);
+static_assert((WATCHPOINT_WRITE_MASK ^ WATCHPOINT_SIZE_MASK ^ WATCHPOINT_ADDR_MASK) == ~0UL);
 
 static inline bool check_encodable(unsigned long addr, size_t size)
 {
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog