** Description changed:

  == Summary ==
  seccomp:seccomp_bpf in ubuntu_kernel_selftests failed on stonking 7.3.0-12.13 
generic ppc64el:
  - 11 of 112 subtests fail (pass:89 fail:11 skip:12): 10 
global.user_notification_* subtests and TRACE_syscall.seccomp.syscall_faked.
- - Reproduced by hand on a 7.3.0-12-generic ppc64le machine. A seccomp user 
notification reply with a successful value (error=0, val=12345) makes the 
intercepted syscall return 0 instead of 12345. A reply with an error (-EPERM) 
works correctly.
+ - Reproduced by hand on a 7.3.0-12-generic ppc64le machine (as root, the same 
way the test suite runs it).
+ - In all the failing subtests that I looked at, a syscall that seccomp skips 
(SECCOMP_RET_USER_NOTIF reply, or SECCOMP_RET_TRACE with the tracer rewriting 
r0/r3) returns 0 to user space instead of the injected successful value. 
Injected errors (e.g. -EPERM, -ESRCH) are returned correctly.
  - The same assertions fail on stonking 7.3.0-5.5 (e.g. syscall_faked 
"Expected 45000 == syscall(207) (0)", user_notification_sync "Expected ret (0) 
== USER_NOTIF_MAGIC"), so this is not new in 7.3.0-12.13.
- - On 7.0.0-27-generic (no CONFIG_GENERIC_ENTRY in its config) the same 
seccomp_bpf binary passes user_notification_basic, user_notification_sync and 
syscall_faked, and the reproducer below returns 12345.
- 
- == Reproducer ==
- Build with "gcc -w -o un un.c" and run as a normal user.
+ - On 7.0.0-27-generic (no CONFIG_GENERIC_ENTRY in its config) the same 
seccomp_bpf binary passes user_notification_basic, user_notification_sync and 
syscall_faked, and the first reproducer below returns 12345. (This 7.0.0-27 run 
was done as a normal user, not as root; see "Not checked".)
+ 
+ == Reproducer 1: seccomp user notification ==
+ Build with "gcc -w -o un un.c" and run as a normal user. (If stdout is 
redirected, the "reply ..." lines may be printed more than once because of 
stdio buffering across fork(); the results are not affected.)
  
  #define _GNU_SOURCE
  #include <stdio.h>
  #include <string.h>
  #include <errno.h>
  #include <unistd.h>
  #include <sys/syscall.h>
  #include <sys/ioctl.h>
  #include <sys/wait.h>
  #include <sys/prctl.h>
  #include <linux/seccomp.h>
  #include <linux/filter.h>
  #include <stddef.h>
  static int run(int err, long val)
  {
        int p[2], q[2]; pipe(p); pipe(q);
        pid_t pid = fork();
        if (pid == 0) {
                struct sock_filter f[] = {
                        BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct 
seccomp_data, nr)),
                        BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1),
                        BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_USER_NOTIF),
                        BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
                };
                struct sock_fprog prog = { .len = 4, .filter = f };
                prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
                int l = syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 
SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog);
                write(p[1], &l, sizeof l);
                char c; read(q[0], &c, 1);
                errno = 0;
                long r = syscall(__NR_getppid);
                printf("  child: syscall(getppid) = %ld errno=%d\n", r, errno);
                fflush(stdout); _exit(0);
        }
        int cfd; read(p[0], &cfd, sizeof cfd);
        int pf = syscall(__NR_pidfd_open, pid, 0);
        int l = syscall(__NR_pidfd_getfd, pf, cfd, 0);
        if (l < 0) { perror("pidfd_getfd"); return 1; }
        write(q[1], "g", 1);
        struct seccomp_notif req; memset(&req, 0, sizeof req);
        if (ioctl(l, SECCOMP_IOCTL_NOTIF_RECV, &req)) { perror("recv"); return 
1; }
        struct seccomp_notif_resp resp = { .id = req.id, .error = err, .val = 
val };
        if (ioctl(l, SECCOMP_IOCTL_NOTIF_SEND, &resp)) { perror("send"); return 
1; }
        int st; waitpid(pid, &st, 0);
        return 0;
  }
  int main(void)
  {
        printf("reply val=12345 error=0:\n"); run(0, 12345);
        printf("reply error=-EPERM:\n"); run(-EPERM, 0);
        return 0;
  }
  
  Result on 7.3.0-12-generic (ppc64le):
  reply val=12345 error=0:
    child: syscall(getppid) = 0 errno=0
  reply error=-EPERM:
    child: syscall(getppid) = -1 errno=1
  
  Result on 7.0.0-27-generic (ppc64le):
  reply val=12345 error=0:
    child: syscall(getppid) = 12345 errno=0
  reply error=-EPERM:
    child: syscall(getppid) = -1 errno=1
  
