Skip to content

Commit cf8918d

Browse files
committed
selftests/seccomp: Make kcmp() less required
The seccomp tests are a bit noisy without CONFIG_CHECKPOINT_RESTORE (due to missing the kcmp() syscall). The seccomp tests are more accurate with kcmp(), but it's not strictly required. Refactor the tests to use alternatives (comparing fd numbers), and provide a central test for kcmp() so there is a single SKIP instead of many. Continue to produce warnings for the other tests, though. Additionally adds some more bad flag EINVAL tests to the addfd selftest. Cc: Andy Lutomirski <[email protected]> Cc: Will Drewry <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Song Liu <[email protected]> Cc: Yonghong Song <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: John Fastabend <[email protected]> Cc: KP Singh <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent e68f9d4 commit cf8918d

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,40 @@ int seccomp(unsigned int op, unsigned int flags, void *args)
242242
#define SIBLING_EXIT_FAILURE 0xbadface
243243
#define SIBLING_EXIT_NEWPRIVS 0xbadfeed
244244

245+
static int __filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2)
246+
{
247+
#ifdef __NR_kcmp
248+
errno = 0;
249+
return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2);
250+
#else
251+
errno = ENOSYS;
252+
return -1;
253+
#endif
254+
}
255+
256+
/* Have TH_LOG report actual location filecmp() is used. */
257+
#define filecmp(pid1, pid2, fd1, fd2) ({ \
258+
int _ret; \
259+
\
260+
_ret = __filecmp(pid1, pid2, fd1, fd2); \
261+
if (_ret != 0) { \
262+
if (_ret < 0 && errno == ENOSYS) { \
263+
TH_LOG("kcmp() syscall missing (test is less accurate)");\
264+
_ret = 0; \
265+
} \
266+
} \
267+
_ret; })
268+
269+
TEST(kcmp)
270+
{
271+
int ret;
272+
273+
ret = __filecmp(getpid(), getpid(), 1, 1);
274+
EXPECT_EQ(ret, 0);
275+
if (ret != 0 && errno == ENOSYS)
276+
SKIP(return, "Kernel does not support kcmp() (missing CONFIG_CHECKPOINT_RESTORE?)");
277+
}
278+
245279
TEST(mode_strict_support)
246280
{
247281
long ret;
@@ -3601,16 +3635,6 @@ TEST(seccomp_get_notif_sizes)
36013635
EXPECT_EQ(sizes.seccomp_notif_resp, sizeof(struct seccomp_notif_resp));
36023636
}
36033637

3604-
static int filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2)
3605-
{
3606-
#ifdef __NR_kcmp
3607-
return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2);
3608-
#else
3609-
errno = ENOSYS;
3610-
return -1;
3611-
#endif
3612-
}
3613-
36143638
TEST(user_notification_continue)
36153639
{
36163640
pid_t pid;
@@ -3635,20 +3659,14 @@ TEST(user_notification_continue)
36353659
int dup_fd, pipe_fds[2];
36363660
pid_t self;
36373661

3638-
ret = pipe(pipe_fds);
3639-
if (ret < 0)
3640-
exit(1);
3662+
ASSERT_GE(pipe(pipe_fds), 0);
36413663

36423664
dup_fd = dup(pipe_fds[0]);
3643-
if (dup_fd < 0)
3644-
exit(1);
3665+
ASSERT_GE(dup_fd, 0);
3666+
EXPECT_NE(pipe_fds[0], dup_fd);
36453667

36463668
self = getpid();
3647-
3648-
ret = filecmp(self, self, pipe_fds[0], dup_fd);
3649-
if (ret)
3650-
exit(2);
3651-
3669+
ASSERT_EQ(filecmp(self, self, pipe_fds[0], dup_fd), 0);
36523670
exit(0);
36533671
}
36543672

0 commit comments

Comments
 (0)