> We were incorrectly assuming that only the first byte of an MTE access
> is checked against the tags. But per the ARM, unaligned accesses are
> pre-decomposed into single-byte accesses. So by the time we reach the
> actual MTE check in the ARM pseudocode, all accesses are aligned.
>
> Therefore, the first failure is always either the first byte of the
> access, or the first byte of the granule.
>
> In addition, some of the arithmetic is off for last-first -> count.
> This does not become directly visible until a later patch that passes
> single bytes into this function, so ptr == ptr_last.
>
> Buglink: https://bugs.launchpad.net/bugs/1921948
Minor note: you can Cc: Bug 1921948 <email address hidden> to
automatically copy patches to the appropriate bugs which is useful if
you don't have the Cc for the reporter.
Anyway I'm trying to get the kasas unit tests running as a way of
testing this (and maybe expanding with a version of Andrey's test). I
suspect this may be a PEBCAC issue but I built an MTE enabled kernel
with:
CONFIG_HAVE_ARCH_KASAN=y
CONFIG_HAVE_ARCH_KASAN_SW_TAGS=y
CONFIG_HAVE_ARCH_KASAN_HW_TAGS=y
CONFIG_CC_HAS_KASAN_GENERIC=y
CONFIG_KASAN=y
# CONFIG_KASAN_GENERIC is not set
CONFIG_KASAN_HW_TAGS=y
CONFIG_KASAN_STACK=1
CONFIG_KASAN_KUNIT_TEST=m
CONFIG_TEST_KASAN_MODULE=m
and was able to boot it. But when I insmod the kasan tests:
insmod test_kasan.ko
it looks like it just keeps looping failing on the same test:
Richard Henderson <email address hidden> writes:
> We were incorrectly assuming that only the first byte of an MTE access /bugs.launchpad .net/bugs/ 1921948
> is checked against the tags. But per the ARM, unaligned accesses are
> pre-decomposed into single-byte accesses. So by the time we reach the
> actual MTE check in the ARM pseudocode, all accesses are aligned.
>
> Therefore, the first failure is always either the first byte of the
> access, or the first byte of the granule.
>
> In addition, some of the arithmetic is off for last-first -> count.
> This does not become directly visible until a later patch that passes
> single bytes into this function, so ptr == ptr_last.
>
> Buglink: https:/
Minor note: you can Cc: Bug 1921948 <email address hidden> to
automatically copy patches to the appropriate bugs which is useful if
you don't have the Cc for the reporter.
Anyway I'm trying to get the kasas unit tests running as a way of
testing this (and maybe expanding with a version of Andrey's test). I
suspect this may be a PEBCAC issue but I built an MTE enabled kernel
with:
CONFIG_ HAVE_ARCH_ KASAN=y HAVE_ARCH_ KASAN_SW_ TAGS=y HAVE_ARCH_ KASAN_HW_ TAGS=y CC_HAS_ KASAN_GENERIC= y KASAN_GENERIC is not set KASAN_HW_ TAGS=y KASAN_STACK= 1 KASAN_KUNIT_ TEST=m TEST_KASAN_ MODULE= m
CONFIG_
CONFIG_
CONFIG_
CONFIG_KASAN=y
# CONFIG_
CONFIG_
CONFIG_
CONFIG_
CONFIG_
and was able to boot it. But when I insmod the kasan tests:
insmod test_kasan.ko
it looks like it just keeps looping failing on the same test:
Ignoring spurious kernel translation fault at virtual address dead00000000010a mm/fault. c:364 __do_kernel_ fault+0xc4/ 0x1bc fault+0xc4/ 0x1bc fault+0xc4/ 0x1bc kernel_ fault+0xc4/ 0x1bc on_fault+ 0x98/0xb0 abort+0x44/ 0xb0 abort+0x40/ 0x6c sync_handler+ 0x6c/0xb0 sync+0x70/ 0x100 update_ kunit_status+ 0x6c/0x1ac report_ invalid_ free+0x34/ 0xa0 kasan_slab_ free.constprop. 0+0xf8/ 0x1a0 slab_free+ 0x10/0x20 free_freelist_ hook+0xf8/ 0x1a0 0x148/0x25c destroy_ resource+ 0x15c/0x1bc stream_ destroy+ 0x20/0x80 do_assertion+ 0x190/0x1e4 double_ kzfree+ 0x158/0x190 [test_kasan] try_run_ case+0x78/ 0xa4 generic_ run_threadfn_ adapter+ 0x20/0x2c 0x134/0x144 from_fork+ 0x10/0x38
WARNING: CPU: 0 PID: 1444 at arch/arm64/
Modules linked in: test_kasan(+)
CPU: 0 PID: 1444 Comm: kunit_try_catch Tainted: G B W 5.11.0-ajb-kasan #3
Hardware name: linux,dummy-virt (DT)
pstate: 60400009 (nZCv daif +PAN -UAO -TCO BTYPE=--)
pc : __do_kernel_
lr : __do_kernel_
sp : ffffffc01191b900
x29: ffffffc01191b900 x28: fcffff8001f7a880
x27: fcffff8001c01e00 x26: 0000000000000000
x25: 0000000000000001 x24: 00000000000000f4
x23: 0000000020400009 x22: dead00000000010a
x21: 0000000000000025 x20: ffffffc01191b9d0
x19: 0000000097c08004 x18: 0000000000000000
x17: 000000000000000a x16: 000017a83fb75794
x15: 0000000000000030 x14: 6c656e72656b2073
x13: ffffffc010e21be0 x12: 00000000000001aa
x11: 000000000000008e x10: ffffffc010e2d930
x9 : 000000000003a6d0 x8 : ffffffc010e21be0
x7 : ffffffc010e2cbe0 x6 : 0000000000000d50
x5 : ffffff8007f9c850 x4 : ffffffc01191b700
x3 : 0000000000000001 x2 : 0000000000000000
x1 : 0000000000000000 x0 : fcffff8001f7a880
Call trace:
__do_
do_translati
do_mem_
el1_
el1_
el1_
kasan_
kasan_
____
__kasan_
slab_
kfree+
kunit_
string_
kunit_
kmalloc_
kunit_
kunit_
kthread+
ret_
---[ end trace 5acd02cdb9b3d3f0 ]---
but maybe I'm using the kunit tests wrong. It's my first time playing
with them.
> Signed-off-by: Richard Henderson <email address hidden> arm/mte_ helper. c | 38 +++++++ +++++++ +++---- ------- ------- --- arm/mte_ helper. c b/target/ arm/mte_ helper. c .c87717127c 100644 arm/mte_ helper. c arm/mte_ helper. c CPUARMState *env, uint32_t desc, CPUARMState *env, uint32_t desc, DOWN(ptr, TAG_GRANULE); UP(ptr_ last, TAG_GRANULE); DOWN(ptr_ last, TAG_GRANULE); DOWN(ptr, 2 * TAG_GRANULE); UP(ptr_ last, 2 * TAG_GRANULE); DOWN(ptr_ last, 2 * TAG_GRANULE); tag_mem( env, mmu_idx, ptr, type, total, CPUARMState *env, uint32_t desc, tag_mem( env, mmu_idx, ptr, type, next_page - ptr, tag_mem( env, mmu_idx, next_page, type, CPUARMState *env, uint32_t desc,
> ---
> target/
> 1 file changed, 17 insertions(+), 21 deletions(-)
>
> diff --git a/target/
> index 8be17e1b70.
> --- a/target/
> +++ b/target/
> @@ -757,10 +757,10 @@ uint64_t mte_checkN(
> uint64_t ptr, uintptr_t ra)
> {
> int mmu_idx, ptr_tag, bit55;
> - uint64_t ptr_last, ptr_end, prev_page, next_page;
> - uint64_t tag_first, tag_end;
> - uint64_t tag_byte_first, tag_byte_end;
> - uint32_t esize, total, tag_count, tag_size, n, c;
> + uint64_t ptr_last, prev_page, next_page;
> + uint64_t tag_first, tag_last;
> + uint64_t tag_byte_first, tag_byte_last;
> + uint32_t total, tag_count, tag_size, n, c;
> uint8_t *mem1, *mem2;
> MMUAccessType type;
>
> @@ -779,29 +779,27 @@ uint64_t mte_checkN(
>
> mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
> type = FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_LOAD;
> - esize = FIELD_EX32(desc, MTEDESC, ESIZE);
> total = FIELD_EX32(desc, MTEDESC, TSIZE);
>
> /* Find the addr of the end of the access, and of the last element. */
> - ptr_end = ptr + total;
> - ptr_last = ptr_end - esize;
> + ptr_last = ptr + total - 1;
>
> /* Round the bounds to the tag granule, and compute the number of tags. */
> tag_first = QEMU_ALIGN_
> - tag_end = QEMU_ALIGN_
> - tag_count = (tag_end - tag_first) / TAG_GRANULE;
> + tag_last = QEMU_ALIGN_
> + tag_count = ((tag_last - tag_first) / TAG_GRANULE) + 1;
>
> /* Round the bounds to twice the tag granule, and compute the bytes. */
> tag_byte_first = QEMU_ALIGN_
> - tag_byte_end = QEMU_ALIGN_
> + tag_byte_last = QEMU_ALIGN_
>
> /* Locate the page boundaries. */
> prev_page = ptr & TARGET_PAGE_MASK;
> next_page = prev_page + TARGET_PAGE_SIZE;
>
> - if (likely(tag_end - prev_page <= TARGET_PAGE_SIZE)) {
> + if (likely(tag_last - prev_page <= TARGET_PAGE_SIZE)) {
> /* Memory access stays on one page. */
> - tag_size = (tag_byte_end - tag_byte_first) / (2 * TAG_GRANULE);
> + tag_size = ((tag_byte_last - tag_byte_first) / (2 * TAG_GRANULE)) + 1;
> mem1 = allocation_
> MMU_DATA_LOAD, tag_size, ra);
> if (!mem1) {
> @@ -815,9 +813,9 @@ uint64_t mte_checkN(
> mem1 = allocation_
> MMU_DATA_LOAD, tag_size, ra);
>
> - tag_size = (tag_byte_end - next_page) / (2 * TAG_GRANULE);
> + tag_size = ((tag_byte_last - next_page) / (2 * TAG_GRANULE)) + 1;
> mem2 = allocation_
> - ptr_end - next_page,
> + ptr_last - next_page + 1,
> MMU_DATA_LOAD, tag_size, ra);
>
> /*
> @@ -838,15 +836,13 @@ uint64_t mte_checkN(
> }
>
> /*
> - * If we failed, we know which granule. Compute the element that
> - * is first in that granule, and signal failure on that element.
> + * If we failed, we know which granule. For the first granule, the
> + * failure address is @ptr, the first byte accessed. Otherwise the
> + * failure address is the first byte of the nth granule.
> */
> if (unlikely(n < tag_count)) {
> - uint64_t fail_ofs;
> -
> - fail_ofs = tag_first + n * TAG_GRANULE - ptr;
> - fail_ofs = ROUND_UP(fail_ofs, esize);
> - mte_check_fail(env, desc, ptr + fail_ofs, ra);
> + uint64_t fault = (n == 0 ? ptr : tag_first + n * TAG_GRANULE);
> + mte_check_fail(env, desc, fault, ra);
> }
>
> done:
--
Alex Bennée