Skip to content

Commit 98f3b56

Browse files
truhuantorvalds
authored andcommitted
kasan: add test for invalid size in memmove
Test negative size in memmove in order to verify whether it correctly get KASAN report. Casting negative numbers to size_t would indeed turn up as a large size_t, so it will have out-of-bounds bug and be detected by KASAN. [[email protected]: fix -Wstringop-overflow warning] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Walter Wu <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]> Reviewed-by: Andrey Ryabinin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: kernel test robot <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8cceeff commit 98f3b56

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/test_kasan.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ static noinline void __init kmalloc_oob_in_memset(void)
285285
kfree(ptr);
286286
}
287287

288+
static noinline void __init kmalloc_memmove_invalid_size(void)
289+
{
290+
char *ptr;
291+
size_t size = 64;
292+
volatile size_t invalid_size = -2;
293+
294+
pr_info("invalid size in memmove\n");
295+
ptr = kmalloc(size, GFP_KERNEL);
296+
if (!ptr) {
297+
pr_err("Allocation failed\n");
298+
return;
299+
}
300+
301+
memset((char *)ptr, 0, 64);
302+
memmove((char *)ptr, (char *)ptr + 4, invalid_size);
303+
kfree(ptr);
304+
}
305+
288306
static noinline void __init kmalloc_uaf(void)
289307
{
290308
char *ptr;
@@ -799,6 +817,7 @@ static int __init kmalloc_tests_init(void)
799817
kmalloc_oob_memset_4();
800818
kmalloc_oob_memset_8();
801819
kmalloc_oob_memset_16();
820+
kmalloc_memmove_invalid_size();
802821
kmalloc_uaf();
803822
kmalloc_uaf_memset();
804823
kmalloc_uaf2();

0 commit comments

Comments
 (0)