: start

include "audit_nr.h"

# check if we are using the expected calling convention
load arch
ifne CHA_AUDIT_NR deny

# load syscall nr
load nr

# socket/file i/o
ifeqdef SYS_read allow
ifeqdef SYS_write allow

# polling
ifeqdef SYS_poll allow
ifeqdef SYS_ppoll allow # poll is sometimes implemented as ppoll, e.g. musl

ifdef CHA_FORCE_SELECT
ifeqdef SYS_select allow
ifeqdef SYS_pselect6 allow
endif

# memory allocation
ifeqdef SYS_mmap allow
ifeqdef SYS_mmap2 allow
ifeqdef SYS_mremap allow
ifeqdef SYS_munmap allow
ifeqdef SYS_brk allow

# less common socket/file i/o
ifeqdef SYS_writev allow
ifeqdef SYS_readv allow
ifeqdef SYS_lseek allow
ifeqdef SYS_close allow
ifeqdef SYS_shutdown allow

# prevent glibc from getting our process murdered
ifeqdef SYS_fstat eperm
ifeqdef SYS_fstat64 eperm
ifeqdef SYS_fstatat64 eperm
ifeqdef SYS_newfstatat eperm
ifeqdef SYS_statx eperm

# accept fcntl(2), but only with F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL.
# (F_SETFL is 4, others are 0..3)
ifeqdef SYS_fcntl64 is_fcntl64
ifne SYS_fcntl not_fcntl
# additional test for fcntl64 on 32-bit systems
: is_fcntl64
load args[1]
ifle F_SETFL allow
load nr
: not_fcntl

# following syscalls are rarely called
ifeqdef SYS_futex allow	# bionic libc & WSL both need it
ifeqdef SYS_exit allow	# for quit
ifeqdef SYS_exit_group allow	# for quit
ifeqdef SYS_restart_syscall allow # for resuming poll on SIGCONT
ifeqdef SYS_getrandom allow	# glibc calls it when initializing its malloc

# so we don't trap on crash
ifeqdef SYS_sigaction eperm
ifeqdef SYS_rt_sigaction eperm
ifeqdef SYS_rt_sigprocmask eperm
ifeqdef SYS_gettid eperm
ifeqdef SYS_tgkill kill

# these are used both by QJS in buffer and by *something* in network
# (I could never reproduce it; might have been libcurl, might have been OpenSSL)
ifeqdef SYS_clock_gettime allow	# used by QuickJS in atomics and cpuTime()
ifeqdef SYS_clock_gettime64 allow # same but 32-bit
ifeqdef SYS_gettimeofday allow	# used by QuickJS in Date.now()

# network: used indirectly by OpenSSL EVP_RAND_CTX_new (through drbg)
# buffer: apparently used in SIGSEGV handler
ifeqdef SYS_getpid allow

# bionic-specific stuff
ifdef __BIONIC__
ifeqdef SYS_rt_sigprocmap allow
ifeqdef SYS_madvise allow #TODO can we make this less broad?

# bionic uses prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME) for its pages.
ifne SYS_prctl not_prctl
load args[0]
ifne PR_SET_VMA deny
load args[1]
ifeq PR_SET_VMA_ANON_NAME allow
load nr
: not_prctl

# bionic also calls mprotect(2) with PROT_READ and PROT_WRITE flags.
# Crucially, we don't want to allow PROT_EXEC.
ifne SYS_mprotect not_mprotect
load args[2]
ifle PROT_READ|PROT_WRITE allow
load nr
: not_mprotect

endif /* __BIONIC__ */