- == Analysis (hypothesis, based on reading the source; the fix has not been 
tested) ==
+ == Reproducer 2: ptrace + SECCOMP_RET_TRACE (same mechanism as 
TRACE_syscall.seccomp.syscall_faked) ==
+ Build with "gcc -w -o pt pt.c". Run "./pt 45000" (inject a value) and "./pt 
-3" (inject -ESRCH). Only run on 7.3.0-12-generic so far.
+ 
+ #define _GNU_SOURCE
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <errno.h>
+ #include <unistd.h>
+ #include <signal.h>
+ #include <elf.h>
+ #include <stddef.h>
+ #include <sys/syscall.h>
+ #include <sys/ptrace.h>
+ #include <sys/uio.h>
+ #include <sys/wait.h>
+ #include <sys/prctl.h>
+ #include <linux/ptrace.h>
+ #include <linux/seccomp.h>
+ #include <linux/filter.h>
+ #include <asm/ptrace.h>
+ 
+ int main(int argc, char **argv)
+ {
+       long inject = argc > 1 ? atol(argv[1]) : 45000;   /* >=0 value, <0 
-errno */
+       pid_t pid = fork();
+       if (pid == 0) {
+               struct sock_filter f[] = {
+                       BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct 
seccomp_data, nr)),
+                       BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_gettid, 0, 1),
+                       BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1003),
+                       BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
+               };
+               struct sock_fprog prog = { .len = 4, .filter = f };
+               ptrace(PTRACE_TRACEME, 0, 0, 0);
+               raise(SIGSTOP);
+               prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+               if (syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog)) { 
perror("seccomp"); _exit(2); }
+               errno = 0;
+               long r = syscall(__NR_gettid);
+               printf("child: syscall(gettid) = %ld errno=%d\n", r, errno);
+               fflush(stdout);
+               _exit(0);
+       }
+       int st;
+       waitpid(pid, &st, 0);
+       ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESECCOMP);
+       ptrace(PTRACE_CONT, pid, 0, 0);
+       for (;;) {
+               waitpid(pid, &st, 0);
+               if (WIFEXITED(st)) break;
+               if ((st >> 16) == PTRACE_EVENT_SECCOMP) {
+                       struct pt_regs regs; struct iovec v = { &regs, sizeof 
regs };
+                       ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &v);
+                       printf("tracer: seccomp event, nr(r0)=%lu 
trap=0x%lx\n", regs.gpr[0], regs.trap);
+                       regs.gpr[0] = -1;
+                       if ((regs.trap & 0xfff0) == 0x3000) regs.gpr[3] = 
inject;
+                       else if (inject < 0) { regs.gpr[3] = -inject; regs.ccr 
|= 0x10000000; }
+                       else { regs.gpr[3] = inject; regs.ccr &= ~0x10000000UL; 
}
+                       ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &v);
+                       printf("tracer: set r0=-1 r3=%lu (inject=%ld)\n", 
regs.gpr[3], inject);
+                       fflush(stdout);
+               }
+               ptrace(PTRACE_CONT, pid, 0, 0);
+       }
+       return 0;
+ }
+ 
+ Result on 7.3.0-12-generic (ppc64le):
+ $ ./pt 45000
+ tracer: seccomp event, nr(r0)=207 trap=0x3000
+ tracer: set r0=-1 r3=45000 (inject=45000)
+ child: syscall(gettid) = 0 errno=0
+ $ ./pt -3
+ tracer: seccomp event, nr(r0)=207 trap=0x3000
+ tracer: set r0=-1 r3=18446744073709551613 (inject=-3)
+ child: syscall(gettid) = -1 errno=3
+ 
+ == Per-subtest observations (7.3.0-12-generic, run as root) ==
+ Each failing subtest was run on its own. A bpftrace probe on the 
raw_syscalls:sys_exit tracepoint for getppid showed what the child got back, 
and the test source was read to see what is checked.
+ 
+  Subtest                                  getppid result seen by child   Why 
the test fails
+  user_notification_basic                  0                              
child does exit(ret != USER_NOTIF_MAGIC) -> exit status 1
+  user_notification_child_pid_ns           0                              same
+  user_notification_sibling_pid_ns         0                              same
+  user_notification_fault_recv             0                              same
+  user_notification_addfd_rlimit           0                              same
+  user_notification_addfd_send_interrupted 0 (expected 42)                
child does exit(ret != 42 || ...) -> exit status 1
+  user_notification_sync                   0                              
ASSERT_EQ(ret, USER_NOTIF_MAGIC) in the child
+  user_notification_fifo                   0 (all 4 children)             
same, exit status 1
+  user_notification_wait_killable          0 (seen twice)                 
same, exit status 1
+  user_notification_addfd                  0                              see 
below
+ 
+ user_notification_addfd times out because of the same problem: the child
+ gets 0 from its first getppid(), so it does exit(1) and never issues the
+ later getppid() calls. The supervisor then waits forever in
+ ioctl(SECCOMP_IOCTL_NOTIF_RECV) for notifications that are never
+ generated. I saw the test process blocked in
+ seccomp_notify_recv()/recv_wait_event() and the child as a zombie. So
+ the timeout is a consequence of the same wrong return value, not a
+ separate problem.
+ 
+ Not every subtest was compared against a fixed kernel, so "same cause"
+ for these ten is based on the observed return value and the test source,
+ not on a fix.
+ 
+ == Analysis (hypothesis, based on reading the source and on probing; the fix 
has not been tested) ==
  The expectations in seccomp_bpf.c are correct (e.g. the child does exit(ret 
!= USER_NOTIF_MAGIC)), so the kernel is the failing layer.
  
  In arch/powerpc/kernel/syscall.c (Ubuntu-7.3.0-12.13, lines 21-27),
  system_call_exception() does:
  
      if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0))) {
          clear_thread_flag(TIF_SYSCALL_RET);
          return syscall_get_error(current, regs);
      }
      if (unlikely(test_and_clear_thread_flag(TIF_SYSCALL_RET)))
          return syscall_get_error(current, regs);
  
  syscall_get_error() (arch/powerpc/include/asm/syscall.h) returns only an
