-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for SELinux AVC statistics
Add interfaces to read the SELinux AVC runtime statistics from /sys/fs/selinux/avc/cache_stats and /sys/fs/selinux/avc/hash_stats. Refactored from prometheus/node_exporter#2418 Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
- Loading branch information
Showing
9 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type AVCStat struct { | ||
Lookups uint64 | ||
Hits uint64 | ||
Misses uint64 | ||
Allocations uint64 | ||
Reclaims uint64 | ||
Frees uint64 | ||
} | ||
|
||
func (fs FS) parseAVCStats() (AVCStat, error) { | ||
avcStat := AVCStat{} | ||
|
||
file, err := os.Open(fs.selinux.Path("avc/cache_stats")) | ||
if err != nil { | ||
return avcStat, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
scanner.Scan() // Skip header | ||
|
||
for scanner.Scan() { | ||
avcValues := strings.Fields(scanner.Text()) | ||
|
||
if len(avcValues) != 6 { | ||
return avcStat, fmt.Errorf("invalid AVC stat line: %s", | ||
scanner.Text()) | ||
} | ||
|
||
lookups, err := strconv.ParseUint(avcValues[0], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for lookups") | ||
} | ||
|
||
hits, err := strconv.ParseUint(avcValues[1], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for hits") | ||
} | ||
|
||
misses, err := strconv.ParseUint(avcValues[2], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for misses") | ||
} | ||
|
||
allocations, err := strconv.ParseUint(avcValues[3], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for allocations") | ||
} | ||
|
||
reclaims, err := strconv.ParseUint(avcValues[4], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for reclaims") | ||
} | ||
|
||
frees, err := strconv.ParseUint(avcValues[5], 0, 64) | ||
if err != nil { | ||
return avcStat, fmt.Errorf("could not parse expected integer value for frees") | ||
} | ||
|
||
avcStat.Lookups += lookups | ||
avcStat.Hits += hits | ||
avcStat.Misses += misses | ||
avcStat.Allocations += allocations | ||
avcStat.Reclaims += reclaims | ||
avcStat.Frees += frees | ||
} | ||
|
||
return avcStat, scanner.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAVCStats(t *testing.T) { | ||
fs, err := NewFS(selinuxTestFixtures) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
avcStats, err := fs.parseAVCStats() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if want, got := uint64(91590784), avcStats.Lookups; want != got { | ||
t.Errorf("want avcstat lookups %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(91569452), avcStats.Hits; want != got { | ||
t.Errorf("want avcstat hits %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(21332), avcStats.Misses; want != got { | ||
t.Errorf("want avcstat misses %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(21332), avcStats.Allocations; want != got { | ||
t.Errorf("want avcstat allocations %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(20400), avcStats.Reclaims; want != got { | ||
t.Errorf("want avcstat reclaims %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(20826), avcStats.Frees; want != got { | ||
t.Errorf("want avcstat frees %v, got %v", want, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type AVCHashStat struct { | ||
Entries uint64 | ||
BucketsUsed uint64 | ||
BucketsAvailable uint64 | ||
LongestChain uint64 | ||
} | ||
|
||
func (fs FS) parseAVCHashStats() (AVCHashStat, error) { | ||
avcHashStat := AVCHashStat{} | ||
|
||
file, err := os.Open(fs.selinux.Path("avc/hash_stats")) | ||
if err != nil { | ||
return avcHashStat, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
|
||
scanner.Scan() | ||
entriesValue := strings.TrimPrefix(scanner.Text(), "entries: ") | ||
|
||
scanner.Scan() | ||
bucketsValues := strings.Split(scanner.Text(), "buckets used: ") | ||
bucketsValuesTuple := strings.Split(bucketsValues[1], "/") | ||
|
||
scanner.Scan() | ||
longestChainValue := strings.TrimPrefix(scanner.Text(), "longest chain: ") | ||
|
||
avcHashStat.Entries, err = strconv.ParseUint(entriesValue, 0, 64) | ||
if err != nil { | ||
return avcHashStat, fmt.Errorf("could not parse expected integer value for hash entries") | ||
} | ||
|
||
avcHashStat.BucketsUsed, err = strconv.ParseUint(bucketsValuesTuple[0], 0, 64) | ||
if err != nil { | ||
return avcHashStat, fmt.Errorf("could not parse expected integer value for hash buckets used") | ||
} | ||
|
||
avcHashStat.BucketsAvailable, err = strconv.ParseUint(bucketsValuesTuple[1], 0, 64) | ||
if err != nil { | ||
return avcHashStat, fmt.Errorf("could not parse expected integer value for hash buckets available") | ||
} | ||
|
||
avcHashStat.LongestChain, err = strconv.ParseUint(longestChainValue, 0, 64) | ||
if err != nil { | ||
return avcHashStat, fmt.Errorf("could not parse expected integer value for hash longest chain") | ||
} | ||
|
||
return avcHashStat, scanner.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAVCHashStat(t *testing.T) { | ||
fs, err := NewFS(selinuxTestFixtures) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
avcHashStats, err := fs.parseAVCHashStats() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if want, got := uint64(503), avcHashStats.Entries; want != got { | ||
t.Errorf("want avc hash stat entries %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(512), avcHashStats.BucketsAvailable; want != got { | ||
t.Errorf("want avc hash stat buckets available %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(257), avcHashStats.BucketsUsed; want != got { | ||
t.Errorf("want avc hash stat buckets used %v, got %v", want, got) | ||
} | ||
|
||
if want, got := uint64(8), avcHashStats.LongestChain; want != got { | ||
t.Errorf("want avc hash stat longest chain %v, got %v", want, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import ( | ||
"github.com/prometheus/procfs/internal/fs" | ||
) | ||
|
||
// FS represents the pseudo-filesystem selinixfs, which provides an interface to | ||
// SELinux data structures. | ||
type FS struct { | ||
selinux fs.FS | ||
} | ||
|
||
// DefaultMountPoint is the common mount point of the selinuxfs filesystem. | ||
const DefaultMountPoint = fs.DefaultSelinuxMountPoint | ||
|
||
// NewDefaultFS returns a new FS mounted under the default mountPoint. It will error | ||
// if the mount point can't be read. | ||
func NewDefaultFS() (FS, error) { | ||
return NewFS(DefaultMountPoint) | ||
} | ||
|
||
// NewFS returns a new FS mounted under the given mountPoint. It will error | ||
// if the mount point can't be read. | ||
func NewFS(mountPoint string) (FS, error) { | ||
fs, err := fs.NewFS(mountPoint) | ||
if err != nil { | ||
return FS{}, err | ||
} | ||
return FS{fs}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !noselinux | ||
// +build linux,!noselinux | ||
|
||
package selinuxfs | ||
|
||
import "testing" | ||
|
||
const ( | ||
selinuxTestFixtures = "testdata/fixtures" + DefaultMountPoint | ||
) | ||
|
||
func TestNewFS(t *testing.T) { | ||
if _, err := NewFS("foobar"); err == nil { | ||
t.Error("want NewFS to fail for non-existing mount point") | ||
} | ||
|
||
if _, err := NewFS("doc.go"); err == nil { | ||
t.Error("want NewFS to fail if mount point is not a directory") | ||
} | ||
|
||
if _, err := NewFS(selinuxTestFixtures); err != nil { | ||
t.Error("want NewFS to succeed if mount point exists") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testdata/fixtures |
Oops, something went wrong.