From: "Mickaël Salaün" <mic@digikod.net>
To: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
Cc: 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 v1 5/9] selftests/landlock: Test listen on connected socket
Date: Thu, 1 Aug 2024 16:46:56 +0200 [thread overview]
Message-ID: <20240801.Ee3Cai7eeD1g@digikod.net> (raw)
In-Reply-To: <20240728002602.3198398-6-ivanov.mikhail1@huawei-partners.com>
On Sun, Jul 28, 2024 at 08:25:58AM +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>
Good to have this test!
> ---
> tools/testing/selftests/landlock/net_test.c | 65 +++++++++++++++++++++
> 1 file changed, 65 insertions(+)
>
> diff --git a/tools/testing/selftests/landlock/net_test.c b/tools/testing/selftests/landlock/net_test.c
> index b6fe9bde205f..a8385f1373f6 100644
> --- a/tools/testing/selftests/landlock/net_test.c
> +++ b/tools/testing/selftests/landlock/net_test.c
> @@ -1644,6 +1644,71 @@ TEST_F(ipv4_tcp, with_fs)
> EXPECT_EQ(-EACCES, bind_variant(bind_fd, &self->srv1));
> }
>
> +TEST_F(ipv4_tcp, listen_on_connected)
We should use the "protocol" fixture and its variants instead to test
with different protocols and also without sandboxing (which is crutial).
I guess espintcp_listen should use "protocol" too.
ipv4_tcp is to run tests that only make sense on an IPv4 socket, but
when we test EINVAL, we should make sure Landlock doesn't introduce
inconsistencies for other/unsupported protocols.
> +{
> + 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;
> + int bind_fd, status;
> + pid_t child;
> +
> + 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));
> +
> + /* Deny listen for the second port. */
nit: Denies listening
> + 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));
> +
> + /* Init listening socket. */
nit: Initializes
> + 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));
> +
> + child = fork();
> + ASSERT_LE(0, child);
> + if (child == 0) {
> + int connect_fd;
> +
> + /* Closes listening socket for the child. */
> + EXPECT_EQ(0, close(bind_fd));
> +
> + 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));
> +
> + 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(port_specific)
> {
> struct service_fixture srv0;
> --
> 2.34.1
>
>
next prev parent reply other threads:[~2024-08-01 14:47 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-28 0:25 [RFC PATCH v1 0/9] Support TCP listen access-control Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 1/9] landlock: Refactor current_check_access_socket() access right check Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 2/9] landlock: Support TCP listen access-control Mikhail Ivanov
2024-07-30 8:24 ` Günther Noack
2024-07-31 17:20 ` Mikhail Ivanov
2024-08-01 10:36 ` Günther Noack
2024-08-01 11:45 ` Mikhail Ivanov
2024-07-31 18:30 ` Mickaël Salaün
2024-08-01 7:52 ` Mikhail Ivanov
2024-08-01 14:45 ` Mickaël Salaün
2024-08-01 15:34 ` Mikhail Ivanov
2024-08-01 16:01 ` Mickaël Salaün
2024-08-01 16:07 ` Mikhail Ivanov
2024-08-01 14:45 ` Mickaël Salaün
2024-08-01 16:04 ` Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 3/9] selftests/landlock: Support LANDLOCK_ACCESS_NET_LISTEN_TCP Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 4/9] selftests/landlock: Test listening restriction Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 5/9] selftests/landlock: Test listen on connected socket Mikhail Ivanov
2024-08-01 14:46 ` Mickaël Salaün [this message]
2024-08-01 15:47 ` Mikhail Ivanov
2024-07-28 0:25 ` [RFC PATCH v1 6/9] selftests/landlock: Test listening without explicit bind restriction Mikhail Ivanov
2024-07-28 0:26 ` [RFC PATCH v1 7/9] selftests/landlock: Test listen on ULP socket without clone method Mikhail Ivanov
2024-08-01 15:08 ` Mickaël Salaün
2024-08-01 17:42 ` Mikhail Ivanov
2024-07-28 0:26 ` [RFC PATCH v1 8/9] selftests/landlock: Test changing socket backlog with listen(2) Mikhail Ivanov
2024-07-28 0:26 ` [RFC PATCH v1 9/9] samples/landlock: Support LANDLOCK_ACCESS_NET_LISTEN 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=20240801.Ee3Cai7eeD1g@digikod.net \
--to=mic@digikod.net \
--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=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).