bpf: skip task with pid=1 in send_signal_common()

Message ID 20230106084838.12690-1-sunhao.th@gmail.com
State New
Headers
Series bpf: skip task with pid=1 in send_signal_common() |

Commit Message

Hao Sun Jan. 6, 2023, 8:48 a.m. UTC
  The following kernel panic can be triggered when a task with pid=1
attach a prog that attempts to send killing signal to itself, also
see [1] for more details:

Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
CPU: 3 PID: 1 Comm: systemd Not tainted 6.1.0-09652-g59fe41b5255f #148
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0x100/0x178 lib/dump_stack.c:106
panic+0x2c4/0x60f kernel/panic.c:275
do_exit.cold+0x63/0xe4 kernel/exit.c:789
do_group_exit+0xd4/0x2a0 kernel/exit.c:950
get_signal+0x2460/0x2600 kernel/signal.c:2858
arch_do_signal_or_restart+0x78/0x5d0 arch/x86/kernel/signal.c:306
exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:203
__syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:296
do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86
entry_SYSCALL_64_after_hwframe+0x63/0xcd

So skip task with pid=1 in bpf_send_signal_common() to avoid the panic.

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

Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
 kernel/trace/bpf_trace.c | 3 +++
 1 file changed, 3 insertions(+)


base-commit: 4aea86b4033f92f01547e6d4388d4451ae9b0980
  

Comments

Stanislav Fomichev Jan. 6, 2023, 7:44 p.m. UTC | #1
On Fri, Jan 6, 2023 at 12:48 AM Hao Sun <sunhao.th@gmail.com> wrote:
>
> The following kernel panic can be triggered when a task with pid=1
> attach a prog that attempts to send killing signal to itself, also
> see [1] for more details:
>
> Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
> CPU: 3 PID: 1 Comm: systemd Not tainted 6.1.0-09652-g59fe41b5255f #148
> Call Trace:
> <TASK>
> __dump_stack lib/dump_stack.c:88 [inline]
> dump_stack_lvl+0x100/0x178 lib/dump_stack.c:106
> panic+0x2c4/0x60f kernel/panic.c:275
> do_exit.cold+0x63/0xe4 kernel/exit.c:789
> do_group_exit+0xd4/0x2a0 kernel/exit.c:950
> get_signal+0x2460/0x2600 kernel/signal.c:2858
> arch_do_signal_or_restart+0x78/0x5d0 arch/x86/kernel/signal.c:306
> exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
> exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:203
> __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
> syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:296
> do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86
> entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> So skip task with pid=1 in bpf_send_signal_common() to avoid the panic.
>
> [1] https://lore.kernel.org/bpf/20221222043507.33037-1-sunhao.th@gmail.com
>
> Signed-off-by: Hao Sun <sunhao.th@gmail.com>

Acked-by: Stanislav Fomichev <sdf@google.com>


> ---
>  kernel/trace/bpf_trace.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 23ce498bca97..ed21ab9fe846 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -844,6 +844,9 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type)
>          */
>         if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
>                 return -EPERM;
> +       /* Task should not be pid=1 to avoid kernel panic. */
> +       if (unlikely(is_global_init(current)))
> +               return -EPERM;
>         if (unlikely(!nmi_uaccess_okay()))
>                 return -EPERM;
>
>
> base-commit: 4aea86b4033f92f01547e6d4388d4451ae9b0980
> --
> 2.39.0
>
  
Daniel Borkmann Jan. 6, 2023, 9:54 p.m. UTC | #2
On 1/6/23 8:44 PM, Stanislav Fomichev wrote:
> On Fri, Jan 6, 2023 at 12:48 AM Hao Sun <sunhao.th@gmail.com> wrote:
>>
>> The following kernel panic can be triggered when a task with pid=1
>> attach a prog that attempts to send killing signal to itself, also
>> see [1] for more details:
>>
>> Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
>> CPU: 3 PID: 1 Comm: systemd Not tainted 6.1.0-09652-g59fe41b5255f #148
>> Call Trace:
>> <TASK>
>> __dump_stack lib/dump_stack.c:88 [inline]
>> dump_stack_lvl+0x100/0x178 lib/dump_stack.c:106
>> panic+0x2c4/0x60f kernel/panic.c:275
>> do_exit.cold+0x63/0xe4 kernel/exit.c:789
>> do_group_exit+0xd4/0x2a0 kernel/exit.c:950
>> get_signal+0x2460/0x2600 kernel/signal.c:2858
>> arch_do_signal_or_restart+0x78/0x5d0 arch/x86/kernel/signal.c:306
>> exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
>> exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:203
>> __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
>> syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:296
>> do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86
>> entry_SYSCALL_64_after_hwframe+0x63/0xcd
>>
>> So skip task with pid=1 in bpf_send_signal_common() to avoid the panic.
>>
>> [1] https://lore.kernel.org/bpf/20221222043507.33037-1-sunhao.th@gmail.com
>>
>> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> 
> Acked-by: Stanislav Fomichev <sdf@google.com>

Looks good, agree & applied. These three tests could actually be refactored and also
reused in bpf_probe_write_user() given also the latter should have no business to
mess with pid 1.

>>   kernel/trace/bpf_trace.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
>> index 23ce498bca97..ed21ab9fe846 100644
>> --- a/kernel/trace/bpf_trace.c
>> +++ b/kernel/trace/bpf_trace.c
>> @@ -844,6 +844,9 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type)
>>           */
>>          if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
>>                  return -EPERM;
>> +       /* Task should not be pid=1 to avoid kernel panic. */
>> +       if (unlikely(is_global_init(current)))
>> +               return -EPERM;
>>          if (unlikely(!nmi_uaccess_okay()))
>>                  return -EPERM;
>>
>>
>> base-commit: 4aea86b4033f92f01547e6d4388d4451ae9b0980
>> --
>> 2.39.0
>>
  
patchwork-bot+netdevbpf@kernel.org Jan. 6, 2023, 10 p.m. UTC | #3
Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Fri,  6 Jan 2023 16:48:38 +0800 you wrote:
> The following kernel panic can be triggered when a task with pid=1
> attach a prog that attempts to send killing signal to itself, also
> see [1] for more details:
> 
> Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
> CPU: 3 PID: 1 Comm: systemd Not tainted 6.1.0-09652-g59fe41b5255f #148
> Call Trace:
> <TASK>
> __dump_stack lib/dump_stack.c:88 [inline]
> dump_stack_lvl+0x100/0x178 lib/dump_stack.c:106
> panic+0x2c4/0x60f kernel/panic.c:275
> do_exit.cold+0x63/0xe4 kernel/exit.c:789
> do_group_exit+0xd4/0x2a0 kernel/exit.c:950
> get_signal+0x2460/0x2600 kernel/signal.c:2858
> arch_do_signal_or_restart+0x78/0x5d0 arch/x86/kernel/signal.c:306
> exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
> exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:203
> __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
> syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:296
> do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86
> entry_SYSCALL_64_after_hwframe+0x63/0xcd
> 
> [...]

Here is the summary with links:
  - bpf: skip task with pid=1 in send_signal_common()
    https://git.kernel.org/bpf/bpf/c/a3d81bc1eaef

You are awesome, thank you!
  
Kumar Kartikeya Dwivedi Jan. 19, 2023, 5:11 p.m. UTC | #4
On Fri, Jan 06, 2023 at 02:18:38PM IST, Hao Sun wrote:
> The following kernel panic can be triggered when a task with pid=1
> attach a prog that attempts to send killing signal to itself, also
> see [1] for more details:
>
> Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
> CPU: 3 PID: 1 Comm: systemd Not tainted 6.1.0-09652-g59fe41b5255f #148
> Call Trace:
> <TASK>
> __dump_stack lib/dump_stack.c:88 [inline]
> dump_stack_lvl+0x100/0x178 lib/dump_stack.c:106
> panic+0x2c4/0x60f kernel/panic.c:275
> do_exit.cold+0x63/0xe4 kernel/exit.c:789
> do_group_exit+0xd4/0x2a0 kernel/exit.c:950
> get_signal+0x2460/0x2600 kernel/signal.c:2858
> arch_do_signal_or_restart+0x78/0x5d0 arch/x86/kernel/signal.c:306
> exit_to_user_mode_loop kernel/entry/common.c:168 [inline]
> exit_to_user_mode_prepare+0x15f/0x250 kernel/entry/common.c:203
> __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline]
> syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:296
> do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86
> entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> So skip task with pid=1 in bpf_send_signal_common() to avoid the panic.
>
> [1] https://lore.kernel.org/bpf/20221222043507.33037-1-sunhao.th@gmail.com
>
> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> ---
>  kernel/trace/bpf_trace.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 23ce498bca97..ed21ab9fe846 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -844,6 +844,9 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type)
>  	 */
>  	if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
>  		return -EPERM;
> +	/* Task should not be pid=1 to avoid kernel panic. */
> +	if (unlikely(is_global_init(current)))
> +		return -EPERM;

Sorry for chiming in late.
This seems a bit too restrictive, there are a lot of legitimate signals that can
be sent to the init process (like the ones listed in man 1 systemd). Maybe you
should only prevent sig == SIGKILL and not others?
  

Patch

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 23ce498bca97..ed21ab9fe846 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -844,6 +844,9 @@  static int bpf_send_signal_common(u32 sig, enum pid_type type)
 	 */
 	if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
 		return -EPERM;
+	/* Task should not be pid=1 to avoid kernel panic. */
+	if (unlikely(is_global_init(current)))
+		return -EPERM;
 	if (unlikely(!nmi_uaccess_okay()))
 		return -EPERM;