Re: [PATCH bpf] bpf: fix %p% runtime check in bpf_bprintf_prepare

From: Yonghong Song
Date: Mon Oct 14 2024 - 17:29:20 EST



On 10/9/24 3:57 AM, Ilya Shchipletsov wrote:
Fuzzing reports a warning in format_decode()

Please remove unsupported %� in format string
WARNING: CPU: 0 PID: 5091 at lib/vsprintf.c:2680 format_decode+0x1193/0x1bb0 lib/vsprintf.c:2680
Modules linked in:
CPU: 0 PID: 5091 Comm: syz-executor879 Not tainted 6.10.0-rc1-syzkaller-00021-ge0cce98fe279 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 04/02/2024
RIP: 0010:format_decode+0x1193/0x1bb0 lib/vsprintf.c:2680
Call Trace:
<TASK>
bstr_printf+0x137/0x1210 lib/vsprintf.c:3253
____bpf_trace_printk kernel/trace/bpf_trace.c:390 [inline]
bpf_trace_printk+0x1a1/0x230 kernel/trace/bpf_trace.c:375
bpf_prog_21da1b68f62e1237+0x36/0x41
bpf_dispatcher_nop_func include/linux/bpf.h:1243 [inline]
__bpf_prog_run include/linux/filter.h:691 [inline]
bpf_prog_run include/linux/filter.h:698 [inline]
bpf_test_run+0x40b/0x910 net/bpf/test_run.c:425
bpf_prog_test_run_skb+0xafa/0x13a0 net/bpf/test_run.c:1066
bpf_prog_test_run+0x33c/0x3b0 kernel/bpf/syscall.c:4291
__sys_bpf+0x48d/0x810 kernel/bpf/syscall.c:5705
__do_sys_bpf kernel/bpf/syscall.c:5794 [inline]
__se_sys_bpf kernel/bpf/syscall.c:5792 [inline]
__x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:5792
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f

The problem occurs when trying to pass %p% at the end of format string,
which would result in skipping last % and passing invalid format string
down to format_decode() that would cause warning because of invalid
character after %.

Indeed, in kernel doing
printk("%p%");
will have following compilation failure.

/home/yhs/work/bpf-next/kernel/bpf/helpers.c:830:10: error: more '%' conversions than data arguments [-Werror,-Wformat-insufficient-args]
830 | printk("%p%");
| ~^
/home/yhs/work/bpf-next/include/linux/printk.h:490:53: note: expanded from macro 'printk'
490 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
| ^~~
/home/yhs/work/bpf-next/include/linux/printk.h:462:11: note: expanded from macro 'printk_index_wrap'
462 | _p_func(_fmt, ##__VA_ARGS__); \
| ^~~~
1 error generated.


Fix issue by advancing pointer only if next char is format modifier.
If next char is null/space/punct, then just accept formatting as is,
without advancing the pointer.

Fixes: 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
Co-developed-by: Nikita Marushkin <hfggklm@xxxxxxxxx>
Signed-off-by: Nikita Marushkin <hfggklm@xxxxxxxxx>
Signed-off-by: Ilya Shchipletsov <rabbelkin@xxxxxxx>

LGTM with some comments and nits below.

Acked-by: Yonghong Song <yonghong.song@xxxxxxxxx>

---
kernel/bpf/helpers.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index c9e235807cac..bd771d6aacdb 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -892,14 +892,19 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
goto fmt_str;
}
+ if (fmt[i + 1] == 'K' || fmt[i + 1] == 'x' ||
+ fmt[i + 1] == 's' || fmt[i + 1] == 'S') {
+ if (tmp_buf)
+ cur_arg = raw_args[num_spec];
+ i++;
+ goto nocopy_fmt;
+ }
+
if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
- ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' ||
- fmt[i + 1] == 'x' || fmt[i + 1] == 's' ||
- fmt[i + 1] == 'S') {
+ ispunct(fmt[i + 1])) {
/* just kernel pointers */
if (tmp_buf)
cur_arg = raw_args[num_spec];
- i++;
goto nocopy_fmt;
}

We could do ispunct(fmt[i + 1]) only in the above 'if' statement.
But your implementation is right too and maybe cleaner, so let us
keep your above implementation.

Could you move comment '/* just kernel pointers */' to previous
if statement.

Also could you add Reported-by mentioned by Florent Revest
in the next revision?