linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/seccomp: Check CAP_SYS_ADMIN capability in the test mode_filter_without_nnp
@ 2022-07-31  9:25 Gautam Menghani
  2022-08-22  8:49 ` Gautam Menghani
  0 siblings, 1 reply; 2+ messages in thread
From: Gautam Menghani @ 2022-07-31  9:25 UTC (permalink / raw)
  To: keescook, shuah, brauner, paul.walmsley, palmer, aou, guoren
  Cc: Gautam Menghani, luto, wad, linux-kselftest, linux-kernel,
	linux-riscv, bpf, linux-csky, linux-kernel-mentees

In the "mode_filter_without_nnp" test in seccomp_bpf, there is currently
a TODO which asks to check the capability CAP_SYS_ADMIN instead of euid.
This patch adds support to check if the calling process has the flag 
CAP_SYS_ADMIN, and also if this flag has CAP_EFFECTIVE set.

Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 136df5b76319..16b0edc520ef 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -392,6 +392,8 @@ TEST(mode_filter_without_nnp)
 		.filter = filter,
 	};
 	long ret;
+	cap_t cap = cap_get_proc();
+	cap_flag_value_t is_cap_sys_admin = 0;
 
 	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0);
 	ASSERT_LE(0, ret) {
@@ -400,8 +402,8 @@ TEST(mode_filter_without_nnp)
 	errno = 0;
 	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
 	/* Succeeds with CAP_SYS_ADMIN, fails without */
-	/* TODO(wad) check caps not euid */
-	if (geteuid()) {
+	cap_get_flag(cap, CAP_SYS_ADMIN, CAP_EFFECTIVE, &is_cap_sys_admin);
+	if (!is_cap_sys_admin) {
 		EXPECT_EQ(-1, ret);
 		EXPECT_EQ(EACCES, errno);
 	} else {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] selftests/seccomp: Check CAP_SYS_ADMIN capability in the test mode_filter_without_nnp
  2022-07-31  9:25 [PATCH] selftests/seccomp: Check CAP_SYS_ADMIN capability in the test mode_filter_without_nnp Gautam Menghani
@ 2022-08-22  8:49 ` Gautam Menghani
  0 siblings, 0 replies; 2+ messages in thread
From: Gautam Menghani @ 2022-08-22  8:49 UTC (permalink / raw)
  To: keescook, shuah, brauner, paul.walmsley, palmer, aou, guoren
  Cc: luto, wad, linux-kselftest, linux-kernel, linux-riscv, bpf,
	linux-csky, linux-kernel-mentees

On Sun, Jul 31, 2022 at 02:55:29PM +0530, Gautam Menghani wrote:
> In the "mode_filter_without_nnp" test in seccomp_bpf, there is currently
> a TODO which asks to check the capability CAP_SYS_ADMIN instead of euid.
> This patch adds support to check if the calling process has the flag 
> CAP_SYS_ADMIN, and also if this flag has CAP_EFFECTIVE set.
> 
> Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> ---
>  tools/testing/selftests/seccomp/seccomp_bpf.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
> index 136df5b76319..16b0edc520ef 100644
> --- a/tools/testing/selftests/seccomp/seccomp_bpf.c
> +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
> @@ -392,6 +392,8 @@ TEST(mode_filter_without_nnp)
>  		.filter = filter,
>  	};
>  	long ret;
> +	cap_t cap = cap_get_proc();
> +	cap_flag_value_t is_cap_sys_admin = 0;
>  
>  	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0);
>  	ASSERT_LE(0, ret) {
> @@ -400,8 +402,8 @@ TEST(mode_filter_without_nnp)
>  	errno = 0;
>  	ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
>  	/* Succeeds with CAP_SYS_ADMIN, fails without */
> -	/* TODO(wad) check caps not euid */
> -	if (geteuid()) {
> +	cap_get_flag(cap, CAP_SYS_ADMIN, CAP_EFFECTIVE, &is_cap_sys_admin);
> +	if (!is_cap_sys_admin) {
>  		EXPECT_EQ(-1, ret);
>  		EXPECT_EQ(EACCES, errno);
>  	} else {
> -- 
> 2.34.1
> 

Hi,

Please review the above patch and let me know if any changes are required.

Thanks,
Gautam

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-22  8:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-31  9:25 [PATCH] selftests/seccomp: Check CAP_SYS_ADMIN capability in the test mode_filter_without_nnp Gautam Menghani
2022-08-22  8:49 ` Gautam Menghani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).