• Florent Revest's avatar
    bpf: Fix nested bpf_bprintf_prepare with more per-cpu buffers · e2d5b2bb
    Florent Revest authored
    The bpf_seq_printf, bpf_trace_printk and bpf_snprintf helpers share one
    per-cpu buffer that they use to store temporary data (arguments to
    bprintf). They "get" that buffer with try_get_fmt_tmp_buf and "put" it
    by the end of their scope with bpf_bprintf_cleanup.
    
    If one of these helpers gets called within the scope of one of these
    helpers, for example: a first bpf program gets called, uses
    bpf_trace_printk which calls raw_spin_lock_irqsave which is traced by
    another bpf program that calls bpf_snprintf, then the second "get"
    fails. Essentially, these helpers are not re-entrant. They would return
    -EBUSY and print a warning message once.
    
    This patch triples the number of bprintf buffers to allow three levels
    of nesting. This is very similar to what was done for tracepoints in
    "9594dc3c bpf: fix nested bpf tracepoints with per-cpu data"
    
    Fixes: d9c9e4db ("bpf: Factorize bpf_trace_printk and bpf_seq_printf")
    Reported-by: syzbot+63122d0bc347f18c1884@syzkaller.appspotmail.com
    Signed-off-by: default avatarFlorent Revest <revest@chromium.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210511081054.2125874-1-revest@chromium.org
    e2d5b2bb
helpers.c 25.1 KB