linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sargun Dhillon <sargun@sargun.me>
To: Kees Cook <keescook@chromium.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Linux Containers <containers@lists.linux-foundation.org>,
	Rodrigo Campos <rodrigo@kinvolk.io>,
	Christian Brauner <christian.brauner@ubuntu.com>,
	Giuseppe Scrivano <gscrivan@redhat.com>,
	Will Drewry <wad@chromium.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Alban Crequy <alban@kinvolk.io>,
	Tycho Andersen <tycho@tycho.pizza>
Subject: Re: [PATCH v3 2/2] selftests/seccomp: Add test for wait killable notifier
Date: Fri, 29 Apr 2022 22:35:57 +0000	[thread overview]
Message-ID: <20220429223557.GB1267404@ircssh-3.c.rugged-nimbus-611.internal> (raw)
In-Reply-To: <202204291053.E04A367@keescook>

On Fri, Apr 29, 2022 at 11:19:33AM -0700, Kees Cook wrote:
> On Thu, Apr 28, 2022 at 07:31:13PM -0700, Sargun Dhillon wrote:
> > +
> > +	ASSERT_EQ(socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair), 0);
> > +
> > +	listener = user_notif_syscall(__NR_getppid,
> > +				      SECCOMP_FILTER_FLAG_NEW_LISTENER |
> > +				      SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV);
> > +	ASSERT_GE(listener, 0);
> > +
> > +	pid = fork();
> > +	ASSERT_GE(pid, 0);
> > +
> > +	if (pid == 0) {
> > +		close(sk_pair[0]);
> > +		handled = sk_pair[1];
> > +
> > +		/* Setup the sigaction without SA_RESTART */
> > +		if (sigaction(SIGUSR1, &new_action, NULL)) {
> > +			perror("sigaction");
> > +			exit(1);
> > +		}
> > +
> > +		/* Make sure that the syscall is completed (no EINTR) */
> > +		ret = syscall(__NR_getppid);
> > +		exit(ret != USER_NOTIF_MAGIC);
> > +	}
> > +
> > +	while (get_proc_syscall(pid) != __NR_getppid &&
> > +	       get_proc_stat(pid) != 'S')
> > +		nanosleep(&delay, NULL);
> > +
> > +	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
> > +	/* Kill the process to make sure it enters the wait_killable state */
> > +	EXPECT_EQ(kill(pid, SIGUSR1), 0);
> > +
> > +	/* TASK_KILLABLE is considered D (Disk Sleep) state */
> > +	while (get_proc_stat(pid) != 'D')
> > +		nanosleep(&delay, NULL);
> 
> Should a NOWAIT waitpid() happen in this loop to make sure this doesn't
> spin forever?
> 
> i.e. running these tests on a kernel that doesn't have the support
> shouldn't hang -- yes it'll time out eventually but that's annoying. ;)
> 
Wouldn't this bail already because user_notif_syscall would assert out
since the kernel would reject the unknown flag?

I might make this a little helper function, something like:
static void wait_for_state(struct __test_metadata *_metadata, pid_t pid, char wait_for) {
	/* 100 ms */
	struct timespec delay = { .tv_nsec = 100000000 };
	int status;

	while (get_proc_stat(pid) != wait_for) {
		ASSERT_EQ(waitpid(pid, &status, WNOHANG), 0) {
			if (WIFEXITED(status))
				TH_LOG("Process %d exited with error code %d", pid, WEXITSTATUS(status));
			else if (WIFSIGNALED(status))
				TH_LOG("Process %d exited due to signal %d", pid, WTERMSIG(status));
			else
				TH_LOG("Process %d exited due to unknown reason", pid);
		}
		nanosleep(&delay, NULL);
	}
}
	
}

> > +
> > +	resp.id = req.id;
> > +	resp.val = USER_NOTIF_MAGIC;
> > +	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0);
> > +
> > +	/*
> > +	 * Make sure that the signal handler does get called once we're back in
> > +	 * userspace.
> > +	 */
> > +	EXPECT_EQ(read(sk_pair[0], &c, 1), 1);
> > +	EXPECT_EQ(waitpid(pid, &status, 0), pid);
> > +	EXPECT_EQ(true, WIFEXITED(status));
> > +	EXPECT_EQ(0, WEXITSTATUS(status));
> > +}
> > +
> > +TEST(user_notification_wait_killable_fatal)
> > +{
> > +	struct seccomp_notif req = {};
> > +	int listener, status;
> > +	pid_t pid;
> > +	long ret;
> > +	/* 100 ms */
> > +	struct timespec delay = { .tv_nsec = 100000000 };
> > +
> > +	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!");
> > +	}
> > +
> > +	listener = user_notif_syscall(__NR_getppid,
> > +				      SECCOMP_FILTER_FLAG_NEW_LISTENER |
> > +				      SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV);
> > +	ASSERT_GE(listener, 0);
> > +
> > +	pid = fork();
> > +	ASSERT_GE(pid, 0);
> > +
> > +	if (pid == 0) {
> > +		/* This should never complete */
> > +		syscall(__NR_getppid);
> > +		exit(1);
> > +	}
> > +
> > +	while (get_proc_stat(pid) != 'S')
> > +		nanosleep(&delay, NULL);
> > +
> > +	EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
> > +	/* Kill the process with a fatal signal */
> > +	EXPECT_EQ(kill(pid, SIGTERM), 0);
> > +
> > +	EXPECT_EQ(waitpid(pid, &status, 0), pid);
> > +	EXPECT_EQ(true, WIFSIGNALED(status));
> > +	EXPECT_EQ(SIGTERM, WTERMSIG(status));
> > +}
> 
> Should there be a test validating the inverse of this, as in _without_
> SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV, how should the above tests
> behave?
Don't we roughly get that from the user_notification_kill_in_middle
and user_notification_signal?

Although, I might cleanup the user_notification_signal test to disable
SA_RESTART like these tests.

> 
> Otherwise, looks good! Yay tests!
> 
> -- 
> Kees Cook

  reply	other threads:[~2022-04-29 22:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-29  2:31 [PATCH v3 0/2] Handle seccomp notification preemption Sargun Dhillon
2022-04-29  2:31 ` [PATCH v3 1/2] seccomp: Add wait_killable semantic to seccomp user notifier Sargun Dhillon
2022-04-29  9:42   ` Rodrigo Campos
2022-04-29 17:14     ` Sargun Dhillon
2022-04-29 18:20       ` Kees Cook
2022-05-02 12:48         ` Rodrigo Campos
2022-04-29 18:22   ` Kees Cook
2022-05-02 14:15   ` Rodrigo Campos
2022-05-02 16:04     ` Sargun Dhillon
2022-05-03 14:27       ` Rodrigo Campos
2022-04-29  2:31 ` [PATCH v3 2/2] selftests/seccomp: Add test for wait killable notifier Sargun Dhillon
2022-04-29 18:19   ` Kees Cook
2022-04-29 22:35     ` Sargun Dhillon [this message]
2022-04-29 22:43       ` Kees Cook
2022-04-29  9:24 ` [PATCH v3 0/2] Handle seccomp notification preemption Rodrigo Campos

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=20220429223557.GB1267404@ircssh-3.c.rugged-nimbus-611.internal \
    --to=sargun@sargun.me \
    --cc=alban@kinvolk.io \
    --cc=christian.brauner@ubuntu.com \
    --cc=containers@lists.linux-foundation.org \
    --cc=gscrivan@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=rodrigo@kinvolk.io \
    --cc=tycho@tycho.pizza \
    --cc=wad@chromium.org \
    /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;
as well as URLs for NNTP newsgroup(s).