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 1/9] landlock: Refactor current_check_access_socket() access right check
Date: Mon, 19 Aug 2024 23:37:03 +0200 [thread overview]
Message-ID: <ZsO6_14c14BAn-kI@google.com> (raw)
In-Reply-To: <20240814030151.2380280-2-ivanov.mikhail1@huawei-partners.com>
Hello!
Thanks for sending round 2 of this patch set!
On Wed, Aug 14, 2024 at 11:01:43AM +0800, Mikhail Ivanov wrote:
> The current_check_access_socket() function contains a set of address
> validation checks for bind(2) and connect(2) hooks. Separate them from
> an actual port access right checking. It is required for the (future)
> hooks that do not perform address validation.
>
> Signed-off-by: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
> ---
> security/landlock/net.c | 41 ++++++++++++++++++++++++-----------------
> 1 file changed, 24 insertions(+), 17 deletions(-)
>
> diff --git a/security/landlock/net.c b/security/landlock/net.c
> index c8bcd29bde09..669ba260342f 100644
> --- a/security/landlock/net.c
> +++ b/security/landlock/net.c
> @@ -2,7 +2,7 @@
> /*
> * Landlock LSM - Network management and hooks
> *
> - * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
> + * Copyright © 2022-2024 Huawei Tech. Co., Ltd.
> * Copyright © 2022-2023 Microsoft Corporation
> */
>
> @@ -61,17 +61,34 @@ static const struct landlock_ruleset *get_current_net_domain(void)
> return dom;
> }
>
> -static int current_check_access_socket(struct socket *const sock,
> - struct sockaddr *const address,
> - const int addrlen,
> - access_mask_t access_request)
> +static int check_access_socket(const struct landlock_ruleset *const dom,
> + __be16 port, access_mask_t access_request)
It might be worth briefly spelling out in documentation that access_request in
current_check_access_socket() may only have a single bit set. This is different
to other places where access_mask_t is used, where combinations of these flags
are possible.
These functions do checks for special cases using "if (access_request ==
LANDLOCK_ACCESS_NET_CONNECT_TCP)" and the same for "bind". I think it's a
reasonable way to simplify the implementation here, but we have to be careful to
not accidentally use it differently.
It is a preexisting issue, so I don't consider it a blocker, but it might be
worth fixing while we are at it?
> {
> - __be16 port;
> layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET] = {};
> const struct landlock_rule *rule;
> struct landlock_id id = {
> .type = LANDLOCK_KEY_NET_PORT,
> };
> +
> + id.key.data = (__force uintptr_t)port;
> + BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
> +
> + rule = landlock_find_rule(dom, id);
> + access_request = landlock_init_layer_masks(
> + dom, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
> + if (landlock_unmask_layers(rule, access_request, &layer_masks,
> + ARRAY_SIZE(layer_masks)))
> + return 0;
> +
> + return -EACCES;
> +}
> +
> +static int current_check_access_socket(struct socket *const sock,
Re-reading the implementation of this function, it was surprised how specialized
it is towards the "connect" and "bind" use cases, which it has specific code
paths for. This does not look like it would extend naturally to additional
operations.
After your refactoring, current_check_access_socket() is now (a) checking that
we are looking at a TCP address, and extracting the port, and then (b) doing
connect- and bind-specific logic, and then (c) calling check_access_socket().
Would it maybe be possible to turn the code logic around by creating a
"get_tcp_port()" helper function for step (a), and then doing all of (a), (b)
and (c) directly from hook_socket_bind() and hook_socket_connect()? It would
have the upside that in step (b) you don't need to distinguish between bind and
connect because it would be clear from the context which of the two cases we are
in. It would also remove the need for a function that only supports one bit in
the access_mask_t, which is potentially surprising.
Thanks,
—Günther
next prev parent reply other threads:[~2024-08-19 21:37 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 [this message]
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
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=ZsO6_14c14BAn-kI@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).