Skip to content

Commit 276b0ea

Browse files
Kenta Tadapcmoore
authored andcommitted
golang: Add support for SCMP_FLTATR_CTL_SSB
Create a new scmpFilterAttr, filterAttrSSB, to represent libseccomp's SCMP_FLTATR_CTL_SSB This filter is only supported in libseccomp 2.5.0 and newer with API level 4 or higher. Signed-off-by: Kenta Tada <[email protected]> Acked-by: Tom Hromatka <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent db0cdca commit 276b0ea

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

seccomp.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,30 @@ func (f *ScmpFilter) GetLogBit() (bool, error) {
793793
return true, nil
794794
}
795795

796+
// GetSSB returns the current state the SSB bit will be set to on the filter
797+
// being loaded, or an error if an issue was encountered retrieving the value.
798+
// The SSB bit tells the kernel that a seccomp user is not interested in enabling
799+
// Speculative Store Bypass mitigation.
800+
// The SSB bit is only usable when libseccomp API level 4 or higher is
801+
// supported.
802+
func (f *ScmpFilter) GetSSB() (bool, error) {
803+
ssb, err := f.getFilterAttr(filterAttrSSB)
804+
if err != nil {
805+
api, apiErr := getAPI()
806+
if (apiErr != nil && api == 0) || (apiErr == nil && api < 4) {
807+
return false, fmt.Errorf("getting the SSB flag is only supported in libseccomp 2.5.0 and newer with API level 4 or higher")
808+
}
809+
810+
return false, err
811+
}
812+
813+
if ssb == 0 {
814+
return false, nil
815+
}
816+
817+
return true, nil
818+
}
819+
796820
// SetBadArchAction sets the default action taken on a syscall for an
797821
// architecture not in the filter, or an error if an issue was encountered
798822
// setting the value.
@@ -841,6 +865,28 @@ func (f *ScmpFilter) SetLogBit(state bool) error {
841865
return err
842866
}
843867

868+
// SetSSB sets the state of the SSB bit, which will be applied on filter
869+
// load, or an error if an issue was encountered setting the value.
870+
// The SSB bit is only usable when libseccomp API level 4 or higher is
871+
// supported.
872+
func (f *ScmpFilter) SetSSB(state bool) error {
873+
var toSet C.uint32_t = 0x0
874+
875+
if state {
876+
toSet = 0x1
877+
}
878+
879+
err := f.setFilterAttr(filterAttrSSB, toSet)
880+
if err != nil {
881+
api, apiErr := getAPI()
882+
if (apiErr != nil && api == 0) || (apiErr == nil && api < 4) {
883+
return fmt.Errorf("setting the SSB flag is only supported in libseccomp 2.5.0 and newer with API level 4 or higher")
884+
}
885+
}
886+
887+
return err
888+
}
889+
844890
// SetSyscallPriority sets a syscall's priority.
845891
// This provides a hint to the filter generator in libseccomp about the
846892
// importance of this syscall. High-priority syscalls are placed

seccomp_internal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,16 @@ const uint32_t C_ACT_ALLOW = SCMP_ACT_ALLOW;
9494
#if (SCMP_VER_MAJOR < 2) || \
9595
(SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 4)
9696
#define SCMP_FLTATR_CTL_LOG _SCMP_FLTATR_MIN
97+
#elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 5
98+
#define SCMP_FLTATR_CTL_SSB _SCMP_FLTATR_MIN
9799
#endif
98100
99101
const uint32_t C_ATTRIBUTE_DEFAULT = (uint32_t)SCMP_FLTATR_ACT_DEFAULT;
100102
const uint32_t C_ATTRIBUTE_BADARCH = (uint32_t)SCMP_FLTATR_ACT_BADARCH;
101103
const uint32_t C_ATTRIBUTE_NNP = (uint32_t)SCMP_FLTATR_CTL_NNP;
102104
const uint32_t C_ATTRIBUTE_TSYNC = (uint32_t)SCMP_FLTATR_CTL_TSYNC;
103105
const uint32_t C_ATTRIBUTE_LOG = (uint32_t)SCMP_FLTATR_CTL_LOG;
106+
const uint32_t C_ATTRIBUTE_SSB = (uint32_t)SCMP_FLTATR_CTL_SSB;
104107
105108
const int C_CMP_NE = (int)SCMP_CMP_NE;
106109
const int C_CMP_LT = (int)SCMP_CMP_LT;
@@ -203,6 +206,7 @@ const (
203206
filterAttrNNP scmpFilterAttr = iota
204207
filterAttrTsync scmpFilterAttr = iota
205208
filterAttrLog scmpFilterAttr = iota
209+
filterAttrSSB scmpFilterAttr = iota
206210
)
207211

208212
const (
@@ -590,6 +594,8 @@ func (a scmpFilterAttr) toNative() uint32 {
590594
return uint32(C.C_ATTRIBUTE_TSYNC)
591595
case filterAttrLog:
592596
return uint32(C.C_ATTRIBUTE_LOG)
597+
case filterAttrSSB:
598+
return uint32(C.C_ATTRIBUTE_SSB)
593599
default:
594600
return 0x0
595601
}

seccomp_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,36 @@ func TestFilterAttributeGettersAndSetters(t *testing.T) {
464464
t.Errorf("Log bit was not set correctly")
465465
}
466466

467+
api, err := GetAPI()
468+
if err != nil {
469+
t.Errorf("Error getting API level: %s", err)
470+
} else if api < 4 {
471+
err = SetAPI(4)
472+
if err != nil {
473+
t.Skipf("Skipping test: API level %d is less than 4", api)
474+
}
475+
}
476+
477+
err = filter.SetSSB(true)
478+
if err != nil {
479+
if !APILevelIsSupported() {
480+
t.Logf("Ignoring failure: %s\n", err)
481+
} else {
482+
t.Errorf("Error setting SSB bit")
483+
}
484+
}
485+
486+
ssb, err := filter.GetSSB()
487+
if err != nil {
488+
if !APILevelIsSupported() {
489+
t.Logf("Ignoring failure: %s\n", err)
490+
} else {
491+
t.Errorf("Error getting SSB bit")
492+
}
493+
} else if ssb != true {
494+
t.Errorf("SSB bit was not set correctly")
495+
}
496+
467497
err = filter.SetBadArchAction(ActInvalid)
468498
if err == nil {
469499
t.Errorf("Setting bad arch action to an invalid action should error")

0 commit comments

Comments
 (0)