linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: x86: instrument __get/__put_kernel_nofault
@ 2024-09-17 20:18 Sabyrzhan Tasbolatov
  2024-09-17 22:51 ` Andrey Konovalov
  0 siblings, 1 reply; 14+ messages in thread
From: Sabyrzhan Tasbolatov @ 2024-09-17 20:18 UTC (permalink / raw)
  To: tglx, bp, glider, andreyknvl, akpm
  Cc: mingo, dave.hansen, ryabinin.a.a, x86, hpa, dvyukov,
	vincenzo.frascino, brauner, dhowells, linux-kernel, kasan-dev,
	linux-mm, snovitoll

Instrument copy_from_kernel_nofault(), copy_to_kernel_nofault(),
strncpy_from_kernel_nofault() where __put_kernel_nofault,
__get_kernel_nofault macros are used.

Regular instrument_read() and instrument_write() handles KASAN, KCSAN
checks for the access address, though instrument_memcpy_before() might
be considered as well for both src and dst address validation.

__get_user_size was appended with instrument_get_user() for KMSAN check in
commit 888f84a6da4d("x86: asm: instrument usercopy in get_user() and
put_user()") but only for CONFIG_CC_HAS_ASM_GOTO_OUTPUT.

Reported-by: Andrey Konovalov <andreyknvl@gmail.com>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=210505
Signed-off-by: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
---
 arch/x86/include/asm/uaccess.h |  4 ++++
 mm/kasan/kasan_test.c          | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 3a7755c1a441..bed84d3f7245 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -353,6 +353,7 @@ do {									\
 	default:							\
 		(x) = __get_user_bad();					\
 	}								\
+	instrument_get_user(x);						\
 } while (0)
 
 #define __get_user_asm(x, addr, err, itype)				\
@@ -620,6 +621,7 @@ do {									\
 
 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
 #define __get_kernel_nofault(dst, src, type, err_label)			\
+	instrument_read(src, sizeof(type));				\
 	__get_user_size(*((type *)(dst)), (__force type __user *)(src),	\
 			sizeof(type), err_label)
 #else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
@@ -627,6 +629,7 @@ do {									\
 do {									\
 	int __kr_err;							\
 									\
+	instrument_read(src, sizeof(type));				\
 	__get_user_size(*((type *)(dst)), (__force type __user *)(src),	\
 			sizeof(type), __kr_err);			\
 	if (unlikely(__kr_err))						\
@@ -635,6 +638,7 @@ do {									\
 #endif // CONFIG_CC_HAS_ASM_GOTO_OUTPUT
 
 #define __put_kernel_nofault(dst, src, type, err_label)			\
+	instrument_write(dst, sizeof(type));				\
 	__put_user_size(*((type *)(src)), (__force type __user *)(dst),	\
 			sizeof(type), err_label)
 
diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index 7b32be2a3cf0..f5086c86e0bd 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -1899,6 +1899,22 @@ static void match_all_mem_tag(struct kunit *test)
 	kfree(ptr);
 }
 
+static void copy_from_to_kernel_nofault(struct kunit *test)
+{
+	char *ptr;
+	char buf[16];
+	size_t size = sizeof(buf);
+
+	ptr = kmalloc(size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
+	kfree(ptr);
+
+	KUNIT_EXPECT_KASAN_FAIL(test,
+		copy_from_kernel_nofault(&buf[0], ptr, size));
+	KUNIT_EXPECT_KASAN_FAIL(test,
+		copy_to_kernel_nofault(ptr, &buf[0], size));
+}
+
 static struct kunit_case kasan_kunit_test_cases[] = {
 	KUNIT_CASE(kmalloc_oob_right),
 	KUNIT_CASE(kmalloc_oob_left),
@@ -1971,6 +1987,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {
 	KUNIT_CASE(match_all_not_assigned),
 	KUNIT_CASE(match_all_ptr_tag),
 	KUNIT_CASE(match_all_mem_tag),
+	KUNIT_CASE(copy_from_to_kernel_nofault),
 	{}
 };
 
-- 
2.34.1



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-09-27 15:18 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-17 20:18 [PATCH] mm: x86: instrument __get/__put_kernel_nofault Sabyrzhan Tasbolatov
2024-09-17 22:51 ` Andrey Konovalov
2024-09-18 10:56   ` [PATCH v2] " Sabyrzhan Tasbolatov
2024-09-18 15:15     ` Andrey Konovalov
2024-09-19 10:57       ` [PATCH v3] " Sabyrzhan Tasbolatov
2024-09-20 22:26         ` Andrey Konovalov
2024-09-21  7:10           ` [PATCH v4] " Sabyrzhan Tasbolatov
2024-09-21 20:49             ` Andrey Konovalov
2024-09-22  9:26               ` Sabyrzhan Tasbolatov
2024-09-22 12:04                 ` Andrey Konovalov
2024-09-22 14:57                   ` [PATCH v5] " Sabyrzhan Tasbolatov
2024-09-23  6:09                     ` Sabyrzhan Tasbolatov
2024-09-27 15:18                       ` Sabyrzhan Tasbolatov
2024-09-22 12:06             ` [PATCH v4] " Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox