Skip to contentSkip to conversation

lib/test_kasan: Add bitops tests

PATCH Superseded Marco Elver 6 participants 21 messages May 28, 2019 – May 29, 2019

Subsystems (7)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 lib/test_kasan: Add bitops tests

This adds bitops tests to the test_kasan module. In a follow-up patch, support for bitops instrumentation will be added.

---
 lib/test_kasan.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 3 deletions(-)
lib/test_kasan.c +70 -3
index 7de2702621dc..f67f3b52251d 100644
@@ -11,16 +11,17 @@
 
 #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
 
+#include <linux/bitops.h>
 #include <linux/delay.h>
+#include <linux/kasan.h>
 #include <linux/kernel.h>
-#include <linux/mman.h>
 #include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/module.h>
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
-#include <linux/module.h>
-#include <linux/kasan.h>
 
 /*
  * Note: test functions are marked noinline so that their names appear in
@@ -623,6 +624,71 @@ static noinline void __init kasan_strings(void)
 	strnlen(ptr, 1);
 }
 
+static noinline void __init kasan_bitops(void)
+{
+	long bits = 0;
+	const long bit = sizeof(bits) * 8;
+
+	pr_info("within-bounds in set_bit");
+	set_bit(0, &bits);
+
+	pr_info("within-bounds in set_bit");
+	set_bit(bit - 1, &bits);
+
+	pr_info("out-of-bounds in set_bit\n");
+	set_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __set_bit\n");
+	__set_bit(bit, &bits);
+
+	pr_info("out-of-bounds in clear_bit\n");
+	clear_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __clear_bit\n");
+	__clear_bit(bit, &bits);
+
+	pr_info("out-of-bounds in clear_bit_unlock\n");
+	clear_bit_unlock(bit, &bits);
+
+	pr_info("out-of-bounds in __clear_bit_unlock\n");
+	__clear_bit_unlock(bit, &bits);
+
+	pr_info("out-of-bounds in change_bit\n");
+	change_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __change_bit\n");
+	__change_bit(bit, &bits);
+
+	pr_info("out-of-bounds in test_and_set_bit\n");
+	test_and_set_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __test_and_set_bit\n");
+	__test_and_set_bit(bit, &bits);
+
+	pr_info("out-of-bounds in test_and_set_bit_lock\n");
+	test_and_set_bit_lock(bit, &bits);
+
+	pr_info("out-of-bounds in test_and_clear_bit\n");
+	test_and_clear_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __test_and_clear_bit\n");
+	__test_and_clear_bit(bit, &bits);
+
+	pr_info("out-of-bounds in test_and_change_bit\n");
+	test_and_change_bit(bit, &bits);
+
+	pr_info("out-of-bounds in __test_and_change_bit\n");
+	__test_and_change_bit(bit, &bits);
+
+	pr_info("out-of-bounds in test_bit\n");
+	(void)test_bit(bit, &bits);
+
+#if defined(clear_bit_unlock_is_negative_byte)
+	pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
+	clear_bit_unlock_is_negative_byte(bit, &bits);
+#endif
+}
+
 static int __init kmalloc_tests_init(void)
 {
 	/*
@@ -664,6 +730,7 @@ static int __init kmalloc_tests_init(void)
 	kasan_memchr();
 	kasan_memcmp();
 	kasan_strings();
+	kasan_bitops();
 
 	kasan_restore_multi_shot(multishot);
 
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
#1 lore raw

PATCH 2/3 tools/objtool: add kasan_check_* to uaccess whitelist

This is a pre-requisite for enabling bitops instrumentation. Some bitops may safely be used with instrumentation in uaccess regions.

For example, on x86, test_bit is used to test a CPU-feature in a
uaccess region: arch/x86/ia32/ia32_signal.c:361

---
 tools/objtool/check.c | 2 ++
 1 file changed, 2 insertions(+)
tools/objtool/check.c +2 -0
index 172f99195726..eff0e5209402 100644
@@ -443,6 +443,8 @@ static void add_ignores(struct objtool_file *file)
 static const char *uaccess_safe_builtin[] = {
 	/* KASAN */
 	"kasan_report",
+	"kasan_check_read",
+	"kasan_check_write",
 	"check_memory_region",
 	/* KASAN out-of-line */
 	"__asan_loadN_noabort",
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
maintainer
#2 lore raw

Re: [PATCH 2/3] tools/objtool: add kasan_check_* to uaccess whitelist

Quoted 5 lines · Marco Elver

This is a pre-requisite for enabling bitops instrumentation. Some bitops may safely be used with instrumentation in uaccess regions.

