Skip to content

Commit b5fd076

Browse files
ratapcmoore
authored andcommitted
all: remove apiLevel cache
Signed-off-by: Rodrigo Campos <[email protected]> Acked-by: Tom Hromatka <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 78c92cb commit b5fd076

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

seccomp.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ type VersionError struct {
4141
minimum string
4242
}
4343

44-
// Caches the libseccomp API level
45-
var apiLevel uint
46-
4744
func init() {
4845
// This forces the cgo libseccomp to initialize its internal API support state,
4946
// which is necessary on older versions of libseccomp in order to work
@@ -451,12 +448,7 @@ func GetLibraryVersion() (major, minor, micro uint) {
451448
// See the seccomp_api_get(3) man page for details on available API levels:
452449
// https://github.com/seccomp/libseccomp/blob/main/doc/man/man3/seccomp_api_get.3
453450
func GetAPI() (uint, error) {
454-
api, err := getAPI()
455-
if err != nil {
456-
return api, err
457-
}
458-
apiLevel = api
459-
return api, err
451+
return getAPI()
460452
}
461453

462454
// SetAPI forcibly sets the API level. General use of this function is strongly
@@ -466,11 +458,7 @@ func GetAPI() (uint, error) {
466458
// See the seccomp_api_get(3) man page for details on available API levels:
467459
// https://github.com/seccomp/libseccomp/blob/main/doc/man/man3/seccomp_api_get.3
468460
func SetAPI(api uint) error {
469-
if err := setAPI(api); err != nil {
470-
return err
471-
}
472-
apiLevel = api
473-
return nil
461+
return setAPI(api)
474462
}
475463

476464
// Syscall functions
@@ -909,6 +897,8 @@ func (f *ScmpFilter) GetNoNewPrivsBit() (bool, error) {
909897
func (f *ScmpFilter) GetLogBit() (bool, error) {
910898
log, err := f.getFilterAttr(filterAttrLog)
911899
if err != nil {
900+
// Ignore error, if not supported returns apiLevel == 0
901+
apiLevel, _ := GetAPI()
912902
if apiLevel < 3 {
913903
return false, fmt.Errorf("getting the log bit is only supported in libseccomp 2.4.0 and newer with API level 3 or higher")
914904
}
@@ -986,6 +976,8 @@ func (f *ScmpFilter) SetLogBit(state bool) error {
986976

987977
err := f.setFilterAttr(filterAttrLog, toSet)
988978
if err != nil {
979+
// Ignore error, if not supported returns apiLevel == 0
980+
apiLevel, _ := GetAPI()
989981
if apiLevel < 3 {
990982
return fmt.Errorf("setting the log bit is only supported in libseccomp 2.4.0 and newer with API level 3 or higher")
991983
}

seccomp_internal.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ func (f *ScmpFilter) getNotifFd() (ScmpFd, error) {
735735
return -1, errBadFilter
736736
}
737737

738+
// Ignore error, if not supported returns apiLevel == 0
739+
apiLevel, _ := GetAPI()
738740
if apiLevel < 5 {
739741
return -1, fmt.Errorf("seccomp notification requires API level >= 5; current level = %d", apiLevel)
740742
}
@@ -748,6 +750,8 @@ func notifReceive(fd ScmpFd) (*ScmpNotifReq, error) {
748750
var req *C.struct_seccomp_notif
749751
var resp *C.struct_seccomp_notif_resp
750752

753+
// Ignore error, if not supported returns apiLevel == 0
754+
apiLevel, _ := GetAPI()
751755
if apiLevel < 5 {
752756
return nil, fmt.Errorf("seccomp notification requires API level >= 5; current level = %d", apiLevel)
753757
}
@@ -772,6 +776,8 @@ func notifRespond(fd ScmpFd, scmpResp *ScmpNotifResp) error {
772776
var req *C.struct_seccomp_notif
773777
var resp *C.struct_seccomp_notif_resp
774778

779+
// Ignore error, if not supported returns apiLevel == 0
780+
apiLevel, _ := GetAPI()
775781
if apiLevel < 5 {
776782
return fmt.Errorf("seccomp notification requires API level >= 5; current level = %d", apiLevel)
777783
}
@@ -795,6 +801,8 @@ func notifRespond(fd ScmpFd, scmpResp *ScmpNotifResp) error {
795801
}
796802

797803
func notifIDValid(fd ScmpFd, id uint64) error {
804+
// Ignore error, if not supported returns apiLevel == 0
805+
apiLevel, _ := GetAPI()
798806
if apiLevel < 5 {
799807
return fmt.Errorf("seccomp notification requires API level >= 5; current level = %d", apiLevel)
800808
}

0 commit comments

Comments
 (0)