[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <306a198f-89dd-77e4-d45d-a1139d13d654@huawei-partners.com>
Date: Sat, 22 Nov 2025 13:21:30 +0300
From: Mikhail Ivanov <ivanov.mikhail1@...wei-partners.com>
To: Günther Noack <gnoack3000@...il.com>
CC: <mic@...ikod.net>, <gnoack@...gle.com>, <willemdebruijn.kernel@...il.com>,
<matthieu@...fet.re>, <linux-security-module@...r.kernel.org>,
<netdev@...r.kernel.org>, <netfilter-devel@...r.kernel.org>,
<yusongping@...wei.com>, <artem.kuzin@...wei.com>,
<konstantin.meskhidze@...wei.com>
Subject: Re: [RFC PATCH v4 12/19] selftests/landlock: Test socketpair(2)
restriction
On 11/22/2025 1:16 PM, Günther Noack wrote:
> On Tue, Nov 18, 2025 at 09:46:32PM +0800, Mikhail Ivanov wrote:
>> diff --git a/tools/testing/selftests/landlock/socket_test.c b/tools/testing/selftests/landlock/socket_test.c
>> index e22e10edb103..d1a004c2e0f5 100644
>> --- a/tools/testing/selftests/landlock/socket_test.c
>> +++ b/tools/testing/selftests/landlock/socket_test.c
>> @@ -866,4 +866,59 @@ TEST_F(tcp_protocol, alias_restriction)
>> }
>> }
>>
>> +static int test_socketpair(int family, int type, int protocol)
>> +{
>> + int fds[2];
>> + int err;
>> +
>> + err = socketpair(family, type | SOCK_CLOEXEC, protocol, fds);
>> + if (err)
>> + return errno;
>> + /*
>> + * Mixing error codes from close(2) and socketpair(2) should not lead to
>> + * any (access type) confusion for this test.
>> + */
>> + if (close(fds[0]) != 0)
>> + return errno;
>> + if (close(fds[1]) != 0)
>> + return errno;
>
> Very minor nit: the function leaks an FD if it returns early after the
> first close() call failed. (Highly unlikely to happen though.)
Yeah, but AFAIK fd[0] may be leaked anyway if close() fails. Anyway
this shouldn't be an issue for tests.
>
>> + return 0;
>> +}
>> +
>> +TEST_F(mini, socketpair)
>> +{
>> + const struct landlock_ruleset_attr ruleset_attr = {
>> + .handled_access_socket = LANDLOCK_ACCESS_SOCKET_CREATE,
>> + };
>> + const struct landlock_socket_attr unix_socket_create = {
>> + .allowed_access = LANDLOCK_ACCESS_SOCKET_CREATE,
>> + .family = AF_UNIX,
>> + .type = SOCK_STREAM,
>> + .protocol = 0,
>> + };
>> + int ruleset_fd;
>> +
>> + /* Tries to create socket when ruleset is not established. */
>> + ASSERT_EQ(0, test_socketpair(AF_UNIX, SOCK_STREAM, 0));
>> + 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,
>> + &unix_socket_create, 0));
>> + enforce_ruleset(_metadata, ruleset_fd);
>> + ASSERT_EQ(0, close(ruleset_fd));
>> +
>> + /* Tries to create socket when protocol is allowed */
>> + EXPECT_EQ(0, test_socketpair(AF_UNIX, SOCK_STREAM, 0));
>> +
>> + ruleset_fd =
>> + landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
>
> You may want to check that landlock_create_ruleset() succeeded here:
>
> ASSERT_LE(0, ruleset_fd)
thanks, I'll fix it.
>
>> + enforce_ruleset(_metadata, ruleset_fd);
>> + ASSERT_EQ(0, close(ruleset_fd));
>> +
>> + /* Tries to create socket when protocol is restricted. */
>> + EXPECT_EQ(EACCES, test_socketpair(AF_UNIX, SOCK_STREAM, 0));
>> +}
>> +
>> TEST_HARNESS_MAIN
>> --
>> 2.34.1
>>
>
> Otherwise, looks good.
> –Günther
Powered by blists - more mailing lists