procnetsocket

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package procnetsocket reads Linux socket state from /proc/net/.

This package is in builtins/internal/ and is therefore exempt from the builtinAllowedSymbols allowlist check. It may use OS-specific APIs freely.

Sandbox bypass

All Read* functions intentionally bypass the AllowedPaths sandbox (callCtx.OpenFile) and call os.Open directly. This is safe because procPath is always a kernel-managed pseudo-filesystem root (/proc by default) that is hardcoded by the caller — it is never derived from user-supplied input and cannot be redirected by a shell script. The caller is responsible for ensuring that procPath remains a safe, non-user-controlled path.

Index

Constants

View Source
const DefaultProcPath = procpath.Default

DefaultProcPath is the default proc filesystem root.

View Source
const MaxEntries = 100_000

MaxEntries caps the number of socket entries retained in memory per /proc/net/ file to prevent memory exhaustion on hosts with very large socket tables.

View Source
const MaxLineBytes = 1 << 20 // 1 MiB

MaxLineBytes is the per-line buffer cap for the /proc/net/ scanner.

View Source
const MaxTotalLines = MaxEntries * 10 // 1 000 000 lines

MaxTotalLines caps the total number of lines (valid + malformed/skipped) scanned per Read* call. This bounds CPU time for pathological files with many malformed/non-matching lines before MaxEntries valid entries are found. MaxEntries is the memory guard; MaxTotalLines is the scan-time guard.

Variables

View Source
var ErrMaxEntries = errors.New("procnetsocket: socket table truncated: exceeded MaxEntries limit")

ErrMaxEntries is returned when the socket table exceeds MaxEntries entries. Callers should treat this as a hard failure: the table was truncated and output may be missing active sockets.

View Source
var ErrMaxTotalLines = errors.New("procnetsocket: socket table truncated: exceeded MaxTotalLines limit")

ErrMaxTotalLines is returned when more than MaxTotalLines lines are scanned. Callers should treat this as a hard failure: the table was truncated and output may be missing active sockets.

Functions

This section is empty.

Types

type SocketEntry

type SocketEntry struct {
	Kind        SocketKind
	State       string
	RecvQ       uint64
	SendQ       uint64
	LocalAddr   string
	LocalPort   string
	PeerAddr    string
	PeerPort    string
	UID         uint32
	Inode       uint64
	HasExtended bool
}

SocketEntry holds a parsed socket entry from /proc/net/.

func ReadTCP4

func ReadTCP4(ctx context.Context, procPath string) ([]SocketEntry, error)

ReadTCP4 reads procPath/net/tcp and returns IPv4 TCP socket entries.

Sandbox bypass: os.Open is used directly; path is derived from procPath, a hardcoded kernel pseudo-filesystem root never supplied by user input.

Defence-in-depth: ".." components are always rejected regardless of context.

func ReadTCP6

func ReadTCP6(ctx context.Context, procPath string) ([]SocketEntry, error)

ReadTCP6 reads procPath/net/tcp6 and returns IPv6 TCP socket entries.

Sandbox bypass: same rationale as ReadTCP4. Defence-in-depth: same ".." guard as ReadTCP4.

func ReadUDP4

func ReadUDP4(ctx context.Context, procPath string) ([]SocketEntry, error)

ReadUDP4 reads procPath/net/udp and returns IPv4 UDP socket entries.

Sandbox bypass: same rationale as ReadTCP4. Defence-in-depth: same ".." guard as ReadTCP4.

func ReadUDP6

func ReadUDP6(ctx context.Context, procPath string) ([]SocketEntry, error)

ReadUDP6 reads procPath/net/udp6 and returns IPv6 UDP socket entries.

Sandbox bypass: same rationale as ReadTCP4. Defence-in-depth: same ".." guard as ReadTCP4.

func ReadUnix

func ReadUnix(ctx context.Context, procPath string) ([]SocketEntry, error)

ReadUnix reads procPath/net/unix and returns Unix domain socket entries.

Sandbox bypass: same rationale as ReadTCP4. Defence-in-depth: same ".." guard as ReadTCP4.

type SocketKind

type SocketKind int

SocketKind identifies the protocol family of a parsed socket entry.

const (
	KindTCP4 SocketKind = iota
	KindTCP6
	KindUDP4
	KindUDP6
	KindUnix
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL