All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf 1/2] mm: Fix copy_from_user_nofault().
@ 2023-01-18  5:14 Alexei Starovoitov
  2023-01-18  5:14 ` [PATCH bpf 2/2] perf: Fix arch_perf_out_copy_user() Alexei Starovoitov
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Alexei Starovoitov @ 2023-01-18  5:14 UTC (permalink / raw)
  To: torvalds
  Cc: x86, davem, daniel, andrii, peterz, keescook, tglx, hsinweih,
	rostedt, vegard.nossum, gregkh, alan.maguire, dylany, riel, bpf,
	kernel-team

From: Alexei Starovoitov <ast@kernel.org>

There are several issues with copy_from_user_nofault():

- access_ok() is designed for user context only and for that reason
it has WARN_ON_IN_IRQ() which triggers when bpf, kprobe, eprobe
and perf on ppc are calling it from irq.

- it's missing nmi_uaccess_okay() which is a nop on all architectures
except x86 where it's required.
The comment in arch/x86/mm/tlb.c explains the details why it's necessary.
Calling copy_from_user_nofault() from bpf, [ke]probe without this check is not safe.

- __copy_from_user_inatomic() under CONFIG_HARDENED_USERCOPY is calling
check_object_size()->__check_object_size()->check_heap_object()->find_vmap_area()->spin_lock()
which is not safe to do from bpf, [ke]probe and perf due to potential deadlock.

Fix all three issues. At the end the copy_from_user_nofault() becomes
equivalent to copy_from_user_nmi() from safety point of view with
a difference in the return value.

Reported-by: Hsin-Wei Hung <hsinweih@uci.edu>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 mm/maccess.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/mm/maccess.c b/mm/maccess.c
index 074f6b086671..6ee9b337c501 100644
--- a/mm/maccess.c
+++ b/mm/maccess.c
@@ -5,6 +5,7 @@
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/uaccess.h>
+#include <asm/tlb.h>
 
 bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src,
 		size_t size)
@@ -113,11 +114,18 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
 long copy_from_user_nofault(void *dst, const void __user *src, size_t size)
 {
 	long ret = -EFAULT;
-	if (access_ok(src, size)) {
-		pagefault_disable();
-		ret = __copy_from_user_inatomic(dst, src, size);
-		pagefault_enable();
-	}
+
+	if (!__access_ok(src, size))
+		return ret;
+
+	if (!nmi_uaccess_okay())
+		return ret;
+
+	pagefault_disable();
+	instrument_copy_from_user_before(dst, src, size);
+	ret = raw_copy_from_user(dst, src, size);
+	instrument_copy_from_user_after(dst, src, size, ret);
+	pagefault_enable();
 
 	if (ret)
 		return -EFAULT;
-- 
2.30.2


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

end of thread, other threads:[~2023-04-06 20:24 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-18  5:14 [PATCH bpf 1/2] mm: Fix copy_from_user_nofault() Alexei Starovoitov
2023-01-18  5:14 ` [PATCH bpf 2/2] perf: Fix arch_perf_out_copy_user() Alexei Starovoitov
2023-01-18 21:32 ` [PATCH bpf 1/2] mm: Fix copy_from_user_nofault() Hsin-Wei Hung
2023-01-19 16:52 ` Kees Cook
2023-01-19 19:21   ` Alexei Starovoitov
2023-01-19 20:08     ` Kees Cook
2023-01-19 20:14       ` Alexei Starovoitov
2023-01-19 20:28       ` Alexei Starovoitov
2023-03-25 14:55 ` Florian Lehner
2023-03-25 19:47   ` Alexei Starovoitov
2023-04-06 20:17     ` Salvatore Bonaccorso
2023-04-06 20:24       ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.