From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE73E2206BD; Mon, 10 Mar 2025 18:11:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741630303; cv=none; b=CyLP1WCK0soZm3sdoZZ8ykAAjMZglxkmRYeAgRbsku3QA6Pkz5JtqGI3eWqQ7VNfsJ2NaJDzPLhSn/E8k5VEtNhzzPbwO6ElcoSiZWII7WLRDnFnXC+07ZvggFziOAXo0VoJ+fq0xUDMaHEAIwfUl4goIAs0S5EeFM6keugWzfc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741630303; c=relaxed/simple; bh=3OxdgUDWK+uFLVvzr/y6KkONgmK1n3ccKZ3Of6DC9m0=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=hd39uvzUUl4JDdMRJ2LhhhTRbADTtEKyiIOG/gHw50L1T7OSL2gznYw1nrHbV9A4n+rg4I9yO4ZUcfV6nxKWb96RlDMhpPvclCDFvq9IBb91Vy62ZBQec+aqZZtuWtkHvIfozvlj1gSnr5NLXui9RBgIYTUsyFvolcYNhglI3Zo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=niqcbDyx; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="niqcbDyx" Received: from narnia (unknown [167.220.2.28]) by linux.microsoft.com (Postfix) with ESMTPSA id 190DE2038F4A; Mon, 10 Mar 2025 11:11:34 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 190DE2038F4A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1741630301; bh=AObcEA8sw67rEti9TZTsNnLV8M/alL5bqjj3P/yE9RI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=niqcbDyxUZZ/tP9cDiXJs0XJDKX5e26H6fSyRgd4xDz4LhAu/nCfDMmMrGrtdhpD0 3HyvjFSfj4oA8UjFbzHxQYph+SQ2wATqpqEdGi/FQA40wlk4ZfT6KCcfxqMDYfenz1 7hlBqekKb40V6BRdyHMtCijhgfJBuZLX6pLApyeM= From: Blaise Boscaccy To: Song Liu Cc: Paul Moore , James Morris , "Serge E. Hallyn" , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Stephen Smalley , Ondrej Mosnacek , Mykola Lysenko , Shuah Khan , Kumar Kartikeya Dwivedi , Matt Bobrowski , Xu Kuohai , linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, bpf@vger.kernel.org, selinux@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v6 bpf-next 2/2] selftests/bpf: Add a kernel flag test for LSM bpf hook In-Reply-To: References: <20250308013314.719150-1-bboscaccy@linux.microsoft.com> <20250308013314.719150-3-bboscaccy@linux.microsoft.com> <87v7sgye6h.fsf@microsoft.com> Date: Mon, 10 Mar 2025 11:11:32 -0700 Message-ID: <87senkycvf.fsf@microsoft.com> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Song Liu writes: > On Mon, Mar 10, 2025 at 10:43=E2=80=AFAM Blaise Boscaccy > wrote: >> >> Song Liu writes: >> >> > On Fri, Mar 7, 2025 at 5:33=E2=80=AFPM Blaise Boscaccy >> > wrote: >> >> >> >> This test exercises the kernel flag added to security_bpf by >> >> effectively blocking light-skeletons from loading while allowing >> >> normal skeletons to function as-is. Since this should work with any >> >> arbitrary BPF program, an existing program from LSKELS_EXTRA was >> >> used as a test payload. >> >> >> >> Signed-off-by: Blaise Boscaccy >> >> --- >> >> .../selftests/bpf/prog_tests/kernel_flag.c | 43 +++++++++++++++++= ++ >> >> .../selftests/bpf/progs/test_kernel_flag.c | 28 ++++++++++++ >> >> 2 files changed, 71 insertions(+) >> >> create mode 100644 tools/testing/selftests/bpf/prog_tests/kernel_fla= g.c >> >> create mode 100644 tools/testing/selftests/bpf/progs/test_kernel_fla= g.c >> >> >> >> diff --git a/tools/testing/selftests/bpf/prog_tests/kernel_flag.c b/t= ools/testing/selftests/bpf/prog_tests/kernel_flag.c >> >> new file mode 100644 >> >> index 0000000000000..479ad5de3737e >> >> --- /dev/null >> >> +++ b/tools/testing/selftests/bpf/prog_tests/kernel_flag.c >> >> @@ -0,0 +1,43 @@ >> >> +// SPDX-License-Identifier: GPL-2.0 >> >> +/* Copyright (c) 2025 Microsoft */ >> >> +#include >> >> +#include "kfunc_call_test.skel.h" >> >> +#include "kfunc_call_test.lskel.h" >> >> +#include "test_kernel_flag.skel.h" >> >> + >> >> +void test_kernel_flag(void) >> >> +{ >> >> + struct test_kernel_flag *lsm_skel; >> >> + struct kfunc_call_test *skel =3D NULL; >> >> + struct kfunc_call_test_lskel *lskel =3D NULL; >> >> + int ret; >> >> + >> >> + lsm_skel =3D test_kernel_flag__open_and_load(); >> >> + if (!ASSERT_OK_PTR(lsm_skel, "lsm_skel")) >> >> + return; >> >> + >> >> + ret =3D test_kernel_flag__attach(lsm_skel); >> >> + if (!ASSERT_OK(ret, "test_kernel_flag__attach")) >> >> + goto close_prog; >> >> + >> >> + lsm_skel->bss->monitored_pid =3D getpid(); >> > >> > We usually set monitored_pid before attaching the program. >> > >> >> Okay, copy that. >> >> >> + >> >> + /* Test with skel. This should pass the gatekeeper */ >> >> + skel =3D kfunc_call_test__open_and_load(); >> >> + if (!ASSERT_OK_PTR(skel, "skel")) >> >> + goto close_prog; >> >> + >> >> + /* Test with lskel. This should fail due to blocking kernel-b= ased bpf() invocations */ >> >> + lskel =3D kfunc_call_test_lskel__open_and_load(); >> >> + if (!ASSERT_ERR_PTR(lskel, "lskel")) >> >> + goto close_prog; >> >> + >> >> +close_prog: >> >> + if (skel) >> >> + kfunc_call_test__destroy(skel); >> >> + if (lskel) >> >> + kfunc_call_test_lskel__destroy(lskel); >> >> + >> >> + lsm_skel->bss->monitored_pid =3D 0; >> >> + test_kernel_flag__destroy(lsm_skel); >> >> +} >> >> diff --git a/tools/testing/selftests/bpf/progs/test_kernel_flag.c b/t= ools/testing/selftests/bpf/progs/test_kernel_flag.c >> >> new file mode 100644 >> >> index 0000000000000..9ca01aadb6656 >> >> --- /dev/null >> >> +++ b/tools/testing/selftests/bpf/progs/test_kernel_flag.c >> >> @@ -0,0 +1,28 @@ >> >> +// SPDX-License-Identifier: GPL-2.0 >> >> + >> >> +/* >> >> + * Copyright (C) 2025 Microsoft Corporation >> >> + * >> >> + * Author: Blaise Boscaccy >> >> + */ >> >> + >> >> +#include "vmlinux.h" >> >> +#include >> >> +#include >> >> +#include >> >> + >> >> +char _license[] SEC("license") =3D "GPL"; >> >> + >> >> +__u32 monitored_pid; >> >> + >> >> +SEC("lsm.s/bpf") >> >> +int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size, = bool kernel) >> >> +{ >> >> + __u32 pid; >> >> + >> >> + pid =3D bpf_get_current_pid_tgid() >> 32; >> >> + if (!kernel || pid !=3D monitored_pid) >> >> + return 0; >> > >> > We are blocking lskel load for the pid. This could make >> > parallel testing (test_progs -j) flaky. We should probably >> > change the logic to filtering on monitored_tiid. >> > >> >> Curious on this for my own edification. The >> >> pid =3D bpf_get_current_pid_tgid() >> 32; >> >> is used extensively in the current test suite in a bunch of other >> tests. Why does that not cause an issue with the other tests during >> parallel testing? > > We are blindly blocking all security_bpf() with kernel=3Dtrue here, so > any lskel load in parallel with this test may fail. On the other hand, > existing tests only block some operations under certain conditions. > For example, test_cgroup1_hierarchy.c only blocks operations for > target_ancestor_cgid. > > Does this make sense? > Not quite. This is only blocking security_bpf where kernel=3Dtrue and pid=3Dmonitored_pid.=20 > Thanks, > Song