seccomp

package
v1.2.4-rancher7 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2016 License: BSD-2-Clause, Apache-2.0 Imports: 8 Imported by: 0

README

libseccomp-golang: Go Language Bindings for the libseccomp Project
===============================================================================
https://github.com/seccomp/libseccomp-golang
https://github.com/seccomp/libseccomp

The libseccomp library provides an easy to use, platform independent, interface
to the Linux Kernel's syscall filtering mechanism.  The libseccomp API is
designed to abstract away the underlying BPF based syscall filter language and
present a more conventional function-call based filtering interface that should
be familiar to, and easily adopted by, application developers.

The libseccomp-golang library provides a Go based interface to the libseccomp
library.

* Online Resources

The library source repository currently lives on GitHub at the following URLs:

	-> https://github.com/seccomp/libseccomp-golang
	-> https://github.com/seccomp/libseccomp

The project mailing list is currently hosted on Google Groups at the URL below,
please note that a Google account is not required to subscribe to the mailing
list.

	-> https://groups.google.com/d/forum/libseccomp

Documentation

Overview

Package seccomp rovides bindings for libseccomp, a library wrapping the Linux seccomp syscall. Seccomp enables an application to restrict system call use for itself and its children.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLibraryVersion

func GetLibraryVersion() (major, minor, micro int)

GetLibraryVersion returns the version of the library the bindings are built against. The version is formatted as follows: Major.Minor.Micro

Types

type ScmpAction

type ScmpAction uint

ScmpAction represents an action to be taken on a filter rule match in libseccomp

const (

	// ActInvalid is a placeholder to ensure uninitialized ScmpAction
	// variables are invalid
	ActInvalid ScmpAction = iota
	// ActKill kills the process
	ActKill ScmpAction = iota
	// ActTrap throws SIGSYS
	ActTrap ScmpAction = iota
	// ActErrno causes the syscall to return a negative error code. This
	// code can be set with the SetReturnCode method
	ActErrno ScmpAction = iota
	// ActTrace causes the syscall to notify tracing processes with the
	// given error code. This code can be set with the SetReturnCode method
	ActTrace ScmpAction = iota
	// ActAllow permits the syscall to continue execution
	ActAllow ScmpAction = iota
)

func (ScmpAction) GetReturnCode

func (a ScmpAction) GetReturnCode() int16

GetReturnCode returns the return code of an ScmpAction

func (ScmpAction) SetReturnCode

func (a ScmpAction) SetReturnCode(code int16) ScmpAction

SetReturnCode adds a return code to a supporting ScmpAction, clearing any existing code Only valid on ActErrno and ActTrace. Takes no action otherwise. Accepts 16-bit return code as argument. Returns a valid ScmpAction of the original type with the new error code set.

func (ScmpAction) String

func (a ScmpAction) String() string

String returns a string representation of a seccomp match action

type ScmpArch

type ScmpArch uint

ScmpArch represents a CPU architecture. Seccomp can restrict syscalls on a per-architecture basis.

const (

	// ArchInvalid is a placeholder to ensure uninitialized ScmpArch
	// variables are invalid
	ArchInvalid ScmpArch = iota
	// ArchNative is the native architecture of the kernel
	ArchNative ScmpArch = iota
	// ArchX86 represents 32-bit x86 syscalls
	ArchX86 ScmpArch = iota
	// ArchAMD64 represents 64-bit x86-64 syscalls
	ArchAMD64 ScmpArch = iota
	// ArchX32 represents 64-bit x86-64 syscalls (32-bit pointers)
	ArchX32 ScmpArch = iota
	// ArchARM represents 32-bit ARM syscalls
	ArchARM ScmpArch = iota
	// ArchARM64 represents 64-bit ARM syscalls
	ArchARM64 ScmpArch = iota
	// ArchMIPS represents 32-bit MIPS syscalls
	ArchMIPS ScmpArch = iota
	// ArchMIPS64 represents 64-bit MIPS syscalls
	ArchMIPS64 ScmpArch = iota
	// ArchMIPS64N32 represents 64-bit MIPS syscalls (32-bit pointers)
	ArchMIPS64N32 ScmpArch = iota
	// ArchMIPSEL represents 32-bit MIPS syscalls (little endian)
	ArchMIPSEL ScmpArch = iota
	// ArchMIPSEL64 represents 64-bit MIPS syscalls (little endian)
	ArchMIPSEL64 ScmpArch = iota
	// ArchMIPSEL64N32 represents 64-bit MIPS syscalls (little endian,
	// 32-bit pointers)
	ArchMIPSEL64N32 ScmpArch = iota
)

