1 // Regression test for https://crbug.com/502974, where ASan was unable to read
2 // the binary name because of sandbox restrictions.
3 // This test uses seccomp-BPF to restrict the readlink() system call and makes
4 // sure ASan is still able to
5 // RUN: not ls /usr/include/linux/seccomp.h || ( %clang_asan %s -o %t && not %run %t 2>&1 | FileCheck %s )
6 // UNSUPPORTED: android
7 
8 #include <errno.h>
9 #include <stddef.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <sys/prctl.h>
13 #include <sys/syscall.h>
14 #include <linux/filter.h>
15 #include <linux/seccomp.h>
16 
17 #define syscall_nr (offsetof(struct seccomp_data, nr))
18 
19 void corrupt() {
20   void *p = malloc(10);
21   free(p);
22   free(p);
23 }
24 
25 int main() {
26   prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
27 
28   struct sock_filter filter[] = {
29     /* Grab the system call number */
30     BPF_STMT(BPF_LD + BPF_W + BPF_ABS, syscall_nr),
31     // If this is __NR_readlink,
32     BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_readlink, 0, 1),
33     // return with EPERM,
34     BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | EPERM),
35     // otherwise allow the syscall.
36     BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW)
37   };
38   struct sock_fprog prog;
39   prog.len = (unsigned short)(sizeof(filter)/sizeof(filter[0]));
40   prog.filter = filter;
41 
42   int res = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);
43   if (res != 0) {
44     fprintf(stderr, "PR_SET_SECCOMP unsupported!\n");
45   }
46   corrupt();
47   // CHECK: AddressSanitizer
48   // CHECK-NOT: reading executable name failed
49   return 0;
50 }
51