Skip to content

Commit 6004b04

Browse files
committed
Merge tag 'landlock-6.8-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux
Pull landlock test fixes from Mickaël Salaün: "Fix build issues for tests, and improve test compatibility" * tag 'landlock-6.8-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux: selftests/landlock: Fix capability for net_test selftests/landlock: Fix fs_test build with old libc selftests/landlock: Fix net_test build with old libc
2 parents 1f3a3e2 + bb6f4db commit 6004b04

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

tools/testing/selftests/landlock/common.h

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <errno.h>
1111
#include <linux/landlock.h>
12+
#include <linux/securebits.h>
1213
#include <sys/capability.h>
1314
#include <sys/socket.h>
1415
#include <sys/syscall.h>
@@ -115,11 +116,16 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
115116
/* clang-format off */
116117
CAP_DAC_OVERRIDE,
117118
CAP_MKNOD,
119+
CAP_NET_ADMIN,
120+
CAP_NET_BIND_SERVICE,
118121
CAP_SYS_ADMIN,
119122
CAP_SYS_CHROOT,
120-
CAP_NET_BIND_SERVICE,
121123
/* clang-format on */
122124
};
125+
const unsigned int noroot = SECBIT_NOROOT | SECBIT_NOROOT_LOCKED;
126+
127+
if ((cap_get_secbits() & noroot) != noroot)
128+
EXPECT_EQ(0, cap_set_secbits(noroot));
123129

124130
cap_p = cap_get_proc();
125131
EXPECT_NE(NULL, cap_p)
@@ -137,6 +143,8 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
137143
TH_LOG("Failed to cap_set_flag: %s", strerror(errno));
138144
}
139145
}
146+
147+
/* Automatically resets ambient capabilities. */
140148
EXPECT_NE(-1, cap_set_proc(cap_p))
141149
{
142150
TH_LOG("Failed to cap_set_proc: %s", strerror(errno));
@@ -145,6 +153,9 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
145153
{
146154
TH_LOG("Failed to cap_free: %s", strerror(errno));
147155
}
156+
157+
/* Quickly checks that ambient capabilities are cleared. */
158+
EXPECT_NE(-1, cap_get_ambient(caps[0]));
148159
}
149160

150161
/* We cannot put such helpers in a library because of kselftest_harness.h . */
@@ -158,8 +169,9 @@ static void __maybe_unused drop_caps(struct __test_metadata *const _metadata)
158169
_init_caps(_metadata, true);
159170
}
160171

161-
static void _effective_cap(struct __test_metadata *const _metadata,
162-
const cap_value_t caps, const cap_flag_value_t value)
172+
static void _change_cap(struct __test_metadata *const _metadata,
173+
const cap_flag_t flag, const cap_value_t cap,
174+
const cap_flag_value_t value)
163175
{
164176
cap_t cap_p;
165177

@@ -168,7 +180,7 @@ static void _effective_cap(struct __test_metadata *const _metadata,
168180
{
169181
TH_LOG("Failed to cap_get_proc: %s", strerror(errno));
170182
}
171-
EXPECT_NE(-1, cap_set_flag(cap_p, CAP_EFFECTIVE, 1, &caps, value))
183+
EXPECT_NE(-1, cap_set_flag(cap_p, flag, 1, &cap, value))
172184
{
173185
TH_LOG("Failed to cap_set_flag: %s", strerror(errno));
174186
}
@@ -183,15 +195,35 @@ static void _effective_cap(struct __test_metadata *const _metadata,
183195
}
184196

185197
static void __maybe_unused set_cap(struct __test_metadata *const _metadata,
186-
const cap_value_t caps)
198+
const cap_value_t cap)
187199
{
188-
_effective_cap(_metadata, caps, CAP_SET);
200+
_change_cap(_metadata, CAP_EFFECTIVE, cap, CAP_SET);
189201
}
190202

191203
static void __maybe_unused clear_cap(struct __test_metadata *const _metadata,
192-
const cap_value_t caps)
204+
const cap_value_t cap)
205+
{
206+
_change_cap(_metadata, CAP_EFFECTIVE, cap, CAP_CLEAR);
207+
}
208+
209+
static void __maybe_unused
210+
set_ambient_cap(struct __test_metadata *const _metadata, const cap_value_t cap)
211+
{
212+
_change_cap(_metadata, CAP_INHERITABLE, cap, CAP_SET);
213+
214+
EXPECT_NE(-1, cap_set_ambient(cap, CAP_SET))
215+
{
216+
TH_LOG("Failed to set ambient capability %d: %s", cap,
217+
strerror(errno));
218+
}
219+
}
220+
221+
static void __maybe_unused clear_ambient_cap(
222+
struct __test_metadata *const _metadata, const cap_value_t cap)
193223
{
194-
_effective_cap(_metadata, caps, CAP_CLEAR);
224+
EXPECT_EQ(1, cap_get_ambient(cap));
225+
_change_cap(_metadata, CAP_INHERITABLE, cap, CAP_CLEAR);
226+
EXPECT_EQ(0, cap_get_ambient(cap));
195227
}
196228