- error code and returns 0 for a non-error value. The return value of
- system_call_exception() is passed to syscall_exit_prepare(), which
- stores it in regs->result and regs->gpr[3], so a successful value set by
- seccomp/ptrace appears to be replaced by 0. Injecting an errno works
- (e.g. TRACE_syscall.seccomp.syscall_errno passes), which is consistent
- with this.
- 
- Two paths lead there: seccomp user notification sets the value with
- syscall_set_return_value() (which sets TIF_SYSCALL_RET), while the
- ptrace-based syscall_faked test writes the registers with
- PTRACE_SETREGSET and sets r0 to -1; I did not trace which of the two
- branches above that case takes.
+ error code and returns 0 for a non-error value (for scv:
+ IS_ERR_VALUE(r3) ? r3 : 0; otherwise the CR0.SO bit decides). The return
+ value of system_call_exception() is passed to syscall_exit_prepare(),
+ which stores it in regs->result and regs->gpr[3], so a successful value
+ set by seccomp or by a tracer appears to be replaced by 0. Injecting an
+ errno works, which is consistent with this.
+ 
+ Both reproducers go through the FIRST branch above:
+ - With a kprobe return probe on __seccomp_permit_syscall I saw it return 0 
(skip the syscall) in both reproducer 1 (user notification, twice) and 
reproducer 2 (RET_TRACE). That makes 
syscall_enter_from_user_mode_randomize_stack() return false.
+ - For reproducer 2 the sys_exit tracepoint then shows "id=-1 ret=0", i.e. r3 
was already 0 at syscall exit although the tracer had set 45000. There was no 
sys_enter for a real syscall.
+ - system_call_exception() and syscall_exit_prepare() are notrace, so I could 
not probe them directly; "first branch" is inferred from the 
__seccomp_permit_syscall result and from reading kernel/seccomp.c and 
include/linux/entry-common.h.
+ In the RET_TRACE case the tracer sets r0 to -1, so __seccomp_filter() goes to 
"skip" (kernel/seccomp.c, this_syscall < 0). In the USER_NOTIF case 
seccomp_do_user_notification() returns true and it also goes to "skip". The 
TIF_SYSCALL_RET flag is set by syscall_set_return_value(), which the 
PTRACE_SETREGSET path does not call, but this does not matter here because both 
cases return from the first branch.
  
  The code comes from "powerpc: Enable GENERIC_ENTRY feature"
