bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier
authorDaniel Borkmann <daniel@iogearbox.net>
Fri, 15 May 2020 10:11:18 +0000 (12:11 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 15 May 2020 15:10:36 +0000 (08:10 -0700)
commitb2a5212fb634561bb734c6356904e37f6665b955
treebb520b4c58247f567a586756c98ab5840d21f8f8
parent47cc0ed574abcbbde0cf143ddb21a0baed1aa2df
bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier

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 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.

Fixes: 8d3b7dce8622 ("bpf: add support for %s specifier to bpf_trace_printk()")
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Reported-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Link: https://lore.kernel.org/bpf/20200515101118.6508-4-daniel@iogearbox.net
Documentation/core-api/printk-formats.rst
kernel/trace/bpf_trace.c
lib/vsprintf.c