mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
A sandbox fixed at the child's start can't tell this machine's loopback, its own addresses and the LAN apart from the internet as a rule changes, so the child's sandbox now refuses it every connection of its own, and the broker looks up each name and opens each connection for it, once its own read of the VM's policy and of the host's addresses allows the address, as the floor does. A child that a VM takes over reaches this machine's loopback, its own addresses and the private and link-local ranges only where a rule names the address, as the VM does; a company proxy, which the child still speaks to with its password, reaches whatever it reaches. - wire.go: lookup (a host name, absolute, with its trailing dot), dial (an address and a port, or the https or http company proxy of the broker's config, never both), the connection as the reply's one descriptor, and cancel, for a lookup or a dial the child gave up on, which frees its slot. - dial.go: the broker's own egress (newBrokerNet: the VM's built-in policies, its policy files, looked at on every dial, and the host's addresses), lookupable, broker.lookup, dial and dialProxy, within 20 s and 15 s; the child's side (brokerClient.lookup, dial, dialProxy) wired into its egress by throughHost; the resolver's errors as codes the child makes into net.DNSError again. - broker.go: the caps (256 lookups, 1024 connections at once); its line for each failed lookup and connection, which the VM drives, at most 20 a minute and then one that says it stops (logFailed, perMinute in limits.go, which egress.abandoned now shares). - child.go and confine.go: every lookup and connection through the broker; the child lowers its open files, soft and hard, before it's confined (limitFiles). - seatbelt: the child's profile allows no network out, TCP, UDP or a unix socket, nor mDNSResponder. - landlock: the seccomp filter refuses every socket but a netlink route socket, connect, socketpair, listen and MSG_FASTOPEN on sendto, sendmsg and sendmmsg; Landlock no longer opens the resolver's files below /etc. - Tests: the broker's dial and its rules (TestBrokerDial, TestBrokerDialFollowsRules, TestBrokerDialProxy), lookups and their errors, cancel, the client over a real socketpair, TestBrokerFailedLogCapped, TestPerMinute, the filter's programs, the child's own connections refused under Seatbelt and under Landlock and seccomp. - Docs: the design note's broker (what crosses to the broker: the names and addresses, never a connection's bytes), its threat check, network's The child's sandbox and proxy.log, processes, attack vectors, troubleshooting, testing, tests, the proxy's README; CHANGELOG, with the company proxy's reach. Co-Authored-By: Claude Opus 5.5 <[email protected]>
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package proxy
|
|
|
|
// What the proxy holds for a VM at once, past what each connection holds: its connections (conns,
|
|
// maxConns) and its requests upstream (requests, maxRequests), each logged at most once a minute
|
|
// when it's reached (everyMinute), and the lines a VM's failures drive, a number of them a minute
|
|
// (perMinute).
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// maxConns is how many connections with the VM's password a VM's proxy holds at once, of every
|
|
// kind: plain HTTP's and idle kept-alive ones, and a request's that outlives its client, as well
|
|
// as CONNECTs. Twice maxConnects, so a tunnel past its limit gets its 503 first, and plain HTTP gets
|
|
// as many again. A request on a connection past it gets 503, and the connection closes.
|
|
const maxConns = 2 * maxConnects
|
|
|
|
// maxWaiting is how many connections a VM's proxy holds at once that have sent no request with the
|
|
// VM's password yet: a new one past it closes the one that has waited longest (conns), so another
|
|
// VM or program on this machine that opens connections without end can't take the VM's room.
|
|
const maxWaiting = 128
|
|
|
|
// maxRequests is how many requests a VM's proxy has upstream at once, plain HTTP's and those in
|
|
// intercepted tunnels alike: each is a connection of its own to the host, so the tunnels' HTTP/2
|
|
// streams (streamsMax each) can't add up to more connections than the proxy has descriptors.
|
|
const maxRequests = maxConns
|
|
|
|
// everyMinute lets one caller through at most once a minute: a line that each refusal past a limit
|
|
// would log, so a VM that keeps reaching it can't fill the log.
|
|
type everyMinute struct{ last atomic.Int64 }
|
|
|
|
// allow reports whether a line may be logged at now, and if so, counts it.
|
|
func (e *everyMinute) allow(now time.Time) bool {
|
|
for {
|
|
last := e.last.Load()
|
|
if last != 0 && now.UnixNano()-last < int64(time.Minute) {
|
|
return false
|
|
}
|
|
if e.last.CompareAndSwap(last, now.UnixNano()) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
// perMinute lets up to a number of lines of one kind through each minute, and then one that says
|
|
// the rest that minute aren't logged: a line for each failure that a VM drives, such as a lookup it
|
|
// gave up on or a connection that failed, so a VM that keeps causing them can't fill the log.
|
|
type perMinute struct {
|
|
mu sync.Mutex
|
|
at time.Time // when the minute that n counts began
|
|
n int
|
|
}
|
|
|
|
// next counts a line at now: whether it goes to the log (log), and whether it's the first past
|
|
// limit, which logs that the rest that minute aren't (capped).
|
|
func (p *perMinute) next(now time.Time, limit int) (log, capped bool) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if now.Sub(p.at) > time.Minute {
|
|
p.at, p.n = now, 0
|
|
}
|
|
p.n++
|
|
return p.n <= limit, p.n == limit+1
|
|
}
|
|
|
|
// requests are the slots of a VM's requests upstream (maxRequests).
|
|
type requests struct {
|
|
slots chan struct{}
|
|
log *log.Logger
|
|
said everyMinute
|
|
}
|
|
|
|
func newRequests(n int, l *log.Logger) *requests {
|
|
return &requests{slots: make(chan struct{}, n), log: l}
|
|
}
|
|
|
|
// take holds a slot for a request upstream until release, or answers w with 503 when every one is
|
|
// held, which a client may try again, and says so in the proxy's log at most once a minute.
|
|
func (q *requests) take(w http.ResponseWriter) (release func(), ok bool) {
|
|
select {
|
|
case q.slots <- struct{}{}:
|
|
return func() { <-q.slots }, true
|
|
default:
|
|
}
|
|
n := strconv.Itoa(cap(q.slots))
|
|
if q.said.allow(time.Now()) {
|
|
q.log.Printf("%s requests upstream at once, the VM's limit: refusing more until one ends", n)
|
|
}
|
|
http.Error(w, "devvm proxy: "+n+" requests upstream at once already; try again", http.StatusServiceUnavailable)
|
|
return nil, false
|
|
}
|