Restrict bpf_probe_read{,str}() and bpf_trace_printk()'s %s
Subsystems (13)
Daniel Borkmann
maintainerv2 cover Restrict bpf_probe_read{,str}() and bpf_trace_printk()'s %s
Small set of fixes in order to restrict BPF helpers for tracing which are broken on archs with overlapping address ranges as per discussion in [0]. I've targetted this for -bpf tree so they can be routed as fixes. Thanks!
v1 -> v2:
- switch to reusable %pks, %pus format specifiers (Yonghong)
- fixate %s on kernel_ds probing for archs with overlapping addr space
[0] https://lore.kernel.org/bpf/CAHk-=wjJKo0GVixYLmqPn-Q22WFu0xHaBSjKEo7e7Yw72y5SPQ@mail.gmail.com/T/
Daniel Borkmann (3):
bpf: restrict bpf_probe_read{,str}() only to archs where they work
bpf: add bpf_probe_read_{user, kernel}_str() to do_refine_retval_range
bpf: restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier
Documentation/core-api/printk-formats.rst | 14 +++ arch/arm/Kconfig | 1 + arch/arm64/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 + kernel/bpf/verifier.c | 4 +- kernel/trace/bpf_trace.c | 100 ++++++++++++++-------- lib/vsprintf.c | 12 +++ 8 files changed, 101 insertions(+), 35 deletions(-)
Signature · 1 line
2.21.0
Daniel Borkmann
maintainerPATCH v2 1/3 bpf: restrict bpf_probe_read{,str}() only to archs where they work
Given the legacy bpf_probe_read{,str}() BPF helpers are broken on archs with overlapping address ranges, we should really take the next step to disable them from BPF use there.
To generally fix the situation, we've recently added new helper variants bpf_probe_read_{user,kernel}() and bpf_probe_read_{user,kernel}_str(). For details on them, see 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user,kernel}_str helpers").
Given bpf_probe_read{,str}() have been around for ~5 years by now, there are plenty of users at least on x86 still relying on them today, so we cannot remove them entirely w/o breaking the BPF tracing ecosystem.
However, their use should be restricted to archs with non-overlapping address ranges where they are working in their current form. Therefore, move this behind a CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE and have x86, arm64, arm select it (other archs supporting it can follow-up on it as well).
For the remaining archs, they can workaround easily by relying on the feature probe from bpftool which spills out defines that can be used out of BPF C code to implement the drop-in replacement for old/new kernels
via: bpftool feature probe macro
--- arch/arm/Kconfig | 1 + arch/arm64/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/trace/bpf_trace.c | 6 ++++-- 5 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/Kconfig +1 -0
@@ -12,6 +12,7 @@ config ARM select ARCH_HAS_KEEPINITRD select ARCH_HAS_KCOV select ARCH_HAS_MEMBARRIER_SYNC_CORE + select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PTE_SPECIAL if ARM_LPAE select ARCH_HAS_PHYS_TO_DMA select ARCH_HAS_SETUP_DMA_OPS
arch/arm64/Kconfig +1 -0
@@ -20,6 +20,7 @@ config ARM64 select ARCH_HAS_KCOV select ARCH_HAS_KEEPINITRD select ARCH_HAS_MEMBARRIER_SYNC_CORE + select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PTE_DEVMAP select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_SETUP_DMA_OPS
arch/x86/Kconfig +1 -0
@@ -68,6 +68,7 @@ config X86 select ARCH_HAS_KCOV if X86_64 select ARCH_HAS_MEM_ENCRYPT select ARCH_HAS_MEMBARRIER_SYNC_CORE + select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PMEM_API if X86_64 select ARCH_HAS_PTE_DEVMAP if X86_64 select ARCH_HAS_PTE_SPECIAL
init/Kconfig +3 -0
@@ -2279,6 +2279,9 @@ config ASN1 source "kernel/Kconfig.locks" +config ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE + bool + config ARCH_HAS_SYNC_CORE_BEFORE_USERMODE bool
kernel/trace/bpf_trace.c +4 -2
@@ -825,14 +825,16 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel:return &bpf_probe_read_kernel_proto; -case BPF_FUNC_probe_read: -return &bpf_probe_read_compat_proto;case BPF_FUNC_probe_read_user_str:return &bpf_probe_read_user_str_proto;case BPF_FUNC_probe_read_kernel_str:return &bpf_probe_read_kernel_str_proto; +#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE +case BPF_FUNC_probe_read: +return &bpf_probe_read_compat_proto;case BPF_FUNC_probe_read_str:return &bpf_probe_read_compat_str_proto; +#endif #ifdef CONFIG_CGROUPS case BPF_FUNC_get_current_cgroup_id:return &bpf_get_current_cgroup_id_proto;
Signature · 1 line
2.21.0
Daniel Borkmann
maintainerPATCH v2 2/3 bpf: add bpf_probe_read_{user, kernel}_str() to do_refine_retval_range
In reply to Daniel BorkmannGiven bpf_probe_read{,str}() BPF helpers are now only available under CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE, we need to add the drop-in replacements of bpf_probe_read_{kernel,user}_str() to do_refine_retval_range() as well to avoid hitting the same issue as in 849fa50662fbc ("bpf/verifier:
refine retval R0 state for bpf_get_stack helper").
--- kernel/bpf/verifier.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +3 -1
@@ -4340,7 +4340,9 @@ static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,if (ret_type !=RET_INTEGER || (func_id != BPF_FUNC_get_stack && - func_id != BPF_FUNC_probe_read_str)) + func_id != BPF_FUNC_probe_read_str && + func_id != BPF_FUNC_probe_read_kernel_str && + func_id != BPF_FUNC_probe_read_user_str))return ; ret_reg->smax_value = meta->msize_max_value;
Signature · 1 line
2.21.0
Daniel Borkmann
maintainerPATCH v2 3/3 bpf: restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier
In reply to Daniel BorkmannUsage of plain %s conversion specifier in bpf_trace_printk() suffers from the very same issue as bpf_probe_read{,str}() helpers, that is, it is broken on archs with overlapping address ranges.
While the helpers have been addressed through work in 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers"), we need an option for bpf_trace_printk() as well to fix it.
Similarly as with the helpers, force users to make an explicit choice by adding %pks and %pus specifier to bpf_trace_printk() which will then pick the corresponding strncpy_from_unsafe*() variant to perform the access under KERNEL_DS or USER_DS. The %pk* (kernel specifier) and %pu* (user specifier) can later also be extended for other objects aside strings that are probed and printed under tracing, and reused out of other facilities like bpf_seq_printf() or BTF based type printing.
Existing behavior of %s for current users is still kept working for archs where it is not broken and therefore gated through CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE. For archs not having this property we fall-back to pick probing under KERNEL_DS as a sensible default.
--- Documentation/core-api/printk-formats.rst | 14 ++++ kernel/trace/bpf_trace.c | 94 +++++++++++++++-------- lib/vsprintf.c | 12 +++ 3 files changed, 88 insertions(+), 32 deletions(-)
Documentation/core-api/printk-formats.rst +14 -0
@@ -112,6 +112,20 @@ used when printing stack backtraces. The specifier takes into consideration the effect of compiler optimisations which may occur when tail-calls are used and marked with the noreturn GCC attribute. +Probed Pointers from BPF / tracing +---------------------------------- + +:: + + %pks kernel string + %pus user string + +The ``k`` and ``u`` specifiers are used for printing prior probed memory from +either kernel memory (k) or user memory (u). The subsequent ``s`` specifier +results in printing a string. For direct use in regular vsnprintf() the (k) +and (u) annotation is ignored, however, when used out of BPF's bpf_trace_printk(), +for example, it reads the memory it is pointing to without faulting. + Kernel Pointers ---------------
kernel/trace/bpf_trace.c +62 -32
125 lines not on this page · raw message
lib/vsprintf.c +12 -0
@@ -2168,6 +2168,10 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,* f full name * P node name, including a possible unit address * - 'x' For printing the address. Equivalent to "%lx". +* - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of +* bpf_trace_printk() where [ku] prefix specifies either kernel (k) +* or user (u) memory to probe, and: +* s a string, equivalent to "%s" on direct vsnprintf() use * * ** When making changes please also update: * Documentation/core-api/printk-formats.rst
@@ -2251,6 +2255,14 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,if (!IS_ERR (ptr))break ;return err_ptr (buf, end, ptr, spec); +case 'u' : +case 'k' : +switch (fmt[1 ]) { +case 's' : +return string (buf, end, ptr, spec); +default : +return error_string (buf, end,"(einval)" , spec); + } }/* default is to _not_ leak addresses, hash before printing */
Signature · 1 line
2.21.0
Alexei Starovoitov
AppliedQuoted 10 lines · Daniel Borkmann
Small set of fixes in order to restrict BPF helpers for tracing which are broken on archs with overlapping address ranges as per discussion in [0]. I've targetted this for -bpf tree so they can be routed as fixes. Thanks!
v1 -> v2:
- switch to reusable %pks, %pus format specifiers (Yonghong)
- fixate %s onkernel_dsprobing for archs with overlapping addr space[0] https://lore.kernel.org/bpf/CAHk-=wjJKo0GVixYLmqPn-Q22WFu0xHaBSjKEo7e7Yw72y5SPQ@mail.gmail.com/T/
Applied to bpf tree. Thanks
We'll send pr to Dave soon to hopefully get this and other fixes into 5.7 before Sunday rc.