func GetArchFromString

func GetArchFromString(arch string) (ScmpArch, error)

GetArchFromString returns an ScmpArch constant from a string representing an architecture

func GetNativeArch

func GetNativeArch() (ScmpArch, error)

GetNativeArch returns architecture token representing the native kernel architecture

func (ScmpArch) String

func (a ScmpArch) String() string

String returns a string representation of an architecture constant

type ScmpCompareOp

type ScmpCompareOp uint

ScmpCompareOp represents a comparison operator which can be used in a filter rule

const (

	// CompareInvalid is a placeholder to ensure uninitialized ScmpCompareOp
	// variables are invalid
	CompareInvalid ScmpCompareOp = iota
	// CompareNotEqual returns true if the argument is not equal to the
	// given value
	CompareNotEqual ScmpCompareOp = iota
	// CompareLess returns true if the argument is less than the given value
	CompareLess ScmpCompareOp = iota
	// CompareLessOrEqual returns true if the argument is less than or equal
	// to the given value
	CompareLessOrEqual ScmpCompareOp = iota
	// CompareEqual returns true if the argument is equal to the given value
	CompareEqual ScmpCompareOp = iota
	// CompareGreaterEqual returns true if the argument is greater than or
	// equal to the given value
	CompareGreaterEqual ScmpCompareOp = iota
	// CompareGreater returns true if the argument is greater than the given
	// value
	CompareGreater ScmpCompareOp = iota
	// CompareMaskedEqual returns true if the argument is equal to the given
	// value, when masked (bitwise &) against the second given value
	CompareMaskedEqual ScmpCompareOp = iota
)

func (ScmpCompareOp) String

func (a ScmpCompareOp) String() string

String returns a string representation of a comparison operator constant

type ScmpCondition

type ScmpCondition struct {
	Argument uint          `json:"argument,omitempty"`
	Op       ScmpCompareOp `json:"operator,omitempty"`
	Operand1 uint64        `json:"operand_one,omitempty"`
	Operand2 uint64        `json:"operand_two,omitempty"`
}

ScmpCondition represents a rule in a libseccomp filter context

func MakeCondition

func MakeCondition(arg uint, comparison ScmpCompareOp, values ...uint64) (ScmpCondition, error)

MakeCondition creates and returns a new condition to attach to a filter rule. Associated rules will only match if this condition is true. Accepts the number the argument we are checking, and a comparison operator and value to compare to. The rule will match if argument $arg (zero-indexed) of the syscall is $COMPARE_OP the provided comparison value. Some comparison operators accept two values. Masked equals, for example, will mask $arg of the syscall with the second value provided (via bitwise AND) and then compare against the first value provided. For example, in the less than or equal case, if the syscall argument was 0 and the value provided was 1, the condition would match, as 0 is less than or equal to 1. Return either an error on bad argument or a valid ScmpCondition struct.

type ScmpFilter

type ScmpFilter struct {
	// contains filtered or unexported fields
}

ScmpFilter represents a filter context in libseccomp. A filter context is initially empty. Rules can be added to it, and it can then be loaded into the kernel.

func NewFilter

func NewFilter(defaultAction ScmpAction) (*ScmpFilter, error)

NewFilter creates and returns a new filter context. Accepts a default action to be taken for syscalls which match no rules in the filter. Returns a reference to a valid filter context, or nil and an error if the filter context could not be created or an invalid default action was given.

func (*ScmpFilter) AddArch

func (f *ScmpFilter) AddArch(arch ScmpArch) error

AddArch adds an architecture to the filter. Accepts an architecture constant. Returns an error on invalid filter context or architecture token, or an issue with the call to libseccomp.

func (*ScmpFilter) AddRule

func (f *ScmpFilter) AddRule(call ScmpSyscall, action ScmpAction) error

AddRule adds a single rule for an unconditional action on a syscall. Accepts the number of the syscall and the action to be taken on the call being made. Returns an error if an issue was encountered adding the rule.

func (*ScmpFilter) AddRuleConditional

