selftests/seccomp: Make kcmp() less required
authorKees Cook <keescook@chromium.org>
Thu, 11 Jun 2020 18:02:21 +0000 (11:02 -0700)
committerKees Cook <keescook@chromium.org>
Fri, 10 Jul 2020 23:01:52 +0000 (16:01 -0700)
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: Kees Cook <keescook@chromium.org>
tools/testing/selftests/seccomp/seccomp_bpf.c

index d76ae98..c589a2d 100644 (file)
@@ -242,6 +242,40 @@ int seccomp(unsigned int op, unsigned int flags, void *args)
 #define SIBLING_EXIT_FAILURE   0xbadface
 #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)
 {
        long ret;
@@ -3601,16 +3635,6 @@ TEST(seccomp_get_notif_sizes)
        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)
 {
        pid_t pid;
@@ -3635,20 +3659,14 @@ TEST(user_notification_continue)
                int dup_fd, pipe_fds[2];
                pid_t self;
 
-               ret = pipe(pipe_fds);
-               if (ret < 0)
-                       exit(1);
+               ASSERT_GE(pipe(pipe_fds), 0);
 
                dup_fd = dup(pipe_fds[0]);
-               if (dup_fd < 0)
-                       exit(1);
+               ASSERT_GE(dup_fd, 0);
+               EXPECT_NE(pipe_fds[0], dup_fd);
 
                self = getpid();
-
-               ret = filecmp(self, self, pipe_fds[0], dup_fd);
-               if (ret)
-                       exit(2);
-
+               ASSERT_EQ(filecmp(self, self, pipe_fds[0], dup_fd), 0);
                exit(0);
        }