Skip to content

Commit 358fdb4

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf_probe_read_user'
Daniel Borkmann says: ==================== This set adds probe_read_{user,kernel}(), probe_read_str_{user,kernel}() helpers, fixes probe_write_user() helper and selftests. For details please see individual patches. Thanks! v2 -> v3: - noticed two more things that are fixed in here: - bpf uapi helper description used 'int size' for *_str helpers, now u32 - we need TASK_SIZE_MAX + guard page on x86-64 in patch 2 otherwise we'll trigger the 00c4237 warn as well, so full range covered now v1 -> v2: - standardize unsafe_ptr terminology in uapi header comment (Andrii) - probe_read_{user,kernel}[_str] naming scheme (Andrii) - use global data in last test case, remove relaxed_maps (Andrii) - add strict non-pagefault kernel read funcs to avoid warning in kernel probe read helpers (Alexei) ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents e1cb7d2 + fa553d9 commit 358fdb4

17 files changed

Lines changed: 597 additions & 197 deletions

File tree

‎arch/x86/mm/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CFLAGS_REMOVE_mem_encrypt_identity.o = -pg
1313
endif
1414

1515
obj-y := init.o init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \
16-
pat.o pgtable.o physaddr.o setup_nx.o tlb.o cpu_entry_area.o
16+
pat.o pgtable.o physaddr.o setup_nx.o tlb.o cpu_entry_area.o maccess.o
1717

1818
# Make sure __phys_addr has no stackprotector
1919
nostackp := $(call cc-option, -fno-stack-protector)

‎arch/x86/mm/maccess.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <linux/uaccess.h>
4+
#include <linux/kernel.h>
5+
6+
#ifdef CONFIG_X86_64
7+
static __always_inline u64 canonical_address(u64 vaddr, u8 vaddr_bits)
8+
{
9+
return ((s64)vaddr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
10+
}
11+
12+
static __always_inline bool invalid_probe_range(u64 vaddr)
13+
{
14+
/*
15+
* Range covering the highest possible canonical userspace address
16+
* as well as non-canonical address range. For the canonical range
17+
* we also need to include the userspace guard page.
18+
*/
19+
return vaddr < TASK_SIZE_MAX + PAGE_SIZE ||
20+
canonical_address(vaddr, boot_cpu_data.x86_virt_bits) != vaddr;
21+
}
22+
#else
23+
static __always_inline bool invalid_probe_range(u64 vaddr)
24+
{
25+
return vaddr < TASK_SIZE_MAX;
26+
}
27+
#endif
28+
29+
long probe_kernel_read_strict(void *dst, const void *src, size_t size)
30+
{
31+
if (unlikely(invalid_probe_range((unsigned long)src)))
32+
return -EFAULT;
33+
34+
return __probe_kernel_read(dst, src, size);
35+
}
36+
37+
long strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr, long count)
38+
{
39+
if (unlikely(invalid_probe_range((unsigned long)unsafe_addr)))
40+
return -EFAULT;
41+
42+
return __strncpy_from_unsafe(dst, unsafe_addr, count);
43+
}

‎include/linux/uaccess.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
311311
* happens, handle that and return -EFAULT.
312312
*/
313313
extern long probe_kernel_read(void *dst, const void *src, size_t size);
314+
extern long probe_kernel_read_strict(void *dst, const void *src, size_t size);
314315
extern long __probe_kernel_read(void *dst, const void *src, size_t size);
315316

316317
/*
@@ -337,7 +338,22 @@ extern long __probe_user_read(void *dst, const void __user *src, size_t size);
337338
extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
338339
extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
339340

341+
/*
342+
* probe_user_write(): safely attempt to write to a location in user space
343+
* @dst: address to write to
344+
* @src: pointer to the data that shall be written
345+
* @size: size of the data chunk
346+
*
347+
* Safely write to address @dst from the buffer at @src. If a kernel fault
348+
* happens, handle that and return -EFAULT.
349+
*/
350+
extern long notrace probe_user_write(void __user *dst, const void *src, size_t size);
351+
extern long notrace __probe_user_write(void __user *dst, const void *src, size_t size);
352+
340353
extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
354+
extern long strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr,
355+
long count);
356+
extern long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
341357
extern long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
342358
long count);
343359
extern long strnlen_unsafe_user(const void __user *unsafe_addr, long count);