func (f *ScmpFilter) AddRuleConditional(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error

AddRuleConditional adds a single rule for a conditional action on a syscall. Returns an error if an issue was encountered adding the rule. All conditions must match for the rule to match. There is a bug in library versions below v2.2.1 which can, in some cases, cause conditions to be lost when more than one are used. Consequently, AddRuleConditional is disabled on library versions lower than v2.2.1

func (*ScmpFilter) AddRuleConditionalExact

func (f *ScmpFilter) AddRuleConditionalExact(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error

AddRuleConditionalExact adds a single rule for a conditional action on a syscall. No modifications will be made to the rule, and it will fail to add if it cannot be applied to the current architecture without modification. The rule will function exactly as described, but it may not function identically (or be able to be applied to) all architectures. Returns an error if an issue was encountered adding the rule. There is a bug in library versions below v2.2.1 which can, in some cases, cause conditions to be lost when more than one are used. Consequently, AddRuleConditionalExact is disabled on library versions lower than v2.2.1

func (*ScmpFilter) AddRuleExact

func (f *ScmpFilter) AddRuleExact(call ScmpSyscall, action ScmpAction) error

AddRuleExact adds a single rule for an unconditional action on a syscall. Accepts the number of the syscall and the action to be taken on the call being made. No modifications will be made to the rule, and it will fail to add if it cannot be applied to the current architecture without modification. The rule will function exactly as described, but it may not function identically (or be able to be applied to) all architectures. Returns an error if an issue was encountered adding the rule.

func (*ScmpFilter) ExportBPF

func (f *ScmpFilter) ExportBPF(file *os.File) error

ExportBPF outputs Berkeley Packet Filter-formatted, kernel-readable dump of a filter context's rules to a file. Accepts file to write to (must be open for writing). Returns an error if writing to the file fails.

func (*ScmpFilter) ExportPFC

func (f *ScmpFilter) ExportPFC(file *os.File) error

ExportPFC output PFC-formatted, human-readable dump of a filter context's rules to a file. Accepts file to write to (must be open for writing). Returns an error if writing to the file fails.

func (*ScmpFilter) GetBadArchAction

func (f *ScmpFilter) GetBadArchAction() (ScmpAction, error)

GetBadArchAction returns the default action taken on a syscall for an architecture not in the filter, or an error if an issue was encountered retrieving the value.

func (*ScmpFilter) GetDefaultAction

func (f *ScmpFilter) GetDefaultAction() (ScmpAction, error)

GetDefaultAction returns the default action taken on a syscall which does not match a rule in the filter, or an error if an issue was encountered retrieving the value.

func (*ScmpFilter) GetNoNewPrivsBit

func (f *ScmpFilter) GetNoNewPrivsBit() (bool, error)

GetNoNewPrivsBit returns the current state the No New Privileges bit will be set to on the filter being loaded, or an error if an issue was encountered retrieving the value. The No New Privileges bit tells the kernel that new processes run with exec() cannot gain more privileges than the process that ran exec(). For example, a process with No New Privileges set would be unable to exec setuid/setgid executables.

func (*ScmpFilter) GetTsyncBit

func (f *ScmpFilter) GetTsyncBit() (bool, error)

GetTsyncBit returns whether Thread Synchronization will be enabled on the filter being loaded, or an error if an issue was encountered retrieving the value. Thread Sync ensures that all members of the thread group of the calling process will share the same Seccomp filter set. Tsync is a fairly recent addition to the Linux kernel and older kernels lack support. If the running kernel does not support Tsync and it is requested in a filter, Libseccomp will not enable TSync support and will proceed as normal. This function is unavailable before v2.2 of libseccomp and will return an error.

func (*ScmpFilter) IsArchPresent

func (f *ScmpFilter) IsArchPresent(arch ScmpArch) (bool, error)

IsArchPresent checks if an architecture is present in a filter. If a filter contains an architecture, it uses its default action for syscalls which do not match rules in it, and its rules can match syscalls for that ABI. If a filter does not contain an architecture, all syscalls made to that kernel ABI will fail with the filter's default Bad Architecture Action (by default, killing the process). Accepts an architecture constant. Returns true if the architecture is present in the filter, false otherwise, and an error on an invalid filter context, architecture constant, or an issue with the call to libseccomp.

func (*ScmpFilter) IsValid

func (f *ScmpFilter) IsValid() bool

IsValid determines whether a filter context is valid to use. Some operations (Release and Merge) render filter contexts invalid and consequently prevent further use.

func (*ScmpFilter) Load

func (f *ScmpFilter) Load() error

Load loads a filter context into the kernel. Returns an error if the filter context is invalid or the syscall failed.

func (*ScmpFilter) Merge

func (f *ScmpFilter) Merge(src *ScmpFilter) error

Merge merges two filter contexts. The source filter src will be released as part of the process, and will no longer be usable or valid after this call. To be merged, filters must NOT share any architectures, and all their attributes (Default Action, Bad Arch Action, No New Privs and TSync bools) must match. The filter src will be merged into the filter this is called on. The architectures of the src filter not present in the destination, and all associated rules, will be added to the destination. Returns an error if merging the filters failed.

func (*ScmpFilter) Release

func (f *ScmpFilter) Release()

Release releases a filter context, freeing its memory. Should be called after loading into the kernel, when the filter is no longer needed. After calling this function, the given filter is no longer valid and cannot be used. Release() will be invoked automatically when a filter context is garbage collected, but can also be called manually to free memory.

func (*ScmpFilter) RemoveArch

func (f *ScmpFilter) RemoveArch(arch ScmpArch) error

RemoveArch removes an architecture from the filter. Accepts an architecture constant. Returns an error on invalid filter context or architecture token, or an issue with the call to libseccomp.

func (*ScmpFilter) Reset

func (f *ScmpFilter) Reset(defaultAction ScmpAction) error

Reset resets a filter context, removing all its existing state. Accepts a new default action to be taken for syscalls which do not match. Returns an error if the filter or action provided are invalid.

func (*ScmpFilter) SetBadArchAction

func (f *ScmpFilter) SetBadArchAction(action ScmpAction) error

SetBadArchAction sets the default action taken on a syscall for an architecture not in the filter, or an error if an issue was encountered setting the value.

func (*ScmpFilter) SetNoNewPrivsBit

func (f *ScmpFilter) SetNoNewPrivsBit(state bool) error

SetNoNewPrivsBit sets the state of the No New Privileges bit, which will be applied on filter load, or an error if an issue was encountered setting the value. Filters with No New Privileges set to 0 can only be loaded if the process has the CAP_SYS_ADMIN capability.

func (*ScmpFilter) SetSyscallPriority

func (f *ScmpFilter) SetSyscallPriority(call ScmpSyscall, priority uint8) error

SetSyscallPriority sets a syscall's priority. This provides a hint to the filter generator in libseccomp about the importance of this syscall. High-priority syscalls are placed first in the filter code, and incur less overhead (at the expense of lower-priority syscalls).

func (*ScmpFilter) SetTsync

func (f *ScmpFilter) SetTsync(enable bool) error

SetTsync sets whether Thread Synchronization will be enabled on the filter being loaded. Returns an error if setting Tsync failed, or the filter is invalid. Thread Sync ensures that all members of the thread group of the calling process will share the same Seccomp filter set. Tsync is a fairly recent addition to the Linux kernel and older kernels lack support. If the running kernel does not support Tsync and it is requested in a filter, Libseccomp will not enable TSync support and will proceed as normal. This function is unavailable before v2.2 of libseccomp and will return an error.

type ScmpSyscall

type ScmpSyscall int32

ScmpSyscall represents a Linux System Call

func GetSyscallFromName

func GetSyscallFromName(name string) (ScmpSyscall, error)

GetSyscallFromName returns the number of a syscall by name on the kernel's native architecture. Accepts a string containing the name of a syscall. Returns the number of the syscall, or an error if no syscall with that name was found.

func GetSyscallFromNameByArch

func GetSyscallFromNameByArch(name string, arch ScmpArch) (ScmpSyscall, error)

GetSyscallFromNameByArch returns the number of a syscall by name for a given architecture's ABI. Accepts the name of a syscall and an architecture constant. Returns the number of the syscall, or an error if an invalid architecture is passed or a syscall with that name was not found.

func (ScmpSyscall) GetName

func (s ScmpSyscall) GetName() (string, error)

GetName retrieves the name of a syscall from its number. Acts on any syscall number. Returns either a string containing the name of the syscall, or an error.

func (ScmpSyscall) GetNameByArch

func (s ScmpSyscall) GetNameByArch(arch ScmpArch) (string, error)

GetNameByArch retrieves the name of a syscall from its number for a given architecture. Acts on any syscall number. Accepts a valid architecture constant. Returns either a string containing the name of the syscall, or an error. if the syscall is unrecognized or an issue occurred.

Jump to

Keyboard shortcuts

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