Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: Sanghyun Park <sanghyun.park.cnu@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	bpf@vger.kernel.org
Cc: John Fastabend <john.fastabend@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Shuah Khan <shuah@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Pu Lehui <pulehui@huawei.com>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 2/2] selftests/bpf: Cover attach type checks in link update
Date: Mon, 31 Aug 2026 22:50:48 +0800	[thread overview]
Message-ID: <425e1052-5c0b-4c0b-8f99-03d90c096ba5@linux.dev> (raw)
In-Reply-To: <20260831023918.1857658-2-sanghyun.park.cnu@gmail.com>

On 2026/8/31 10:39, Sanghyun Park wrote:
> Add regression coverage for link update compatibility. Verify that a UDP6
> sock_addr program cannot replace a UDP4 program and that an LSM_MAC
> program cannot replace an LSM_CGROUP program.
> 
> Also verify that CGROUP_SKB attachment still requires CAP_NET_ADMIN, while
> an existing link can be updated after dropping CAP_NET_ADMIN and
> CAP_SYS_ADMIN.
> 
> Signed-off-by: Sanghyun Park <sanghyun.park.cnu@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/cgroup_link.c    | 51 +++++++++++++++++++
>  .../selftests/bpf/prog_tests/lsm_cgroup.c     | 10 ++++
>  .../testing/selftests/bpf/progs/lsm_cgroup.c  |  6 +++
>  .../selftests/bpf/progs/test_cgroup_link.c    | 14 ++++-
>  4 files changed, 80 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
> index 15093a69510e..b3219314596e 100644
> --- a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
> +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
>  #include <test_progs.h>
> +#include "cap_helpers.h"
>  #include "cgroup_helpers.h"
>  #include "testing_helpers.h"
>  #include "test_cgroup_link.skel.h"
> @@ -24,6 +25,55 @@ int ping_and_check(int exp_calls, int exp_alt_calls)
>  	return 0;
>  }
>  
> +static void test_cgroup_link_update(int cg_fd)
> +{
> +	const __u64 caps = (1ULL << CAP_NET_ADMIN) | (1ULL << CAP_SYS_ADMIN);
> +	struct bpf_link *link = NULL;
> +	__u64 saved_caps = 0;
> +	int err;
> +
> +	link = bpf_program__attach_cgroup(skel->progs.sendmsg4, cg_fd);
> +	if (!ASSERT_OK_PTR(link, "attach_sendmsg4"))
> +		goto cleanup;
> +
> +	err = bpf_link__update_program(link, skel->progs.sendmsg6);
> +	ASSERT_EQ(err, -EINVAL, "reject_sendmsg6_update");
> +	bpf_link__destroy(link);
> +	link = NULL;
> +
> +	err = cap_disable_effective(caps, &saved_caps);
> +	if (!ASSERT_OK(err, "drop_caps_for_attach"))
> +		goto cleanup;
> +
> +	link = bpf_program__attach_cgroup(skel->progs.egress, cg_fd);
> +	if (!ASSERT_ERR_PTR(link, "attach_without_net_admin"))
> +		goto cleanup;
> +	err = libbpf_get_error(link);
> +	link = NULL;
> +	if (!ASSERT_EQ(err, -EPERM, "attach_err"))
> +		goto cleanup;
> +
> +	err = cap_enable_effective(saved_caps & caps, NULL);
> +	if (!ASSERT_OK(err, "restore_caps_for_attach"))
> +		goto cleanup;
> +
> +	link = bpf_program__attach_cgroup(skel->progs.egress, cg_fd);
> +	if (!ASSERT_OK_PTR(link, "attach_egress"))
> +		goto cleanup;
> +
> +	err = cap_disable_effective(caps, &saved_caps);
> +	if (!ASSERT_OK(err, "drop_caps"))
> +		goto cleanup;
> +
> +	err = bpf_link__update_program(link, skel->progs.egress_alt);
> +	ASSERT_OK(err, "update_without_net_admin");
> +
> +cleanup:
> +	err = cap_enable_effective(saved_caps & caps, NULL);
> +	ASSERT_OK(err, "restore_caps");
> +	bpf_link__destroy(link);
> +}
> +
>  void serial_test_cgroup_link(void)
>  {
>  	struct {
> @@ -61,6 +111,7 @@ void serial_test_cgroup_link(void)
>  	err = join_cgroup(cgs[last_cg].path);
>  	if (CHECK(err, "cg_join", "fail: %d\n", err))
>  		goto cleanup;
> +	test_cgroup_link_update(cgs[last_cg].fd);

imo, it is not a good idea to embed test_cgroup_link_update() here.

Better to add it as a subtest with its own env setup/cleanup:

static void test_cgroup_link_attach(void)
{
	/* previous serial_test_cgroup_link() function body */
}

static void test_cgroup_link_update(void)
{
	/* setup its own skel and cg env */

	/* test link_update */

	/* cleanup the skel and cg env */
}

void serial_test_cgroup_link(void)
{
	if (test__start_subtest("attach"))
		test_cgroup_link_attach();
	if (test__start_subtest("update"))
		test_cgroup_link_update();
}

>  
>  	for (i = 0; i < cg_nr; i++) {
>  		links[i] = bpf_program__attach_cgroup(skel->progs.egress,
> diff --git a/tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c b/tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c
> index 41e867467f6c..85b0af4f486a 100644
> --- a/tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c
> @@ -74,6 +74,7 @@ static void test_lsm_cgroup_functional(void)
>  	int bind_prog_fd = -1;
>  	int bind_link_fd = -1;
>  	int clone_prog_fd = -1;
> +	int mac_prog_fd;
>  	int err, fd, prio;
>  	socklen_t socklen;
>  
> @@ -156,6 +157,15 @@ static void test_lsm_cgroup_functional(void)
>  	ASSERT_EQ(query_prog_cnt(cgroup_fd, "bpf_lsm_socket_bind"), 1, "prog count");
>  	ASSERT_EQ(query_prog_cnt(cgroup_fd, NULL), 4, "total prog count");
>  
> +	fd = bpf_link_create(bpf_program__fd(skel->progs.socket_first),
> +			     cgroup_fd, BPF_LSM_CGROUP, NULL);
> +	if (!ASSERT_GE(fd, 0, "link create socket_first"))
> +		goto detach_cgroup;
> +	mac_prog_fd = bpf_program__fd(skel->progs.socket_create_lsm);
> +	err = bpf_link_update(fd, mac_prog_fd, NULL);

Rewrite to bpf_link_update(fd,
bpf_program__fd(skel->progs.socket_create_lsm), NULL).

Then, the 'mac_prog_fd' can be dropped.

Thanks,
Leon

> +	ASSERT_EQ(err, -EINVAL, "reject lsm_mac link update");> +	close(fd);
> [...]


  parent reply	other threads:[~2026-08-31 14:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31  2:39 [PATCH bpf-next v4 1/2] bpf: Validate program attach type during link update Sanghyun Park
2026-08-31  2:39 ` [PATCH bpf-next v4 2/2] selftests/bpf: Cover attach type checks in " Sanghyun Park
2026-08-31  3:49   ` bot+bpf-ci
2026-08-31 14:50   ` Leon Hwang [this message]
2026-08-31 14:48 ` [PATCH bpf-next v4 1/2] bpf: Validate program attach type during " Leon Hwang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=425e1052-5c0b-4c0b-8f99-03d90c096ba5@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=pulehui@huawei.com \
    --cc=sanghyun.park.cnu@gmail.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox