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 03/12] selftests/landlock: Add protocol.create to socket tests
Date: Mon, 27 May 2024 17:27:22 +0200 [thread overview]
Message-ID: <ZlSmAhLV00iry6we@google.com> (raw)
In-Reply-To: <20240524093015.2402952-4-ivanov.mikhail1@huawei-partners.com>
On Fri, May 24, 2024 at 05:30:06PM +0800, Mikhail Ivanov wrote:
> Initiate socket_test.c selftests. Add protocol fixture for tests
> with changeable family-type values. Only most common variants of
> protocols (like ipv4-tcp,ipv6-udp, unix) were added.
> Add simple socket access right checking test.
>
> Signed-off-by: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
> ---
>
> Changes since v1:
> * Replaces test_socket_create() and socket_variant() helpers
> with test_socket().
> * Renames domain to family in protocol fixture.
> * Remove AF_UNSPEC fixture entry and add unspec_srv0 fixture field to
> check AF_UNSPEC socket creation case.
> * Formats code with clang-format.
> * Refactors commit message.
> ---
> .../testing/selftests/landlock/socket_test.c | 181 ++++++++++++++++++
> 1 file changed, 181 insertions(+)
> create mode 100644 tools/testing/selftests/landlock/socket_test.c
>
> diff --git a/tools/testing/selftests/landlock/socket_test.c b/tools/testing/selftests/landlock/socket_test.c
> new file mode 100644
> index 000000000000..4c51f89ed578
> --- /dev/null
> +++ b/tools/testing/selftests/landlock/socket_test.c
> @@ -0,0 +1,181 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Landlock tests - Socket
> + *
> + * Copyright © 2024 Huawei Tech. Co., Ltd.
> + * Copyright © 2024 Microsoft Corporation
It looked to me like these patches came from Huawei?
Was this left by accident?
> + */
> +
> +#define _GNU_SOURCE
> +
> +#include <errno.h>
> +#include <linux/landlock.h>
> +#include <sched.h>
> +#include <string.h>
> +#include <sys/prctl.h>
> +#include <sys/socket.h>
> +
> +#include "common.h"
> +
> +/* clang-format off */
> +
> +#define ACCESS_LAST LANDLOCK_ACCESS_SOCKET_CREATE
> +
> +#define ACCESS_ALL ( \
> + LANDLOCK_ACCESS_SOCKET_CREATE)
> +
> +/* clang-format on */
It does not look like clang-format would really mess up this format in a bad
way. Maybe we can remove the "clang-format off" section here and just write the
"#define"s on one line?
ACCESS_ALL is unused in this commit.
Should it be introduced in a subsequent commit instead?
> +static int test_socket(const struct service_fixture *const srv)
> +{
> + int fd;
> +
> + fd = socket(srv->protocol.family, srv->protocol.type | SOCK_CLOEXEC, 0);
> + if (fd < 0)
> + return errno;
> + /*
> + * Mixing error codes from close(2) and socket(2) should not lead to any
> + * (access type) confusion for this test.
> + */
> + if (close(fd) != 0)
> + return errno;
> + return 0;
> +}
I personally find that it helps me remember if these test helpers have the same
signature as the syscall that they are exercising. (But I don't feel very
strongly about it. Just a suggestion.)
> [...]
>
> +TEST_F(protocol, create)
> +{
> + const struct landlock_ruleset_attr ruleset_attr = {
> + .handled_access_socket = LANDLOCK_ACCESS_SOCKET_CREATE,
> + };
> + const struct landlock_socket_attr create_socket_attr = {
> + .allowed_access = LANDLOCK_ACCESS_SOCKET_CREATE,
> + .family = self->srv0.protocol.family,
> + .type = self->srv0.protocol.type,
> + };
> +
> + int ruleset_fd;
> +
> + /* Allowed create */
> + ruleset_fd =
> + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
> + ASSERT_LE(0, ruleset_fd);
> +
> + ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET,
> + &create_socket_attr, 0));
> +
> + enforce_ruleset(_metadata, ruleset_fd);
> + EXPECT_EQ(0, close(ruleset_fd));
> +
> + ASSERT_EQ(0, test_socket(&self->srv0));
> + ASSERT_EQ(EAFNOSUPPORT, test_socket(&self->unspec_srv0));
> +
> + /* Denied create */
> + ruleset_fd =
> + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
> + ASSERT_LE(0, ruleset_fd);
> +
> + enforce_ruleset(_metadata, ruleset_fd);
> + EXPECT_EQ(0, close(ruleset_fd));
> +
> + ASSERT_EQ(EACCES, test_socket(&self->srv0));
> + ASSERT_EQ(EAFNOSUPPORT, test_socket(&self->unspec_srv0));
Should we exhaustively try out the other combinations (other than selv->srv0)
here? I assume socket() should always fail for these?
(If you are alredy doing this in another commit that I have not looked at yet,
please ignore this comment.)
—Günther
next prev parent reply other threads:[~2024-05-27 15:27 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-24 9:30 [RFC PATCH v2 00/12] Socket type control for Landlock Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 01/12] landlock: Support socket access-control Mikhail Ivanov
2024-05-27 9:57 ` Günther Noack
2024-05-30 12:05 ` Mikhail Ivanov
2024-06-05 17:04 ` Günther Noack
2024-06-07 13:34 ` Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 02/12] landlock: Add hook on socket creation Mikhail Ivanov
2024-05-27 8:48 ` Günther Noack
2024-05-30 12:20 ` Mikhail Ivanov
2024-06-05 17:27 ` Günther Noack
2024-06-07 14:45 ` Mikhail Ivanov
2024-09-25 18:31 ` Mickaël Salaün
2024-05-24 9:30 ` [RFC PATCH v2 03/12] selftests/landlock: Add protocol.create to socket tests Mikhail Ivanov
2024-05-27 15:27 ` Günther Noack [this message]
2024-05-30 12:50 ` Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 04/12] selftests/landlock: Add protocol.socket_access_rights " Mikhail Ivanov
2024-05-27 20:52 ` Günther Noack
2024-05-30 14:35 ` Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 05/12] selftests/landlock: Add protocol.rule_with_unknown_access " Mikhail Ivanov
2024-05-27 21:11 ` Günther Noack
2024-05-24 9:30 ` [RFC PATCH v2 06/12] selftests/landlock: Add protocol.rule_with_unhandled_access " Mikhail Ivanov
2024-05-27 21:15 ` Günther Noack
2024-05-24 9:30 ` [RFC PATCH v2 07/12] selftests/landlock: Add protocol.inval " Mikhail Ivanov
2024-05-27 21:27 ` Günther Noack
2024-05-30 15:28 ` Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 08/12] selftests/landlock: Add tcp_layers.ruleset_overlap " Mikhail Ivanov
2024-05-27 21:09 ` Günther Noack
2024-05-30 15:08 ` Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 09/12] selftests/landlock: Add mini.ruleset_with_unknown_access " Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 10/12] selftests/landlock: Add mini.socket_overflow " Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 11/12] selftests/landlock: Add mini.socket_invalid_type " Mikhail Ivanov
2024-05-24 9:30 ` [RFC PATCH v2 12/12] samples/landlock: Support socket protocol restrictions Mikhail Ivanov
2024-06-04 20:22 ` [RFC PATCH v2 00/12] Socket type control for Landlock Günther Noack
2024-06-06 11:44 ` Mikhail Ivanov
2024-06-06 13:32 ` Günther Noack
2024-06-06 19:32 ` Günther Noack
2024-06-07 13:58 ` Mikhail Ivanov
2024-06-10 8:03 ` Günther Noack
2024-06-10 8:21 ` [PATCH] landlock: Use bit-fields for storing handled layer access masks Günther Noack
2024-06-13 21:20 ` Mickaël Salaün
2024-06-14 12:06 ` Günther Noack
2024-06-15 15:08 ` Mickaël Salaün
2024-06-11 11:35 ` [RFC PATCH v2 00/12] Socket type control for Landlock 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=ZlSmAhLV00iry6we@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.