mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
fix(sandbox,traffic): keep sandbox command output and free scan dial slots
Docker exec no longer wraps commands in setsid, which exited immediately and cut stdout after about two seconds. Netstack releases the pending-dial slot once the handshake finishes, raises the in-flight limits, and treats local file-descriptor exhaustion as a silent drop. The gateway container gets a higher open-file limit.
This commit is contained in:
1 parent
ee5ac6e6dd
commit
8bac4960e7
6 files changed
+346
-24
No files matched your search
+54
-10
@@ -34,9 +34,9 @@ import (
|
||||
const (
|
||||
netstackNICID tcpip.NICID = 1
|
||||
netstackDefaultMTU uint32 = 1500
|
||||
netstackMaxInFlightTCP = 1024
|
||||
netstackMaxPendingDials = 512
|
||||
netstackDefaultDialTTL = 5 * time.Second
|
||||
netstackMaxInFlightTCP = 8192
|
||||
netstackMaxPendingDials = 4096
|
||||
netstackDefaultDialTTL = 2500 * time.Millisecond
|
||||
)
|
||||
|
||||
// NetstackConfig configures the gVisor user-space TCP/IP stack attached to a
|
||||
@@ -331,20 +331,29 @@ func (n *NetstackGateway) handleTCPForward(r *tcp.ForwarderRequest) {
|
||||
dstPort := int(id.LocalPort)
|
||||
|
||||
// Guard against SYN flood / full-port-scan goroutine exhaustion.
|
||||
// Drop silently (sendReset=false) when saturated so scanners back off and
|
||||
// retry instead of falsely classifying open/filtered ports as "closed".
|
||||
select {
|
||||
case n.dialCh <- struct{}{}:
|
||||
default:
|
||||
r.Complete(true)
|
||||
r.Complete(false)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() { <-n.dialCh }()
|
||||
n.processTCPForward(r, srcIP, srcPort, dstIP, dstPort)
|
||||
released := false
|
||||
releaseSlot := func() {
|
||||
if !released {
|
||||
released = true
|
||||
<-n.dialCh
|
||||
}
|
||||
}
|
||||
defer releaseSlot()
|
||||
n.processTCPForward(r, srcIP, srcPort, dstIP, dstPort, releaseSlot)
|
||||
}()
|
||||
}
|
||||
|
||||
func (n *NetstackGateway) processTCPForward(r *tcp.ForwarderRequest, srcIP string, srcPort int, dstIP string, dstPort int) {
|
||||
func (n *NetstackGateway) processTCPForward(r *tcp.ForwarderRequest, srcIP string, srcPort int, dstIP string, dstPort int, releaseDialSlot func()) {
|
||||
start := time.Now()
|
||||
src := n.backend.ResolveSource(srcIP)
|
||||
if src.SrcIP == "" {
|
||||
@@ -381,13 +390,22 @@ func (n *NetstackGateway) processTCPForward(r *tcp.ForwarderRequest, srcIP strin
|
||||
dialTarget = targetHostPort
|
||||
}
|
||||
|
||||
dialCtx, cancel := context.WithTimeout(n.ctx, n.cfg.DialTimeout)
|
||||
dialTimeout := n.cfg.DialTimeout
|
||||
if upstream != nil && dialTimeout < 5*time.Second {
|
||||
dialTimeout = 5 * time.Second
|
||||
}
|
||||
dialCtx, cancel := context.WithTimeout(n.ctx, dialTimeout)
|
||||
dialFn := n.cfg.DialFunc
|
||||
if dialFn == nil {
|
||||
dialFn = dialTCPThroughUpstream
|
||||
}
|
||||
dstConn, err := dialFn(dialCtx, upstream, dialTarget, n.cfg.DialTimeout)
|
||||
dstConn, err := dialFn(dialCtx, upstream, dialTarget, dialTimeout)
|
||||
cancel()
|
||||
// Dial phase finished: free the pending-dial slot immediately so long-lived
|
||||
// established connections do not starve new outbound dials or port scans.
|
||||
if releaseDialSlot != nil {
|
||||
releaseDialSlot()
|
||||
}
|
||||
if err != nil {
|
||||
// Distinguish closed/refused (send RST -> nmap "closed") from timeout/drop
|
||||
// (drop silently -> nmap "filtered").
|
||||
@@ -421,6 +439,28 @@ func (n *NetstackGateway) processTCPForward(r *tcp.ForwarderRequest, srcIP strin
|
||||
r.Complete(false)
|
||||
if tcpErr != nil {
|
||||
_ = dstConn.Close()
|
||||
// Remote TCP handshake genuinely succeeded (e.g. nmap -sT received SYN-ACK,
|
||||
// sent ACK, and immediately sent RST before CreateEndpoint returned). Record
|
||||
// the allowed flow so open-port scan hits are visible in net_flows.
|
||||
end := time.Now()
|
||||
n.backend.RecordNetFlow(NetFlowRecord{
|
||||
TS: start.Unix(),
|
||||
EndedAt: end.Unix(),
|
||||
DurationMS: end.Sub(start).Milliseconds(),
|
||||
Protocol: "tcp",
|
||||
DstIP: dstIP,
|
||||
DstPort: dstPort,
|
||||
DstHost: dstHost,
|
||||
Action: "allow",
|
||||
UpstreamProxy: upstreamLabel,
|
||||
SandboxScope: src.Scope,
|
||||
SandboxOwnerID: src.OwnerID,
|
||||
ContainerID: src.ContainerID,
|
||||
SessionID: src.SessionID,
|
||||
IntentID: src.IntentID,
|
||||
SrcIP: src.SrcIP,
|
||||
SrcPort: srcPort,
|
||||
})
|
||||
return
|
||||
}
|
||||
rawClientConn := gonet.NewTCPConn(&wq, ep)
|
||||
@@ -1051,7 +1091,11 @@ func isDialTimeoutErr(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || os.IsTimeout(err) {
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) || os.IsTimeout(err) {
|
||||
return true
|
||||
}
|
||||
// Local gateway resource exhaustion must never synthesize a remote RST ("closed").
|
||||
if errors.Is(err, syscall.EMFILE) || errors.Is(err, syscall.ENFILE) || errors.Is(err, syscall.EADDRNOTAVAIL) {
|
||||
return true
|
||||
}
|
||||
var ne net.Error
|
||||
|
||||
Reference in new issue
Block a user