libseccomp-golang: github.com/seccomp/libseccomp-golang Index | Files

package seccomp

import "github.com/seccomp/libseccomp-golang"

Package seccomp provides 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

Package Files

seccomp.go seccomp_internal.go

func GetApi Uses

func GetApi() (uint, error)

GetApi returns the API level supported by the system. Returns a positive int containing the API level, or 0 with an error if the API level could not be detected due to the library being older than v2.4.0. See the seccomp_api_get(3) man page for details on available API levels: https://github.com/seccomp/libseccomp/blob/master/doc/man/man3/seccomp_api_get.3

func GetLibraryVersion Uses

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

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

func SetApi Uses

func SetApi(api uint) error

SetApi forcibly sets the API level. General use of this function is strongly discouraged. Returns an error if the API level could not be set. An error is always returned if the library is older than v2.4.0 See the seccomp_api_get(3) man page for details on available API levels: https://github.com/seccomp/libseccomp/blob/master/doc/man/man3/seccomp_api_get.3

type ScmpAction Uses

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
    // ActLog permits the syscall to continue execution after logging it.
    // This action is only usable when libseccomp API level 3 or higher is
    // supported.
    ActLog ScmpAction = iota
)

func (ScmpAction) GetReturnCode Uses

func (a ScmpAction) GetReturnCode() int16

GetReturnCode returns the return code of an ScmpAction

func (ScmpAction) SetReturnCode Uses

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 Uses

func (a ScmpAction) String() string

String returns a string representation of a seccomp match action

type ScmpArch Uses

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
    // ArchPPC represents 32-bit POWERPC syscalls
    ArchPPC ScmpArch = iota
    // ArchPPC64 represents 64-bit POWER syscalls (big endian)
    ArchPPC64 ScmpArch = iota
    // ArchPPC64LE represents 64-bit POWER syscalls (little endian)
    ArchPPC64LE ScmpArch = iota
    // ArchS390 represents 31-bit System z/390 syscalls
    ArchS390 ScmpArch = iota
    // ArchS390X represents 64-bit System z/390 syscalls
    ArchS390X ScmpArch = iota
)

func GetArchFromString Uses

func GetArchFromString(arch string) (ScmpArch, error)

GetArchFromString returns an ScmpArch constant from a string representing an architecture

func GetNativeArch Uses

func GetNativeArch() (ScmpArch, error)

GetNativeArch returns architecture token representing the native kernel architecture

func (ScmpArch) String Uses

func (a ScmpArch) String() string

String returns a string representation of an architecture constant

type ScmpCompareOp Uses

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 Uses

func (a ScmpCompareOp) String() string

String returns a string representation of a comparison operator constant

type ScmpCondition Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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) GetLogBit Uses

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

GetLogBit returns the current state the Log bit will be set to on the filter being loaded, or an error if an issue was encountered retrieving the value. The Log bit tells the kernel that all actions taken by the filter, with the exception of ActAllow, should be logged. The Log bit is only usable when libseccomp API level 3 or higher is supported.

func (*ScmpFilter) GetNoNewPrivsBit Uses

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) IsArchPresent Uses

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 Uses

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 Uses

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 Uses

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, and No New Privs 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 Uses

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 Uses

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 Uses

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 Uses

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) SetLogBit Uses

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

SetLogBit sets the state of the Log bit, which will be applied on filter load, or an error if an issue was encountered setting the value. The Log bit is only usable when libseccomp API level 3 or higher is supported.

func (*ScmpFilter) SetNoNewPrivsBit Uses

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 Uses

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).

type ScmpSyscall Uses

type ScmpSyscall int32

ScmpSyscall represents a Linux System Call

func GetSyscallFromName Uses

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 Uses

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 Uses

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 Uses

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.

type VersionError Uses

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

VersionError denotes that the system libseccomp version is incompatible with this package.

func (VersionError) Error Uses

func (e VersionError) Error() string

Package seccomp imports 8 packages (graph) and is imported by 11 packages. Updated 2019-07-31. Refresh now. Tools for package owners.