linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/seccomp: More closely track fds being assigned
@ 2021-05-27  3:29 Kees Cook
  2021-05-27 12:45 ` Rodrigo Campos
  2021-05-27 13:06 ` Christian Brauner
  0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2021-05-27  3:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, Sargun Dhillon, containers, Tycho Andersen,
	Rodrigo Campos, Mauricio Vásquez Bernal, Giuseppe Scrivano,
	Christian Brauner, Mickaël Salaün, Andy Lutomirski,
	Will Drewry

Since the open fds might not always start at "4" (especially when
running under kselftest, etc), start counting from the first assigned
fd, rather than using the more permissive EXPECT_GE(fd, 0).

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
index e2ba7adc2694..03b37e660965 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -3954,7 +3954,7 @@ TEST(user_notification_addfd)
 {
 	pid_t pid;
 	long ret;
-	int status, listener, memfd, fd;
+	int status, listener, memfd, fd, nextfd;
 	struct seccomp_notif_addfd addfd = {};
 	struct seccomp_notif_addfd_small small = {};
 	struct seccomp_notif_addfd_big big = {};
@@ -3963,18 +3963,21 @@ TEST(user_notification_addfd)
 	/* 100 ms */
 	struct timespec delay = { .tv_nsec = 100000000 };
 
+	/* There may be arbitrary already-open fds at test start. */
 	memfd = memfd_create("test", 0);
 	ASSERT_GE(memfd, 0);
+	nextfd = memfd + 1;
 
 	ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
 	ASSERT_EQ(0, ret) {
 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
 	}
 
+	/* fd: 4 */
 	/* Check that the basic notification machinery works */
 	listener = user_notif_syscall(__NR_getppid,
 				      SECCOMP_FILTER_FLAG_NEW_LISTENER);
-	ASSERT_GE(listener, 0);
+	ASSERT_EQ(listener, nextfd++);
 
 	pid = fork();
 	ASSERT_GE(pid, 0);
@@ -4029,14 +4032,14 @@ TEST(user_notification_addfd)
 
 	/* Verify we can set an arbitrary remote fd */
 	fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
-	EXPECT_GE(fd, 0);
+	EXPECT_EQ(fd, nextfd++);
 	EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
 
 	/* Verify we can set an arbitrary remote fd with large size */
 	memset(&big, 0x0, sizeof(big));
 	big.addfd = addfd;
 	fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big);
-	EXPECT_GE(fd, 0);
+	EXPECT_EQ(fd, nextfd++);
 
 	/* Verify we can set a specific remote fd */
 	addfd.newfd = 42;
@@ -4070,9 +4073,11 @@ TEST(user_notification_addfd)
 	addfd.newfd = 0;
 	addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
 	fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
-
-	/* Child has fds 0-6 and 42 used, we expect the lower fd available: 7 */
-	EXPECT_EQ(fd, 7);
+	/*
+	 * Child has earlier "low" fds and now 42, so we expect the next
+	 * lowest available fd to be assigned here.
+	 */
+	EXPECT_EQ(fd, nextfd++);
 	EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
 
 	/*
-- 
2.25.1


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

* Re: [PATCH] selftests/seccomp: More closely track fds being assigned
  2021-05-27  3:29 [PATCH] selftests/seccomp: More closely track fds being assigned Kees Cook
@ 2021-05-27 12:45 ` Rodrigo Campos
  2021-05-27 18:37   ` Kees Cook
  2021-05-27 13:06 ` Christian Brauner
  1 sibling, 1 reply; 4+ messages in thread
From: Rodrigo Campos @ 2021-05-27 12:45 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, Sargun Dhillon, containers, Tycho Andersen,
	Mauricio Vásquez Bernal, Giuseppe Scrivano,
	Christian Brauner, Mickaël Salaün, Andy Lutomirski,
	Will Drewry

On Thu, May 27, 2021 at 5:29 AM Kees Cook <keescook@chromium.org> wrote:
>
> Since the open fds might not always start at "4" (especially when
> running under kselftest, etc), start counting from the first assigned
> fd, rather than using the more permissive EXPECT_GE(fd, 0).
>
> Signed-off-by: Kees Cook <keescook@chromium.org>

Nice cleanup, thanks! Just in case, tested it here, works fine. Feel
free to add:

Reviewed-by: Rodrigo Campos <rodrigo@kinvolk.io>

I can improve the selftest to test the new addfd flag we just added
also in combination existing flags (like setting the fd number to
use), and maybe also split the big chunk test, if you think that is
valuable.

Best,
Rodrigo

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

* Re: [PATCH] selftests/seccomp: More closely track fds being assigned
  2021-05-27  3:29 [PATCH] selftests/seccomp: More closely track fds being assigned Kees Cook
  2021-05-27 12:45 ` Rodrigo Campos
@ 2021-05-27 13:06 ` Christian Brauner
  1 sibling, 0 replies; 4+ messages in thread
From: Christian Brauner @ 2021-05-27 13:06 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, Sargun Dhillon, containers, Tycho Andersen,
	Rodrigo Campos, Mauricio Vásquez Bernal, Giuseppe Scrivano,
	Mickaël Salaün, Andy Lutomirski, Will Drewry

On Wed, May 26, 2021 at 08:29:48PM -0700, Kees Cook wrote:
> Since the open fds might not always start at "4" (especially when
> running under kselftest, etc), start counting from the first assigned
> fd, rather than using the more permissive EXPECT_GE(fd, 0).
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---

Looks good,
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>

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

* Re: [PATCH] selftests/seccomp: More closely track fds being assigned
  2021-05-27 12:45 ` Rodrigo Campos
@ 2021-05-27 18:37   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2021-05-27 18:37 UTC (permalink / raw)
  To: Rodrigo Campos
  Cc: LKML, Sargun Dhillon, containers, Tycho Andersen,
	Mauricio Vásquez Bernal, Giuseppe Scrivano,
	Christian Brauner, Mickaël Salaün, Andy Lutomirski,
	Will Drewry

On Thu, May 27, 2021 at 02:45:26PM +0200, Rodrigo Campos wrote:
> On Thu, May 27, 2021 at 5:29 AM Kees Cook <keescook@chromium.org> wrote:
> >
> > Since the open fds might not always start at "4" (especially when
> > running under kselftest, etc), start counting from the first assigned
> > fd, rather than using the more permissive EXPECT_GE(fd, 0).
> >
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> 
> Nice cleanup, thanks! Just in case, tested it here, works fine. Feel
> free to add:
> 
> Reviewed-by: Rodrigo Campos <rodrigo@kinvolk.io>

Thanks!

> I can improve the selftest to test the new addfd flag we just added
> also in combination existing flags (like setting the fd number to
> use), and maybe also split the big chunk test, if you think that is
> valuable.

Yeah, I was pondering splitting the test up, but I think it's okay how
it is for now.

-- 
Kees Cook

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

end of thread, other threads:[~2021-05-27 18:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27  3:29 [PATCH] selftests/seccomp: More closely track fds being assigned Kees Cook
2021-05-27 12:45 ` Rodrigo Campos
2021-05-27 18:37   ` Kees Cook
2021-05-27 13:06 ` Christian Brauner

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).