• Peter Collingbourne's avatar
    kasan: test: add memcpy test that avoids out-of-bounds write · 758cabae
    Peter Collingbourne authored
    With HW tag-based KASAN, error checks are performed implicitly by the
    load and store instructions in the memcpy implementation.  A failed
    check results in tag checks being disabled and execution will keep
    going.  As a result, under HW tag-based KASAN, prior to commit
    1b0668be ("kasan: test: disable kmalloc_memmove_invalid_size for
    HW_TAGS"), this memcpy would end up corrupting memory until it hits an
    inaccessible page and causes a kernel panic.
    
    This is a pre-existing issue that was revealed by commit 28513304
    ("arm64: Import latest memcpy()/memmove() implementation") which changed
    the memcpy implementation from using signed comparisons (incorrectly,
    resulting in the memcpy being terminated early for negative sizes) to
    using unsigned comparisons.
    
    It is unclear how this could be handled by memcpy itself in a reasonable
    way.  One possibility would be to add an exception handler that would
    force memcpy to return if a tag check fault is detected -- this would
    make the behavior roughly similar to generic and SW tag-based KASAN.
    However, this wouldn't solve the problem for asynchronous mode and also
    makes memcpy behavior inconsistent with manually copying data.
    
    This test was added as a part of a series that taught KASAN to detect
    negative sizes in memory operations, see commit 8cceeff4 ("kasan:
    detect negative size in memory operation function").  Therefore we
    should keep testing for negative sizes with generic and SW tag-based
    KASAN.  But there is some value in testing small memcpy overflows, so
    let's add another test with memcpy that does not destabilize the kernel
    by performing out-of-bounds writes, and run it in all modes.
    
    Link: https://linux-review.googlesource.com/id/I048d1e6a9aff766c4a53f989fb0c83de68923882
    Link: https://lkml.kernel.org/r/20210910211356.3603758-1-pcc@google.comSigned-off-by: default avatarPeter Collingbourne <pcc@google.com>
    Reviewed-by: default avatarAndrey Konovalov <andreyknvl@gmail.com>
    Acked-by: default avatarMarco Elver <elver@google.com>
    Cc: Robin Murphy <robin.murphy@arm.com>
    Cc: Will Deacon <will@kernel.org>
    Cc: Catalin Marinas <catalin.marinas@arm.com>
    Cc: Mark Rutland <mark.rutland@arm.com>
    Cc: Evgenii Stepanov <eugenis@google.com>
    Cc: Alexander Potapenko <glider@google.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    758cabae
test_kasan.c 30.9 KB