197229
/* Receives an FD from a UNIX socket. Returns the received FD, or -errno. */

tools/testing/selftests/landlock/fs_test.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,11 @@ struct mnt_opt {
241241
const char *const data;
242242
};
243243

244-
const struct mnt_opt mnt_tmp = {
244+
#define MNT_TMP_DATA "size=4m,mode=700"
245+
246+
static const struct mnt_opt mnt_tmp = {
245247
.type = "tmpfs",
246-
.data = "size=4m,mode=700",
248+
.data = MNT_TMP_DATA,
247249
};
248250

249251
static int mount_opt(const struct mnt_opt *const mnt, const char *const target)
@@ -4632,7 +4634,10 @@ FIXTURE_VARIANT(layout3_fs)
46324634
/* clang-format off */
46334635
FIXTURE_VARIANT_ADD(layout3_fs, tmpfs) {
46344636
/* clang-format on */
4635-
.mnt = mnt_tmp,
4637+
.mnt = {
4638+
.type = "tmpfs",
4639+
.data = MNT_TMP_DATA,
4640+
},
46364641
.file_path = file1_s1d1,
46374642
};
46384643

tools/testing/selftests/landlock/net_test.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <string.h>
1818
#include <sys/prctl.h>
1919
#include <sys/socket.h>
20+
#include <sys/syscall.h>
2021
#include <sys/un.h>
2122

2223
#include "common.h"
@@ -54,6 +55,11 @@ struct service_fixture {
5455
};
5556
};
5657

58+
static pid_t sys_gettid(void)
59+
{
60+
return syscall(__NR_gettid);
61+
}
62+
5763
static int set_service(struct service_fixture *const srv,
5864
const struct protocol_variant prot,
5965
const unsigned short index)
@@ -88,7 +94,7 @@ static int set_service(struct service_fixture *const srv,
8894
case AF_UNIX:
8995
srv->unix_addr.sun_family = prot.domain;
9096
sprintf(srv->unix_addr.sun_path,
91-
"_selftests-landlock-net-tid%d-index%d", gettid(),
97+
"_selftests-landlock-net-tid%d-index%d", sys_gettid(),
9298
index);
9399
srv->unix_addr_len = SUN_LEN(&srv->unix_addr);
94100
srv->unix_addr.sun_path[0] = '\0';
@@ -101,8 +107,11 @@ static void setup_loopback(struct __test_metadata *const _metadata)
101107
{
102108
set_cap(_metadata, CAP_SYS_ADMIN);
103109
ASSERT_EQ(0, unshare(CLONE_NEWNET));
104-
ASSERT_EQ(0, system("ip link set dev lo up"));
105110
clear_cap(_metadata, CAP_SYS_ADMIN);
111+
112+
set_ambient_cap(_metadata, CAP_NET_ADMIN);
113+
ASSERT_EQ(0, system("ip link set dev lo up"));
114+
clear_ambient_cap(_metadata, CAP_NET_ADMIN);
106115
}
107116

108117
static bool is_restricted(const struct protocol_variant *const prot,

0 commit comments

Comments
 (0)