Skip to content

Commit 4b17538

Browse files
tyhicksmheon
authored andcommitted
golang: Add support for SCMP_FLTATR_CTL_LOG
Create a new scmpFilterAttr, filterAttrLog, to represent libseccomp's SCMP_FLTATR_CTL_LOG. A new set of getter and setter functions are created to set the log filter attribute. They are named GetLogBit() and SetLogBit(). Signed-off-by: Tyler Hicks <[email protected]> Signed-off-by: Matthew Heon <[email protected]>
1 parent 62d5d2b commit 4b17538

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

seccomp.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,30 @@ func (f *ScmpFilter) GetNoNewPrivsBit() (bool, error) {
749749
return true, nil
750750
}
751751

752+
// GetLogBit returns the current state the Log bit will be set to on the filter
753+
// being loaded, or an error if an issue was encountered retrieving the value.
754+
// The Log bit tells the kernel that all actions taken by the filter, with the
755+
// exception of ActAllow, should be logged.
756+
// The Log bit is only usable when libseccomp API level 3 or higher is
757+
// supported.
758+
func (f *ScmpFilter) GetLogBit() (bool, error) {
759+
log, err := f.getFilterAttr(filterAttrLog)
760+
if err != nil {
761+
api, apiErr := getApi()
762+
if (apiErr != nil && api == 0) || (apiErr == nil && api < 3) {
763+
return false, fmt.Errorf("getting the log bit is only supported in libseccomp 2.4.0 and newer with API level 3 or higher")
764+
}
765+
766+
return false, err
767+
}
768+
769+
if log == 0 {
770+
return false, nil
771+
}
772+
773+
return true, nil
774+
}
775+
752776
// SetBadArchAction sets the default action taken on a syscall for an
753777
// architecture not in the filter, or an error if an issue was encountered
754778
// setting the value.
@@ -775,6 +799,28 @@ func (f *ScmpFilter) SetNoNewPrivsBit(state bool) error {
775799
return f.setFilterAttr(filterAttrNNP, toSet)
776800
}
777801

802+
// SetLogBit sets the state of the Log bit, which will be applied on filter
803+
// load, or an error if an issue was encountered setting the value.
804+
// The Log bit is only usable when libseccomp API level 3 or higher is
805+
// supported.
806+
func (f *ScmpFilter) SetLogBit(state bool) error {
807+
var toSet C.uint32_t = 0x0
808+
809+
if state {
810+
toSet = 0x1
811+
}
812+
813+
err := f.setFilterAttr(filterAttrLog, toSet)
814+
if err != nil {
815+
api, apiErr := getApi()
816+
if (apiErr != nil && api == 0) || (apiErr == nil && api < 3) {
817+
return fmt.Errorf("setting the log bit is only supported in libseccomp 2.4.0 and newer with API level 3 or higher")
818+
}
819+
}
820+
821+
return err
822+
}
823+
778824
// SetSyscallPriority sets a syscall's priority.
779825
// This provides a hint to the filter generator in libseccomp about the
780826
// importance of this syscall. High-priority syscalls are placed

seccomp_internal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ const uint32_t C_ACT_ERRNO = SCMP_ACT_ERRNO(0);
7474
const uint32_t C_ACT_TRACE = SCMP_ACT_TRACE(0);
7575
const uint32_t C_ACT_ALLOW = SCMP_ACT_ALLOW;
7676
77+
// The libseccomp SCMP_FLTATR_CTL_LOG member of the scmp_filter_attr enum was
78+
// added in v2.4.0
79+
#if (SCMP_VER_MAJOR < 2) || \
80+
(SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 4)
81+
#define SCMP_FLTATR_CTL_LOG _SCMP_FLTATR_MIN
82+
#endif
83+
7784
const uint32_t C_ATTRIBUTE_DEFAULT = (uint32_t)SCMP_FLTATR_ACT_DEFAULT;
7885
const uint32_t C_ATTRIBUTE_BADARCH = (uint32_t)SCMP_FLTATR_ACT_BADARCH;
7986
const uint32_t C_ATTRIBUTE_NNP = (uint32_t)SCMP_FLTATR_CTL_NNP;
8087
const uint32_t C_ATTRIBUTE_TSYNC = (uint32_t)SCMP_FLTATR_CTL_TSYNC;
88+
const uint32_t C_ATTRIBUTE_LOG = (uint32_t)SCMP_FLTATR_CTL_LOG;
8189
8290
const int C_CMP_NE = (int)SCMP_CMP_NE;
8391
const int C_CMP_LT = (int)SCMP_CMP_LT;
@@ -179,6 +187,7 @@ const (
179187
filterAttrActBadArch scmpFilterAttr = iota
180188
filterAttrNNP scmpFilterAttr = iota
181189
filterAttrTsync scmpFilterAttr = iota
190+
filterAttrLog scmpFilterAttr = iota
182191
)
183192

184193
const (
@@ -545,6 +554,8 @@ func (a scmpFilterAttr) toNative() uint32 {
545554
return uint32(C.C_ATTRIBUTE_NNP)
546555
case filterAttrTsync:
547556
return uint32(C.C_ATTRIBUTE_TSYNC)
557+
case filterAttrLog:
558+
return uint32(C.C_ATTRIBUTE_LOG)
548559
default:
549560
return 0x0
550561
}

0 commit comments

Comments
 (0)