| 1 | //go:build linux |
| 2 | |
| 3 | package render |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "time" |
| 13 | |
| 14 | "golang.org/x/sys/unix" |
| 15 | ) |
| 16 | |
| 17 | // limitMemory is render-sandbox's limit on itself and so on the browser |
| 18 | // and every process it starts: a data segment (private writable memory) |
| 19 | // of at most MemoryLimit, soft and hard. Nothing in the sandbox can raise |
| 20 | // it: that needs CAP_SYS_RESOURCE in the host's user namespace. The |
| 21 | // address space is not limited: the browser reserves far more than it |
| 22 | // uses (V8's sandbox alone is a terabyte, mapped inaccessible). |
| 23 | func limitMemory() error { |
| 24 | return unix.Setrlimit(unix.RLIMIT_DATA, &unix.Rlimit{Cur: MemoryLimit, Max: MemoryLimit}) |
| 25 | } |
| 26 | |
| 27 | // reasonLimits is render-sandbox's "failed" reason when it cannot set |
| 28 | // limitMemory. |
| 29 | const reasonLimits = "limits" |
| 30 | |
| 31 | // memoryEvery is how often the runner sums the sandbox's memory. |
| 32 | const memoryEvery = 100 * time.Millisecond |
| 33 | |
| 34 | // watchMemory sums the resident memory of every process in the sandbox |
| 35 | // (its PID namespace's init and all below it, read from the host's |
| 36 | // /proc) every memoryEvery, and calls over once, when the sum is above |
| 37 | // limit. The returned stop ends the watch and waits for it. |
| 38 | func watchMemory(s *Sandbox, limit int64, over func()) (stop func()) { |
| 39 | done := make(chan struct{}) |
| 40 | // The init is known once Launch returns; read once, its number |
| 41 | // cannot name another process while the sandbox runs. |
| 42 | pid := s.initPID() |
| 43 | var wg sync.WaitGroup |
| 44 | wg.Add(1) |
| 45 | go func() { |
| 46 | defer wg.Done() |
| 47 | tick := time.NewTicker(memoryEvery) |
| 48 | defer tick.Stop() |
| 49 | for { |
| 50 | if pid > 0 && treeRSS(pid) > limit { |
| 51 | over() |
| 52 | return |
| 53 | } |
| 54 | select { |
| 55 | case <-done: |
| 56 | return |
| 57 | case <-s.done: |
| 58 | return |
| 59 | case <-tick.C: |
| 60 | } |
| 61 | } |
| 62 | }() |
| 63 | var once sync.Once |
| 64 | return func() { |
| 65 | once.Do(func() { close(done) }) |
| 66 | wg.Wait() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // initPID is the sandbox's PID namespace init, from its pidfd, or 0. |
| 71 | func (s *Sandbox) initPID() int { |
| 72 | s.mu.Lock() |
| 73 | fd := s.pidfd |
| 74 | s.mu.Unlock() |
| 75 | if fd < 0 { |
| 76 | return 0 |
| 77 | } |
| 78 | raw, err := os.ReadFile(fmt.Sprintf("/proc/self/fdinfo/%d", fd)) |
| 79 | if err != nil { |
| 80 | return 0 |
| 81 | } |
| 82 | for _, line := range strings.Split(string(raw), "\n") { |
| 83 | if v, ok := strings.CutPrefix(line, "Pid:"); ok { |
| 84 | n, _ := strconv.Atoi(strings.TrimSpace(v)) |
| 85 | return max(n, 0) |
| 86 | } |
| 87 | } |
| 88 | return 0 |
| 89 | } |
| 90 | |
| 91 | // maxProcs bounds the processes one sum reads. |
| 92 | const maxProcs = 1 << 16 |
| 93 | |
| 94 | // treeRSS is the resident memory, in bytes, of root and every process |
| 95 | // below it. Shared pages count once a process, so the sum is never less |
| 96 | // than what the processes hold. |
| 97 | func treeRSS(root int) int64 { |
| 98 | children := map[int][]int{} |
| 99 | dir, err := os.Open("/proc") |
| 100 | if err != nil { |
| 101 | return 0 |
| 102 | } |
| 103 | names, _ := dir.Readdirnames(maxProcs) |
| 104 | dir.Close() |
| 105 | for _, name := range names { |
| 106 | pid, err := strconv.Atoi(name) |
| 107 | if err != nil || pid <= 0 { |
| 108 | continue |
| 109 | } |
| 110 | if ppid := parentFromStat(pid); ppid > 0 { |
| 111 | children[ppid] = append(children[ppid], pid) |
| 112 | } |
| 113 | } |
| 114 | page := int64(os.Getpagesize()) |
| 115 | var sum int64 |
| 116 | seen := map[int]bool{} |
| 117 | queue := []int{root} |
| 118 | for len(queue) > 0 && len(seen) < maxProcs { |
| 119 | pid := queue[0] |
| 120 | queue = queue[1:] |
| 121 | if seen[pid] { |
| 122 | continue |
| 123 | } |
| 124 | seen[pid] = true |
| 125 | sum += residentPages(pid) * page |
| 126 | queue = append(queue, children[pid]...) |
| 127 | } |
| 128 | return sum |
| 129 | } |
| 130 | |
| 131 | // parentFromStat is pid's parent from /proc/<pid>/stat, or 0. The |
| 132 | // command name, in parentheses, may hold anything: the fields after its |
| 133 | // last ")" are the kernel's. |
| 134 | func parentFromStat(pid int) int { |
| 135 | raw, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat") |
| 136 | if err != nil { |
| 137 | return 0 |
| 138 | } |
| 139 | i := bytes.LastIndexByte(raw, ')') |
| 140 | if i < 0 { |
| 141 | return 0 |
| 142 | } |
| 143 | fields := strings.Fields(string(raw[i+1:])) |
| 144 | if len(fields) < 2 { |
| 145 | return 0 |
| 146 | } |
| 147 | n, _ := strconv.Atoi(fields[1]) |
| 148 | return n |
| 149 | } |
| 150 | |
| 151 | // residentPages is pid's resident set in pages, from /proc/<pid>/statm. |
| 152 | func residentPages(pid int) int64 { |
| 153 | raw, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/statm") |
| 154 | if err != nil { |
| 155 | return 0 |
| 156 | } |
| 157 | fields := strings.Fields(string(raw)) |
| 158 | if len(fields) < 2 { |
| 159 | return 0 |
| 160 | } |
| 161 | n, _ := strconv.ParseInt(fields[1], 10, 64) |
| 162 | return max(n, 0) |
| 163 | } |
| 164 | |