Documentation
¶
Overview ¶
Package sandboxposture collects the externally-observable Linux security posture of a running Sentry (`gvisor_sentry boot`) process, and compares it against expectations depending on the runsc configuration.
Index ¶
- Constants
- Variables
- func CoreTagsSupported() (bool, error)
- func DeviceCgroupOf(pid int, procRoot string) (string, error)
- func Diff(want, got *Posture, opts ...cmp.Option) string
- func ExecutableByOthers(path string) (bool, error)
- func GoDebug(platform string, profile bool) string
- func KVMFDClasses() []string
- func NSJoined(name string) string
- func UnsupportedFeatures() []string
- func UserNamespaceMapsID(id int) (bool, error)
- type Attrs
- type Capabilities
- type Credentials
- type ExpectedOpts
- type FDTable
- type Filesystem
- type Namespaces
- type Opts
- type Posture
- type Reference
- type Seccomp
- type Threads
Constants ¶
const ( // NSInherited means the namespace is the same as the reference process's. NSInherited = "inherited" // NSNew means the namespace is *not* the reference process's namespace. NSNew = "new" // NSJoinedPrefix prefixes a namespace that matches the specific namespace // named after the prefix. See `NSJoined`. NSJoinedPrefix = "joined:" )
Normalized namespace classifications, used by every field of `Namespaces`.
const ( // DeviceCgroupConfined means the process's cgroup denies access to devices // outside a whitelist. DeviceCgroupConfined = "confined" // DeviceCgroupUnconfined means the process's cgroup allows every device. DeviceCgroupUnconfined = "unconfined" // DeviceCgroupUnknown means the process's cgroup is not visible from here, // as from a cgroup namespace that does not mount it. DeviceCgroupUnknown = "unknown" // DeviceCgroupSameAsRef means the process is in the reference process's // cgroups, so it has the same device access. DeviceCgroupSameAsRef = "same-as-ref" )
Normalized values for `Attrs.DeviceCgroup`.
const ( // GroupsNone means the process has an empty supplementary group list. GroupsNone = "none" // GroupsSameAsRef means the process's supplementary group list is the same // as the reference process's. GroupsSameAsRef = "same-as-ref" )
Normalized values for `Credentials.Groups`.
const ( // IDSameAsRef means the ID is the reference process's ID. IDSameAsRef = "same-as-ref" // IDNobody is the conventional "nobody" unprivileged ID. IDNobody = "nobody(65534)" // IDRoot is uid/gid 0. Note that inside a user namespace, this means // "root of that user namespace", not necessarily root-userns root. IDRoot = "root(0)" )
Normalized values for `Credentials.UID` and `Credentials.GID`.
const ( // RootSameAsRef means the process shares the reference process's root // directory. RootSameAsRef = "same-as-ref" // RootOwn means the process's root directory is *not* the reference // process's root directory: it has been chrooted or pivot_root'ed away. RootOwn = "own" )
Normalized values for `Filesystem.Root` (as in filesystem).
const ( // MapSameAsRef means the process is in the reference process's user // namespace. MapSameAsRef = "same-as-ref" // MapIdentityOfRef means the process is in a new user namespace whose map // covers exactly the same IDs as the reference process's map, mapped to // themselves. MapIdentityOfRef = "identity-of-ref" // MapNobodyToNobody means the map is the single entry "65534 65534 1". MapNobodyToNobody = "nobody->nobody" // MapNobodyToRef means the map is the single entry "65534 <refuid> 1". MapNobodyToRef = "nobody->ref" // MapEmpty means the map file is empty. // A process in this state has no valid IDs. MapEmpty = "empty" )
Normalized values for `Attrs.UIDMap` and `Attrs.GIDMap`.
const ( // OwnerSameAsProcess means the files under `/proc/<pid>` are owned by the // process's own effective IDs, which is what the kernel does for a // "dumpable" process. OwnerSameAsProcess = "same-as-process" // OwnerRoot means the files under `/proc/<pid>` are owned by 0:0 while // the process itself runs as something else. The kernel does this when a // process is not "dumpable", so this doubles as the observable proxy for // dumpability (see `Attrs.ProcFileOwner`). OwnerRoot = "root" )
Normalized values for `Attrs.ProcFileOwner`.
const ( // LimitSameAsRef means the limit is identical to the reference process's. LimitSameAsRef = "same-as-ref" // LimitUnlimited means both soft and hard limit are `RLIM_INFINITY`. LimitUnlimited = "unlimited/unlimited" )
Normalized values for `Attrs.Limits`.
const ( // CoreTagNone means no thread has a core scheduling cookie, or all threads // inherited the reference process's core scheduling cookie. CoreTagNone = "none" // CoreTagOwn means all threads share one core scheduling cookie, which the // reference process does not have. CoreTagOwn = "own" )
Normalized values for `Posture.CoreTag`.
const ( FDDirectory = "dir" FDSocket = "socket" FDPipe = "pipe" FDNullDev = "null-dev" FDRegular = "regular" FDDeleted = "regular-deleted" FDCharDev = "chardev" FDBlockDev = "blockdev" FDFifo = "fifo" FDSymlink = "symlink" FDUnknown = "unknown" // FDErrorLog is an FD to the error log (`--log`) of the process. FDErrorLog = "error-log" // FDAnonPrefix prefixes anonymous inode FDs, e.g. "anon:eventfd". FDAnonPrefix = "anon:" // FDMemfdPrefix prefixes memfd FDs, e.g. "memfd:systrap-memory". FDMemfdPrefix = "memfd:" )
FD classes reported in `FDTable.Classes`.
const EnvGOMAXPROCSFromCPUNum = envGOMAXPROCSPrefix + "<--cpu-num>"
EnvGOMAXPROCSFromCPUNum is the normalized `Attrs.Environ` entry for `GOMAXPROCS`.
Variables ¶
var PlatformCapSysPtrace = map[string]bool{ "systrap": true, "ptrace": true, }
PlatformCapSysPtrace contains which platforms need CAP_SYS_PTRACE. Platforms absent here are expected to not need it.
Functions ¶
func CoreTagsSupported ¶
CoreTagsSupported reports whether this kernel supports core scheduling. It requires CONFIG_SCHED_CORE and active SMT.
func DeviceCgroupOf ¶
DeviceCgroupOf classifies the device cgroup of the given process. `procRoot` defaults to "/proc".
func Diff ¶
Diff compares a collected `Posture` against an expected one. `posture` field tags determine comparison semantics. Returns the empty string when they match.
func ExecutableByOthers ¶
ExecutableByOthers reports whether a process holding none of the file's owning IDs, and no capability, could execute it. Needed to know if `runsc` is re-exec-able as `nobody`.
func GoDebug ¶
GoDebug returns the expected GODEBUG for a sandbox on the given platform. profile is whether the sandbox runs with --profile.
func KVMFDClasses ¶
func KVMFDClasses() []string
KVMFDClasses returns the FD classes a sandbox on the KVM platform holds beyond the ones other platforms have.
func NSJoined ¶
NSJoined returns the namespace value meaning "this namespace is the one registered under `name` in `Opts.NamespaceTargets`".
func UnsupportedFeatures ¶
func UnsupportedFeatures() []string
UnsupportedFeatures returns the posture-relevant things this kernel cannot report. Callers should skip the whole test when this function returns anything.
func UserNamespaceMapsID ¶
UserNamespaceMapsID reports whether the calling process's user namespace maps the given ID, i.e. whether a child user namespace could be created that maps it.
Types ¶
type Attrs ¶
type Attrs struct {
// Umask is the process's file mode creation mask, e.g. "0022".
Umask string
// SessionLeader reports whether the process is its own session leader.
SessionLeader bool
// ProcessGroupLeader reports whether the process is its own group leader.
ProcessGroupLeader bool
// ChildOfRef reports whether the process's parent is the reference
// process. False under shim.
ChildOfRef bool
// Environ is the environment, sorted, with variables like
// `RUNSC_START_TIME_NANOS` and `GVISOR_ENFORCE_RELEASE` removed, and
// others like `GOMAXPROCS` normalized.
Environ []string
// Argv0 is argv[0], which `runsc` should set to "runsc-sandbox".
Argv0 string
// Limits maps resource names to normalized "<soft>/<hard>" strings, or to
// `LimitSameAsRef`.
Limits map[string]string
// UIDMap and GIDMap classify the process's user namespace ID mappings.
UIDMap string
GIDMap string
// ProcFileOwner is the ownership of the files under `/proc/<pid>`, which
// the kernel gives to root when a process is not dumpable.
// We use this as a proxy for whether `/proc/<pid>/mem` is reachable by
// the process's own user.
ProcFileOwner string
// DeviceCgroup is `DeviceCgroupConfined`, `DeviceCgroupUnconfined`,
// `DeviceCgroupUnknown` or `DeviceCgroupSameAsRef`.
DeviceCgroup string
// Argv is the full command line. Informational only.
Argv []string `posture:"info"`
}
Attrs holds per-process attributes that do not group naturally elsewhere.
type Capabilities ¶
type Capabilities struct {
Inheritable []string
Permitted []string
Effective []string
Bounding []string
Ambient []string
}
Capabilities is a process's capability sets, each as a sorted list, with a "CAP_" prefix.
type Credentials ¶
type Credentials struct {
// UID is the real/effective/saved/filesystem UID, normalized.
UID string
// GID is the group equivalent of `UID`.
GID string
// ExtraGroups is the sorted set of supplementary groups the process holds
// that the reference process did not (excluding the overflow GID).
ExtraGroups []string
// Groups is the raw supplementary group list. Informational only.
Groups string `posture:"info"`
}
Credentials is a process's user and group identity.
type ExpectedOpts ¶
type ExpectedOpts struct {
// Ref describes the process the sandbox was launched from.
Ref Reference
DirectFS bool
// NoDirectFSMounts is true when DirectFS is enabled at the sandbox level
// but no mount uses directfs (for example, directfs is suppressed via
// annotations on every mount, or all mounts are EROFS). The sandbox still
// starts with directfs credentials, capabilities, and umask, but closes
// /proc/self/fd and holds no host directory FDs.
NoDirectFSMounts bool
NetworkMode string
EnableRaw bool
RequiresCapSysPtrace bool
SpecHasUserNamespace bool
NetNamespaceTarget string
CgoEnabled bool
GoDebug string
CoreTags bool
// Cgroup is whether `runsc` put the sandbox in a cgroup.
Cgroup bool
// ChildOfRef is whether the sandbox is expected to be a direct child
// of the reference process. False under shim.
ChildOfRef bool
// Umask defaults to Ref.Umask, then to "0022".
Umask string
// NOFILE defaults to LimitSameAsRef.
NOFILE string
// MEMLOCK defaults to LimitUnlimited.
MEMLOCK string
// ExtraRootEntries, ExtraFDClasses and ExtraMounts widen the corresponding
// expectations.
ExtraRootEntries []string
ExtraFDClasses []string
ExtraMounts []string
}
ExpectedOpts describes the runsc configuration a sandbox was started with, or at least the subset of it that determines its expected security posture.
func (*ExpectedOpts) HostNetwork ¶
func (e *ExpectedOpts) HostNetwork() bool
HostNetwork returns true if the sandbox is using hostinet.
type FDTable ¶
type FDTable struct {
// Classes is the sorted set of distinct FD classes the process holds.
// It is a set, not a count, because the number of memfds or eventfds a
// sandbox holds is an implementation detail while the *kind* of thing it
// holds is its security posture.
//
// Compared as a subset, as the the sandbox holding fewer kinds of FDs
// than expected is not a regression.
Classes []string `posture:"subset"`
// HasDirectories reports whether the process holds any directory FD.
//
// This is critical because a directory FD grants the ability to reach
// anything under it, and survives `chroot` and `pivot_root`.
HasDirectories bool
// Counts is the number of FDs in each class. Informational.
Counts map[string]int `posture:"info"`
// Links is the sorted set of readlink targets with inode numbers stripped,
// so that a failure report says what the unexpected FD actually was.
Links []string `posture:"info"`
}
FDTable describes a process's open file descriptors.
type Filesystem ¶
type Filesystem struct {
// Root is `RootOwn` or `RootSameAsRef`.
Root string
// Entries is a recursive listing of the process's root directory, sorted,
// with paths relative to that root and directories suffixed with "/".
// Does not cross mountpoints.
//
// Compared as a subset: the sandbox may see fewer files than expected,
// but never more.
Entries []string `posture:"subset"`
// Cwd is the working directory as seen from inside the process's own root.
Cwd string
// ReadOnly reports whether the filesystem mounted at the process's root is
// read-only.
ReadOnly bool
// Mounts is the process's mount table, one entry per line, normalized to
// "<mountpoint> <fstype> <flags>" with flags restricted to the
// security-relevant ones + deduped + sorted.
Mounts []string
// RootLink is the raw target of `/proc/<pid>/root`, for debugging.
RootLink string `posture:"info"`
}
Filesystem is a process's view of the filesystem.
type Namespaces ¶
type Namespaces struct {
Mnt string
PID string
IPC string
UTS string
Net string
User string
Cgroup string
Time string
}
Namespaces classifies each of a process's namespaces relative to the reference process and to any named targets.
type Opts ¶
type Opts struct {
// PID is the process to collect.
PID int
// RefPID is the reference process that posture is expressed relative to.
RefPID int
// NamespaceTargets maps a name to a path naming a namespace, such as
// {"container-netns": "/var/run/docker/netns/abc123"}.
NamespaceTargets map[string]string
// ProcRoot is the procfs mount to read through. Defaults to "/proc".
// Its PID namespace must be the caller's, as `CoreTag` is collected by PID.
ProcRoot string
}
Opts is the set of options to `Collect`.
type Posture ¶
type Posture struct {
// Creds is the process's user and group identity.
Creds Credentials
// Caps is the process's capability sets.
Caps Capabilities
// Seccomp is the process's seccomp status.
Seccomp Seccomp
// Namespaces classifies each namespace the process is in.
Namespaces Namespaces
// FS is the process's view of the filesystem.
FS Filesystem
// FDs describes what the process holds open.
FDs FDTable
// Attrs holds the process attributes.
Attrs Attrs
// Threads reports whether every thread in the process all share the
// same process-level credentials, capabilities and seccomp state.
Threads Threads
// CoreTag classifies the core scheduling cookies of the threads of the
// process and of its descendants (e.g. systrap stubs), or describes them
// if they are mixed.
CoreTag string
// Children counts the process's direct children by command name.
// Useful for debugging, not used for comparison.
Children map[string]int `posture:"info"`
}
Posture is a normalized, comparable view of a process's externally-observable Linux security posture.
func Expected ¶
func Expected(opts ExpectedOpts) *Posture
Expected returns the posture a sandbox started with the given configuration must have.
type Reference ¶
type Reference struct {
// Umask is the reference process's umask, e.g. "0022".
Umask string
// PermittedCaps is the reference process's permitted capability set.
PermittedCaps []string
// Limits is the reference process's resource limits.
Limits map[string]string
// StdioFDs holds the FD classes of the reference process's own
// stdin/stdout/stderr.
StdioFDs []string
// TMPDIR is the value of the reference process's `TMPDIR` environment
// variable, or empty if it is unset.
TMPDIR string
}
Reference describes the posture of process the sandbox was launched from.
func ReferenceOf ¶
ReferenceOf builds a `Reference` from the given process. `procRoot` defaults to "/proc".
func SelfReference ¶
SelfReference builds a `Reference` from the calling process.
type Seccomp ¶
type Seccomp struct {
// Mode is the value of the Seccomp field in procfs status:
// 0 = disabled, 1 = strict, 2 = filtered.
Mode int
// NumFilters is the value of the Seccomp_filters field in procfs status,
// i.e. how many filter programs are attached. Informational only.
NumFilters int `posture:"info"`
// NoNewPrivs is the `no_new_privs` bit, which must be set before a
// non-privileged process may install a filter, and which prevents
// subsequent execve's from regaining privileges.
NoNewPrivs int
}
Seccomp is a process's seccomp state.
Does not test for filter contents, as this requires custom kernel settings. Other gVisor tests cover seccomp filter contents.
type Threads ¶
type Threads struct {
// Uniform reports whether every thread's credentials, capabilities and
// seccomp state match process-wide values.
Uniform bool
// Divergences describes each mismatch found.
// Empty when `Uniform` is true.
Divergences []string
}
Threads reports whether the process's threads share its posture.