For example, on x86, test_bit is used to test a CPU-feature in a
uaccess region: arch/x86/ia32/ia32_signal.c:361

That one can easily be moved out of the uaccess region. Any else?

Quoted 21 lines
---
 tools/objtool/check.c | 2 ++
 1 file changed, 2 insertions(+)
tools/objtool/check.c +2 -0
index 172f99195726..eff0e5209402 100644
@@ -443,6 +443,8 @@ static void add_ignores(struct objtool_file *file)
 static const char *uaccess_safe_builtin[] = {
 	/* KASAN */
 	"kasan_report",
+	"kasan_check_read",
+	"kasan_check_write",
 	"check_memory_region",
 	/* KASAN out-of-line */
 	"__asan_loadN_noabort",
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
#3 lore raw

Re: [PATCH 2/3] tools/objtool: add kasan_check_* to uaccess whitelist

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Marco Elver

This is a pre-requisite for enabling bitops instrumentation. Some bitops may safely be used with instrumentation in uaccess regions.

For example, on x86, test_bit is used to test a CPU-feature in a
uaccess region: arch/x86/ia32/ia32_signal.c:361

That one can easily be moved out of the uaccess region. Any else?

Marco, try to update config with "make allyesconfig" and then build the kernel without this change.

Quoted 22 lines
Quoted 22 lines
---
 tools/objtool/check.c | 2 ++
 1 file changed, 2 insertions(+)
tools/objtool/check.c +2 -1
index 172f99195726..eff0e5209402 100644
@@ -443,6 +443,8 @@ static void add_ignores(struct objtool_file *file)
 static const char *uaccess_safe_builtin[] = {
      /* KASAN */
      "kasan_report",
+     "kasan_check_read",
+     "kasan_check_write",
      "check_memory_region",
      /* KASAN out-of-line */
      "__asan_loadN_noabort",
--

2.22.0.rc1.257.g3120a18244-goog

#4 lore raw

Re: [PATCH 2/3] tools/objtool: add kasan_check_* to uaccess whitelist

Quoted 7 lines · Dmitry Vyukov
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Marco Elver

This is a pre-requisite for enabling bitops instrumentation. Some bitops may safely be used with instrumentation in uaccess regions.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

For example, on x86, test_bit is used to test a CPU-feature in a
uaccess region: arch/x86/ia32/ia32_signal.c:361

That one can easily be moved out of the uaccess region. Any else?

Marco, try to update config with "make allyesconfig" and then build the kernel without this change.

Done. The only instance of the uaccess warning is still in arch/x86/ia32/ia32_signal.c.

Change the patch to move this access instead? Let me know what you prefer.

Thanks,
-- Marco

maintainer
#5 lore raw

Re: [PATCH 2/3] tools/objtool: add kasan_check_* to uaccess whitelist

Quoted 13 lines · Marco Elver
Quoted 12 lines · Dmitry Vyukov
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Marco Elver

This is a pre-requisite for enabling bitops instrumentation. Some bitops may safely be used with instrumentation in uaccess regions.

For example, on x86, test_bit is used to test a CPU-feature in a
uaccess region: arch/x86/ia32/ia32_signal.c:361

That one can easily be moved out of the uaccess region. Any else?

Quoted 8 lines
Quoted 3 lines

Marco, try to update config with "make allyesconfig" and then build the kernel without this change.

Done. The only instance of the uaccess warning is still in arch/x86/ia32/ia32_signal.c.

Change the patch to move this access instead? Let me know what you prefer.

Yes, I think that might be best. The whitelist should be minimal.

#6 lore raw

PATCH 3/3 asm-generic, x86: Add bitops instrumentation for KASAN

In reply to Marco Elver

This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.

This change includes the required change for x86 as reference and changes the kernel API doc to point to bitops-instrumented.h instead.
Rationale: the functions in x86's bitops.h are no longer the kernel API functions, but instead the arch_ prefixed functions, which are then instrumented via bitops-instrumented.h.

Other architectures can similarly add support for asm implementations of bitops.

The documentation text has been copied/moved, and *no* changes to it have been made in this patch.

Tested: using lib/test_kasan with bitops tests (pre-requisite patch).

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439

---
 Documentation/core-api/kernel-api.rst     |   2 +-
 arch/x86/include/asm/bitops.h             | 210 ++++----------
 include/asm-generic/bitops-instrumented.h | 327 ++++++++++++++++++++++
 3 files changed, 380 insertions(+), 159 deletions(-)
 create mode 100644 include/asm-generic/bitops-instrumented.h
Documentation/core-api/kernel-api.rst +1 -1
index a29c99d13331..65266fa1b706 100644
@@ -51,7 +51,7 @@ The Linux kernel provides more basic utility functions.
 Bit Operations
 --------------
 
-.. kernel-doc:: arch/x86/include/asm/bitops.h
+.. kernel-doc:: include/asm-generic/bitops-instrumented.h
    :internal:
 
 Bitmap Operations
arch/x86/include/asm/bitops.h +52 -158

313 lines not on this page · raw message

include/asm-generic/bitops-instrumented.h new file +327 -0

327 lines not on this page · raw message

Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
reviewer
#7 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 25 lines · Marco Elver

This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.

This change includes the required change for x86 as reference and changes the kernel API doc to point to bitops-instrumented.h instead.
Rationale: the functions in x86's bitops.h are no longer the kernel API functions, but instead the arch_ prefixed functions, which are then instrumented via bitops-instrumented.h.

Other architectures can similarly add support for asm implementations of bitops.

The documentation text has been copied/moved, and *no* changes to it have been made in this patch.

Tested: using lib/test_kasan with bitops tests (pre-requisite patch).

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439

---
 Documentation/core-api/kernel-api.rst     |   2 +-
 arch/x86/include/asm/bitops.h             | 210 ++++----------
 include/asm-generic/bitops-instrumented.h | 327 ++++++++++++++++++++++
 3 files changed, 380 insertions(+), 159 deletions(-)
 create mode 100644 include/asm-generic/bitops-instrumented.h

[...]

include/asm-generic/bitops-instrumented.h line 27

Quoted 9 lines
include/asm-generic/bitops-instrumented.h new file +9 -0
+#if !defined(BITOPS_INSTRUMENT_RANGE)
+/*
+ * This may be defined by an arch's bitops.h, in case bitops do not operate on
+ * single bytes only. The default version here is conservative and assumes that
+ * bitops operate only on the byte with the target bit.
+ */
+#define BITOPS_INSTRUMENT_RANGE(addr, nr)                                  \
+	(const volatile char *)(addr) + ((nr) / BITS_PER_BYTE), 1
+#endif

I was under the impression that logically, all the bitops operated on the entire long the bit happend to be contained in, so checking the entire long would make more sense to me.

FWIW, arm64's atomic bit ops are all implemented atop of atomic_long_* functions, which are instrumented, and always checks at the granularity of a long. I haven't seen splats from that when fuzzing with Syzkaller.

Are you seeing bugs without this?

Thanks,
Mark.

#8 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 44 lines · Mark Rutland
Quoted 25 lines · Marco Elver

This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.

This change includes the required change for x86 as reference and changes the kernel API doc to point to bitops-instrumented.h instead.
Rationale: the functions in x86's bitops.h are no longer the kernel API functions, but instead the arch_ prefixed functions, which are then instrumented via bitops-instrumented.h.

Other architectures can similarly add support for asm implementations of bitops.

The documentation text has been copied/moved, and *no* changes to it have been made in this patch.

Tested: using lib/test_kasan with bitops tests (pre-requisite patch).

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439

---
 Documentation/core-api/kernel-api.rst     |   2 +-
 arch/x86/include/asm/bitops.h             | 210 ++++----------
 include/asm-generic/bitops-instrumented.h | 327 ++++++++++++++++++++++
 3 files changed, 380 insertions(+), 159 deletions(-)
 create mode 100644 include/asm-generic/bitops-instrumented.h

[...]

include/asm-generic/bitops-instrumented.h line 27

Quoted 9 lines
include/asm-generic/bitops-instrumented.h new file +9 -0
+#if !defined(BITOPS_INSTRUMENT_RANGE)
+/*
+ * This may be defined by an arch's bitops.h, in case bitops do not operate on
+ * single bytes only. The default version here is conservative and assumes that
+ * bitops operate only on the byte with the target bit.
+ */
+#define BITOPS_INSTRUMENT_RANGE(addr, nr)                                  \
+     (const volatile char *)(addr) + ((nr) / BITS_PER_BYTE), 1
+#endif

I was under the impression that logically, all the bitops operated on the entire long the bit happend to be contained in, so checking the entire long would make more sense to me.

Quoted 5 lines

FWIW, arm64's atomic bit ops are all implemented atop of atomic_long_* functions, which are instrumented, and always checks at the granularity of a long. I haven't seen splats from that when fuzzing with Syzkaller.

Are you seeing bugs without this?

bitops are not instrumented on x86 at all at the moment, so we have not seen any splats. What we've seen are assorted crashes caused by previous silent memory corruptions by incorrect bitops :)

Good point. If arm already does this, I guess we also need to check whole long's.

#9 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 50 lines · Dmitry Vyukov
Quoted 48 lines · Mark Rutland
Quoted 25 lines · Marco Elver

This adds a new header to asm-generic to allow optionally instrumenting architecture-specific asm implementations of bitops.

This change includes the required change for x86 as reference and changes the kernel API doc to point to bitops-instrumented.h instead.
Rationale: the functions in x86's bitops.h are no longer the kernel API functions, but instead the arch_ prefixed functions, which are then instrumented via bitops-instrumented.h.

Other architectures can similarly add support for asm implementations of bitops.

The documentation text has been copied/moved, and *no* changes to it have been made in this patch.

Tested: using lib/test_kasan with bitops tests (pre-requisite patch).

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=198439

---
 Documentation/core-api/kernel-api.rst     |   2 +-
 arch/x86/include/asm/bitops.h             | 210 ++++----------
 include/asm-generic/bitops-instrumented.h | 327 ++++++++++++++++++++++
 3 files changed, 380 insertions(+), 159 deletions(-)
 create mode 100644 include/asm-generic/bitops-instrumented.h

[...]

include/asm-generic/bitops-instrumented.h line 27

Quoted 9 lines
include/asm-generic/bitops-instrumented.h new file +9 -0
+#if !defined(BITOPS_INSTRUMENT_RANGE)
+/*
+ * This may be defined by an arch's bitops.h, in case bitops do not operate on
+ * single bytes only. The default version here is conservative and assumes that
+ * bitops operate only on the byte with the target bit.
+ */
+#define BITOPS_INSTRUMENT_RANGE(addr, nr)                                  \
+     (const volatile char *)(addr) + ((nr) / BITS_PER_BYTE), 1
+#endif

I was under the impression that logically, all the bitops operated on the entire long the bit happend to be contained in, so checking the entire long would make more sense to me.

FWIW, arm64's atomic bit ops are all implemented atop of atomic_long_* functions, which are instrumented, and always checks at the granularity of a long. I haven't seen splats from that when fuzzing with Syzkaller.

Quoted 8 lines
Quoted 1 line

Are you seeing bugs without this?

bitops are not instrumented on x86 at all at the moment, so we have not seen any splats. What we've seen are assorted crashes caused by previous silent memory corruptions by incorrect bitops :)

Good point. If arm already does this, I guess we also need to check whole long's.

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on. Other architectures that need bitops-instrumented.h may redefine BITOPS_INSTRUMENT_RANGE.

Let me know what you prefer.

Thanks,
-- Marco

maintainer
#10 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
	if (IS_IMMEDIATE(nr)) {
		asm volatile(LOCK_PREFIX "orb %1,%0"
			: CONST_MASK_ADDR(nr, addr)
			: "iq" ((u8)CONST_MASK(nr))
			: "memory");
	} else {
		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
	}
}

That results in:

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

#11 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 21 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}
Quoted 5 lines

That results in:

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

maintainer
#12 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]
#13 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 35 lines · Peter Zijlstra
Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Quoted 6 lines

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

This probably should be done as a separate config as not related to KASAN per se. But obviously via the same {atomicops,bitops}-instrumented.h hooks which will make it significantly easier.

#14 lore raw

RE: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

From: Dmitry Vyukov

Quoted 41 lines

Sent: 29 May 2019 11:57

Quoted 39 lines · Peter Zijlstra
Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

Quoted 5 lines
Quoted 2 lines

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

The bitops are defined on 'long []' and it is expected to be aligned. Any code that casts the argument is likely to be broken on big-endian. I did a quick grep a few weeks ago and found some very dubious code. Not all the casts seemed to be on code that was LE only (although I didn't try to find out what the casts were from).

The alignment trap on x86 could be avoided by only ever requesting 32bit cycles - and assuming the buffer is always 32bit aligned (eg int []). But on BE passing an 'int []' is just so wrong ....

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

maintainer
#15 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 12 lines · David Laight

From: Dmitry Vyukov

Quoted 4 lines

Sent: 29 May 2019 11:57

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

The bitops are defined on 'long []' and it is expected to be aligned. Any code that casts the argument is likely to be broken on big-endian. I did a quick grep a few weeks ago and found some very dubious code. Not all the casts seemed to be on code that was LE only (although I didn't try to find out what the casts were from).

Quoted 3 lines

The alignment trap on x86 could be avoided by only ever requesting 32bit cycles - and assuming the buffer is always 32bit aligned (eg int []). But on BE passing an 'int []' is just so wrong ....

Right, but as argued elsewhere, I feel we should clean up the dubious code instead of enabling it.

#16 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

In reply to Dmitry Vyukov
Quoted 40 lines · Dmitry Vyukov
Quoted 39 lines · Peter Zijlstra
Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

Quoted 6 lines
Quoted 2 lines

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

It should be aligned. This even documented in Documentation/core-api/atomic_ops.rst:

Native atomic bit operations are defined to operate on objects aligned to the size of an "unsigned long" C data type, and are least of that size. The endianness of the bits within each "unsigned long" are the native endianness of the cpu.

Quoted 5 lines

This probably should be done as a separate config as not related to KASAN per se. But obviously via the same {atomicops,bitops}-instrumented.h hooks which will make it significantly easier.

Agreed.

#17 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

Quoted 56 lines · Andrey Ryabinin
Quoted 40 lines · Dmitry Vyukov
Quoted 39 lines · Peter Zijlstra
Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

Quoted 6 lines
Quoted 2 lines

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

It should be aligned. This even documented in Documentation/core-api/atomic_ops.rst:

Native atomic bit operations are defined to operate on objects aligned to the size of an "unsigned long" C data type, and are least of that size. The endianness of the bits within each "unsigned long" are the native endianness of the cpu.

Quoted 7 lines
Quoted 5 lines

This probably should be done as a separate config as not related to KASAN per se. But obviously via the same {atomicops,bitops}-instrumented.h hooks which will make it significantly easier.

Agreed.

Thanks. I've filed https://bugzilla.kernel.org/show_bug.cgi?id=203751
for checking alignment with all the points and references, so that it's not lost.

reviewer
#19 lore raw

Re: [PATCH 3/3] asm-generic, x86: Add bitops instrumentation for KASAN

In reply to Dmitry Vyukov
Quoted 43 lines · Dmitry Vyukov
Quoted 35 lines · Peter Zijlstra
Quoted 24 lines · Marco Elver
Quoted 23 lines · Peter Zijlstra
Quoted 2 lines · Marco Elver

For the default, we decided to err on the conservative side for now, since it seems that e.g. x86 operates only on the byte the bit is on.

This is not correct, see for instance set_bit():

static __always_inline void
set_bit(long nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
        }
}

That results in:

Quoted 8 lines
Quoted 3 lines

LOCK BTSQ nr, (addr)

when @nr is not an immediate.

Thanks for the clarification. Given that arm64 already instruments bitops access to whole words, and x86 may also do so for some bitops, it seems fine to instrument word-sized accesses by default. Is that reasonable?

Quoted 6 lines

Eminently -- the API is defined such; for bonus points KASAN should also do alignment checks on atomic ops. Future hardware will #AC on unaligned [*] LOCK prefix instructions.

(*) not entirely accurate, it will only trap when crossing a line.

    https://lkml.kernel.org/r/[email protected]
Quoted 7 lines

Interesting. Does an address passed to bitops also should be aligned, or alignment is supposed to be handled by bitops themselves?

This probably should be done as a separate config as not related to KASAN per se. But obviously via the same {atomicops,bitops}-instrumented.h hooks which will make it significantly easier.

Makes sense to me -- that should be easy to hack into gen_param_check() in gen-atomic-instrumented.sh, something like:

----

scripts/atomic/gen-atomic-instrumented.sh +7 -0
index e09812372b17..2f6b8f521e57 100755
@@ -21,6 +21,13 @@ gen_param_check()
        [ ${type#c} != ${type} ] && rw="read"
 
        printf "\tkasan_check_${rw}(${name}, sizeof(*${name}));\n"
+
+       [ "${type#c}" = "v" ] || return
+
+cat <<EOF
+       if (IS_ENABLED(CONFIG_PETERZ))
+               WARN_ON(!IS_ALIGNED(${name}, sizeof(*${name})));
+EOF
 }
 
 #gen_param_check(arg...)

----

On arm64 our atomic instructions always perform an alignment check, so we'd only miss if an atomic op bailed out after a plain READ_ONCE() of an unaligned atomic variable.

Thanks,
Mark.

reviewer
#20 lore raw
In reply to Marco Elver

Hi,

lib/test_kasan.c static noinline void __init kasan_strings(void) · line 630

Quoted 4 lines · Marco Elver
lib/test_kasan.c +4 -0
+static noinline void __init kasan_bitops(void)
+{
+	long bits = 0;
+	const long bit = sizeof(bits) * 8;

You can use BITS_PER_LONG here.

Thanks,
Mark.