Commit cf8918db authored by Kees Cook's avatar Kees Cook

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 <luto@amacapital.net>
Cc: Will Drewry <wad@chromium.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: Andrii Nakryiko <andriin@fb.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: KP Singh <kpsingh@chromium.org>
Cc: linux-kselftest@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent e68f9d49
...@@ -242,6 +242,40 @@ int seccomp(unsigned int op, unsigned int flags, void *args) ...@@ -242,6 +242,40 @@ int seccomp(unsigned int op, unsigned int flags, void *args)
#define SIBLING_EXIT_FAILURE 0xbadface #define SIBLING_EXIT_FAILURE 0xbadface
#define SIBLING_EXIT_NEWPRIVS 0xbadfeed #define SIBLING_EXIT_NEWPRIVS 0xbadfeed
static int __filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2)
{
#ifdef __NR_kcmp
errno = 0;
return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2);
#else
errno = ENOSYS;
return -1;
#endif
}
/* Have TH_LOG report actual location filecmp() is used. */
#define filecmp(pid1, pid2, fd1, fd2) ({ \
int _ret; \
\
_ret = __filecmp(pid1, pid2, fd1, fd2); \
if (_ret != 0) { \
if (_ret < 0 && errno == ENOSYS) { \
TH_LOG("kcmp() syscall missing (test is less accurate)");\
_ret = 0; \
} \
} \
_ret; })
TEST(kcmp)
{
int ret;
ret = __filecmp(getpid(), getpid(), 1, 1);
EXPECT_EQ(ret, 0);
if (ret != 0 && errno == ENOSYS)
SKIP(return, "Kernel does not support kcmp() (missing CONFIG_CHECKPOINT_RESTORE?)");
}
TEST(mode_strict_support) TEST(mode_strict_support)
{ {
long ret; long ret;
...@@ -3601,16 +3635,6 @@ TEST(seccomp_get_notif_sizes) ...@@ -3601,16 +3635,6 @@ TEST(seccomp_get_notif_sizes)
EXPECT_EQ(sizes.seccomp_notif_resp, sizeof(struct seccomp_notif_resp)); EXPECT_EQ(sizes.seccomp_notif_resp, sizeof(struct seccomp_notif_resp));
} }
static int filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2)
{
#ifdef __NR_kcmp
return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2);
#else
errno = ENOSYS;
return -1;
#endif
}
TEST(user_notification_continue) TEST(user_notification_continue)
{ {
pid_t pid; pid_t pid;
...@@ -3635,20 +3659,14 @@ TEST(user_notification_continue) ...@@ -3635,20 +3659,14 @@ TEST(user_notification_continue)
int dup_fd, pipe_fds[2]; int dup_fd, pipe_fds[2];
pid_t self; pid_t self;
ret = pipe(pipe_fds); ASSERT_GE(pipe(pipe_fds), 0);
if (ret < 0)
exit(1);
dup_fd = dup(pipe_fds[0]); dup_fd = dup(pipe_fds[0]);
if (dup_fd < 0) ASSERT_GE(dup_fd, 0);
exit(1); EXPECT_NE(pipe_fds[0], dup_fd);
self = getpid(); self = getpid();
ASSERT_EQ(filecmp(self, self, pipe_fds[0], dup_fd), 0);
ret = filecmp(self, self, pipe_fds[0], dup_fd);
if (ret)
exit(2);
exit(0); exit(0);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment