11 KiB
The Linux widget helper's system call allowlist
On Linux a helper process, the Qt-free TelegramWidgetHelper executable
started as <path> 3 with the channel on descriptor 3, serves each
widget's engine and, in builds with the private Qt API, its validator and
renderer too. It lies in TelegramHelpers/ next to the client's binary, or
in the package's libexec directory (its TelegramDesktop/ folder) in a
package-managed build, and the lockdown is compiled only into it. Before it
serves its first widget, while it still has one thread,
Platform::WidgetHelper::LockDownHelper() in
platform/linux/widget_lockdown_linux.cpp loads the time zone, empties its
root where the system allows it, drops its capabilities, sets
no-new-privileges, applies a Landlock ruleset that allows nothing where the
kernel has Landlock, and last installs the seccomp filter that
BuildFilter() builds for x86_64 and aarch64. The filter kills the process
for a call of any other architecture and, on x86_64, for an x32 call, and it
answers every call it does not name with ENOSYS.
The allowlist follows, by kind, in the order BuildFilter() checks it.
"Seen" means that strace recorded the call after the filter was installed:
while the Qt-free helper served the framework's attack corpus and
conformance set on glibc 2.39, 2.28 and 2.41, and again while the helper
that renders, started by a Debug Telegram without an account through a
strace wrapper at its package-managed install's helper path, rendered the
attack corpus, the conformance automatic set and a widget reading its time
zone on the same three versions and, on 2.39, also widget-sandbox's widget
corpus in its day and night partitions. The versions are named where only
some of them make it; the other calls are fallbacks or paths no current
widget takes. Measured again on the Qt-free helper, the list lost
clock_getres, time, nanosleep, clock_nanosleep, sched_yield,
rt_sigreturn, prctl with PR_GET_NAME, the futex operations
FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, FUTEX_WAKE_OP and
FUTEX_WAKE_BITSET, and the madvise advice MADV_NORMAL, MADV_RANDOM,
MADV_SEQUENTIAL, MADV_WILLNEED and MADV_FREE, because nothing recorded
them and no code linked into the helper makes them after the lockdown. The
helper that renders added nothing to the list: on glibc 2.39, 2.28 and 2.41
it made no call and no argument value that the filter did not already
allow, and the only call refused was clone3, on 2.39 and 2.41.
- The channel (
kChannelCalls, then the rules forrecvfromandsendto): reading and writing the channel and stderr.recvfromonly with the flags 0 orMSG_DONTWAIT | MSG_PEEKandsendtoonly withMSG_NOSIGNAL, both only with a null address (both words of the address zero; of the flags only the low word, which is what the kernel reads); any other flags,MSG_OOBamong them, or any address getENOSYS:PosixStream::read()andPosixStream::write()inplatform/posix/widget_channel_posix.cpp, throughrecv()with flags 0 andsend()withMSG_NOSIGNAL, which glibc makes as these calls with a null address, seen; the Debug probe's descriptor listing peeks withMSG_DONTWAIT | MSG_PEEK. The kernel's out-of-band and addressed socket paths stay out of the helper's reach.shutdown:PosixStream::shutdown(),SHUT_WRthenSHUT_RD, when the engine end stops. Seen.write: stderr is unbuffered, so eachfprintf(stderr, ...)is awrite:Exit()in_other/widget_helper.cppprints a reasonserve()returned, and an assertion or libstdc++'s terminate handler prints its message beforeabort().writev: glibc's fatal error reports (__libc_message(): malloc corruption, stack-protector and fortify failures) are onewritevto stderr.read: a plain read of a descriptor the helper holds. The channel is read withrecv(), so only the Debug probe's status read uses it.close: the Debug probe closes its status descriptor after the lockdown, before it lists its descriptors;PosixStream's destructor would too, but the helper ends withstd::_Exit()before it runs.
- Memory management (
kMemoryCalls, then the rules formmap,mprotectandmadvise):brk: malloc's heap.mallopt(M_ARENA_MAX, 1)in the helper's LinuxAdopt()in_other/widget_helper_linux.cppleaves the helper one arena, which grows and trims withbrk. Seen.munmap: QuickJS's arena slabs (arena_slab_destroy()inquickjs.c), malloc's large blocks and glibc's thread stacks. Seen.mremap: glibc'srealloc()of a block that malloc mapped on its own.mmapandmprotect, withEPERMwhen the low word ofprothasPROT_EXEC: glibc'spthread_create()maps a thread stackPROT_NONEand makes all of it but the guard page readable and writable withmprotect; QuickJS's 64 KiB arena slabs (arena_slab_take()) and malloc's large blocks are mapped readable and writable. Seen. The executable's stack is not executable, so after the start nothing honest maps executable memory.madvisewithMADV_DONTNEED,MADV_GUARD_INSTALLorMADV_GUARD_REMOVE, any other adviceENOSYS:MADV_DONTNEEDreleases the free pages of QuickJS's arena slabs (arena_slab_release_pages()) and, in glibc, a finished thread's stack, seen; the two guard values are the guard pages glibc'spthread_create()puts on thread stacks from 2.42 on, not seen.
- Futexes (the rule for
futex): withFUTEX_PRIVATE_FLAGandFUTEX_CLOCK_REALTIMEmasked off, the operation must beFUTEX_WAIT,FUTEX_WAKEorFUTEX_WAIT_BITSET; the priority-inheritance operations and anything else getENOSYS.- Private
WAITandWAKE: glibc's mutexes behindstd::mutex, and the wake of astd::condition_variable. Seen. - Private
WAIT_BITSETwith the realtime clock flag:pthread_cond_wait()behindstd::condition_variable::wait(). Seen. - Shared
WAITandWAKE: libstdc++'sstd::promiseandstd::future(__atomic_futex_unsigned), the handshake with whichJSThreadwaits for its thread to start, and on 2.28 the wait ofpthread_join(). Seen. - Shared
WAIT_BITSETwith the realtime clock flag:pthread_join()on 2.39 and 2.41, waiting for the kernel to clear a finished thread's id. Seen.
- Private
- Clocks (
kClockCalls):clock_gettime:CLOCK_THREAD_CPUTIME_IDalways reaches the kernel, and the engine's CPU budget reads it (QuickJSContext::threadCpuTime(), also from QuickJS's interrupt handler). Seen. The other clocks (std::chrono, QuickJS'sjs__hrtime_ns()) are answered by the vDSO and need the call only where it cannot answer.gettimeofday: QuickJS'sjs__gettimeofday_us()behindDate.now(),new Date()and the seed ofMath.random(), answered by the vDSO. Not seen.
- Threads (
kThreadCalls, then the rules forprctl,cloneandclone3): starting, naming and ending the helper'sJSThreads (widget.sandboxfor each runtime,widget.probe,widget.reaperand, where the helper renders, each widget'swidget.validatorand its served host'stwidget-host), and their signal masks.clone, only when the low word offlagsis exactly glibc's thread setCLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID, and any other flags kill the process: glibc'spthread_create(), whichJSThreadcalls with an 8 MiB stack, afterclone3gotENOSYSwhere glibc tries that first. Seen.clone3, answeredENOSYS: glibc 2.34 and later try it first, and its flags sit behind a pointer the filter cannot read. glibc 2.39 and 2.41 remember the answer and useclonefor every later thread. Seen once per helper on 2.39 and 2.41.set_robust_listand, where the kernel headers define it,rseq: glibc's start of a new thread registers its robust-mutex list and its restartable-sequence area. Seen,rseqon 2.39 and 2.41.rt_sigprocmask:pthread_create()blocks every signal around thecloneand the new thread restores its creator's mask; a finishing thread blocks every signal butSIGSETXID; newer glibc's firstpthread_create()unblocksSIGCANCELandSIGSETXID;raise()andabort()use it too. Seen on 2.39 and 2.41.prctlwithPR_SET_NAMEonly, any other optionENOSYS:JSThread::entry()names its thread withpthread_setname_np(pthread_self(), ...), which glibc makes asprctl(PR_SET_NAME)for the calling thread. Seen.exit: the end of a thread, in glibc'sstart_thread()after the thread function returns. Seen.
- Signals and the process's end (
kSignalCalls, then the rule fortgkill):rt_sigaction: newer glibc's firstpthread_create()installs glibc'sSIGSETXIDhandler, seen once per helper on 2.39 and 2.41;abort()resetsSIGABRT.restart_syscall: the kernel resumes a timed futex wait that a stop and continue interrupted, as a debugger or a stopped helper causes. Not seen.getpid,gettid, andtgkillonly when itstgidis the helper's own pid as read when the filter was built (1 inside the PID namespace), elseENOSYS: glibc'sraise(), and soabort(), sends its signal withtgkill(getpid(), gettid(), ...), andpthread_kill()signals a thread of the helper. Not seen.exit_group: the end of the process,std::_Exit()inExit()in_other/widget_helper.cpp. Seen once per helper.
- Random (
kRandomCalls):getrandom: the engine'srandomhost request (randomBytes()insandbox_engine.cpp) behindcrypto.getRandomValues()andcrypto.randomUUID(). Seen.
- Killed (
kKilledCalls; the process dies withSIGSYS):execveandexecveat(another program),socketandconnect(a new or a connected endpoint),ptrace,process_vm_readvandprocess_vm_writev(another process), and on x86_64forkandvfork. aarch64 has neither, and itsfork()is aclonewithout the thread set, which theclonerule kills. No honest engine makes them. - Everything else gets
ENOSYS: opening or examining files (openat, thestatfamily),ioctl,kill,socketpair,sendmsg,recvmsg,poll,mount,unshare,setns,bpf,userfaultfd,io_uring_*,perf_event_open,keyctl,memfd_create, andseccompand Landlock themselves. glibc and libstdc++ readENOSYSas a missing kernel feature and take their fallbacks instead of crashing; in the strace passes onlyclone3got it, apart from the Debug probe's own opens.
A later addition goes inside one of these kinds and only for a call the
helper is measured to make there; a call that opens, creates or connects
anything is never added. The first rule whose call number matches decides,
and each kind's plain table comes before that kind's argument rules and every
table before the killed calls: a call number that has an argument rule or is
killed (recvfrom, sendto, mmap, mprotect, madvise, futex, prctl,
tgkill, clone, clone3, or a killed call) must never be added to a plain
table, or the rule it already has silently becomes an unconditional allow.