- (bee25f97ad24) and the follow-ups 69cb2be898be and 2c1dde8a69a3. Other
- differences between 7.0.0-27 and 7.3.0-12 may exist, so GENERIC_ENTRY is
- the suspected cause, not a proven one. No fix has been tried: no kernel
+ (bee25f97ad24) and the follow-ups 69cb2be898be and 2c1dde8a69a3.
+ 7.0.0-27 and 7.3 differ in many other ways, so GENERIC_ENTRY is the
+ suspected cause, not a proven one. No fix has been tried and no kernel
  was rebuilt.
  
  Upstream (kernel.org mainline): v7.3, v7.4-rc4 and master (25faec9ce61c)
  still return syscall_get_error() from system_call_exception() in the
  same way. Between v7.3 and master the only commit touching
  arch/powerpc/kernel/syscall.c, asm/syscall.h, interrupt.c,
- interrupt_64.S or ptrace/ is 66236558dfac ("Avoid atomic
- test_and_clear_thread_flag on fast path"), which does not change the
- returned value. I did not search mailing lists, so a pending patch may
- exist.
- 
- Not checked: the individual cause of each of the other
- user_notification_* failures, and global.tsync_vs_dead_thread_leader,
- which failed only on 7.3.0-5.5. On 7.0.0-27,
- user_notification_child_pid_ns and user_notification_fault_recv fail
- too, with a different error (seccomp NEW_LISTENER returns EACCES after
- unshare(CLONE_NEWUSER|CLONE_NEWPID)); I did not investigate it and do
- not consider it the same issue.
+ interrupt_64.S or ptrace/ is 66236558dfac ("powerpc/syscall: Avoid
+ atomic test_and_clear_thread_flag on fast path"), which does not change
+ the returned value. I did not search mailing lists, so a pending patch
+ may exist.
+ 
+ == Not checked ==
+ - 7.0.0-27: the comparison was done as a normal user, not as root. As a 
normal user the subtests user_notification_child_pid_ns and 
user_notification_fault_recv fail there with EACCES from seccomp(NEW_LISTENER) 
after unshare(CLONE_NEWUSER|CLONE_NEWPID), and sibling_pid_ns / 
addfd_send_interrupted are skipped. I think this is a non-root environment 
limit and not the problem in this report, but I have not run the full 
seccomp_bpf as root on 7.0.0-27, so those four subtests were not compared 
there. Reproducer 2 was not run on 7.0.0-27 (only the seccomp_bpf syscall_faked 
subtest, which passes).
+ - Any kernel between 7.0.0-27 and 7.3.0-5.5 (for example a 7.1/7.2 build 
without GENERIC_ENTRY).
+ - A kernel with a fix. The suggested direction (return the value in 
regs->gpr[3] instead of syscall_get_error() in the early-return paths) has not 
been built or tested.
+ - global.tsync_vs_dead_thread_leader, which failed only on 7.3.0-5.5 (status 
768).
+ - Why "basic" shows one extra child that gets getppid = -38 and exits with 
status 0; that exit status is 0, so I assumed it is an expected path of the 
test.
  
  == Test Information ==
  * SRU Cycle: sru-20261123
  * Series: stonking
  * Package: linux
  * Kernel Version: 7.3.0-12.13
  * Arch: ppc64el
  * Cloud / Target: openstack
  * Test Suite: ubuntu_kernel_selftests
  * Test Case: seccomp:seccomp_bpf
  
  == Failure Log ==
  # # seccomp_bpf.c:3488:user_notification_basic:Expected 0 (0) == 
WEXITSTATUS(status) (1)
  # #          FAIL  global.user_notification_basic
  # not ok 37 global.user_notification_basic
  # # seccomp_bpf.c:3713:user_notification_child_pid_ns:Expected 0 (0) == 
WEXITSTATUS(status) (1)
  # not ok 42 global.user_notification_child_pid_ns
+ # # seccomp_bpf.c:3755:user_notification_sibling_pid_ns:Expected 0 (0) == 
WEXITSTATUS(status) (1)
+ # # seccomp_bpf.c:3791:user_notification_sibling_pid_ns:Expected 0 (0) == 
WEXITSTATUS(status) (1)
+ # not ok 43 global.user_notification_sibling_pid_ns
  # # seccomp_bpf.c:3836:user_notification_fault_recv:Expected 0 (0) == 
WEXITSTATUS(status) (1)
  # not ok 44 global.user_notification_fault_recv
  # # user_notification_addfd: Test terminated by timeout
  # not ok 50 global.user_notification_addfd
+ # # seccomp_bpf.c:4366:user_notification_addfd_rlimit:Expected 0 (0) == 
WEXITSTATUS(status) (1)
+ # not ok 51 global.user_notification_addfd_rlimit
+ # # seccomp_bpf.c:4544:user_notification_addfd_send_interrupted:Expected 0 
(0) == WEXITSTATUS(status) (1)
+ # not ok 52 global.user_notification_addfd_send_interrupted
  # # seccomp_bpf.c:4583:user_notification_sync:Expected ret (0) == 
USER_NOTIF_MAGIC (2147483647)
  # # seccomp_bpf.c:4601:user_notification_sync:Expected status (256) == 0 (0)
  # not ok 53 global.user_notification_sync
+ # # seccomp_bpf.c:4760:user_notification_fifo:Expected 0 (0) == 
WEXITSTATUS(status) (1)
+ # # seccomp_bpf.c:4794:user_notification_fifo:Expected 0 (0) == 
WEXITSTATUS(status) (1)
+ # not ok 54 global.user_notification_fifo
  # # seccomp_bpf.c:4967:user_notification_wait_killable:Expected 0 (0) == 
WEXITSTATUS(status) (1)
  # not ok 56 global.user_notification_wait_killable
  # # seccomp_bpf.c:2253:syscall_faked:Expected 45000 (45000) == syscall(207) 
(0)
  # #          FAIL  TRACE_syscall.seccomp.syscall_faked
  # not ok 88 TRACE_syscall.seccomp.syscall_faked
  # # FAILED: 101 / 112 tests passed.
  # # Totals: pass:89 fail:11 xfail:0 xpass:0 skip:12 error:0
  not ok 1 selftests: seccomp: seccomp_bpf # exit=1

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2176163

Title:
  seccomp:seccomp_bpf in ubuntu_kernel_selftests failed on stonking
  ppc64el (injected syscall return value becomes 0)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu-kernel-tests/+bug/2176163/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to