Restrict bpf_probe_read{,str}() and bpf_trace_printk()'s %s
Subsystems (13)
Daniel Borkmann
maintainercover 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!
[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 %psK, %psU 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 | 98 +++++++++++++++-------- lib/vsprintf.c | 7 +- 8 files changed, 94 insertions(+), 35 deletions(-)
Signature · 1 line
2.21.0
Daniel Borkmann
maintainerPATCH 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).
--- 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
Re: [PATCH bpf 1/3] bpf: restrict bpf_probe_read{,str}() only to archs where they work
Quoted 6 lines · Daniel Borkmann
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_SPACEand have x86, arm64, arm select it (other archs supporting it can follow-up on it as well).
Ack, looks sane to me.
Linus
Re: [PATCH bpf 1/3] bpf: restrict bpf_probe_read{,str}() only to archs where they work
In reply to Daniel BorkmannQuoted 19 lines · On Thu, 14 May 2020 18:16:05 +0200 Daniel Borkmann <[email protected]> wrote:
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_SPACEand have x86, arm64, arm select it (other archs supporting it can follow-up on it as well).
Quoted 5 lines
Suggested-by Linus Torvalds <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Brendan Gregg <[email protected]>Cc Christoph Hellwig <[email protected]>
Thanks for the config! Looks good to me.
Quoted 84 lines
--- 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_OPSarch/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_OPSarch/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_SPECIALinit/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 boolkernel/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
Signature · 1 line
Masami Hiramatsu <[email protected]>
Daniel Borkmann
maintainerPATCH 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
RE: [PATCH bpf 2/3] bpf: add bpf_probe_read_{user, kernel}_str() to do_refine_retval_range
Quoted 10 lines · Daniel Borkmann
Given 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() todo_refine_retval_range()as well to avoid hitting the same issue as in849fa50662fbc("bpf/verifier:
refine retval R0 state forbpf_get_stackhelper").Signed-off-by Daniel Borkmann <[email protected]>Cc John Fastabend <[email protected]>Cc Yonghong Song <[email protected]>---
Re: [PATCH bpf 2/3] bpf: add bpf_probe_read_{user, kernel}_str() to do_refine_retval_range
In reply to Daniel BorkmannQuoted 9 lines · Daniel Borkmann
Given 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() todo_refine_retval_range()as well to avoid hitting the same issue as in849fa50662fbc("bpf/verifier:
refine retval R0 state forbpf_get_stackhelper").Signed-off-by Daniel Borkmann <[email protected]>Cc John Fastabend <[email protected]>Cc Yonghong Song <[email protected]>
LGTM.
Daniel Borkmann
maintainerPATCH 3/3 bpf: restrict bpf_trace_printk()'s %s usage and add %psK, %psU 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 %psK and %psU 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.
Existing %s for legacy users is still kept working for archs where it is not broken and therefore gated through CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE.
--- Documentation/core-api/printk-formats.rst | 14 ++++ kernel/trace/bpf_trace.c | 92 +++++++++++++++-------- lib/vsprintf.c | 7 +- 3 files changed, 81 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 Strings from BPF +----------------------- + +:: + + %psK kernel_string + %psU user_string + +The ``sK`` and ``sU`` specifiers are used for printing a string from probed +memory. From regular vsnprintf(), they are equivalent to ``%s``, however, +when used out of BPF's bpf_trace_printk() it reads a string of up to 64 bytes +in memory without faulting. For ``K`` specifier, the string is probed out of +kernel memory whereas for ``U`` specifier, it is probed out of user memory. + Kernel Pointers ---------------
kernel/trace/bpf_trace.c +61 -31
124 lines not on this page · raw message
lib/vsprintf.c +6 -1
@@ -2168,6 +2168,8 @@ 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". +* - 's[KU]' For printing a string, used in bpf_trace_printk(). For non-BPF +* context this is equivalent to "%s". * * ** When making changes please also update: * Documentation/core-api/printk-formats.rst
@@ -2180,8 +2182,11 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr, struct printf_spec spec) {switch (*fmt) { -case 'S' :case 's' : +if (fmt[1 ] =='K' || fmt[1 ] =='U' ) +return string (buf, end, ptr, spec); +/* Fallthrough */ +case 'S' : ptr =dereference_symbol_descriptor (ptr);/* Fallthrough */ case 'B' :
Signature · 1 line
2.21.0
Re: [PATCH bpf 3/3] bpf: restrict bpf_trace_printk()'s %s usage and add %psK, %psU specifier
Quoted 11 lines · Daniel Borkmann
Usage 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 forbpf_trace_printk()as well to fix it.Similarly as with the helpers, force users to make an explicit choice by adding %psK and %psU specifier to
bpf_trace_printk()which will then pick the corresponding strncpy_from_unsafe*() variant to perform the access underKERNEL_DSorUSER_DS.
In bpf_trace_printk(), we only print strings.
In bpf-next bpf_iter bpf_seq_printf() helper, introduced by
commit 492e639f0c22 ("bpf: Add bpf_seq_printf and bpf_seq_write helpers"), print strings and ip addresses %p{i,I}{4,6}.
Alan in
https://lore.kernel.org/bpf/alpine.LRH.2.21.2005141738050.23867@localhost/T
proposed BTF based type printing with a new format specifier
%pT, which potentially will be used in bpf_trace_printk() and bpf_seq_printf().
In the future, we may want to support more %p<...> format in these helpers. I am wondering whether we can have generic way so we only need to change lib/vsprintf.c once.
Maybe using %pk<...> to specify the kernel address and %pu<...> to specify user address space. In the above example, we will have %pks, %pus, %pki4 or %pui4, etc. Does this make sense?
Quoted 42 lines
Existing %s for legacy users is still kept working for archs where it is not broken and therefore gated through
CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE.Fixes 8d3b7dce8622 ("bpf: add support for %s specifier to bpf_trace_printk()")Reported-by Linus Torvalds <[email protected]>Reported-by Christoph Hellwig <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Brendan Gregg <[email protected]>Cc Christoph Hellwig <[email protected]>--- Documentation/core-api/printk-formats.rst | 14 ++++ kernel/trace/bpf_trace.c | 92 +++++++++++++++-------- lib/vsprintf.c | 7 +- 3 files changed, 81 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 Strings from BPF +----------------------- + +:: + + %psK kernel_string + %psU user_string + +The ``sK`` and ``sU`` specifiers are used for printing a string from probed +memory. From regular vsnprintf(), they are equivalent to ``%s``, however, +when used out of BPF's bpf_trace_printk() it reads a string of up to 64 bytes +in memory without faulting. For ``K`` specifier, the string is probed out of +kernel memory whereas for ``U`` specifier, it is probed out of user memory. + Kernel Pointers ---------------
[...]
Daniel Borkmann
maintainerRe: [PATCH bpf 3/3] bpf: restrict bpf_trace_printk()'s %s usage and add %psK, %psU specifier
Quoted 9 lines · Yonghong Song
Quoted 8 lines · Daniel Borkmann
Usage 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 forbpf_trace_printk()as well to fix it.
Quoted 5 lines
Quoted 3 lines
Similarly as with the helpers, force users to make an explicit choice by adding %psK and %psU specifier to
bpf_trace_printk()which will then pick the corresponding strncpy_from_unsafe*() variant to perform the access underKERNEL_DSorUSER_DS.In
bpf_trace_printk(), we only print strings.
Right ...
Quoted 2 lines
In bpf-next
bpf_iterbpf_seq_printf()helper, introduced by
commit492e639f0c22("bpf: Addbpf_seq_printfandbpf_seq_writehelpers"), print strings and ip addresses %p{i,I}{4,6}.
... and here only kernel buffers.
Quoted 10 lines
Alan in
https://lore.kernel.org/bpf/alpine.LRH.2.21.2005141738050.23867@localhost/T
proposed BTF based type printing with a new format specifier
%pT, which potentially will be used inbpf_trace_printk()andbpf_seq_printf().In the future, we may want to support more %p<...> format in these helpers. I am wondering whether we can have generic way so we only need to change
lib/vsprintf.conce.Maybe using %pk<...> to specify the kernel address and %pu<...> to specify user address space. In the above example, we will have %pks, %pus, %pki4 or %pui4, etc. Does this make sense?
Ah, right, once bpf merges back into bpf-next, we should consolidate these. I didn't want to add the strncpy_from_unsafe*() right into lib/vsprintf.c since then we'd open it up to all possible call-sites whereas it's really just needed out of bpf_trace_printk() for the fix. I think it probably might make sense to add a generic lightweight layer to consolidate all the bpf-related printk handling where we can statically specify a config e.g. as flags of allowed specifiers which then internally takes care of checking the fmt specifiers for sanity and does the probe read handling before passing down into lower layers. Thinking of the case of adding %p{i,I}{4,6} to bpf_trace_printk(), for example, and assuming we'd had a case where it needs to be probed out of both, then %pk<...> and %pu<...> modifier feels reasonable indeed. I'll do a v2 to implement %pks and %pus instead.
Thanks,
Daniel
Quoted 3 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!
Does that mean you are targeting them for 5.7?
Daniel Borkmann
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 3 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!
Does that mean you are targeting them for 5.7?
Yes, it would make most sense to me based on the discussion we had in the other thread. If there is concern wrt latency we could route these to DaveM's net tree in a timely manner (e.g. still tonight or so).
Thanks,
Daniel
Quoted 11 lines · Daniel Borkmann
Quoted 6 lines · Christoph Hellwig
Quoted 3 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!
Does that mean you are targeting them for 5.7?
Yes, it would make most sense to me based on the discussion we had in the other thread. If there is concern wrt latency we could route these to DaveM's net tree in a timely manner (e.g. still tonight or so).
I don't think we should rush this too much. I just want to make sure it either goes into 5.7 or that we have a coordinated tree that I can base the maccess series on.
Daniel Borkmann
maintainerQuoted 9 lines · Christoph Hellwig
Quoted 8 lines · Daniel Borkmann
Quoted 6 lines · Christoph Hellwig
Quoted 3 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!
Does that mean you are targeting them for 5.7?
Quoted 7 lines
Quoted 3 lines
Yes, it would make most sense to me based on the discussion we had in the other thread. If there is concern wrt latency we could route these to DaveM's net tree in a timely manner (e.g. still tonight or so).
I don't think we should rush this too much. I just want to make sure it either goes into 5.7 or that we have a coordinated tree that I can base the maccess series on.
Yep, makes sense, we'll target 5.7 for it.
Thanks,
Daniel