Skip to content

Commit db94112

Browse files
committed
selftests/landlock: Test abstract UNIX socket scoping
JIRA: https://issues.redhat.com/browse/RHEL-94688 Add three tests that examine different scenarios for abstract UNIX socket: 1) scoped_domains: Base tests of the abstract socket scoping mechanism for a landlocked process, same as the ptrace test. 2) scoped_vs_unscoped: Generates three processes with different domains and tests if a process with a non-scoped domain can connect to other processes. 3) outside_socket: Since the socket's creator credentials are used for scoping sockets, this test examines the cases where the socket's credentials are different from the process using it. Move protocol_variant, service_fixture, and sys_gettid() from net_test.c to common.h, and factor out code into a new set_unix_address() helper. Signed-off-by: Tahera Fahimi <fahimitahera@gmail.com> Link: https://lore.kernel.org/r/9321c3d3bcd9212ceb4b50693e29349f8d625e16.1725494372.git.fahimitahera@gmail.com [mic: Fix commit message, remove useless clang-format tags, move drop_caps() calls, move and rename variables, rename variants, use more EXPECT, improve comments, simplify the outside_socket test] Signed-off-by: Mickaël Salaün <mic@digikod.net> (cherry picked from commit fefcf0f) Signed-off-by: Ryan Sullivan <rysulliv@redhat.com>
1 parent 1f00666 commit db94112

File tree

6 files changed

+998
-30
lines changed

6 files changed

+998
-30
lines changed

tools/testing/selftests/landlock/common.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
* Copyright © 2021 Microsoft Corporation
88
*/
99

10+
#include <arpa/inet.h>
1011
#include <errno.h>
1112
#include <linux/landlock.h>
1213
#include <linux/securebits.h>
1314
#include <sys/capability.h>
1415
#include <sys/socket.h>
1516
#include <sys/syscall.h>
1617
#include <sys/types.h>
18+
#include <sys/un.h>
1719
#include <sys/wait.h>
1820
#include <unistd.h>
1921

@@ -226,3 +228,38 @@ enforce_ruleset(struct __test_metadata *const _metadata, const int ruleset_fd)
226228
TH_LOG("Failed to enforce ruleset: %s", strerror(errno));
227229
}
228230
}
231+
232+
struct protocol_variant {
233+
int domain;
234+
int type;
235+
};
236+
237+
struct service_fixture {
238+
struct protocol_variant protocol;
239+
/* port is also stored in ipv4_addr.sin_port or ipv6_addr.sin6_port */
240+
unsigned short port;
241+
union {
242+
struct sockaddr_in ipv4_addr;
243+
struct sockaddr_in6 ipv6_addr;
244+
struct {
245+
struct sockaddr_un unix_addr;
246+
socklen_t unix_addr_len;
247+
};
248+
};
249+
};
250+
251+
static pid_t __maybe_unused sys_gettid(void)
252+
{
253+
return syscall(__NR_gettid);
254+
}
255+
256+
static void __maybe_unused set_unix_address(struct service_fixture *const srv,
257+
const unsigned short index)
258+
{
259+
srv->unix_addr.sun_family = AF_UNIX;
260+
sprintf(srv->unix_addr.sun_path,
261+
"_selftests-landlock-abstract-unix-tid%d-index%d", sys_gettid(),
262+
index);
263+
srv->unix_addr_len = SUN_LEN(&srv->unix_addr);
264+
srv->unix_addr.sun_path[0] = '\0';
265+
}

tools/testing/selftests/landlock/net_test.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,6 @@ enum sandbox_type {
3636
TCP_SANDBOX,
3737
};
3838

39-
struct protocol_variant {
40-
int domain;
41-
int type;
42-
};
43-
44-
struct service_fixture {
45-
struct protocol_variant protocol;
46-
/* port is also stored in ipv4_addr.sin_port or ipv6_addr.sin6_port */
47-
unsigned short port;
48-
union {
49-
struct sockaddr_in ipv4_addr;
50-
struct sockaddr_in6 ipv6_addr;
51-
struct {
52-
struct sockaddr_un unix_addr;
53-
socklen_t unix_addr_len;
54-
};
55-
};
56-
};
57-
58-
static pid_t sys_gettid(void)
59-
{
60-
return syscall(__NR_gettid);
61-
}
62-
6339
static int set_service(struct service_fixture *const srv,
6440
const struct protocol_variant prot,
6541
const unsigned short index)
@@ -92,12 +68,7 @@ static int set_service(struct service_fixture *const srv,
9268
return 0;
9369

9470
case AF_UNIX:
95-
srv->unix_addr.sun_family = prot.domain;
96-
sprintf(srv->unix_addr.sun_path,
97-
"_selftests-landlock-net-tid%d-index%d", sys_gettid(),
98-
index);
99-
srv->unix_addr_len = SUN_LEN(&srv->unix_addr);
100-
srv->unix_addr.sun_path[0] = '\0';
71+
set_unix_address(srv, index);
10172
return 0;
10273
}
10374
return 1;

0 commit comments

Comments
 (0)