//go:build linux package webread import ( "runtime/debug" "golang.org/x/sys/unix" ) // Memory bounds. A Go process reserves about 1.2 GB of address space // before main runs, so a 1 GB RLIMIT_AS (the spec's figure) would end it // at its first large allocation. The 1 GB bound is on RLIMIT_DATA, which // counts the writable memory the heap actually maps; RLIMIT_AS stays as a // backstop above the runtime's reservations. const ( limitData = 1 << 30 limitAddressSpace = 4 << 30 limitCPU = 120 ) // limitSelf bounds the fetcher process: 1 GB of data, 120 s of CPU, no // core file, files no larger than it ever writes, and few descriptors. // Hard limits too, so nothing in the process can raise them. Then it // installs the filter that stops it starting any other process. func limitSelf() error { debug.SetMemoryLimit(limitData * 3 / 4) for _, l := range []struct { resource int value uint64 }{ {unix.RLIMIT_DATA, limitData}, {unix.RLIMIT_AS, limitAddressSpace}, {unix.RLIMIT_CPU, limitCPU}, {unix.RLIMIT_CORE, 0}, {unix.RLIMIT_FSIZE, max(MaxImageFile, MaxManifestBytes)}, {unix.RLIMIT_NOFILE, 256}, } { if err := unix.Setrlimit(l.resource, &unix.Rlimit{Cur: l.value, Max: l.value}); err != nil { return err } } // No child processes (seccomp_linux.go), before any input is read. return noChildren() }