From nobody Mon Oct 6 16:54:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0CA57204866; Sun, 20 Jul 2025 11:25:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753010740; cv=none; b=fQ/E7ZppS54WS4V4TSuW4RJGKeqcCeraLJWcXLeFp/4gD1xwnEbgDKGGd2ysYoff7gLlbRLxp1ORC7W15Wf67lnJYhzu9/apwx48i4AW+JQn3FYaTeJhjXToVhMnNfHSpq3EGJZQ2Qt9eiz6gWm/uUPFcJzyxg7NCTwllap4Y00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753010740; c=relaxed/simple; bh=ivjFKwQACAGTKpsYMbShtvns3ViBFuL3QShhOxBiUpM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I8ysO2fHrcfyvUbBgwBtzN6LT9lxL5ROSymnBzVRqroqyVOxbSee0UqBxdLPrsnGBMFJok93NBsY2/xBrA0+lU/PoS/QyHLyvyJxENuh/S+96+M9iciqCr0Z4uCE/UQeu9TwKB/i/B22g3qnuncWQehEz1pkFrt/HmYntvqRn74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iE4HkYhC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iE4HkYhC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07BBAC4CEE7; Sun, 20 Jul 2025 11:25:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753010739; bh=ivjFKwQACAGTKpsYMbShtvns3ViBFuL3QShhOxBiUpM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iE4HkYhCgloSPxbs3pP3MBt3alrWfgRqL9FWdcTeOSXi3glUE+1xa5xl71DUbflBM C9cJXVCydHeO+TXvPBznWkaT22qtr4c5ImQX/+nE5y/mtlc5kHkMOxbgT6v3zKMor0 eDSG3g1aWs2GI+jF4ECPh/JXv+7R7mub8mt62cJVHQM55TKTuCdSkZe9sV6Ao7Hmv5 H9nap/fW9V35Q7IELA/1DEQk6UVYrJWZGGn6A8BPoB7sNsQci/dBxp4wmhvlNlxyxC 0heTWcJSZFoQ5NlJQfnrcsAEzO2wYhE/uq0cDXoNyqOexgqDNTkR3QCU8l9k+BMy5s 9AYnup2IRWyag== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Andrii Nakryiko Cc: Kees Cook , Eyal Birger , Kees Cook , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Masami Hiramatsu , Alan Maguire , David Laight , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Ingo Molnar Subject: [PATCHv6 perf/core 21/22] selftests/seccomp: validate uprobe syscall passes through seccomp Date: Sun, 20 Jul 2025 13:21:31 +0200 Message-ID: <20250720112133.244369-22-jolsa@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250720112133.244369-1-jolsa@kernel.org> References: <20250720112133.244369-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adding uprobe checks into the current uretprobe tests. All the related tests are now executed with attached uprobe or uretprobe or without any probe. Renaming the test fixture to uprobe, because it seems better. Cc: Kees Cook Cc: Eyal Birger Reviewed-by: Kees Cook Signed-off-by: Jiri Olsa --- tools/testing/selftests/seccomp/seccomp_bpf.c | 107 ++++++++++++++---- 1 file changed, 86 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/= selftests/seccomp/seccomp_bpf.c index 61acbd45ffaa..2cf6fc825d86 100644 --- a/tools/testing/selftests/seccomp/seccomp_bpf.c +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c @@ -73,6 +73,14 @@ #define noinline __attribute__((noinline)) #endif =20 +#ifndef __nocf_check +#define __nocf_check __attribute__((nocf_check)) +#endif + +#ifndef __naked +#define __naked __attribute__((__naked__)) +#endif + #ifndef PR_SET_NO_NEW_PRIVS #define PR_SET_NO_NEW_PRIVS 38 #define PR_GET_NO_NEW_PRIVS 39 @@ -4896,7 +4904,36 @@ TEST(tsync_vs_dead_thread_leader) EXPECT_EQ(0, status); } =20 -noinline int probed(void) +#ifdef __x86_64__ + +/* + * We need naked probed_uprobe function. Using __nocf_check + * check to skip possible endbr64 instruction and ignoring + * -Wattributes, otherwise the compilation might fail. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wattributes" + +__naked __nocf_check noinline int probed_uprobe(void) +{ + /* + * Optimized uprobe is possible only on top of nop5 instruction. + */ + asm volatile (" \n" + ".byte 0x0f, 0x1f, 0x44, 0x00, 0x00 \n" + "ret \n" + ); +} +#pragma GCC diagnostic pop + +#else +noinline int probed_uprobe(void) +{ + return 1; +} +#endif + +noinline int probed_uretprobe(void) { return 1; } @@ -4949,35 +4986,46 @@ static ssize_t get_uprobe_offset(const void *addr) return found ? (uintptr_t)addr - start + base : -1; } =20 -FIXTURE(URETPROBE) { +FIXTURE(UPROBE) { int fd; }; =20 -FIXTURE_VARIANT(URETPROBE) { +FIXTURE_VARIANT(UPROBE) { /* - * All of the URETPROBE behaviors can be tested with either - * uretprobe attached or not + * All of the U(RET)PROBE behaviors can be tested with either + * u(ret)probe attached or not */ bool attach; + /* + * Test both uprobe and uretprobe. + */ + bool uretprobe; }; =20 -FIXTURE_VARIANT_ADD(URETPROBE, attached) { +FIXTURE_VARIANT_ADD(UPROBE, not_attached) { + .attach =3D false, + .uretprobe =3D false, +}; + +FIXTURE_VARIANT_ADD(UPROBE, uprobe_attached) { .attach =3D true, + .uretprobe =3D false, }; =20 -FIXTURE_VARIANT_ADD(URETPROBE, not_attached) { - .attach =3D false, +FIXTURE_VARIANT_ADD(UPROBE, uretprobe_attached) { + .attach =3D true, + .uretprobe =3D true, }; =20 -FIXTURE_SETUP(URETPROBE) +FIXTURE_SETUP(UPROBE) { const size_t attr_sz =3D sizeof(struct perf_event_attr); struct perf_event_attr attr; ssize_t offset; int type, bit; =20 -#ifndef __NR_uretprobe - SKIP(return, "__NR_uretprobe syscall not defined"); +#if !defined(__NR_uprobe) || !defined(__NR_uretprobe) + SKIP(return, "__NR_uprobe ot __NR_uretprobe syscalls not defined"); #endif =20 if (!variant->attach) @@ -4987,12 +5035,17 @@ FIXTURE_SETUP(URETPROBE) =20 type =3D determine_uprobe_perf_type(); ASSERT_GE(type, 0); - bit =3D determine_uprobe_retprobe_bit(); - ASSERT_GE(bit, 0); - offset =3D get_uprobe_offset(probed); + + if (variant->uretprobe) { + bit =3D determine_uprobe_retprobe_bit(); + ASSERT_GE(bit, 0); + } + + offset =3D get_uprobe_offset(variant->uretprobe ? probed_uretprobe : prob= ed_uprobe); ASSERT_GE(offset, 0); =20 - attr.config |=3D 1 << bit; + if (variant->uretprobe) + attr.config |=3D 1 << bit; attr.size =3D attr_sz; attr.type =3D type; attr.config1 =3D ptr_to_u64("/proc/self/exe"); @@ -5003,7 +5056,7 @@ FIXTURE_SETUP(URETPROBE) PERF_FLAG_FD_CLOEXEC); } =20 -FIXTURE_TEARDOWN(URETPROBE) +FIXTURE_TEARDOWN(UPROBE) { /* we could call close(self->fd), but we'd need extra filter for * that and since we are calling _exit right away.. @@ -5017,11 +5070,17 @@ static int run_probed_with_filter(struct sock_fprog= *prog) return -1; } =20 - probed(); + /* + * Uprobe is optimized after first hit, so let's hit twice. + */ + probed_uprobe(); + probed_uprobe(); + + probed_uretprobe(); return 0; } =20 -TEST_F(URETPROBE, uretprobe_default_allow) +TEST_F(UPROBE, uprobe_default_allow) { struct sock_filter filter[] =3D { BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), @@ -5034,7 +5093,7 @@ TEST_F(URETPROBE, uretprobe_default_allow) ASSERT_EQ(0, run_probed_with_filter(&prog)); } =20 -TEST_F(URETPROBE, uretprobe_default_block) +TEST_F(UPROBE, uprobe_default_block) { struct sock_filter filter[] =3D { BPF_STMT(BPF_LD|BPF_W|BPF_ABS, @@ -5051,11 +5110,14 @@ TEST_F(URETPROBE, uretprobe_default_block) ASSERT_EQ(0, run_probed_with_filter(&prog)); } =20 -TEST_F(URETPROBE, uretprobe_block_uretprobe_syscall) +TEST_F(UPROBE, uprobe_block_syscall) { struct sock_filter filter[] =3D { BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)), +#ifdef __NR_uprobe + BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_uprobe, 1, 2), +#endif #ifdef __NR_uretprobe BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_uretprobe, 0, 1), #endif @@ -5070,11 +5132,14 @@ TEST_F(URETPROBE, uretprobe_block_uretprobe_syscall) ASSERT_EQ(0, run_probed_with_filter(&prog)); } =20 -TEST_F(URETPROBE, uretprobe_default_block_with_uretprobe_syscall) +TEST_F(UPROBE, uprobe_default_block_with_syscall) { struct sock_filter filter[] =3D { BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)), +#ifdef __NR_uprobe + BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_uprobe, 3, 0), +#endif #ifdef __NR_uretprobe BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_uretprobe, 2, 0), #endif --=20 2.50.1