Documentation ¶
Overview ¶
Package seccomp provides a way to install a syscall filter for a Linux process. It uses the seccomp (secure computing) BPF filters.
Example ¶
package main import ( "fmt" seccomp "github.com/elastic/go-seccomp-bpf" ) func main() { // Create a filter. filter := seccomp.Filter{ NoNewPrivs: true, Flag: seccomp.FilterFlagTSync, Policy: seccomp.Policy{ DefaultAction: seccomp.ActionAllow, Syscalls: []seccomp.SyscallGroup{ { Action: seccomp.ActionErrno, Names: []string{ "fork", "vfork", "execve", "execveat", }, }, }, }, } // Load it. This will set no_new_privs before loading. if err := seccomp.LoadFilter(filter); err != nil { fmt.Println("failed to load filter: ", err) return } }
Output:
Index ¶
- Variables
- func LoadFilter(filter Filter) error
- func SetNoNewPrivs() error
- func Supported() bool
- type Action
- type ArgumentConditions
- type Condition
- type Filter
- type FilterFlag
- type Index
- type JumpIf
- type Label
- type NameWithConditions
- type Operation
- type Policy
- type Program
- func (p *Program) Assemble() ([]bpf.Instruction, error)
- func (p *Program) JmpIf(cond bpf.JumpTest, val uint32, trueLabel Label, falseLabel Label)
- func (p *Program) JmpIfTrue(cond bpf.JumpTest, val uint32, trueLabel Label)
- func (p *Program) LdHi(arg uint32)
- func (p *Program) LdLo(arg uint32)
- func (p *Program) NewLabel() Label
- func (p *Program) Ret(action Action)
- func (p *Program) SetLabel(label Label)
- type SyscallGroup
- type SyscallWithConditions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Operations = []Operation{Equal, NotEqual, GreaterThan, LessThan, GreaterOrEqual, LessOrEqual, BitsSet, BitsNotSet}
Functions ¶
func LoadFilter ¶
LoadFilter will install seccomp using native methods.
func SetNoNewPrivs ¶
func SetNoNewPrivs() error
SetNoNewPrivs will use prctl to set the calling thread's no_new_privs bit to 1 (true). Once set, this bit cannot be unset.
Types ¶
type Action ¶
type Action uint32
Action specifies what to do when a syscall matches during filter evaluation.
const ( ActionKillThread Action = unix.SECCOMP_RET_KILL_THREAD // Kill the calling thread. ActionKillProcess Action = unix.SECCOMP_RET_KILL_PROCESS // Kill the process (since kernel 4.14). ActionTrap Action = unix.SECCOMP_RET_TRAP // Disallow and force a SIGSYS signal. ActionErrno Action = unix.SECCOMP_RET_ERRNO // Disallow and return an errno. ActionTrace Action = unix.SECCOMP_RET_TRACE // Pass to a tracer or disallow. ActionLog Action = unix.SECCOMP_RET_LOG // Allow after logging. ActionAllow Action = unix.SECCOMP_RET_ALLOW // Allow. ActionUserNotify Action = unix.SECCOMP_RET_USER_NOTIF // Forward to user-space supervisor. )
List of actions. https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/seccomp.h#L32-L39
func (Action) MarshalText ¶
MarshalText marshals the value to text.
type ArgumentConditions ¶ added in v1.4.0
type ArgumentConditions []Condition
ArgumentConditions consist of a list of up to six conditions for the six arguments.
func (ArgumentConditions) Validate ¶ added in v1.4.0
func (a ArgumentConditions) Validate() []string
type Filter ¶
type Filter struct { NoNewPrivs bool `config:"no_new_privs" json:"no_new_privs"` // Set the process's no new privs bit. Flag FilterFlag `config:"flag" json:"flag"` // Flag to pass to the seccomp call. Policy Policy `config:"policy" json:"policy"` // Policy that will be assembled into a BPF filter. }
Filter contains all the parameters necessary to install a Linux seccomp filter for the process.
type FilterFlag ¶
type FilterFlag uint32
FilterFlag is a flag that is passed to the seccomp. Multiple flags can be OR'ed together.
const ( // When adding a new filter, synchronize all other threads of the calling // process to the same seccomp filter tree. Since Linux 3.17. FilterFlagTSync FilterFlag = unix.SECCOMP_FILTER_FLAG_TSYNC // All filter return actions except SECCOMP_RET_ALLOW should be logged. // Since Linux 4.14. FilterFlagLog FilterFlag = unix.SECCOMP_FILTER_FLAG_LOG )
List of SECCOMP_SET_MODE_FILTER values. https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/seccomp.h#L19-L21
func (FilterFlag) MarshalText ¶
func (f FilterFlag) MarshalText() ([]byte, error)
MarshalText marshals the value to text.
func (FilterFlag) String ¶
func (f FilterFlag) String() string
String returns a string representation of the FilterFlag.
type Index ¶ added in v1.4.0
type Index int
Index is the concrete index of an instruction in the instruction list.
type JumpIf ¶ added in v1.4.0
type JumpIf struct {
// contains filtered or unexported fields
}
JumpIf jumps conditionally to the true or the false label. The concrete condition is not relevant to resolve the jumps.
type Label ¶ added in v1.4.0
type Label int
Label marks a jump destination in the instruction list of the Program.
type NameWithConditions ¶ added in v1.4.0
type NameWithConditions struct { Name string `config:"name" validate:"required" json:"name" yaml:"name"` Conditions ArgumentConditions `config:"arguments" validate:"required" json:"arguments" yaml:"arguments"` }
type Policy ¶
type Policy struct { DefaultAction Action `config:"default_action" json:"default_action" yaml:"default_action"` // Action when no syscalls match. Syscalls []SyscallGroup `config:"syscalls" json:"syscalls" yaml:"syscalls"` // Groups of syscalls and actions. // contains filtered or unexported fields }
Policy defines the BPF seccomp filter.
func (*Policy) Assemble ¶
func (p *Policy) Assemble() ([]bpf.Instruction, error)
Assemble assembles the policy into a list of BPF instructions. If the policy contains any unknown syscalls or invalid actions an error will be returned.
type Program ¶ added in v1.4.0
type Program struct {
// contains filtered or unexported fields
}
The Program consists of a list of bpf.Instructions. Conditional jumps can point to different labels in the program and must be resolved by calling ResolveJumps.
NewLabel creates a new label that can be used as jump destination.
SetLabel must be used to specify the concrete instruction. Only forward jumps are supported; this means a label must not be used after setting it.
func NewProgram ¶ added in v1.4.0
func NewProgram() Program
NewProgram returns an initialized empty program.
func (*Program) Assemble ¶ added in v1.4.0
func (p *Program) Assemble() ([]bpf.Instruction, error)
Assemble resolves all jump destinations to concrete instructions using the labels. This method takes care of long jumps and resolves them by using early returns or unconditional long jumps.
func (*Program) JmpIf ¶ added in v1.4.0
JmpIf inserts a conditional jump. If the condition is true, it jumps to the true label. If it is false, it jumps to the false label.
func (*Program) JmpIfTrue ¶ added in v1.4.0
JmpIfTrue inserts a conditional jump. If the condition is true, it jumps to the given label. If it is false, the program flow continues with the next instruction.
func (*Program) LdHi ¶ added in v1.4.0
LdHi inserts an instruction to load the most significant 32-bit of the 64-bit argument.
func (*Program) LdLo ¶ added in v1.4.0
LdLo inserts an instruction to load the least significant 32-bit of the 64-bit argument.
func (*Program) NewLabel ¶ added in v1.4.0
NewLabel creates a new label. It must be used with SetLabel.
type SyscallGroup ¶
type SyscallGroup struct { Names []string `config:"names" json:"names" yaml:"names"` // List of syscall names (all must exist). NamesWithCondtions []NameWithConditions `config:"names_with_args" json:"names_with_args" yaml:"names_with_args"` // List of syscall with argument filters Action Action `config:"action" validate:"required" json:"action" yaml:"action"` // Action to take upon a match. // contains filtered or unexported fields }
SyscallGroup is a logical block within a Policy that contains a set of syscalls to match against and an action to take.
func (*SyscallGroup) Assemble ¶
func (g *SyscallGroup) Assemble(defaultAction Action) ([]bpf.Instruction, error)
type SyscallWithConditions ¶ added in v1.4.0
type SyscallWithConditions struct { Num uint32 Conditions []ArgumentConditions }
SyscallWithConditions consists of a syscall number and optional conditions.
The conditions are applied to the arguments of the syscall. So, conditions consist of a list of up to six argument conditions. This filter matches if all argument conditions match for any Conditions.
func (SyscallWithConditions) Assemble ¶ added in v1.4.0
func (s SyscallWithConditions) Assemble(p *Program, action Label)
Directories ¶
Path | Synopsis |
---|---|
Package arch provides architecture specific Linux constants like the audit arch constant and syscall tables.
|
Package arch provides architecture specific Linux constants like the audit arch constant and syscall tables. |
cmd
|
|
internal
|
|
unix
Package unix re-exports Linux specific parts of golang.org/x/sys/unix.
|
Package unix re-exports Linux specific parts of golang.org/x/sys/unix. |