Seccomp profile:
We'll create a seccomp profile that blocks all syscalls except a whitelist. We can start from Docker's default seccomp profile and remove the ones we don't want.
The default Docker seccomp profile (from Docker 20.10) is:
{
"defaultAction": "SCMP_ACT_ERRNO",
"syscalls": [
{
"name": "accept",
"action": "SCMP_ACT_ALLOW"
},
... many allowed syscalls ...
]
}
We want to block network, so we remove all socket, bind, listen, accept, connect, etc. But note: some syscalls like getpeername might be allowed? We'll remove all that are related to networking.
We can use a profile that is more restrictive. Let's create a minimal one for a simple program that does only computation and file I/O (to /tmp and stdin/out/err).
Minimal allowed syscalls (for a simple C program) might include:
- read, write, exit, fstat, lseek, close, brk, mmap, munmap, rt_sigaction, rt_sigprocmask, ioctl, getpid, getuid, getgid, geteuid, getegid, getppid, stat, lstat, fstatat, arch_prctl, set_tid_address, set_robust_list, prlimit64, getrandom, etc.
But we also need for dynamic linking: open, openat, access, futex, clock_gettime, getcwd, dup2, pipe, pipe2, socketcall (if using old socketcall interface) but we are blocking sockets.
We can generate a profile by running a simple "hello world" program in a container with strace -f and then collect the syscalls.
However, for different runtimes (Python, Node.js, etc.) the required syscalls might be different.
We might need multiple seccomp profiles per runtime? Or we can try to have a more permissive one that allows all non-dangerous syscalls but blocks network and file access outside of /tmp.
Another idea: use the seccomp filter to allow all syscalls but then use a SECCOMP_RET_TRACE to log and then deny? Not practical.
We'll design a profile that:
- Allows all read, write, and lseek on file descriptors that are 0,1,2 (stdin, stdout, stderr) and on /tmp and the current working directory? But seccomp doesn't know about paths, only syscall numbers and arguments. We cannot filter by path in seccomp. So we rely on the filesystem isolation (the container only has /tmp and the necessary system libraries, and we make everything else read-only and not writable). So even if the code tries to open /etc/passwd, it will fail because the filesystem is read-only and we might not even have /etc/passwd (or we have a minimal one). But we might have some system files (like /etc/ld.so.cache) that are needed for dynamic linking.
So we can allow open, openat, access, etc. but because the filesystem is restricted, the code cannot access sensitive files. However, the code might try to open a file that doesn't exist, and that's okay.
We also want to block ptrace, mount, umount2, pivot_root, unshare (but note: we are already in a separate user namespace, so unshare might be allowed? We can block it anyway).
We'll create a seccomp profile that blocks:
- All syscalls that are known to be dangerous (like
mount, umount2, pivot_root, reboot, kexec_load, iopl, ioperm, syslog, tkill, tgkill, ptrace, etc.)
- All socket-related syscalls (if we don't want network).
But note: some syscalls are necessary for the runtime. We can start with a permissive profile (like the Docker default) and then remove the ones we don't want.
Alternatively, we can use the scmp_act_allow for a list of safe syscalls and scmp_act_errno for the rest. But we must be careful not to break the runtime.
Given the complexity, we might use the Docker default seccomp profile and then remove the network-related ones. But Docker's default profile is designed for general use and allows many syscalls that might not be necessary.
We can also use the unconfined profile (which is essentially no seccomp) and rely on the other isolation mechanisms. But that's less secure.
Given that we are using multiple layers of isolation (namespaces, cgroups, read-only rootfs, user namespace), we might be okay with a less strict seccomp profile. However, seccomp is a critical layer to prevent kernel exploits.
We'll design a profile that:
- Allows all syscalls that are not explicitly blocked.
- Blocks:
mount, umount2, pivot_root, reboot, kexec_load, iopl, ioperm, syslog, tkill, tgkill, ptrace, acct, add_key, keyctl, request_key, nice (maybe allow? but might be used to change priority), setpriority, sched_setparam, sched_setscheduler, sched_setattr, sched_getscheduler (maybe allow?), get_mempolicy, mbind, set_mempolicy, migrate_pages, move_pages, mknod, mknodat (we don't want device creation), mknod might be needed for /dev/null? But we have /dev already. We can allow mknodat only for specific device? Not in seccomp. So we might block mknod and mknodat and hope that the runtime doesn't need to create device nodes. We can pre-create /dev/null, /dev/zero, etc. in the rootfs.
We also block clone (but note: clone is used to create threads and processes. We want to allow clone with CLONE_VM? Actually, clone is the underlying syscall for fork and pthread_create. We want to allow clone but with restrictions? We can allow clone but then set a PID limit via cgroup. So we allow clone but the cgroup will limit the number of processes.
We also block unshare because we don't want the code to create new namespaces? But we are already in a user namespace and the code might try to unshare? We can block it.
We also block setns (to join other namespaces).
We also block swapoff, swapon.
We also block sysfs (obsolete).
We also block sysctl (obsolete).
We also block nfsservctl (obsolete).
We also block adjtimex, clock_adjtime (maybe allow? not necessary).
We also block kexec_load (already mentioned).
We also block init_module, finit_module (loading kernel modules).
We also block delete_module.
We also block lookup_dcookie (obsolete).
We also block perf_event_open (performance monitoring, might be used to gather information?).
We also block fanotify_init, fanotify_mark (file notification, might be used to monitor files?).
We also block name_to_handle_at, open_by_handle_at (file handle operations).
We also block set_robust_list (already in the allowed list? We might need it for futex). We can allow set_robust_list and get_robust_list? But get_robust_list might leak information? We can allow set_robust_list and block get_robust_list? Actually, get_robust_list is used by the kernel to get the robust list for the process, and it's not dangerous. We can allow.
We also block getcpu? Might be used to get CPU information, not dangerous.
We also block vhangup? Not needed.
We also block process_vm_readv, process_vm_writev (accessing other processes' memory, but in our container, there are only the processes we allow, so maybe safe? But we don't want the code to read other processes? It might be used to attack the host? Since we are in a user namespace, the processes are isolated by PID namespace, so it cannot see host processes. But within the container, it can read its own processes? That's okay. We can allow? But to be safe, we block.
We also block kcmp (compare processes).
We also block seccomp (to prevent the code from setting its own seccomp filter? We want to set our own seccomp filter and then prevent the code from changing it? We can block seccomp syscall).
We also block bpf (eBPF) because it can be used to manipulate kernel.
We also block userfaultfd (memory management, might be used for attacks).
We also block membarrier (memory barrier, might be used for timing attacks?).
We also block mlock2, memfd_create? memfd_create is used to create anonymous memory files, might be used for malicious purposes? We can allow if we want? But it's not necessary for most code. We block.
We also block landlock_create_ruleset, landlock_add_rule, landlock_restrict_self (if we don't use landlock).
We also block io_uring_setup, io_uring_enter, io_uring_register (if we don't want async I/O).
We also block rseq (restartable sequences, might be used for race condition attacks?).
We also block pidfd_send_signal, pidfd_open (if we don't want to send signals to other processes).
We also block io_pgetevents, io_pgetevents64 (if not using io_uring).
We also block statx? We might need statx for newer glibc? We can allow statx because it's like stat but extended.
We also block fsopen, fsmount, fspick (filesystem mounting).
We also block mount_setattr (new mount attributes).
We also block quotactl (disk quotas).
We also block quotactl and quota related? Not needed.
We also block msgctl, msgget, msgrcv, msgsnd (System V IPC). We might want to block all IPC? We are using PID and user namespaces, so the code cannot see host IPC, but within the container it can. We might want to block to prevent inter-process communication between multiple instances? But each container is isolated, so it's okay. However, if we run multiple processes in the same container (like the user code might spawn children) then they can use IPC. That's acceptable. But if we want to prevent the code from using IPC at all? We can block. But then the runtime might use POSIX message queues? Unlikely. We can block System V IPC.
We also block shmctl, shmget, shmat, shmdt (shared memory). Similarly.
We also block semget, semop, semctl (semaphores).
We also block msgctl, etc.
We also block ipc related? We can block all ipc syscalls? But note: ipc is a multiplexed syscall for System V IPC. We can block ipc and then allow the specific ones? Actually, the specific ones (like msgget) are separate syscalls? In x86_64, System V IPC uses msgget, msgsnd, etc. as separate syscalls. The ipc syscall is for the old i386. We can block ipc and allow the others? But we want to block System V IPC, so we block all of them.
We also block add_key and friends (keyring).
We also block keyctl.
We also block request_key.
We also block perf_event_open.
We also block futex_waitv? New futex? We might allow futex and futex_waitv? We can allow.
This is getting complex. We might start with a profile that allows a set of known safe syscalls and then expand as needed.
Alternatively, we can use the SCMP_ACT_ALLOW for a whitelist and then SCMP_ACT_ERRNO for the rest. We can generate the whitelist by running a set of test programs in the container and logging the syscalls.
But for the initial design, we'll assume we have a seccomp profile that is reasonably restrictive.
We can use the Docker default profile and remove the following (if present):
"socket", "bind", "listen", "accept", "connect", "getpeername", "getsockname", "getsockopt", "setsockopt", "shutdown", "sendto", "recvfrom", "sendmsg", "recvmsg", "sendmmsg", "recvmmsg", "accept4", "recvmmsg", "sendfile", "sendfile64", "sockpair", "socketpair", "bind", "listen", "accept", "accept4", "connect", "getsockname", "getpeername", "getsockopt", "setsockopt", "shutdown", "sendto", "recvfrom", "sendmsg", "recvmsg", "sendmmsg", "recvmmsg", "sendfile", "sendfile64", "socketpair", "sockpair", "getaddrinfo", "getnameinfo" (note: `getaddrinfo` and `getnameinfo` are libc functions, not syscalls). The syscalls are the ones listed.
Actually, the syscalls for networking are: socket, bind, listen, accept, connect, getsockname, getpeername, getsockopt, setsockopt, shutdown, sendto, recvfrom, sendmsg, recvmsg, sendmmsg, recvmmsg, accept4, sendfile, sendfile64, socketpair, sockpair, recvfrom, sendto, etc.
We'll block all of these.
Also block ioctl? ioctl can be used on sockets? We might want to block ioctl entirely? But ioctl is also used for terminals (e.g., TCGETS). We might need for tty? We are running headless, so maybe not. We can block ioctl or only allow specific requests? Seccomp can filter by arguments, but it's complex. We might allow ioctl and rely on the fact that we don't have any devices? But we have /dev/null, /dev/zero, etc. We can allow ioctl for specific file descriptors? Not in seccomp. So we might allow ioctl and hope that the code doesn't misuse it.
Alternatively, we can set the container to have no devices (except the minimal ones) and then ioctl on those devices might be safe? But ioctl can be used on any file descriptor. We might want to block ioctl entirely to be safe? But then some programs might break (e.g., if they use isatty which might call ioctl). We can allow ioctl with a pass for specific requests? That's too complex.
We might allow ioctl and rely on the fact that the code cannot access any device files because the rootfs is minimal and read-only. But if the code can open /dev/tty, then it might use ioctl on it. But we don't have /dev/tty? We can remove it.
So we'll allow ioctl but we make sure that the only devices available are the ones we create (like /dev/null, /dev/zero, /dev/random, /dev/urandom, /dev/full) and we set them to safe permissions.
Given the complexity, we might use a seccomp profile that is based on the Docker default but without network.
We'll use the Docker default profile and then remove all the socket related actions.
But note: the Docker default profile already blocks some dangerous syscalls. We can use that and then add our own blocks.
We can also use the traced profile from Docker? Not necessary.
Let's assume we have a seccomp profile that is:
{
"defaultAction": "SCMP_ACT_ERRNO",
"syscalls": [
{ "name": "read", "action": "SCMP_ACT_ALLOW" },
{ "name": "write", "action": "SCMP_ACT_ALLOW" },
... a long list of allowed syscalls ...
]
}
We'll build this list from the Docker default and then remove the ones we don't want.
Since this is a design, we don't need the exact list.