Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2913014rwl; Fri, 6 Jan 2023 12:37:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXsfZ8B2rtzrcOZB6LjIHW1TraER6hjsh8kgYYmkEl+/uV0Cqo2ZrIKVJO2ItFVBeXB8jX1j X-Received: by 2002:a17:90a:c795:b0:225:bd44:cf0e with SMTP id gn21-20020a17090ac79500b00225bd44cf0emr54770397pjb.32.1673037446391; Fri, 06 Jan 2023 12:37:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673037446; cv=none; d=google.com; s=arc-20160816; b=bVQpgNLXyNkrHB/B47GHePZBO6ugZ78r21PpIjzRY08OUCmdFECtyN787KWfClEY7F FDAmv39MuJ8y2f+xQFB0Hvmu9Eex/MFDGvRG6artiAOqpOQXDZu3Y2T+cmTxJVOqvV47 gaTrocPBl++3fLxxFrPl3RMMEHN+vbjl3K8VpYcIjYmktqccQ1GFuJtbzzc6x47uMfLl GDD1NW4GE7f3zzSYYi/Pu8+vRmFN9hCcAE8V9E3azv7IIlUiB8Oyw2dolpFxovhBRz21 Q+XeNbjrXyDkv+3Iu6SeEAik+1gr3OJfDG8JJ+MhtWHPyWk4hdLI/D0cDs8m5YrMM9eK 7hAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Snh7uk5DxOJPdJR59DpaCuP9ts2AuGCJFA9IRo6NaCE=; b=zIAvDHuRtoGFZWy7Lrua35mux1jF8t7MTuf3K0LYV6q7+fWdPxbTHI47eFtmgAlshq +0SU7eftI6DRnevAiigsJLFy+gHRNo/7SyAEaHc5y92S1szPBSZNX0RgRnthdJPibic4 q0Loyel3TFJrPRYEOodncoCcElDj6Yn3uKaUAd0W8ic3BUKHNOHLyXh6nEnkB4En5Hvi KZFL0GNToNscE/rrH0+lXlA2LmiWmsdHmER55ftqkkcnUMfvL2NuwN2kDMm3E/QIwXV4 a8qTyWEoGMH3zGYwL+LqGYD9QdyvIZ0zw8LpQAyoAo2r9Y9/sLZ9pSMsw67q94WMHQPi Ugqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rk94C250; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pi13-20020a17090b1e4d00b0020d5867aab6si2321597pjb.141.2023.01.06.12.37.19; Fri, 06 Jan 2023 12:37:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rk94C250; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231531AbjAFTpE (ORCPT + 54 others); Fri, 6 Jan 2023 14:45:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229711AbjAFTpB (ORCPT ); Fri, 6 Jan 2023 14:45:01 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3BF178A5A for ; Fri, 6 Jan 2023 11:45:00 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so6222566pjj.2 for ; Fri, 06 Jan 2023 11:45:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Snh7uk5DxOJPdJR59DpaCuP9ts2AuGCJFA9IRo6NaCE=; b=rk94C250kx/kjnSVWVpYN/WGNwWwJEGGuHnOmQWX7CROKD1qlLyqYGaKtDg7zh1Eir YOoGL8SM11scDNCz9RggAIygVwqocqZxt86SV2JvLmMMn0XFMkfrDu1YaLukhwLfYFCT 2s6QtYEbR24YBdWh5wAkHfqYTdWucJR8nA1560T2/Q2p+YjKsBaF0F3OTggs7cgahFLw lSuF7q92S8rO74jTIpGKM4RZo5gNciSKJUs75ElTtNytassPPzMYbBGx8yK1xWCTTkMY SfRO8HJo9LVCo10Xj1G0GkPjidk1jkbr0KzVPIGgQGoE5+xH2ZebzMUdk1x7zskCfEzU rxXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Snh7uk5DxOJPdJR59DpaCuP9ts2AuGCJFA9IRo6NaCE=; b=XpxChoOEh9XUe0NbxOKJja06mUwVI/ZJPMX/Rllr7AIBI9LWf5A+QjBVnKyKgVTy35 XWPX6JeySYGCLCSAietZXcF8FbaY53s2knQOxS/TM39y+Og4R/EwD1yIQoFDV5onIwfw zmqi4b3n8DyqtBY2Ht0kscUZsWwpduJcslq2KliRay5Mp6Q/L+ic1nCWyGjoL7unHvdI LHGqCzk1Y9HkC03pbkDiTPPWfxeWF90qwcq0n6EaakZANmPyI/gWMzD2LxgnPGkTJElM ai49SRcqQVvSHXdQaqe5/FoiV14PxWlPaTIlAHQtwOwwsqDw68/MCKnq3hR0G+1RtPbI DsyQ== X-Gm-Message-State: AFqh2krGN2jTT5LamENXBFPfMSgSTe4oGGWpJ9+xYm9yNmkR1mBByxj9 FJyVbLG6VA8LL8YE4qqCGuRww2XMhxVZJ8CyGMjMrQ== X-Received: by 2002:a17:90a:5296:b0:219:fbc:a088 with SMTP id w22-20020a17090a529600b002190fbca088mr5293875pjh.162.1673034300135; Fri, 06 Jan 2023 11:45:00 -0800 (PST) MIME-Version: 1.0 References: <20230106084838.12690-1-sunhao.th@gmail.com> In-Reply-To: <20230106084838.12690-1-sunhao.th@gmail.com> From: Stanislav Fomichev Date: Fri, 6 Jan 2023 11:44:48 -0800 Message-ID: Subject: Re: [PATCH] bpf: skip task with pid=1 in send_signal_common() To: Hao Sun Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 6, 2023 at 12:48 AM 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: > > __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 Acked-by: Stanislav Fomichev > --- > 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 >