diff mbox series

[bpf] bpf: Fix a possible task gone issue with bpf_send_signal[_thread]() helpers

Message ID 20230118204815.3331855-1-yhs@fb.com (mailing list archive)
State Accepted
Commit bdb7fdb0aca8b96cef9995d3a57e251c2289322f
Delegated to: BPF
Headers show
Series [bpf] bpf: Fix a possible task gone issue with bpf_send_signal[_thread]() helpers | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-9 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-10 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-12 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-16 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-20 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-22 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-26 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-28 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-29 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-30 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-31 success Logs for test_verifier on x86_64 with llvm-16
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 15 this patch: 15
netdev/cc_maintainers warning 10 maintainers not CCed: kpsingh@kernel.org haoluo@google.com rostedt@goodmis.org song@kernel.org martin.lau@linux.dev sdf@google.com linux-trace-kernel@vger.kernel.org john.fastabend@gmail.com jolsa@kernel.org mhiramat@kernel.org
netdev/build_clang success Errors and warnings before: 1 this patch: 1
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 15 this patch: 15
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-6 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-7 success Logs for set-matrix

Commit Message

Yonghong Song Jan. 18, 2023, 8:48 p.m. UTC
In current bpf_send_signal() and bpf_send_signal_thread() helper
implementation, irq_work is used to handle nmi context. Hao Sun
reported in [1] that the current task at the entry of the helper
might be gone during irq_work callback processing. To fix the issue,
a reference is acquired for the current task before enqueuing into
the irq_work so that the queued task is still available during
irq_work callback processing.

  [1] https://lore.kernel.org/bpf/20230109074425.12556-1-sunhao.th@gmail.com/

Fixes: 8b401f9ed244 ("bpf: implement bpf_send_signal() helper")
Tested-by: Hao Sun <sunhao.th@gmail.com>
Reported-by: Hao Sun <sunhao.th@gmail.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
---
 kernel/trace/bpf_trace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NOTE:
  I didn't add a unit test case since it is very hard to construct one
  which can reliably reproducing the issue in short amount of time.
  I cannot even reproduce the issue with Hao's reproducer in my local
  environment. Hopefully, the patch itself can explain the issue
  and the fix.

Comments

patchwork-bot+netdevbpf@kernel.org Jan. 19, 2023, 2:50 a.m. UTC | #1
Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Wed, 18 Jan 2023 12:48:15 -0800 you wrote:
> In current bpf_send_signal() and bpf_send_signal_thread() helper
> implementation, irq_work is used to handle nmi context. Hao Sun
> reported in [1] that the current task at the entry of the helper
> might be gone during irq_work callback processing. To fix the issue,
> a reference is acquired for the current task before enqueuing into
> the irq_work so that the queued task is still available during
> irq_work callback processing.
> 
> [...]

Here is the summary with links:
  - [bpf] bpf: Fix a possible task gone issue with bpf_send_signal[_thread]() helpers
    https://git.kernel.org/bpf/bpf/c/bdb7fdb0aca8

You are awesome, thank you!
diff mbox series

Patch

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index f47274de012b..c09792c551bf 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -833,6 +833,7 @@  static void do_bpf_send_signal(struct irq_work *entry)
 
 	work = container_of(entry, struct send_signal_irq_work, irq_work);
 	group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
+	put_task_struct(work->task);
 }
 
 static int bpf_send_signal_common(u32 sig, enum pid_type type)
@@ -867,7 +868,7 @@  static int bpf_send_signal_common(u32 sig, enum pid_type type)
 		 * to the irq_work. The current task may change when queued
 		 * irq works get executed.
 		 */
-		work->task = current;
+		work->task = get_task_struct(current);
 		work->sig = sig;
 		work->type = type;
 		irq_work_queue(&work->irq_work);