VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
d2912cb15bdda (Thomas Gleixner     2019-06-04 10:11:33 +0200    1) // SPDX-License-Identifier: GPL-2.0-only
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    2) /*
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    3)  *
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    4)  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    5)  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    6)  */
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800    7) 
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700    8) #include <linux/bitops.h>
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800    9) #include <linux/delay.h>
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700   10) #include <linux/kasan.h>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800   11) #include <linux/kernel.h>
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700   12) #include <linux/mm.h>
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700   13) #include <linux/mman.h>
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700   14) #include <linux/module.h>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800   15) #include <linux/printk.h>
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800   16) #include <linux/random.h>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800   17) #include <linux/slab.h>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800   18) #include <linux/string.h>
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700   19) #include <linux/uaccess.h>
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700   20) #include <linux/io.h>
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800   21) #include <linux/vmalloc.h>
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700   22) 
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700   23) #include <asm/page.h>
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800   24) 
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   25) #include <kunit/test.h>
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   26) 
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700   27) #include "../mm/kasan/kasan.h"
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700   28) 
1f600626b3a9b (Andrey Konovalov    2020-12-22 12:00:24 -0800   29) #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700   30) 
adb72ae1915db (Daniel Axtens       2020-06-03 15:56:43 -0700   31) /*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   32)  * Some tests use these global variables to store return values from function
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   33)  * calls that could otherwise be eliminated by the compiler as dead code.
adb72ae1915db (Daniel Axtens       2020-06-03 15:56:43 -0700   34)  */
adb72ae1915db (Daniel Axtens       2020-06-03 15:56:43 -0700   35) void *kasan_ptr_result;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   36) int kasan_int_result;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   37) 
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   38) static struct kunit_resource resource;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   39) static struct kunit_kasan_expectation fail_data;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   40) static bool multishot;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   41) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   42) /*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   43)  * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   44)  * first detected bug and panic the kernel if panic_on_warn is enabled. For
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   45)  * hardware tag-based KASAN also allow tag checking to be reenabled for each
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   46)  * test, see the comment for KUNIT_EXPECT_KASAN_FAIL().
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   47)  */
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   48) static int kasan_test_init(struct kunit *test)
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   49) {
d82dc3a40d12c (Andrey Konovalov    2021-02-24 12:06:02 -0800   50) 	if (!kasan_enabled()) {
d82dc3a40d12c (Andrey Konovalov    2021-02-24 12:06:02 -0800   51) 		kunit_err(test, "can't run KASAN tests with KASAN disabled");
d82dc3a40d12c (Andrey Konovalov    2021-02-24 12:06:02 -0800   52) 		return -1;
d82dc3a40d12c (Andrey Konovalov    2021-02-24 12:06:02 -0800   53) 	}
d82dc3a40d12c (Andrey Konovalov    2021-02-24 12:06:02 -0800   54) 
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   55) 	multishot = kasan_save_enable_multi_shot();
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   56) 	kasan_set_tagging_report_once(false);
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   57) 	fail_data.report_found = false;
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   58) 	fail_data.report_expected = false;
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   59) 	kunit_add_named_resource(test, NULL, NULL, &resource,
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   60) 					"kasan_data", &fail_data);
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   61) 	return 0;
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   62) }
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   63) 
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   64) static void kasan_test_exit(struct kunit *test)
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   65) {
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   66) 	kasan_set_tagging_report_once(true);
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   67) 	kasan_restore_multi_shot(multishot);
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   68) 	KUNIT_EXPECT_FALSE(test, fail_data.report_found);
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   69) }
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   70) 
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   71) /**
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   72)  * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   73)  * KASAN report; causes a test failure otherwise. This relies on a KUnit
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   74)  * resource named "kasan_data". Do not use this name for KUnit resources
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800   75)  * outside of KASAN tests.
f05842cfb9ae2 (Andrey Konovalov    2021-02-24 12:05:26 -0800   76)  *
e80a76aa1a910 (Andrey Konovalov    2021-03-15 13:20:19 +0000   77)  * For hardware tag-based KASAN in sync mode, when a tag fault happens, tag
e80a76aa1a910 (Andrey Konovalov    2021-03-15 13:20:19 +0000   78)  * checking is auto-disabled. When this happens, this test handler reenables
e80a76aa1a910 (Andrey Konovalov    2021-03-15 13:20:19 +0000   79)  * tag checking. As tag checking can be only disabled or enabled per CPU,
e80a76aa1a910 (Andrey Konovalov    2021-03-15 13:20:19 +0000   80)  * this handler disables migration (preemption).
2e4bde6a1e3a3 (Andrey Konovalov    2021-02-24 12:05:34 -0800   81)  *
2e4bde6a1e3a3 (Andrey Konovalov    2021-02-24 12:05:34 -0800   82)  * Since the compiler doesn't see that the expression can change the fail_data
2e4bde6a1e3a3 (Andrey Konovalov    2021-02-24 12:05:34 -0800   83)  * fields, it can reorder or optimize away the accesses to those fields.
2e4bde6a1e3a3 (Andrey Konovalov    2021-02-24 12:05:34 -0800   84)  * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
2e4bde6a1e3a3 (Andrey Konovalov    2021-02-24 12:05:34 -0800   85)  * expression to prevent that.
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   86)  *
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   87)  * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   88)  * false. This allows detecting KASAN reports that happen outside of the checks
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   89)  * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   90)  * and in kasan_test_exit.
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700   91)  */
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   92) #define KUNIT_EXPECT_KASAN_FAIL(test, expression) do {			\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   93) 	if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) &&				\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   94) 	    !kasan_async_mode_enabled())				\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   95) 		migrate_disable();					\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   96) 	KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found));	\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   97) 	WRITE_ONCE(fail_data.report_expected, true);			\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   98) 	barrier();							\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700   99) 	expression;							\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  100) 	barrier();							\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  101) 	KUNIT_EXPECT_EQ(test,						\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  102) 			READ_ONCE(fail_data.report_expected),		\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  103) 			READ_ONCE(fail_data.report_found));		\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  104) 	if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) {				\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  105) 		if (READ_ONCE(fail_data.report_found))			\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  106) 			kasan_enable_tagging_sync();			\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  107) 		migrate_enable();					\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  108) 	}								\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  109) 	WRITE_ONCE(fail_data.report_found, false);			\
99734b535d9bf (Andrey Konovalov    2021-04-29 23:00:49 -0700  110) 	WRITE_ONCE(fail_data.report_expected, false);			\
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700  111) } while (0)
83c4e7a0363bd (Patricia Alfonso    2020-10-13 16:55:02 -0700  112) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  113) #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do {			\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  114) 	if (!IS_ENABLED(config)) {					\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  115) 		kunit_info((test), "skipping, " #config " required");	\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  116) 		return;							\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  117) 	}								\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  118) } while (0)
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  119) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  120) #define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do {			\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  121) 	if (IS_ENABLED(config)) {					\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  122) 		kunit_info((test), "skipping, " #config " enabled");	\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  123) 		return;							\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  124) 	}								\
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  125) } while (0)
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  126) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  127) static void kmalloc_oob_right(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  128) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  129) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  130) 	size_t size = 123;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  131) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  132) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  133) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  134) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  135) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  136) 	kfree(ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  137) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  138) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  139) static void kmalloc_oob_left(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  140) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  141) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  142) 	size_t size = 15;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  143) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  144) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  145) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  146) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  147) 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  148) 	kfree(ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  149) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  150) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  151) static void kmalloc_node_oob_right(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  152) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  153) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  154) 	size_t size = 4096;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  155) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  156) 	ptr = kmalloc_node(size, GFP_KERNEL, 0);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  157) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  158) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  159) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  160) 	kfree(ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  161) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  162) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  163) /*
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  164)  * These kmalloc_pagealloc_* tests try allocating a memory chunk that doesn't
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  165)  * fit into a slab cache and therefore is allocated via the page allocator
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  166)  * fallback. Since this kind of fallback is only implemented for SLUB, these
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  167)  * tests are limited to that allocator.
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  168)  */
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  169) static void kmalloc_pagealloc_oob_right(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  170) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  171) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  172) 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  173) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  174) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  175) 
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  176) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  177) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  178) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  179) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  180) 
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  181) 	kfree(ptr);
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  182) }
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  183) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  184) static void kmalloc_pagealloc_uaf(struct kunit *test)
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  185) {
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  186) 	char *ptr;
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  187) 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  188) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  189) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  190) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  191) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  192) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  193) 	kfree(ptr);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  194) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  195) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  196) }
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  197) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  198) static void kmalloc_pagealloc_invalid_free(struct kunit *test)
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  199) {
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  200) 	char *ptr;
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  201) 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  202) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  203) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  204) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  205) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  206) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  207) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  208) 	KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
47adccce3e8a3 (Dmitry Vyukov       2018-02-06 15:36:23 -0800  209) }
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  210) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  211) static void pagealloc_oob_right(struct kunit *test)
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  212) {
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  213) 	char *ptr;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  214) 	struct page *pages;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  215) 	size_t order = 4;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  216) 	size_t size = (1UL << (PAGE_SHIFT + order));
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  217) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  218) 	/*
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  219) 	 * With generic KASAN page allocations have no redzones, thus
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  220) 	 * out-of-bounds detection is not guaranteed.
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  221) 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=210503.
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  222) 	 */
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  223) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  224) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  225) 	pages = alloc_pages(GFP_KERNEL, order);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  226) 	ptr = page_address(pages);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  227) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  228) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  229) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  230) 	free_pages((unsigned long)ptr, order);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  231) }
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  232) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  233) static void pagealloc_uaf(struct kunit *test)
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  234) {
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  235) 	char *ptr;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  236) 	struct page *pages;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  237) 	size_t order = 4;
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  238) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  239) 	pages = alloc_pages(GFP_KERNEL, order);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  240) 	ptr = page_address(pages);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  241) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  242) 	free_pages((unsigned long)ptr, order);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  243) 
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  244) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  245) }
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800  246) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  247) static void kmalloc_large_oob_right(struct kunit *test)
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  248) {
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  249) 	char *ptr;
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  250) 	size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  251) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  252) 	/*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  253) 	 * Allocate a chunk that is large enough, but still fits into a slab
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  254) 	 * and does not trigger the page allocator fallback in SLUB.
e6e8379c876de (Alexander Potapenko 2016-03-25 14:21:56 -0700  255) 	 */
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  256) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  257) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  258) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  259) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  260) 	kfree(ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  261) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  262) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  263) static void krealloc_more_oob_helper(struct kunit *test,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  264) 					size_t size1, size_t size2)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  265) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  266) 	char *ptr1, *ptr2;
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  267) 	size_t middle;
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  268) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  269) 	KUNIT_ASSERT_LT(test, size1, size2);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  270) 	middle = size1 + (size2 - size1) / 2;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  271) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  272) 	ptr1 = kmalloc(size1, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  273) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  274) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  275) 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  276) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  277) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  278) 	/* All offsets up to size2 must be accessible. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  279) 	ptr2[size1 - 1] = 'x';
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  280) 	ptr2[size1] = 'x';
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  281) 	ptr2[middle] = 'x';
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  282) 	ptr2[size2 - 1] = 'x';
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  283) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  284) 	/* Generic mode is precise, so unaligned size2 must be inaccessible. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  285) 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  286) 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  287) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  288) 	/* For all modes first aligned offset after size2 must be inaccessible. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  289) 	KUNIT_EXPECT_KASAN_FAIL(test,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  290) 		ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  291) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  292) 	kfree(ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  293) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  294) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  295) static void krealloc_less_oob_helper(struct kunit *test,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  296) 					size_t size1, size_t size2)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  297) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  298) 	char *ptr1, *ptr2;
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  299) 	size_t middle;
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  300) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  301) 	KUNIT_ASSERT_LT(test, size2, size1);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  302) 	middle = size2 + (size1 - size2) / 2;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  303) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  304) 	ptr1 = kmalloc(size1, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  305) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  306) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  307) 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  308) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  309) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  310) 	/* Must be accessible for all modes. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  311) 	ptr2[size2 - 1] = 'x';
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  312) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  313) 	/* Generic mode is precise, so unaligned size2 must be inaccessible. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  314) 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  315) 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  316) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  317) 	/* For all modes first aligned offset after size2 must be inaccessible. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  318) 	KUNIT_EXPECT_KASAN_FAIL(test,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  319) 		ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  320) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  321) 	/*
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  322) 	 * For all modes all size2, middle, and size1 should land in separate
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  323) 	 * granules and thus the latter two offsets should be inaccessible.
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  324) 	 */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  325) 	KUNIT_EXPECT_LE(test, round_up(size2, KASAN_GRANULE_SIZE),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  326) 				round_down(middle, KASAN_GRANULE_SIZE));
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  327) 	KUNIT_EXPECT_LE(test, round_up(middle, KASAN_GRANULE_SIZE),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  328) 				round_down(size1, KASAN_GRANULE_SIZE));
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  329) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[middle] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  330) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1 - 1] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  331) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size1] = 'x');
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  332) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  333) 	kfree(ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  334) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  335) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  336) static void krealloc_more_oob(struct kunit *test)
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  337) {
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  338) 	krealloc_more_oob_helper(test, 201, 235);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  339) }
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  340) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  341) static void krealloc_less_oob(struct kunit *test)
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  342) {
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  343) 	krealloc_less_oob_helper(test, 235, 201);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  344) }
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  345) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  346) static void krealloc_pagealloc_more_oob(struct kunit *test)
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  347) {
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  348) 	/* page_alloc fallback in only implemented for SLUB. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  349) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  350) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  351) 	krealloc_more_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 201,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  352) 					KMALLOC_MAX_CACHE_SIZE + 235);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  353) }
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  354) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  355) static void krealloc_pagealloc_less_oob(struct kunit *test)
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  356) {
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  357) 	/* page_alloc fallback in only implemented for SLUB. */
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  358) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  359) 
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  360) 	krealloc_less_oob_helper(test, KMALLOC_MAX_CACHE_SIZE + 235,
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  361) 					KMALLOC_MAX_CACHE_SIZE + 201);
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  362) }
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800  363) 
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  364) /*
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  365)  * Check that krealloc() detects a use-after-free, returns NULL,
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  366)  * and doesn't unpoison the freed object.
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  367)  */
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  368) static void krealloc_uaf(struct kunit *test)
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  369) {
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  370) 	char *ptr1, *ptr2;
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  371) 	int size1 = 201;
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  372) 	int size2 = 235;
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  373) 
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  374) 	ptr1 = kmalloc(size1, GFP_KERNEL);
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  375) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  376) 	kfree(ptr1);
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  377) 
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  378) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL));
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  379) 	KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL);
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  380) 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1);
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  381) }
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800  382) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  383) static void kmalloc_oob_16(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  384) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  385) 	struct {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  386) 		u64 words[2];
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  387) 	} *ptr1, *ptr2;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  388) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  389) 	/* This test is specifically crafted for the generic mode. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  390) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  391) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  392) 	ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  393) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  394) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  395) 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  396) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  397) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  398) 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  399) 	kfree(ptr1);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  400) 	kfree(ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  401) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  402) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  403) static void kmalloc_uaf_16(struct kunit *test)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  404) {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  405) 	struct {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  406) 		u64 words[2];
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  407) 	} *ptr1, *ptr2;
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  408) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  409) 	ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  410) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  411) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  412) 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  413) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  414) 	kfree(ptr2);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  415) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  416) 	KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  417) 	kfree(ptr1);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  418) }
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  419) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  420) static void kmalloc_oob_memset_2(struct kunit *test)
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  421) {
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  422) 	char *ptr;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  423) 	size_t size = 8;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  424) 
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  425) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  426) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  427) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  428) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  429) 	kfree(ptr);
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  430) }
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  431) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  432) static void kmalloc_oob_memset_4(struct kunit *test)
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  433) {
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  434) 	char *ptr;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  435) 	size_t size = 8;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  436) 
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  437) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  438) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  439) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  440) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  441) 	kfree(ptr);
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  442) }
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  443) 
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  444) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  445) static void kmalloc_oob_memset_8(struct kunit *test)
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  446) {
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  447) 	char *ptr;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  448) 	size_t size = 8;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  449) 
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  450) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  451) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  452) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  453) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  454) 	kfree(ptr);
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  455) }
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  456) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  457) static void kmalloc_oob_memset_16(struct kunit *test)
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  458) {
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  459) 	char *ptr;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  460) 	size_t size = 16;
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  461) 
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  462) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  463) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  464) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  465) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  466) 	kfree(ptr);
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  467) }
f523e737c08f5 (Wang Long           2015-11-05 18:51:15 -0800  468) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  469) static void kmalloc_oob_in_memset(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  470) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  471) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  472) 	size_t size = 666;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  473) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  474) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  475) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
f33a01492a24a (Walter Wu           2020-08-06 23:24:54 -0700  476) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  477) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  478) 	kfree(ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  479) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  480) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  481) static void kmalloc_memmove_invalid_size(struct kunit *test)
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  482) {
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  483) 	char *ptr;
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  484) 	size_t size = 64;
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  485) 	volatile size_t invalid_size = -2;
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  486) 
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  487) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  488) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  489) 
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  490) 	memset((char *)ptr, 0, 64);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  491) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  492) 	KUNIT_EXPECT_KASAN_FAIL(test,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  493) 		memmove((char *)ptr, (char *)ptr + 4, invalid_size));
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  494) 	kfree(ptr);
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  495) }
98f3b56fa62a6 (Walter Wu           2020-04-01 21:09:40 -0700  496) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  497) static void kmalloc_uaf(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  498) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  499) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  500) 	size_t size = 10;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  501) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  502) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  503) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  504) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  505) 	kfree(ptr);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  506) 	KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  507) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  508) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  509) static void kmalloc_uaf_memset(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  510) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  511) 	char *ptr;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  512) 	size_t size = 33;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  513) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  514) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  515) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  516) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  517) 	kfree(ptr);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  518) 	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  519) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  520) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  521) static void kmalloc_uaf2(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  522) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  523) 	char *ptr1, *ptr2;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  524) 	size_t size = 43;
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  525) 	int counter = 0;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  526) 
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  527) again:
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  528) 	ptr1 = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  529) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  530) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  531) 	kfree(ptr1);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  532) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  533) 	ptr2 = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  534) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  535) 
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  536) 	/*
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  537) 	 * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same.
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  538) 	 * Allow up to 16 attempts at generating different tags.
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  539) 	 */
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  540) 	if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) {
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  541) 		kfree(ptr2);
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  542) 		goto again;
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  543) 	}
1b1df4c4e2576 (Andrey Konovalov    2021-02-24 12:05:38 -0800  544) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  545) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  546) 	KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  547) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  548) 	kfree(ptr2);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  549) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  550) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  551) static void kfree_via_page(struct kunit *test)
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  552) {
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  553) 	char *ptr;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  554) 	size_t size = 8;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  555) 	struct page *page;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  556) 	unsigned long offset;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  557) 
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  558) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  559) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  560) 
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  561) 	page = virt_to_page(ptr);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  562) 	offset = offset_in_page(ptr);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  563) 	kfree(page_address(page) + offset);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  564) }
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  565) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  566) static void kfree_via_phys(struct kunit *test)
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  567) {
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  568) 	char *ptr;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  569) 	size_t size = 8;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  570) 	phys_addr_t phys;
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  571) 
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  572) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  573) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  574) 
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  575) 	phys = virt_to_phys(ptr);
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  576) 	kfree(phys_to_virt(phys));
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  577) }
b92a953cb7f72 (Mark Rutland        2019-09-23 15:34:16 -0700  578) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  579) static void kmem_cache_oob(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  580) {
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  581) 	char *p;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  582) 	size_t size = 200;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  583) 	struct kmem_cache *cache;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  584) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  585) 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  586) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  587) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  588) 	p = kmem_cache_alloc(cache, GFP_KERNEL);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  589) 	if (!p) {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  590) 		kunit_err(test, "Allocation failed: %s\n", __func__);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  591) 		kmem_cache_destroy(cache);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  592) 		return;
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  593) 	}
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  594) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  595) 	KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  596) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  597) 	kmem_cache_free(cache, p);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  598) 	kmem_cache_destroy(cache);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  599) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  600) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  601) static void kmem_cache_accounted(struct kunit *test)
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  602) {
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  603) 	int i;
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  604) 	char *p;
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  605) 	size_t size = 200;
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  606) 	struct kmem_cache *cache;
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  607) 
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  608) 	cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  609) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  610) 
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  611) 	/*
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  612) 	 * Several allocations with a delay to allow for lazy per memcg kmem
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  613) 	 * cache creation.
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  614) 	 */
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  615) 	for (i = 0; i < 5; i++) {
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  616) 		p = kmem_cache_alloc(cache, GFP_KERNEL);
dc2bf000a2848 (Markus Elfring      2017-11-17 15:28:00 -0800  617) 		if (!p)
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  618) 			goto free_cache;
dc2bf000a2848 (Markus Elfring      2017-11-17 15:28:00 -0800  619) 
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  620) 		kmem_cache_free(cache, p);
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  621) 		msleep(100);
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  622) 	}
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  623) 
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  624) free_cache:
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  625) 	kmem_cache_destroy(cache);
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  626) }
0386bf385d9db (Greg Thelen         2017-02-24 15:00:08 -0800  627) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  628) static void kmem_cache_bulk(struct kunit *test)
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  629) {
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  630) 	struct kmem_cache *cache;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  631) 	size_t size = 200;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  632) 	char *p[10];
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  633) 	bool ret;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  634) 	int i;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  635) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  636) 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  637) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  638) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  639) 	ret = kmem_cache_alloc_bulk(cache, GFP_KERNEL, ARRAY_SIZE(p), (void **)&p);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  640) 	if (!ret) {
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  641) 		kunit_err(test, "Allocation failed: %s\n", __func__);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  642) 		kmem_cache_destroy(cache);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  643) 		return;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  644) 	}
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  645) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  646) 	for (i = 0; i < ARRAY_SIZE(p); i++)
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  647) 		p[i][0] = p[i][size - 1] = 42;
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  648) 
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  649) 	kmem_cache_free_bulk(cache, ARRAY_SIZE(p), (void **)&p);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  650) 	kmem_cache_destroy(cache);
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  651) }
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800  652) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  653) static char global_array[10];
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  654) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  655) static void kasan_global_oob(struct kunit *test)
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  656) {
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  657) 	/*
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  658) 	 * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  659) 	 * from failing here and panicing the kernel, access the array via a
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  660) 	 * volatile pointer, which will prevent the compiler from being able to
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  661) 	 * determine the array bounds.
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  662) 	 *
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  663) 	 * This access uses a volatile pointer to char (char *volatile) rather
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  664) 	 * than the more conventional pointer to volatile char (volatile char *)
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  665) 	 * because we want to prevent the compiler from making inferences about
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  666) 	 * the pointer itself (i.e. its array bounds), not the data that it
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  667) 	 * refers to.
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  668) 	 */
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  669) 	char *volatile array = global_array;
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  670) 	char *p = &array[ARRAY_SIZE(global_array) + 3];
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  671) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  672) 	/* Only generic mode instruments globals. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  673) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  674) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  675) 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  676) }
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800  677) 
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  678) /* Check that ksize() makes the whole object accessible. */
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  679) static void ksize_unpoisons_memory(struct kunit *test)
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  680) {
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  681) 	char *ptr;
48c232395431c (Colin Ian King      2018-02-06 15:36:48 -0800  682) 	size_t size = 123, real_size;
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  683) 
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  684) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  685) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  686) 	real_size = ksize(ptr);
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  687) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  688) 	/* This access shouldn't trigger a KASAN report. */
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  689) 	ptr[size] = 'x';
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  690) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  691) 	/* This one must. */
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  692) 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  693) 
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  694) 	kfree(ptr);
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  695) }
96fe805fb6fe9 (Alexander Potapenko 2016-05-20 16:59:17 -0700  696) 
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  697) /*
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  698)  * Check that a use-after-free is detected by ksize() and via normal accesses
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  699)  * after it.
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  700)  */
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  701) static void ksize_uaf(struct kunit *test)
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  702) {
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  703) 	char *ptr;
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  704) 	int size = 128 - KASAN_GRANULE_SIZE;
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  705) 
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  706) 	ptr = kmalloc(size, GFP_KERNEL);
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  707) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  708) 	kfree(ptr);
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  709) 
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  710) 	KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  711) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *ptr);
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  712) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = *(ptr + size));
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  713) }
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800  714) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  715) static void kasan_stack_oob(struct kunit *test)
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700  716) {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  717) 	char stack_array[10];
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  718) 	/* See comment in kasan_global_oob. */
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  719) 	char *volatile array = stack_array;
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  720) 	char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700  721) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  722) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700  723) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  724) 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700  725) }
eae08dcab80c6 (Andrey Ryabinin     2016-05-20 16:59:34 -0700  726) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  727) static void kasan_alloca_oob_left(struct kunit *test)
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  728) {
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  729) 	volatile int i = 10;
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  730) 	char alloca_array[i];
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  731) 	/* See comment in kasan_global_oob. */
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  732) 	char *volatile array = alloca_array;
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  733) 	char *p = array - 1;
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  734) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  735) 	/* Only generic mode instruments dynamic allocas. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  736) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  737) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  738) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  739) 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  740) }
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  741) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  742) static void kasan_alloca_oob_right(struct kunit *test)
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  743) {
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  744) 	volatile int i = 10;
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  745) 	char alloca_array[i];
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  746) 	/* See comment in kasan_global_oob. */
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  747) 	char *volatile array = alloca_array;
f649dc0e0d7b5 (Peter Collingbourne 2021-05-14 17:27:27 -0700  748) 	char *p = array + i;
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  749) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  750) 	/* Only generic mode instruments dynamic allocas. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  751) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  752) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  753) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  754) 	KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  755) }
00a14294bb33a (Paul Lawrence       2018-02-06 15:36:16 -0800  756) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  757) static void kmem_cache_double_free(struct kunit *test)
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  758) {
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  759) 	char *p;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  760) 	size_t size = 200;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  761) 	struct kmem_cache *cache;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  762) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  763) 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  764) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  765) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  766) 	p = kmem_cache_alloc(cache, GFP_KERNEL);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  767) 	if (!p) {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  768) 		kunit_err(test, "Allocation failed: %s\n", __func__);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  769) 		kmem_cache_destroy(cache);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  770) 		return;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  771) 	}
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  772) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  773) 	kmem_cache_free(cache, p);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  774) 	KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  775) 	kmem_cache_destroy(cache);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  776) }
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  777) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  778) static void kmem_cache_invalid_free(struct kunit *test)
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  779) {
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  780) 	char *p;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  781) 	size_t size = 200;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  782) 	struct kmem_cache *cache;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  783) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  784) 	cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  785) 				  NULL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  786) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  787) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  788) 	p = kmem_cache_alloc(cache, GFP_KERNEL);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  789) 	if (!p) {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  790) 		kunit_err(test, "Allocation failed: %s\n", __func__);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  791) 		kmem_cache_destroy(cache);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  792) 		return;
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  793) 	}
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  794) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  795) 	/* Trigger invalid free, the object doesn't get freed. */
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  796) 	KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  797) 
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  798) 	/*
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  799) 	 * Properly free the object to prevent the "Objects remaining in
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  800) 	 * test_cache on __kmem_cache_shutdown" BUG failure.
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  801) 	 */
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  802) 	kmem_cache_free(cache, p);
91c93ed07f04f (Andrey Konovalov    2018-04-10 16:30:35 -0700  803) 
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  804) 	kmem_cache_destroy(cache);
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  805) }
b1d5728939ebe (Dmitry Vyukov       2018-02-06 15:36:37 -0800  806) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  807) static void kasan_memchr(struct kunit *test)
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  808) {
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  809) 	char *ptr;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  810) 	size_t size = 24;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  811) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  812) 	/*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  813) 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  814) 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  815) 	 */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  816) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  817) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  818) 	if (OOB_TAG_OFF)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  819) 		size = round_up(size, OOB_TAG_OFF);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  820) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  821) 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  822) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  823) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  824) 	KUNIT_EXPECT_KASAN_FAIL(test,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  825) 		kasan_ptr_result = memchr(ptr, '1', size + 1));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  826) 
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  827) 	kfree(ptr);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  828) }
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  829) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  830) static void kasan_memcmp(struct kunit *test)
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  831) {
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  832) 	char *ptr;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  833) 	size_t size = 24;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  834) 	int arr[9];
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  835) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  836) 	/*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  837) 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  838) 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  839) 	 */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  840) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  841) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  842) 	if (OOB_TAG_OFF)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  843) 		size = round_up(size, OOB_TAG_OFF);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  844) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  845) 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  846) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  847) 	memset(arr, 0, sizeof(arr));
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  848) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  849) 	KUNIT_EXPECT_KASAN_FAIL(test,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  850) 		kasan_int_result = memcmp(ptr, arr, size+1));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  851) 	kfree(ptr);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  852) }
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  853) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  854) static void kasan_strings(struct kunit *test)
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  855) {
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  856) 	char *ptr;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  857) 	size_t size = 24;
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  858) 
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  859) 	/*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  860) 	 * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  861) 	 * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  862) 	 */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  863) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  864) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  865) 	ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  866) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  867) 
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  868) 	kfree(ptr);
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  869) 
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  870) 	/*
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  871) 	 * Try to cause only 1 invalid access (less spam in dmesg).
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  872) 	 * For that we need ptr to point to zeroed byte.
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  873) 	 * Skip metadata that could be stored in freed object so ptr
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  874) 	 * will likely point to zeroed byte.
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  875) 	 */
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  876) 	ptr += 16;
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  877) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  878) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  879) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  880) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  881) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  882) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  883) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  884) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  885) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  886) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  887) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  888) }
0c96350a2d2f6 (Andrey Ryabinin     2018-10-26 15:02:34 -0700  889) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  890) static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  891) {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  892) 	KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  893) 	KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  894) 	KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  895) 	KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  896) 	KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  897) 	KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  898) 	KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  899) 	KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  900) }
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  901) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  902) static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  903) {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  904) 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  905) 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  906) 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  907) 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  908) 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  909) 	KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  910) 	KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  911) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  912) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  913) #if defined(clear_bit_unlock_is_negative_byte)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  914) 	KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  915) 				clear_bit_unlock_is_negative_byte(nr, addr));
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  916) #endif
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  917) }
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  918) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  919) static void kasan_bitops_generic(struct kunit *test)
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  920) {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  921) 	long *bits;
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  922) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  923) 	/* This test is specifically crafted for the generic mode. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  924) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  925) 
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  926) 	/*
0fd379253691e (Andrey Konovalov    2021-02-24 12:05:13 -0800  927) 	 * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  928) 	 * this way we do not actually corrupt other memory.
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  929) 	 */
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  930) 	bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  931) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  932) 
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  933) 	/*
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  934) 	 * Below calls try to access bit within allocated memory; however, the
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  935) 	 * below accesses are still out-of-bounds, since bitops are defined to
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  936) 	 * operate on the whole long the bit is in.
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  937) 	 */
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  938) 	kasan_bitops_modify(test, BITS_PER_LONG, bits);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  939) 
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  940) 	/*
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  941) 	 * Below calls try to access bit beyond allocated memory.
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  942) 	 */
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  943) 	kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  944) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  945) 	kfree(bits);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  946) }
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  947) 
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  948) static void kasan_bitops_tags(struct kunit *test)
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  949) {
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  950) 	long *bits;
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  951) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  952) 	/* This test is specifically crafted for tag-based modes. */
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  953) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  954) 
e66e1799a7662 (Andrey Konovalov    2021-02-24 12:05:42 -0800  955) 	/* kmalloc-64 cache will be used and the last 16 bytes will be the redzone. */
e66e1799a7662 (Andrey Konovalov    2021-02-24 12:05:42 -0800  956) 	bits = kzalloc(48, GFP_KERNEL);
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800  957) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  958) 
e66e1799a7662 (Andrey Konovalov    2021-02-24 12:05:42 -0800  959) 	/* Do the accesses past the 48 allocated bytes, but within the redone. */
e66e1799a7662 (Andrey Konovalov    2021-02-24 12:05:42 -0800  960) 	kasan_bitops_modify(test, BITS_PER_LONG, (void *)bits + 48);
e66e1799a7662 (Andrey Konovalov    2021-02-24 12:05:42 -0800  961) 	kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, (void *)bits + 48);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  962) 
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  963) 	kfree(bits);
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  964) }
19a33ca6c209f (Marco Elver         2019-07-11 20:53:52 -0700  965) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  966) static void kmalloc_double_kzfree(struct kunit *test)
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  967) {
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  968) 	char *ptr;
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  969) 	size_t size = 16;
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  970) 
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  971) 	ptr = kmalloc(size, GFP_KERNEL);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  972) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  973) 
453431a54934d (Waiman Long         2020-08-06 23:18:13 -0700  974) 	kfree_sensitive(ptr);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  975) 	KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  976) }
bb104ed785521 (Marco Elver         2019-07-11 20:54:11 -0700  977) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  978) static void vmalloc_oob(struct kunit *test)
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  979) {
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  980) 	void *area;
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  981) 
da17e377723f5 (Andrey Konovalov    2021-02-24 12:05:17 -0800  982) 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  983) 
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  984) 	/*
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  985) 	 * We have to be careful not to hit the guard page.
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  986) 	 * The MMU will catch that and crash us.
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  987) 	 */
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  988) 	area = vmalloc(3000);
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  989) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  990) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700  991) 	KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  992) 	vfree(area);
0651391693012 (Daniel Axtens       2019-11-30 17:54:53 -0800  993) }
387d6e46681b0 (Walter Wu           2020-08-06 23:24:42 -0700  994) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800  995) /*
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800  996)  * Check that the assigned pointer tag falls within the [KASAN_TAG_MIN,
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800  997)  * KASAN_TAG_KERNEL) range (note: excluding the match-all tag) for tag-based
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800  998)  * modes.
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800  999)  */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1000) static void match_all_not_assigned(struct kunit *test)
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1001) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1002) 	char *ptr;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1003) 	struct page *pages;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1004) 	int i, size, order;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1005) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1006) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1007) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1008) 	for (i = 0; i < 256; i++) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1009) 		size = (get_random_int() % 1024) + 1;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1010) 		ptr = kmalloc(size, GFP_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1011) 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1012) 		KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1013) 		KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1014) 		kfree(ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1015) 	}
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1016) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1017) 	for (i = 0; i < 256; i++) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1018) 		order = (get_random_int() % 4) + 1;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1019) 		pages = alloc_pages(GFP_KERNEL, order);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1020) 		ptr = page_address(pages);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1021) 		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1022) 		KUNIT_EXPECT_GE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_MIN);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1023) 		KUNIT_EXPECT_LT(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1024) 		free_pages((unsigned long)ptr, order);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1025) 	}
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1026) }
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1027) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1028) /* Check that 0xff works as a match-all pointer tag for tag-based modes. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1029) static void match_all_ptr_tag(struct kunit *test)
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1030) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1031) 	char *ptr;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1032) 	u8 tag;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1033) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1034) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1035) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1036) 	ptr = kmalloc(128, GFP_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1037) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1038) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1039) 	/* Backup the assigned tag. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1040) 	tag = get_tag(ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1041) 	KUNIT_EXPECT_NE(test, tag, (u8)KASAN_TAG_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1042) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1043) 	/* Reset the tag to 0xff.*/
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1044) 	ptr = set_tag(ptr, KASAN_TAG_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1045) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1046) 	/* This access shouldn't trigger a KASAN report. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1047) 	*ptr = 0;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1048) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1049) 	/* Recover the pointer tag and free. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1050) 	ptr = set_tag(ptr, tag);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1051) 	kfree(ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1052) }
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1053) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1054) /* Check that there are no match-all memory tags for tag-based modes. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1055) static void match_all_mem_tag(struct kunit *test)
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1056) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1057) 	char *ptr;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1058) 	int tag;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1059) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1060) 	KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1061) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1062) 	ptr = kmalloc(128, GFP_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1063) 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1064) 	KUNIT_EXPECT_NE(test, (u8)get_tag(ptr), (u8)KASAN_TAG_KERNEL);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1065) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1066) 	/* For each possible tag value not matching the pointer tag. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1067) 	for (tag = KASAN_TAG_MIN; tag <= KASAN_TAG_KERNEL; tag++) {
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1068) 		if (tag == get_tag(ptr))
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1069) 			continue;
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1070) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1071) 		/* Mark the first memory granule with the chosen memory tag. */
aa5c219c60ccb (Andrey Konovalov    2021-04-29 22:59:59 -0700 1072) 		kasan_poison(ptr, KASAN_GRANULE_SIZE, (u8)tag, false);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1073) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1074) 		/* This access must cause a KASAN report. */
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1075) 		KUNIT_EXPECT_KASAN_FAIL(test, *ptr = 0);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1076) 	}
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1077) 
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1078) 	/* Recover the memory tag and free. */
aa5c219c60ccb (Andrey Konovalov    2021-04-29 22:59:59 -0700 1079) 	kasan_poison(ptr, KASAN_GRANULE_SIZE, get_tag(ptr), false);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1080) 	kfree(ptr);
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1081) }
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1082) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1083) static struct kunit_case kasan_kunit_test_cases[] = {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1084) 	KUNIT_CASE(kmalloc_oob_right),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1085) 	KUNIT_CASE(kmalloc_oob_left),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1086) 	KUNIT_CASE(kmalloc_node_oob_right),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1087) 	KUNIT_CASE(kmalloc_pagealloc_oob_right),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1088) 	KUNIT_CASE(kmalloc_pagealloc_uaf),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1089) 	KUNIT_CASE(kmalloc_pagealloc_invalid_free),
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800 1090) 	KUNIT_CASE(pagealloc_oob_right),
858bdeb046f6d (Andrey Konovalov    2021-02-24 12:05:55 -0800 1091) 	KUNIT_CASE(pagealloc_uaf),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1092) 	KUNIT_CASE(kmalloc_large_oob_right),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800 1093) 	KUNIT_CASE(krealloc_more_oob),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800 1094) 	KUNIT_CASE(krealloc_less_oob),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800 1095) 	KUNIT_CASE(krealloc_pagealloc_more_oob),
b87c28b9a7ef6 (Andrey Konovalov    2021-02-25 17:20:15 -0800 1096) 	KUNIT_CASE(krealloc_pagealloc_less_oob),
26a5ca7a73be3 (Andrey Konovalov    2021-02-25 17:20:19 -0800 1097) 	KUNIT_CASE(krealloc_uaf),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1098) 	KUNIT_CASE(kmalloc_oob_16),
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800 1099) 	KUNIT_CASE(kmalloc_uaf_16),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1100) 	KUNIT_CASE(kmalloc_oob_in_memset),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1101) 	KUNIT_CASE(kmalloc_oob_memset_2),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1102) 	KUNIT_CASE(kmalloc_oob_memset_4),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1103) 	KUNIT_CASE(kmalloc_oob_memset_8),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1104) 	KUNIT_CASE(kmalloc_oob_memset_16),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1105) 	KUNIT_CASE(kmalloc_memmove_invalid_size),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1106) 	KUNIT_CASE(kmalloc_uaf),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1107) 	KUNIT_CASE(kmalloc_uaf_memset),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1108) 	KUNIT_CASE(kmalloc_uaf2),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1109) 	KUNIT_CASE(kfree_via_page),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1110) 	KUNIT_CASE(kfree_via_phys),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1111) 	KUNIT_CASE(kmem_cache_oob),
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800 1112) 	KUNIT_CASE(kmem_cache_accounted),
115161354d0e0 (Andrey Konovalov    2021-02-24 12:05:59 -0800 1113) 	KUNIT_CASE(kmem_cache_bulk),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1114) 	KUNIT_CASE(kasan_global_oob),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1115) 	KUNIT_CASE(kasan_stack_oob),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1116) 	KUNIT_CASE(kasan_alloca_oob_left),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1117) 	KUNIT_CASE(kasan_alloca_oob_right),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1118) 	KUNIT_CASE(ksize_unpoisons_memory),
611806b4bf8dd (Andrey Konovalov    2021-02-24 12:05:50 -0800 1119) 	KUNIT_CASE(ksize_uaf),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1120) 	KUNIT_CASE(kmem_cache_double_free),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1121) 	KUNIT_CASE(kmem_cache_invalid_free),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1122) 	KUNIT_CASE(kasan_memchr),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1123) 	KUNIT_CASE(kasan_memcmp),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1124) 	KUNIT_CASE(kasan_strings),
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800 1125) 	KUNIT_CASE(kasan_bitops_generic),
58b999d7a22c5 (Andrey Konovalov    2020-11-01 17:07:37 -0800 1126) 	KUNIT_CASE(kasan_bitops_tags),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1127) 	KUNIT_CASE(kmalloc_double_kzfree),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1128) 	KUNIT_CASE(vmalloc_oob),
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1129) 	KUNIT_CASE(match_all_not_assigned),
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1130) 	KUNIT_CASE(match_all_ptr_tag),
573a48092313d (Andrey Konovalov    2021-02-24 12:05:21 -0800 1131) 	KUNIT_CASE(match_all_mem_tag),
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1132) 	{}
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1133) };
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1134) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1135) static struct kunit_suite kasan_kunit_test_suite = {
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1136) 	.name = "kasan",
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1137) 	.init = kasan_test_init,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1138) 	.test_cases = kasan_kunit_test_cases,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1139) 	.exit = kasan_test_exit,
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1140) };
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1141) 
73228c7ecc5e4 (Patricia Alfonso    2020-10-13 16:55:06 -0700 1142) kunit_test_suite(kasan_kunit_test_suite);
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800 1143) 
3f15801cdc237 (Andrey Ryabinin     2015-02-13 14:39:53 -0800 1144) MODULE_LICENSE("GPL");