linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Günther Noack" <gnoack@google.com>
To: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
Cc: mic@digikod.net, willemdebruijn.kernel@gmail.com,
	gnoack3000@gmail.com,  linux-security-module@vger.kernel.org,
	netdev@vger.kernel.org,  netfilter-devel@vger.kernel.org,
	yusongping@huawei.com,  artem.kuzin@huawei.com,
	konstantin.meskhidze@huawei.com
Subject: Re: [RFC PATCH v2 5/9] selftests/landlock: Test listen on connected socket
Date: Tue, 20 Aug 2024 15:01:22 +0200	[thread overview]
Message-ID: <ZsSTouBzDOHFKC1L@google.com> (raw)
In-Reply-To: <20240814030151.2380280-6-ivanov.mikhail1@huawei-partners.com>

On Wed, Aug 14, 2024 at 11:01:47AM +0800, Mikhail Ivanov wrote:
> Test checks that listen(2) doesn't wrongfully return -EACCES instead
> of -EINVAL when trying to listen for an incorrect socket state.
> 
> Signed-off-by: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
> ---
> 
> Changes since v1:
> * Uses 'protocol' fixture instead of 'ipv4_tcp'.
> * Minor fixes.
> ---
>  tools/testing/selftests/landlock/net_test.c | 74 +++++++++++++++++++++
>  1 file changed, 74 insertions(+)
> 
> diff --git a/tools/testing/selftests/landlock/net_test.c b/tools/testing/selftests/landlock/net_test.c
> index b6fe9bde205f..551891b18b7a 100644
> --- a/tools/testing/selftests/landlock/net_test.c
> +++ b/tools/testing/selftests/landlock/net_test.c
> @@ -926,6 +926,80 @@ TEST_F(protocol, connect_unspec)
>  	EXPECT_EQ(0, close(bind_fd));
>  }
>  
> +TEST_F(protocol, listen_on_connected)
> +{
> +	int bind_fd, status;
> +	pid_t child;
> +
> +	if (variant->sandbox == TCP_SANDBOX) {
> +		const struct landlock_ruleset_attr ruleset_attr = {
> +			.handled_access_net = ACCESS_ALL,
> +		};
> +		const struct landlock_net_port_attr tcp_not_restricted_p0 = {
> +			.allowed_access = ACCESS_ALL,
> +			.port = self->srv0.port,
> +		};
> +		const struct landlock_net_port_attr tcp_denied_listen_p1 = {
> +			.allowed_access = ACCESS_ALL &
> +					  ~LANDLOCK_ACCESS_NET_LISTEN_TCP,
> +			.port = self->srv1.port,
> +		};
> +		int ruleset_fd;
> +
> +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
> +						     sizeof(ruleset_attr), 0);
> +		ASSERT_LE(0, ruleset_fd);
> +
> +		/* Allows all actions for the first port. */
> +		ASSERT_EQ(0,
> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> +					    &tcp_not_restricted_p0, 0));
> +
> +		/* Denies listening for the second port. */
> +		ASSERT_EQ(0,
> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> +					    &tcp_denied_listen_p1, 0));
> +
> +		enforce_ruleset(_metadata, ruleset_fd);
> +		EXPECT_EQ(0, close(ruleset_fd));
> +	}

Same remarks as in the previous commit apply here as well:

  - The if condition does the same thing, can maybe be deduplicated.
  - Can merge ruleset_fd declaration and assignment into one line.
    (This happens in a few more tests in later commits as well,
    please double check these as well.)

> +
> +	if (variant->prot.type != SOCK_STREAM)
> +		SKIP(return, "listen(2) is supported only on stream sockets");
> +
> +	/* Initializes listening socket. */
> +	bind_fd = socket_variant(&self->srv0);
> +	ASSERT_LE(0, bind_fd);
> +	EXPECT_EQ(0, bind_variant(bind_fd, &self->srv0));
> +	EXPECT_EQ(0, listen_variant(bind_fd, backlog));

I believe if bind() or listen() fail here, it does not make sense to continue
the test execution, so ASSERT_EQ would be more appropriate than EXPECT_EQ.


