[RFC PATCH v2 08/12] selftests/landlock: Add tcp_layers.ruleset_overlap to socket tests
Mikhail Ivanov
ivanov.mikhail1 at huawei-partners.com
Thu May 30 15:08:53 UTC 2024
5/28/2024 12:09 AM, Günther Noack wrote:
> On Fri, May 24, 2024 at 05:30:11PM +0800, Mikhail Ivanov wrote:
>> * Add tcp_layers fixture for tests that check multiple layer
>> configuration scenarios.
>>
>> * Add test that validates multiple layer behavior with overlapped
>> restrictions.
>>
>> Signed-off-by: Mikhail Ivanov <ivanov.mikhail1 at huawei-partners.com>
>> ---
>>
>> Changes since v1:
>> * Replaces test_socket_create() with test_socket().
>> * Formats code with clang-format.
>> * Refactors commit message.
>> * Minor fixes.
>> ---
>> .../testing/selftests/landlock/socket_test.c | 109 ++++++++++++++++++
>> 1 file changed, 109 insertions(+)
>>
>> diff --git a/tools/testing/selftests/landlock/socket_test.c b/tools/testing/selftests/landlock/socket_test.c
>> index 751596c381fe..52edc1a8ac21 100644
>> --- a/tools/testing/selftests/landlock/socket_test.c
>> +++ b/tools/testing/selftests/landlock/socket_test.c
>> @@ -299,4 +299,113 @@ TEST_F(protocol, inval)
>> &protocol, 0));
>> }
>>
>> +FIXTURE(tcp_layers)
>> +{
>> + struct service_fixture srv0;
>> +};
>> +
>> +FIXTURE_VARIANT(tcp_layers)
>> +{
>> + const size_t num_layers;
>> +};
>> +
>> +FIXTURE_SETUP(tcp_layers)
>> +{
>> + const struct protocol_variant prot = {
>> + .family = AF_INET,
>> + .type = SOCK_STREAM,
>> + };
>> +
>> + disable_caps(_metadata);
>> + self->srv0.protocol = prot;
>> + setup_namespace(_metadata);
>> +};
>> +
>> +FIXTURE_TEARDOWN(tcp_layers)
>> +{
>> +}
>> +
>> +/* clang-format off */
>> +FIXTURE_VARIANT_ADD(tcp_layers, no_sandbox_with_ipv4) {
>> + /* clang-format on */
>> + .num_layers = 0,
>> +};
>> +
>> +/* clang-format off */
>> +FIXTURE_VARIANT_ADD(tcp_layers, one_sandbox_with_ipv4) {
>> + /* clang-format on */
>> + .num_layers = 1,
>> +};
>> +
>> +/* clang-format off */
>> +FIXTURE_VARIANT_ADD(tcp_layers, two_sandboxes_with_ipv4) {
>> + /* clang-format on */
>> + .num_layers = 2,
>> +};
>> +
>> +/* clang-format off */
>> +FIXTURE_VARIANT_ADD(tcp_layers, three_sandboxes_with_ipv4) {
>> + /* clang-format on */
>> + .num_layers = 3,
>> +};
>> +
>> +TEST_F(tcp_layers, ruleset_overlap)
>> +{
>> + const struct landlock_ruleset_attr ruleset_attr = {
>> + .handled_access_socket = LANDLOCK_ACCESS_SOCKET_CREATE,
>> + };
>> + const struct landlock_socket_attr tcp_create = {
>> + .allowed_access = LANDLOCK_ACCESS_SOCKET_CREATE,
>> + .family = self->srv0.protocol.family,
>> + .type = self->srv0.protocol.type,
>> + };
>> +
>> + if (variant->num_layers >= 1) {
>> + int ruleset_fd;
>> +
>> + ruleset_fd = landlock_create_ruleset(&ruleset_attr,
>> + sizeof(ruleset_attr), 0);
>> + ASSERT_LE(0, ruleset_fd);
>> +
>> + /* Allows create. */
>> + ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET,
>> + &tcp_create, 0));
>> + enforce_ruleset(_metadata, ruleset_fd);
>> + EXPECT_EQ(0, close(ruleset_fd));
>> + }
>> +
>> + if (variant->num_layers >= 2) {
>> + int ruleset_fd;
>> +
>> + /* Creates another ruleset layer with 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));
>> + }
>> +
>> + if (variant->num_layers >= 3) {
>> + int ruleset_fd;
>> +
>> + /* Creates another ruleset layer. */
>> + ruleset_fd = landlock_create_ruleset(&ruleset_attr,
>> + sizeof(ruleset_attr), 0);
>> + ASSERT_LE(0, ruleset_fd);
>> +
>> + /* Try to allow create second time. */
>> + ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_SOCKET,
>> + &tcp_create, 0));
>> + enforce_ruleset(_metadata, ruleset_fd);
>> + EXPECT_EQ(0, close(ruleset_fd));
>> + }
>> +
>> + if (variant->num_layers < 2) {
>> + ASSERT_EQ(0, test_socket(&self->srv0));
>> + } else {
>> + ASSERT_EQ(EACCES, test_socket(&self->srv0));
>> + }
>> +}
>
> Wouldn't this be simpler if you did multiple checks in one test, in a sequence?
>
> * Expect that socket() works
> * Enforce ruleset 1 with a rule
> * Expect that socket() works
> * Enforce ruleset 2 without a rule
> * Expect that socket() fails
> * Enforce ruleset 3
> * Expect that socket() still fails
>
> Then it would test the same and you would not need the fixture.
> If you extracted these if bodies above into helper functions,
> I think it would also read reasonably well.
I adapted this test from net_test.c and wanted it to remain similar to
the original. But I agree that such simplification is rational, probably
it's worth a little inconsistency.
Perhaps this test should be made common, like the tests that were
discussed earlier [1].
[1]
https://lore.kernel.org/all/f4b5e2b9-e960-fd08-fdf4-328bb475e2ef@huawei-partners.com/
>
> —Günther
More information about the Linux-security-module-archive
mailing list