Skip to content

Instantly share code, notes, and snippets.

@tiborvass
Last active November 20, 2020 16:11
Show Gist options
  • Save tiborvass/eb0a4054679a43aaca22690a7c4452ed to your computer and use it in GitHub Desktop.
Save tiborvass/eb0a4054679a43aaca22690a7c4452ed to your computer and use it in GitHub Desktop.
Retrieve list of blocking Linux syscall functions (apart from [Raw]Syscall*) callable from Go (skips syscalls marked as sysnb, also accounts for golang.org/x/sys/unix), along with their corresponding syscall constant number
Accept4 SYS_ACCEPT
Accept4 SYS_ACCEPT4
Accept SYS_ACCEPT
Access SYS_FACCESSAT
Acct SYS_ACCT
AddKey SYS_ADD_KEY
Adjtimex SYS_ADJTIMEX
AttachLsf SYS_SETSOCKOPT
Bind SYS_BIND
BindToDevice SYS_SETSOCKOPT
Chdir SYS_CHDIR
Chmod SYS_FCHMODAT
Chown SYS_FCHOWNAT
Chroot SYS_CHROOT
ClockGetres SYS_CLOCK_GETRES
ClockGettime SYS_CLOCK_GETTIME
ClockNanosleep SYS_CLOCK_NANOSLEEP
Close SYS_CLOSE
Connect SYS_CONNECT
CopyFileRange SYS_COPY_FILE_RANGE
Creat SYS_OPENAT
DeleteModule SYS_DELETE_MODULE
DetachLsf SYS_SETSOCKOPT
Dup2 SYS_DUP2
Dup3 SYS_DUP3
Dup SYS_DUP
EpollWait SYS_EPOLL_WAIT
Eventfd SYS_EVENTFD2
Exit SYS_EXIT_GROUP
Faccessat2 SYS_FACCESSAT2
Faccessat SYS_FACCESSAT
Fadvise SYS_FADVISE64
Fallocate SYS_FALLOCATE
FanotifyInit SYS_FANOTIFY_INIT
FanotifyMark SYS_FANOTIFY_MARK
Fchdir SYS_FCHDIR
Fchmodat SYS_FCHMODAT
Fchmod SYS_FCHMOD
Fchownat SYS_FCHOWNAT
Fchown SYS_FCHOWN
Fdatasync SYS_FDATASYNC
Fgetxattr SYS_FGETXATTR
FinitModule SYS_FINIT_MODULE
Flistxattr SYS_FLISTXATTR
Flock SYS_FLOCK
ForkExec SYS_CHDIR
Fremovexattr SYS_FREMOVEXATTR
Fsetxattr SYS_FSETXATTR
Fstatat SYS_NEWFSTATAT
Fstatfs SYS_FSTATFS
Fstat SYS_FSTAT
Fsync SYS_FSYNC
Ftruncate SYS_FTRUNCATE
Futimesat SYS_FUTIMESAT
Futimes SYS_UTIMENSAT
Futimes SYS_UTIMES
Getcwd SYS_GETCWD
Getdents SYS_GETDENTS64
Getpeername SYS_ACCEPT
Getpriority SYS_GETPRIORITY
Getrandom SYS_GETRANDOM
Getsockname SYS_ACCEPT
GetsockoptByte SYS_GETSOCKOPT
GetsockoptICMPv6Filter SYS_GETSOCKOPT
GetsockoptInet4Addr SYS_GETSOCKOPT
GetsockoptInt SYS_GETSOCKOPT
GetsockoptIPMreqn SYS_GETSOCKOPT
GetsockoptIPMreq SYS_GETSOCKOPT
GetsockoptIPv6Mreq SYS_GETSOCKOPT
GetsockoptIPv6MTUInfo SYS_GETSOCKOPT
GetsockoptLinger SYS_GETSOCKOPT
GetsockoptString SYS_GETSOCKOPT
GetsockoptTCPInfo SYS_GETSOCKOPT
GetsockoptTimeval SYS_GETSOCKOPT
GetsockoptTpacketStats SYS_GETSOCKOPT
GetsockoptTpacketStatsV3 SYS_GETSOCKOPT
GetsockoptUcred SYS_GETSOCKOPT
GetsockoptUint64 SYS_GETSOCKOPT
Getwd SYS_GETCWD
Getxattr SYS_GETXATTR
InitModule SYS_INIT_MODULE
InotifyAddWatch SYS_INOTIFY_ADD_WATCH
IoctlFileCloneRange SYS_IOCTL
IoctlFileClone SYS_IOCTL
IoctlFileDedupeRange SYS_IOCTL
IoctlGetInt SYS_IOCTL
IoctlGetRTCTime SYS_IOCTL
IoctlGetRTCWkAlrm SYS_IOCTL
IoctlGetTermios SYS_IOCTL
IoctlGetUint32 SYS_IOCTL
IoctlGetWatchdogInfo SYS_IOCTL
IoctlGetWinsize SYS_IOCTL
IoctlSetInt SYS_IOCTL
IoctlSetPointerInt SYS_IOCTL
IoctlSetRTCTime SYS_IOCTL
IoctlSetRTCWkAlrm SYS_IOCTL
IoctlSetTermios SYS_IOCTL
IoctlSetWinsize SYS_IOCTL
IoctlWatchdogKeepalive SYS_IOCTL
Ioperm SYS_IOPERM
Iopl SYS_IOPL
KexecFileLoad SYS_KEXEC_FILE_LOAD
KeyctlBuffer SYS_KEYCTL
KeyctlDHCompute SYS_KEYCTL
KeyctlGetKeyringID SYS_KEYCTL
KeyctlInstantiateIOV SYS_KEYCTL
KeyctlInt SYS_KEYCTL
KeyctlJoinSessionKeyring SYS_KEYCTL
KeyctlRestrictKeyring SYS_KEYCTL
KeyctlSearch SYS_KEYCTL
KeyctlSetperm SYS_KEYCTL
KeyctlString SYS_KEYCTL
Klogctl SYS_SYSLOG
Lchown SYS_FCHOWNAT
Lchown SYS_LCHOWN
Lgetxattr SYS_LGETXATTR
Linkat SYS_LINKAT
Link SYS_LINKAT
Listen SYS_LISTEN
Listxattr SYS_LISTXATTR
Llistxattr SYS_LLISTXATTR
Lremovexattr SYS_LREMOVEXATTR
Lsetxattr SYS_LSETXATTR
LsfSocket SYS_BIND
Lstat SYS_NEWFSTATAT
Lutimes SYS_UTIMENSAT
Madvise SYS_MADVISE
MemfdCreate SYS_MEMFD_CREATE
Mkdirat SYS_MKDIRAT
Mkdir SYS_MKDIRAT
Mkfifoat SYS_MKNODAT
Mkfifo SYS_MKNODAT
Mknodat SYS_MKNODAT
Mknod SYS_MKNODAT
Mlockall SYS_MLOCKALL
Mlock SYS_MLOCK
Mmap SYS_MMAP
Mount SYS_MOUNT
Mprotect SYS_MPROTECT
Msync SYS_MSYNC
Munlockall SYS_MUNLOCKALL
Munlock SYS_MUNLOCK
Munmap SYS_MUNMAP
NameToHandleAt SYS_NAME_TO_HANDLE_AT
Nanosleep SYS_NANOSLEEP
NetlinkRIB SYS_BIND
Openat2 SYS_OPENAT2
Openat SYS_OPENAT
OpenByHandleAt SYS_OPEN_BY_HANDLE_AT
Open SYS_OPENAT
Pause SYS_PAUSE
PerfEventOpen SYS_PERF_EVENT_OPEN
PivotRoot SYS_PIVOT_ROOT
Poll SYS_POLL
Ppoll SYS_PPOLL
Prctl SYS_PRCTL
Pread SYS_PREAD64
Preadv2 SYS_PREADV2
Preadv SYS_PREADV
ProcessVMReadv SYS_PROCESS_VM_READV
ProcessVMWritev SYS_PROCESS_VM_WRITEV
Pselect SYS_PSELECT6
PtraceCont SYS_PTRACE
PtraceGetEventMsg SYS_PTRACE
PtraceGetRegs386 SYS_PTRACE
PtraceGetRegsAmd64 SYS_PTRACE
PtraceGetRegs SYS_PTRACE
PtracePeekData SYS_PTRACE
PtracePeekText SYS_PTRACE
PtracePeekUser SYS_PTRACE
PtracePokeData SYS_PTRACE
PtracePokeText SYS_PTRACE
PtracePokeUser SYS_PTRACE
PtraceSetOptions SYS_PTRACE
PtraceSetRegs386 SYS_PTRACE
PtraceSetRegsAmd64 SYS_PTRACE
PtraceSetRegs SYS_PTRACE
PtraceSyscall SYS_PTRACE
Pwrite SYS_PWRITE64
Pwritev2 SYS_PWRITEV2
Pwritev SYS_PWRITEV
ReadDirent SYS_GETDENTS64
Readlinkat SYS_READLINKAT
Readlink SYS_READLINKAT
Read SYS_READ
Readv SYS_READV
Reboot SYS_REBOOT
Recvfrom SYS_ACCEPT
Recvfrom SYS_RECVFROM
Recvmsg SYS_ACCEPT
Recvmsg SYS_GETSOCKOPT
Removexattr SYS_REMOVEXATTR
Renameat2 SYS_RENAMEAT2
Renameat SYS_RENAMEAT
Rename SYS_RENAMEAT
RequestKey SYS_REQUEST_KEY
Rmdir SYS_UNLINKAT
Seek SYS_LSEEK
Select SYS_PSELECT6
Select SYS_SELECT
Sendfile SYS_SENDFILE
SendmsgN SYS_GETSOCKOPT
Sendmsg SYS_GETSOCKOPT
Sendto SYS_SENDTO
Setdomainname SYS_SETDOMAINNAME
SetfsgidRetGid SYS_SETFSGID
Setfsgid SYS_SETFSGID
SetfsuidRetUid SYS_SETFSUID
Setfsuid SYS_SETFSUID
Sethostname SYS_SETHOSTNAME
SetLsfPromisc SYS_CLOSE
SetNonblock SYS_FCNTL
Setns SYS_SETNS
Setpriority SYS_SETPRIORITY
SetsockoptByte SYS_SETSOCKOPT
SetsockoptCanRawFilter SYS_SETSOCKOPT
SetsockoptICMPv6Filter SYS_SETSOCKOPT
SetsockoptInet4Addr SYS_SETSOCKOPT
SetsockoptInt SYS_SETSOCKOPT
SetsockoptIPMreqn SYS_SETSOCKOPT
SetsockoptIPMreq SYS_SETSOCKOPT
SetsockoptIPv6Mreq SYS_SETSOCKOPT
SetsockoptLinger SYS_SETSOCKOPT
SetsockoptPacketMreq SYS_SETSOCKOPT
SetsockoptSockFprog SYS_SETSOCKOPT
SetsockoptString SYS_SETSOCKOPT
SetsockoptTimeval SYS_SETSOCKOPT
SetsockoptTpacketReq3 SYS_SETSOCKOPT
SetsockoptTpacketReq SYS_SETSOCKOPT
SetsockoptUint64 SYS_SETSOCKOPT
Setxattr SYS_SETXATTR
Shutdown SYS_SHUTDOWN
Signalfd SYS_SIGNALFD4
Splice SYS_SPLICE
StartProcess SYS_CHDIR
Statfs SYS_STATFS
Stat SYS_NEWFSTATAT
Statx SYS_STATX
Symlinkat SYS_SYMLINKAT
Symlink SYS_SYMLINKAT
SyncFileRange SYS_SYNC_FILE_RANGE
Syncfs SYS_SYNCFS
Sync SYS_SYNC
Tee SYS_TEE
Truncate SYS_TRUNCATE
Unlinkat SYS_UNLINKAT
Unlink SYS_UNLINKAT
Unmount SYS_UMOUNT2
Unshare SYS_UNSHARE
Ustat SYS_USTAT
UtimesNanoAt SYS_UTIMENSAT
UtimesNano SYS_UTIMENSAT
Utimes SYS_UTIMENSAT
Utimes SYS_UTIMES
Utime SYS_UTIME
Wait4 SYS_WAIT4
Write SYS_WRITE
Writev SYS_WRITEV
#!/usr/bin/env bash
set -e
if [ -n "$DEBUG" ]; then
set -x
fi
tmpdir=$(mktemp -d)
cleanup() {
popd &>/dev/null
if [ -n "$DEBUG" ]; then
echo "$tmpdir"
else
rm -rf "$tmpdir"
fi
}
list_syscalls() {
syscalls="$1"
cp "$syscalls" allfuncs
shift
paths="$@"
# map of functions to syscall numbers
declare -A m
# iterate over each syscall
while read s; do
# find corresponding first argument to Syscall function
sys=$(awk '/^func /{lastFunc=$0} /Syscall[_A-Za-z0-9]*\([^,]+?/&&lastFunc~/^func '"$s"'\(/ {print}' $paths | sed -E 's|.*Syscall[_A-Za-z0-9]*\(([^,]+?).*|\1|' | sort -u)
echo "$s" > funcs
m["$s"]="$sys "
while :; do
# list of current functions
flist="($(echo $(cat funcs) | tr ' ' '|'))"
# store in newfuncs, the list of NEW callers of the current functions.
awk '/^func /{lastFunc=$0} !/^(\/\/|func ).*\<'"$flist"'\>/&&/\<'"$flist"'\>/{print lastFunc} /^\}$/{lastFunc=""}' $paths | grep -vE '^$' | sed -E 's|^func (\([^\)]+\) )?([^\(]+).*|\2|' | sort -u | grep -vE '^[tT]est' | comm -1 -3 allfuncs - > newfuncs
# for each new caller associate the syscall number
for f in $(cat newfuncs); do
x=$(echo ${m["$f"]})
y=$( echo "$x"; echo "$sys" )
y=$(echo $(echo "$y" | sort -u))
m["$f"]="$y"
done
# prepare next iteration by setting current functions to be the new callers
cat newfuncs | sort -u | tee funcs | cat - allfuncs | sort -u > tmpfuncs
# maintain list of all functions encountered
mv tmpfuncs allfuncs
# stop when there are no new functions found
if [ $(cat newfuncs | wc -l) -eq 0 ]; then
break
fi
done
done < "$syscalls"
# print all public syscall functions with the syscall numbers used in their implementation.
# do not quote ${m[$k]} so that if there are multiple syscall numbers for a same function, they stay on the same line
for k in "${!m[@]}"; do echo "$k" ${m[$k]}; done | grep -vE '^[a-z_]'
}
trap cleanup EXIT
pushd $tmpdir &>/dev/null
sysdir=$(go env GOROOT)/src/syscall
files_syscall=$(for f in $(go list -tags linux -json syscall | jq -r .GoFiles[]); do echo "$sysdir/$f"; done)
grep -E '^//sys\b' $files_syscall | sed -E 's|.*//sys[^A-Za-z]+([A-Za-z_0-9]+).*|\1|' | sort -u > syscall
go get -u golang.org/x/sys/unix
sysdir=$(go env GOPATH)/src/golang.org/x/sys/unix
files_unix=$(for f in $(go list -tags linux -json golang.org/x/sys/unix | jq -r .GoFiles[]); do echo "$sysdir/$f"; done)
grep -E '^//sys\b' $files_unix | sed -E 's|.*//sys[^A-Za-z]+([A-Za-z_0-9]+).*|\1|' | sort -u > unix
(
list_syscalls syscall $files_syscall
list_syscalls unix $files_unix
) | sort -u
# Note: some syscall functions are implemented differently between syscall package and unix package.
# For instance, the above will output:
# Lchown SYS_FCHOWNAT
# Lchown SYS_LCHOWN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment