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 C302622DFB1; Mon, 10 Mar 2025 17:43:28 +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=1741628610; cv=none; b=Fe6rDyhHaiuKZmFsW3TiKJyhyWlOE2N3Mi921rt+SiCmCP+nuGf9jwizqcOSsVXKkSut9TP/0nTc5NNrXB0AeqqykWr2mcl6Mx+RD/4m+hQRbbYp9y1qTSNyDyjEwJwGyf2Ah23vmTFoEcMLMu/ThH+ZlemEI0vqWI8ppbHDzbw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741628610; c=relaxed/simple; bh=nVu5HN6r55xWpWlJj8oBTLsoBZcAk38gbF4FNnZ7ZFc=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=U97sVm/a9FmA2SQBsj/6QAga8tzz3B5DXdKybbxB4IzNFUic82Kwka+4BBG4InUBNjqwyBe6v0oZ9On3Dd0bWTo65sQU1dz+lws78cRs/MBF8d7eqPWZRLDt9peSG0JHuwPYf6t72dNhUpdgYtouv5y9CsYVpTVXbiooiJVXnsc= 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=V3IjL0ca; 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="V3IjL0ca" Received: from narnia (unknown [167.220.2.28]) by linux.microsoft.com (Postfix) with ESMTPSA id A18EC2038F31; Mon, 10 Mar 2025 10:43:20 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A18EC2038F31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1741628608; bh=ggsx2Gq6zrbtpEOFlIbtQA76waAnu6lidjNvvMFeczU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=V3IjL0caWZjWbSBABQ34XNSIVJ7jmgbqqeoGaUPq04B6WbaFpIiLfHxOqBFezDIfq kqbjHVvQ2XI9oiVBOf+z0OZIcozNsk4VOj8LjvlXx9qklkgvetClV3iYXgNoYDkS0K FYtHdXFstUZI/9BUpaItT9ACuyGIqsTfktYx7uWc= 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> Date: Mon, 10 Mar 2025 10:43:18 -0700 Message-ID: <87v7sgye6h.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 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_flag.c >> create mode 100644 tools/testing/selftests/bpf/progs/test_kernel_flag.c >> >> diff --git a/tools/testing/selftests/bpf/prog_tests/kernel_flag.c b/tool= s/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.=20 >> + >> + /* 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-base= d 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/tool= s/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, boo= l 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?=20 > Thanks, > Song > >> + else >> + return -EINVAL; >> +} >> -- >> 2.48.1 >> >>