diff mbox series

[bpf-next] bpf: Lock bpf_trace_printk's tmp buf before it is written to

Message ID 20210427112958.773132-1-revest@chromium.org (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf-next] bpf: Lock bpf_trace_printk's tmp buf before it is written to | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 7 maintainers not CCed: netdev@vger.kernel.org yhs@fb.com mingo@redhat.com kafai@fb.com rostedt@goodmis.org john.fastabend@gmail.com songliubraving@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 6 this patch: 6
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 14 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 6 this patch: 6
netdev/header_inline success Link

Commit Message

Florent Revest April 27, 2021, 11:29 a.m. UTC
bpf_trace_printk uses a shared static buffer to hold strings before they
are printed. A recent refactoring moved the locking of that buffer after
it gets filled by mistake.

Fixes: d9c9e4db186a ("bpf: Factorize bpf_trace_printk and bpf_seq_printf")
Reported-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Florent Revest <revest@chromium.org>
---
 kernel/trace/bpf_trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Alexei Starovoitov April 27, 2021, 3:07 p.m. UTC | #1
On Tue, Apr 27, 2021 at 4:30 AM Florent Revest <revest@chromium.org> wrote:
>
> bpf_trace_printk uses a shared static buffer to hold strings before they
> are printed. A recent refactoring moved the locking of that buffer after
> it gets filled by mistake.
>
> Fixes: d9c9e4db186a ("bpf: Factorize bpf_trace_printk and bpf_seq_printf")
> Reported-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Signed-off-by: Florent Revest <revest@chromium.org>

Applied.

Pls send v2 of bstr_printf series as soon as possible. Thanks!
Florent Revest April 27, 2021, 3:20 p.m. UTC | #2
On Tue, Apr 27, 2021 at 5:08 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Apr 27, 2021 at 4:30 AM Florent Revest <revest@chromium.org> wrote:
> >
> > bpf_trace_printk uses a shared static buffer to hold strings before they
> > are printed. A recent refactoring moved the locking of that buffer after
> > it gets filled by mistake.
> >
> > Fixes: d9c9e4db186a ("bpf: Factorize bpf_trace_printk and bpf_seq_printf")
> > Reported-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> > Signed-off-by: Florent Revest <revest@chromium.org>
>
> Applied.

Thanks!

> Pls send v2 of bstr_printf series as soon as possible. Thanks!

Sure, I just assumed there would be more reviews on v1. The feedback
I'll address is only about the commit description wording but I can
send a v2 today.
Alexei Starovoitov April 27, 2021, 3:23 p.m. UTC | #3
On Tue, Apr 27, 2021 at 8:20 AM Florent Revest <revest@chromium.org> wrote:
>
> On Tue, Apr 27, 2021 at 5:08 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Tue, Apr 27, 2021 at 4:30 AM Florent Revest <revest@chromium.org> wrote:
> > >
> > > bpf_trace_printk uses a shared static buffer to hold strings before they
> > > are printed. A recent refactoring moved the locking of that buffer after
> > > it gets filled by mistake.
> > >
> > > Fixes: d9c9e4db186a ("bpf: Factorize bpf_trace_printk and bpf_seq_printf")
> > > Reported-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> > > Signed-off-by: Florent Revest <revest@chromium.org>
> >
> > Applied.
>
> Thanks!
>
> > Pls send v2 of bstr_printf series as soon as possible. Thanks!
>
> Sure, I just assumed there would be more reviews on v1. The feedback
> I'll address is only about the commit description wording but I can
> send a v2 today.

Yes. Please.
diff mbox series

Patch

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 2a8bcdc927c7..0e67d12a8f40 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -391,13 +391,13 @@  BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
 	if (ret < 0)
 		return ret;
 
+	raw_spin_lock_irqsave(&trace_printk_lock, flags);
 	ret = snprintf(buf, sizeof(buf), fmt, BPF_CAST_FMT_ARG(0, args, mod),
 		BPF_CAST_FMT_ARG(1, args, mod), BPF_CAST_FMT_ARG(2, args, mod));
 	/* snprintf() will not append null for zero-length strings */
 	if (ret == 0)
 		buf[0] = '\0';
 
-	raw_spin_lock_irqsave(&trace_printk_lock, flags);
 	trace_bpf_trace_printk(buf);
 	raw_spin_unlock_irqrestore(&trace_printk_lock, flags);