| 1 | //go:build linux |
| 2 | |
| 3 | package webread |
| 4 | |
| 5 | import ( |
| 6 | "runtime/debug" |
| 7 | |
| 8 | "golang.org/x/sys/unix" |
| 9 | ) |
| 10 | |
| 11 | // Memory bounds. A Go process reserves about 1.2 GB of address space |
| 12 | // before main runs, so a 1 GB RLIMIT_AS (the spec's figure) would end it |
| 13 | // at its first large allocation. The 1 GB bound is on RLIMIT_DATA, which |
| 14 | // counts the writable memory the heap actually maps; RLIMIT_AS stays as a |
| 15 | // backstop above the runtime's reservations. |
| 16 | const ( |
| 17 | limitData = 1 << 30 |
| 18 | limitAddressSpace = 4 << 30 |
| 19 | limitCPU = 120 |
| 20 | ) |
| 21 | |
| 22 | // limitSelf bounds the fetcher process: 1 GB of data, 120 s of CPU, no |
| 23 | // core file, files no larger than it ever writes, and few descriptors. |
| 24 | // Hard limits too, so nothing in the process can raise them. Then it |
| 25 | // installs the filter that stops it starting any other process. |
| 26 | func limitSelf() error { |
| 27 | debug.SetMemoryLimit(limitData * 3 / 4) |
| 28 | for _, l := range []struct { |
| 29 | resource int |
| 30 | value uint64 |
| 31 | }{ |
| 32 | {unix.RLIMIT_DATA, limitData}, |
| 33 | {unix.RLIMIT_AS, limitAddressSpace}, |
| 34 | {unix.RLIMIT_CPU, limitCPU}, |
| 35 | {unix.RLIMIT_CORE, 0}, |
| 36 | {unix.RLIMIT_FSIZE, max(MaxImageFile, MaxManifestBytes)}, |
| 37 | {unix.RLIMIT_NOFILE, 256}, |
| 38 | } { |
| 39 | if err := unix.Setrlimit(l.resource, &unix.Rlimit{Cur: l.value, Max: l.value}); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | } |
| 43 | // No child processes (seccomp_linux.go), before any input is read. |
| 44 | return noChildren() |
| 45 | } |
| 46 | |