> +
> +	child = fork();
> +	ASSERT_LE(0, child);
> +	if (child == 0) {
> +		int connect_fd;
> +
> +		/* Closes listening socket for the child. */
> +		EXPECT_EQ(0, close(bind_fd));

You don't need to do this from a child process, you can just connect() from the
same process to the listening port.  (Since you are not calling accept(), the
server won't pick up the phone on the other end, but that is still enough to
connect successfully.)  It would simplify the story of correctly propagating
test exit statuses as well.

> +
> +		connect_fd = socket_variant(&self->srv1);
> +		ASSERT_LE(0, connect_fd);
> +		EXPECT_EQ(0, connect_variant(connect_fd, &self->srv0));
> +
> +		/* Tries to listen on connected socket. */
> +		EXPECT_EQ(-EINVAL, listen_variant(connect_fd, backlog));

Since this assertion is the actual point of the test,
maybe we could emphasize it a bit more with a comment here?

e.g:

/*
 * Checks that we always return EINVAL
 * and never accidentally return EACCES, if listen(2) fails.
 */

> +
> +		EXPECT_EQ(0, close(connect_fd));
> +		_exit(_metadata->exit_code);
> +		return;
> +	}
> +
> +	EXPECT_EQ(child, waitpid(child, &status, 0));
> +	EXPECT_EQ(1, WIFEXITED(status));
> +	EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
> +
> +	EXPECT_EQ(0, close(bind_fd));
> +}
> +
>  FIXTURE(ipv4)
>  {
>  	struct service_fixture srv0, srv1;
> -- 
> 2.34.1
> 

  reply	other threads:[~2024-08-20 13:01 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-14  3:01 [RFC PATCH v2 0/9] Support TCP listen access-control Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 1/9] landlock: Refactor current_check_access_socket() access right check Mikhail Ivanov
2024-08-19 21:37   ` Günther Noack
2024-08-20 11:20     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 2/9] landlock: Support TCP listen access-control Mikhail Ivanov
2024-10-05 16:56   ` Günther Noack
2024-10-05 17:53     ` Mikhail Ivanov
2024-10-05 18:22       ` Günther Noack
2024-10-05 18:32         ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 3/9] selftests/landlock: Support LANDLOCK_ACCESS_NET_LISTEN_TCP Mikhail Ivanov
2024-08-19 21:52   ` Günther Noack
2024-08-20 12:32     ` Mikhail Ivanov
2024-08-20 13:14     ` Günther Noack
2024-08-20 18:27       ` Mikhail Ivanov
2024-09-25 18:31         ` Mickaël Salaün
2024-09-26 11:59           ` Mikhail Ivanov
2024-08-19 21:53   ` Günther Noack
2024-08-20 12:35     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 4/9] selftests/landlock: Test listening restriction Mikhail Ivanov
2024-08-20 12:31   ` Günther Noack
2024-08-20 18:46     ` Mikhail Ivanov
2024-09-25 18:31       ` Mickaël Salaün
2024-09-26 13:51         ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 5/9] selftests/landlock: Test listen on connected socket Mikhail Ivanov
2024-08-20 13:01   ` Günther Noack [this message]
2024-08-20 13:42     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 6/9] selftests/landlock: Test listening without explicit bind restriction Mikhail Ivanov
2024-08-20 13:02   ` Günther Noack
2024-08-20 13:46     ` Mikhail Ivanov
2024-08-21 11:52       ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 7/9] selftests/landlock: Test listen on ULP socket without clone method Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 8/9] selftests/landlock: Test changing socket backlog with listen(2) Mikhail Ivanov
2024-10-05 16:57   ` Günther Noack
2024-10-05 17:29     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 9/9] samples/landlock: Support LANDLOCK_ACCESS_NET_LISTEN Mikhail Ivanov
2024-10-05 16:57   ` Günther Noack
2024-10-05 17:30     ` Mikhail Ivanov
2024-08-20 13:11 ` [RFC PATCH v2 0/9] Support TCP listen access-control Günther Noack
2024-08-20 13:23   ` Günther Noack
2024-08-20 13:53     ` Mikhail Ivanov

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=ZsSTouBzDOHFKC1L@google.com \
    --to=gnoack@google.com \
    --cc=artem.kuzin@huawei.com \
    --cc=gnoack3000@gmail.com \
    --cc=ivanov.mikhail1@huawei-partners.com \
    --cc=konstantin.meskhidze@huawei.com \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=yusongping@huawei.com \
    /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).