From patchwork Sat Oct 7 13:59:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412353 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B55AD23749 for ; Sat, 7 Oct 2023 14:00:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mxiPt5qF" Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 925DCB6 for ; Sat, 7 Oct 2023 07:00:06 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6969b391791so2386726b3a.3 for ; Sat, 07 Oct 2023 07:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687201; x=1697292001; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=dpzkmLeAt7BQEAYyhM465wQCi7VPMI6aZUboDp9HsGU=; b=mxiPt5qFxEN02yQRrWAKUCOzKl5vW1nFMLpW1iBcm8UWtMKP94ihRdDZFngeGpTUCv v+bRI1Wjc7pFflVniDHVGatGMOs/erRRF3VrWpCvGGZumKycTeO0gpH2Pgn2yhKDRqQG SiDlA9UmsgC4EG/dwbsLDol4qBB5JjIfK7hIffsJ9Rx8jDyuQGPAP8sVBZZDvaEf1iIJ Y1kGJ2DW02edpm4XCDlAULlavyFLq1uHrdCuVzUVsBvEEPMu/AdqeFfkC1AH80bLgJNh JpxJ6WE3uS0dPl0+sHzD3u55bqdEBjSKpzzkmFjxTnu52IygH1NMe4lM10Au7BSG5o5j b7eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687201; x=1697292001; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dpzkmLeAt7BQEAYyhM465wQCi7VPMI6aZUboDp9HsGU=; b=ByN5MnyHC0iCTpL6sRqQ4WJZJPipOy4fzUZ+JtsIXmRIfaCtKWG9n9IF7n2qQrK6rN rW18O0zooPiMaGu+y6gTrAZOa77L2rXYCXh2niL25LdLulANQPEhCl23E8b2qdErx16Y LFOS7yJaOEtKVqJh8Q7OuCElGnQ151T9Te5vU1qqSqF37nqUk6aBkW0AMLHssx06r6jz CKoYMbSBVQ+ZgXQf7RU4CXBvIUexc2Q1AwjMouiVk42zEj9MgnKTEIKmOB/+tzdy6k8H rGOQIn2WrVKcVdZbnsGoOAo2Qjs8zQ3dliiEQL7tp/Mz8M2AQYlMYJtYyme0u2f5WgV5 +smQ== X-Gm-Message-State: AOJu0YzRi/g+IGK/Hk2dgtILbfyblNfsEQzK0bIHxNLAWzSdcK523mk+ sDdZXew2IfM9X5KghWJxhm0= X-Google-Smtp-Source: AGHT+IG00leOJgmzz5affaYPBsYGUIdBOyfB21lFLm6gCJQ7sZS7VKB13NaCIfVUxmbQjGDlGkCfhA== X-Received: by 2002:a05:6a00:39a2:b0:691:2d4:23b2 with SMTP id fi34-20020a056a0039a200b0069102d423b2mr11671653pfb.15.1696687200723; Sat, 07 Oct 2023 07:00:00 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id i23-20020aa787d7000000b00682a908949bsm3302978pfo.92.2023.10.07.06.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:00:00 -0700 (PDT) From: Yafang Shao To: 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, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Feng Zhou Subject: [PATCH bpf-next v2 1/2] bpf: Fix missed rcu read lock in bpf_task_under_cgroup() Date: Sat, 7 Oct 2023 13:59:44 +0000 Message-Id: <20231007135945.4306-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net When employed within a sleepable program not under RCU protection, the use of 'bpf_task_under_cgroup()' may trigger a warning in the kernel log, particularly when CONFIG_PROVE_RCU is enabled. [ 1259.662354] ============================= [ 1259.662357] WARNING: suspicious RCU usage [ 1259.662358] 6.5.0+ #33 Not tainted [ 1259.662360] ----------------------------- [ 1259.662361] include/linux/cgroup.h:423 suspicious rcu_dereference_check() usage! [ 1259.662364] other info that might help us debug this: [ 1259.662366] rcu_scheduler_active = 2, debug_locks = 1 [ 1259.662368] 1 lock held by trace/72954: [ 1259.662369] #0: ffffffffb5e3eda0 (rcu_read_lock_trace){....}-{0:0}, at: __bpf_prog_enter_sleepable+0x0/0xb0 [ 1259.662383] stack backtrace: [ 1259.662385] CPU: 50 PID: 72954 Comm: trace Kdump: loaded Not tainted 6.5.0+ #33 [ 1259.662391] Call Trace: [ 1259.662393] [ 1259.662395] dump_stack_lvl+0x6e/0x90 [ 1259.662401] dump_stack+0x10/0x20 [ 1259.662404] lockdep_rcu_suspicious+0x163/0x1b0 [ 1259.662412] task_css_set.part.0+0x23/0x30 [ 1259.662417] bpf_task_under_cgroup+0xe7/0xf0 [ 1259.662422] bpf_prog_7fffba481a3bcf88_lsm_run+0x5c/0x93 [ 1259.662431] bpf_trampoline_6442505574+0x60/0x1000 [ 1259.662439] bpf_lsm_bpf+0x5/0x20 [ 1259.662443] ? security_bpf+0x32/0x50 [ 1259.662452] __sys_bpf+0xe6/0xdd0 [ 1259.662463] __x64_sys_bpf+0x1a/0x30 [ 1259.662467] do_syscall_64+0x38/0x90 [ 1259.662472] entry_SYSCALL_64_after_hwframe+0x6e/0xd8 [ 1259.662479] RIP: 0033:0x7f487baf8e29 ... [ 1259.662504] This issue can be reproduced by executing a straightforward program, as demonstrated below: SEC("lsm.s/bpf") int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size) { struct cgroup *cgrp = NULL; struct task_struct *task; int ret = 0; if (cmd != BPF_LINK_CREATE) return 0; // The cgroup2 should be mounted first cgrp = bpf_cgroup_from_id(1); if (!cgrp) goto out; task = bpf_get_current_task_btf(); if (bpf_task_under_cgroup(task, cgrp)) ret = -1; bpf_cgroup_release(cgrp); out: return ret; } After running the program, if you subsequently execute another BPF program, you will encounter the warning. It's worth noting that task_under_cgroup_hierarchy() is also utilized by bpf_current_task_under_cgroup(). However, bpf_current_task_under_cgroup() doesn't exhibit this issue because it cannot be used in sleepable BPF programs. Fixes: b5ad4cdc46c7 ("bpf: Add bpf_task_under_cgroup() kfunc") Signed-off-by: Yafang Shao Acked-by: Stanislav Fomichev Cc: Feng Zhou --- kernel/bpf/helpers.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index dd1c69ee3375..bb521b181cc3 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2212,7 +2212,12 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid) __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) { - return task_under_cgroup_hierarchy(task, ancestor); + long ret; + + rcu_read_lock(); + ret = task_under_cgroup_hierarchy(task, ancestor); + rcu_read_unlock(); + return ret; } #endif /* CONFIG_CGROUPS */ From patchwork Sat Oct 7 13:59:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412352 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B895026E32 for ; Sat, 7 Oct 2023 14:00:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L/YROvrl" Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98480BC for ; Sat, 7 Oct 2023 07:00:07 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-690b7cb71aeso2361890b3a.0 for ; Sat, 07 Oct 2023 07:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687202; x=1697292002; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lhv3VG9dJ365KYTBiiu8UfDv4XisyKaCg1gY3xuMN5s=; b=L/YROvrlQIrhrx3ojgGHn2zbnwBj/gViKcCXT7TFmym7IaXFTwiWunZo58X6qU1XCH Jq8q2btkQDIXf8R9Zg6FnVYqtlWIoJevlMn14Vb/Rqdhxk2PTmiQZhRMcWA92yu9lFjg s8qxcZDjUwbneDVP6I81HiY9WhCXon2bnxKTO0+tuaS+kNby4inPkrsBgANov6u5fpyA CHycwGv2OjuySvoMuFG8bORpprUhYlBss1zPs5StkqVJMVGqr4GomudNi8wHDP0YhE7D LUPVJK/6GhLIe88NSGkGb5qOdln4SGNcQ8vzFRL5luPvJ5SgzzSATY3+Y6BFbsnbiJWr Meqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687202; x=1697292002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lhv3VG9dJ365KYTBiiu8UfDv4XisyKaCg1gY3xuMN5s=; b=XANFsLisbOL8nJi/eoSA5qCxVSTzQJu9YvYoKz+JpAtTw7zbDkM2deDR6J83qge2di 4ikHe8VSWRGXHIr/MlR8ZPvcuIwFl4dBrXn1cijqEaOLOf7R/V5SYIRkfYaesQw6c6k8 kwTtx1NXpHWexX+HOckQygo38o/QM/HQahkhcwz36qbNIfYGrb3Q3ACG0IwL6eFSHVnd irmuVZX8w4H2Uqnsfyw7l8vm2chX9yp95g5SbNosCTr3p//4peZ+/4LRPTxfPk0m1dKf 4xKJL1EFd8oeQs1YL/OxwdRgz5eaIdYqWU4/yoqtd7QmHHUeZnth4ND6mE7TNTER3GG5 0HLw== X-Gm-Message-State: AOJu0YzD8rMEZYBzfJgoAw4BFUjtB+9bZrgss95wytAcNO3OG61W4tJZ UqXneHc2ZGEp8X9Bv2RPVSQ= X-Google-Smtp-Source: AGHT+IG14ps+2CM6ZBmwVgvbMD0kOXTQX0SwBiwQop3gmHxKBSdbf993ITRO+DiqqDn+eBQ4tQFlrw== X-Received: by 2002:a05:6a00:2d1e:b0:68f:bb16:d16a with SMTP id fa30-20020a056a002d1e00b0068fbb16d16amr13625770pfb.5.1696687201925; Sat, 07 Oct 2023 07:00:01 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id i23-20020aa787d7000000b00682a908949bsm3302978pfo.92.2023.10.07.07.00.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:00:01 -0700 (PDT) From: Yafang Shao To: 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, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Add selftest for bpf_task_under_cgroup() in sleepable prog Date: Sat, 7 Oct 2023 13:59:45 +0000 Message-Id: <20231007135945.4306-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007135945.4306-1-laoar.shao@gmail.com> References: <20231007135945.4306-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net The result as follows, $ tools/testing/selftests/bpf/test_progs --name=task_under_cgroup #237 task_under_cgroup:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Without the prev patch, there will be RCU warnings in dmesg when CONFIG_PROVE_RCU is enabled. While with prev patch, there will be no warnings. Signed-off-by: Yafang Shao Acked-by: Stanislav Fomichev --- .../bpf/prog_tests/task_under_cgroup.c | 11 ++++++-- .../bpf/progs/test_task_under_cgroup.c | 28 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) --- v1 -> v2: Add comments on the attachment (Stanislav) diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c index 4224727fb364..626d76fe43a2 100644 --- a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c @@ -30,8 +30,15 @@ void test_task_under_cgroup(void) if (!ASSERT_OK(ret, "test_task_under_cgroup__load")) goto cleanup; - ret = test_task_under_cgroup__attach(skel); - if (!ASSERT_OK(ret, "test_task_under_cgroup__attach")) + /* First, attach the LSM program, and then it will be triggered when the + * TP_BTF program is attached. + */ + skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run); + if (!ASSERT_OK_PTR(skel->links.lsm_run, "attach_lsm")) + goto cleanup; + + skel->links.tp_btf_run = bpf_program__attach_trace(skel->progs.tp_btf_run); + if (!ASSERT_OK_PTR(skel->links.tp_btf_run, "attach_tp_btf")) goto cleanup; pid = fork(); diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c index 56cdc0a553f0..7e750309ce27 100644 --- a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c @@ -18,7 +18,7 @@ const volatile __u64 cgid; int remote_pid; SEC("tp_btf/task_newtask") -int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags) +int BPF_PROG(tp_btf_run, struct task_struct *task, u64 clone_flags) { struct cgroup *cgrp = NULL; struct task_struct *acquired; @@ -48,4 +48,30 @@ int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags) return 0; } +SEC("lsm.s/bpf") +int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + struct cgroup *cgrp = NULL; + struct task_struct *task; + int ret = 0; + + task = bpf_get_current_task_btf(); + if (local_pid != task->pid) + return 0; + + if (cmd != BPF_LINK_CREATE) + return 0; + + /* 1 is the root cgroup */ + cgrp = bpf_cgroup_from_id(1); + if (!cgrp) + goto out; + if (!bpf_task_under_cgroup(task, cgrp)) + ret = -1; + bpf_cgroup_release(cgrp); + +out: + return ret; +} + char _license[] SEC("license") = "GPL";