linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [bug] kernel: bpf: syscall: a possible sleep-in-atomic bug in __bpf_prog_put()
@ 2023-05-16 11:18 starmiku1207184332
  2023-05-16 17:08 ` Yonghong Song
  0 siblings, 1 reply; 12+ messages in thread
From: starmiku1207184332 @ 2023-05-16 11:18 UTC (permalink / raw)
  To: ast, daniel, john.fastabend, andrii, martin.lau, song, yhs,
	kpsingh, sdf, haoluo, jolsa, davem, kuba, hawk
  Cc: bpf, linux-kernel, netdev, Teng Qi

From: Teng Qi <starmiku1207184332@gmail.com>

Hi, bpf developers,

We are developing a static tool to check the matching between helpers and the
context of hooks. During our analysis, we have discovered some important
findings that we would like to report.

‘kernel/bpf/syscall.c: 2097 __bpf_prog_put()’ shows that function
bpf_prog_put_deferred() won`t be called in the condition of
‘in_irq() || irqs_disabled()’.
if (in_irq() || irqs_disabled()) {
    INIT_WORK(&aux->work, bpf_prog_put_deferred);
    schedule_work(&aux->work);
} else {

    bpf_prog_put_deferred(&aux->work);
}

We suspect this condition exists because there might be sleepable operations
in the callees of the bpf_prog_put_deferred() function:
kernel/bpf/syscall.c: 2097 __bpf_prog_put()
kernel/bpf/syscall.c: 2084 bpf_prog_put_deferred()
kernel/bpf/syscall.c: 2063 __bpf_prog_put_noref()
kvfree(prog->aux->jited_linfo);
kvfree(prog->aux->linfo);

Additionally, we found that array prog->aux->jited_linfo is initialized in
‘kernel/bpf/core.c: 157 bpf_prog_alloc_jited_linfo()’:
prog->aux->jited_linfo = kvcalloc(prog->aux->nr_linfo,
  sizeof(*prog->aux->jited_linfo), bpf_memcg_flags(GFP_KERNEL | __GFP_NOWARN));

Our question is whether the condition 'in_irq() || irqs_disabled() == false' is
sufficient for calling 'kvfree'. We are aware that calling 'kvfree' within the
context of a spin lock or an RCU lock is unsafe.

Therefore, we propose modifying the condition to include in_atomic(). Could we
update the condition as follows: "in_irq() || irqs_disabled() || in_atomic()"?

Thank you! We look forward to your feedback.

Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-06-19 19:01 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-16 11:18 [bug] kernel: bpf: syscall: a possible sleep-in-atomic bug in __bpf_prog_put() starmiku1207184332
2023-05-16 17:08 ` Yonghong Song
     [not found]   ` <CALyQVax8X63qekZVhvRTmZFFs+ucPKRkBB7UnRZk6Hu3ggi7Og@mail.gmail.com>
2023-05-21  3:44     ` Yonghong Song
     [not found]       ` <CALyQVazb=D1ejapiFdTnan6JbjFJA2q9ifhSsmF4OC9MDz3oAw@mail.gmail.com>
2023-05-23  4:33         ` Yonghong Song
2023-05-24 12:42           ` Teng Qi
2023-05-24 19:34             ` Yonghong Song
2023-05-24 19:44               ` Alexei Starovoitov
2023-05-25  5:37                 ` Yonghong Song
2023-06-11 13:02               ` Teng Qi
2023-06-12  0:01                 ` Yonghong Song
2023-06-19  9:05                   ` Teng Qi
2023-06-19 19:01                     ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).