‎include/uapi/linux/bpf.h‎

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,13 @@ union bpf_attr {
563563
* Return
564564
* 0 on success, or a negative error in case of failure.
565565
*
566-
* int bpf_probe_read(void *dst, u32 size, const void *src)
566+
* int bpf_probe_read(void *dst, u32 size, const void *unsafe_ptr)
567567
* Description
568568
* For tracing programs, safely attempt to read *size* bytes from
569-
* address *src* and store the data in *dst*.
569+
* kernel space address *unsafe_ptr* and store the data in *dst*.
570+
*
571+
* Generally, use bpf_probe_read_user() or bpf_probe_read_kernel()
572+
* instead.
570573
* Return
571574
* 0 on success, or a negative error in case of failure.
572575
*
@@ -1428,45 +1431,14 @@ union bpf_attr {
14281431
* Return
14291432
* 0 on success, or a negative error in case of failure.
14301433
*
1431-
* int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
1434+
* int bpf_probe_read_str(void *dst, u32 size, const void *unsafe_ptr)
14321435
* Description
1433-
* Copy a NUL terminated string from an unsafe address
1434-
* *unsafe_ptr* to *dst*. The *size* should include the
1435-
* terminating NUL byte. In case the string length is smaller than
1436-
* *size*, the target is not padded with further NUL bytes. If the
1437-
* string length is larger than *size*, just *size*-1 bytes are
1438-
* copied and the last byte is set to NUL.
1439-
*
1440-
* On success, the length of the copied string is returned. This
1441-
* makes this helper useful in tracing programs for reading
1442-
* strings, and more importantly to get its length at runtime. See
1443-
* the following snippet:
1444-
*
1445-
* ::
1446-
*
1447-
* SEC("kprobe/sys_open")
1448-
* void bpf_sys_open(struct pt_regs *ctx)
1449-
* {
1450-
* char buf[PATHLEN]; // PATHLEN is defined to 256
1451-
* int res = bpf_probe_read_str(buf, sizeof(buf),
1452-
* ctx->di);
1453-
*
1454-
* // Consume buf, for example push it to
1455-
* // userspace via bpf_perf_event_output(); we
1456-
* // can use res (the string length) as event
1457-
* // size, after checking its boundaries.
1458-
* }
1459-
*
1460-
* In comparison, using **bpf_probe_read()** helper here instead
1461-
* to read the string would require to estimate the length at
1462-
* compile time, and would often result in copying more memory
1463-
* than necessary.
1436+
* Copy a NUL terminated string from an unsafe kernel address
1437+
* *unsafe_ptr* to *dst*. See bpf_probe_read_kernel_str() for
1438+
* more details.
14641439
*
1465-
* Another useful use case is when parsing individual process
1466-
* arguments or individual environment variables navigating
1467-
* *current*\ **->mm->arg_start** and *current*\
1468-
* **->mm->env_start**: using this helper and the return value,
1469-
* one can quickly iterate at the right offset of the memory area.
1440+
* Generally, use bpf_probe_read_user_str() or bpf_probe_read_kernel_str()
1441+
* instead.
14701442
* Return
14711443
* On success, the strictly positive length of the string,
14721444
* including the trailing NUL character. On error, a negative
@@ -2777,6 +2749,72 @@ union bpf_attr {
27772749
* restricted to raw_tracepoint bpf programs.
27782750
* Return
27792751
* 0 on success, or a negative error in case of failure.
2752+
*
2753+
* int bpf_probe_read_user(void *dst, u32 size, const void *unsafe_ptr)
2754+
* Description
2755+
* Safely attempt to read *size* bytes from user space address
2756+
* *unsafe_ptr* and store the data in *dst*.
2757+
* Return
2758+
* 0 on success, or a negative error in case of failure.
2759+
*
2760+
* int bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
2761+
* Description
2762+
* Safely attempt to read *size* bytes from kernel space address
2763+
* *unsafe_ptr* and store the data in *dst*.
2764+
* Return
2765+
* 0 on success, or a negative error in case of failure.
2766+
*
2767+
* int bpf_probe_read_user_str(void *dst, u32 size, const void *unsafe_ptr)
2768+
* Description
2769+
* Copy a NUL terminated string from an unsafe user address
2770+
* *unsafe_ptr* to *dst*. The *size* should include the
2771+
* terminating NUL byte. In case the string length is smaller than
2772+
* *size*, the target is not padded with further NUL bytes. If the
2773+
* string length is larger than *size*, just *size*-1 bytes are
2774+
* copied and the last byte is set to NUL.
2775+
*
2776+
* On success, the length of the copied string is returned. This
2777+
* makes this helper useful in tracing programs for reading
2778+
* strings, and more importantly to get its length at runtime. See
2779+
* the following snippet:
2780+
*
2781+
* ::
2782+
*
2783+
* SEC("kprobe/sys_open")
2784+
* void bpf_sys_open(struct pt_regs *ctx)
2785+
* {
2786+
* char buf[PATHLEN]; // PATHLEN is defined to 256
2787+
* int res = bpf_probe_read_user_str(buf, sizeof(buf),
2788+
* ctx->di);
2789+
*
2790+
* // Consume buf, for example push it to
2791+
* // userspace via bpf_perf_event_output(); we
2792+
* // can use res (the string length) as event
2793+
* // size, after checking its boundaries.
2794+
* }
2795+
*
2796+
* In comparison, using **bpf_probe_read_user()** helper here
2797+
* instead to read the string would require to estimate the length
2798+
* at compile time, and would often result in copying more memory
2799+
* than necessary.
2800+
*
2801+
* Another useful use case is when parsing individual process
2802+
* arguments or individual environment variables navigating
2803+
* *current*\ **->mm->arg_start** and *current*\
2804+
* **->mm->env_start**: using this helper and the return value,
2805+
* one can quickly iterate at the right offset of the memory area.
2806+
* Return
2807+
* On success, the strictly positive length of the string,
2808+
* including the trailing NUL character. On error, a negative
2809+
* value.
2810+
*
2811+
* int bpf_probe_read_kernel_str(void *dst, u32 size, const void *unsafe_ptr)
2812+
* Description
2813+
* Copy a NUL terminated string from an unsafe kernel address *unsafe_ptr*
2814+
* to *dst*. Same semantics as with bpf_probe_read_user_str() apply.
2815+
* Return
2816+
* On success, the strictly positive length of the string, including
2817+
* the trailing NUL character. On error, a negative value.
27802818
*/
27812819
#define __BPF_FUNC_MAPPER(FN) \
27822820
FN(unspec), \
@@ -2890,7 +2928,11 @@ union bpf_attr {
28902928
FN(sk_storage_delete), \
28912929
FN(send_signal), \
28922930
FN(tcp_gen_syncookie), \
2893-
FN(skb_output),
2931+
FN(skb_output), \
2932+
FN(probe_read_user), \
2933+
FN(probe_read_kernel), \
2934+
FN(probe_read_user_str), \
2935+
FN(probe_read_kernel_str),
28942936

28952937
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
28962938
* function eBPF program intends to call

‎kernel/bpf/core.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,11 +1306,12 @@ bool bpf_opcode_in_insntable(u8 code)
13061306
}
13071307

13081308
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1309-
u64 __weak bpf_probe_read(void * dst, u32 size, const void * unsafe_ptr)
1309+
u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
13101310
{
13111311
memset(dst, 0, size);
13121312
return -EFAULT;
13131313
}
1314+
13141315
/**
13151316
* __bpf_prog_run - run eBPF program on a given context
13161317
* @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
@@ -1566,9 +1567,9 @@ static u64 __no_fgcse ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u6
15661567
LDST(W, u32)
15671568
LDST(DW, u64)
15681569
#undef LDST
1569-
#define LDX_PROBE(SIZEOP, SIZE) \
1570-
LDX_PROBE_MEM_##SIZEOP: \
1571-
bpf_probe_read(&DST, SIZE, (const void *)(long) SRC); \
1570+
#define LDX_PROBE(SIZEOP, SIZE) \
1571+
LDX_PROBE_MEM_##SIZEOP: \
1572+
bpf_probe_read_kernel(&DST, SIZE, (const void *)(long) SRC); \
15721573
CONT;
15731574
LDX_PROBE(B, 1)
15741575
LDX_PROBE(H, 2)

0 commit comments

Comments
 (0)