brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · 5d4a812 Raw
59 lines · c
1// Regression test for https://crbug.com/502974, where ASan was unable to read2// the binary name because of sandbox restrictions.3// This test uses seccomp-BPF to restrict the readlink() system call and makes4// sure ASan is still able to5// Disable symbolizing results, since this will invoke llvm-symbolizer, which6// will be unable to resolve its $ORIGIN due to readlink() restriction and will7// thus fail to start, causing the test to die with SIGPIPE when attempting to8// talk to it.9// RUN: not ls /usr/include/linux/seccomp.h || %clang_asan %s -o %t || not env ASAN_OPTIONS=symbolize=0 %run %t 2>&1 | FileCheck %s10// UNSUPPORTED: android11 12#include <errno.h>13#include <stddef.h>14#include <stdlib.h>15#include <stdio.h>16#include <sys/prctl.h>17#include <sys/syscall.h>18#include <linux/filter.h>19#include <linux/seccomp.h>20 21#ifndef __NR_readlink22# define __NR_readlink __NR_readlinkat23#endif24 25#define syscall_nr (offsetof(struct seccomp_data, nr))26 27void corrupt() {28  void *p = malloc(10);29  free(p);30  free(p);31}32 33int main() {34  prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);35 36  struct sock_filter filter[] = {37    /* Grab the system call number */38    BPF_STMT(BPF_LD + BPF_W + BPF_ABS, syscall_nr),39    // If this is __NR_readlink,40    BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_readlink, 0, 1),41    // return with EPERM,42    BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | EPERM),43    // otherwise allow the syscall.44    BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW)45  };46  struct sock_fprog prog;47  prog.len = (unsigned short)(sizeof(filter)/sizeof(filter[0]));48  prog.filter = filter;49 50  int res = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0);51  if (res != 0) {52    fprintf(stderr, "PR_SET_SECCOMP unsupported!\n");53  }54  corrupt();55  // CHECK: AddressSanitizer56  // CHECK-NOT: reading executable name failed57  return 0;58}59