-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility helper to determine the kernel lockdown mode (#461)
- Loading branch information
Showing
2 changed files
with
113 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,61 @@ | ||
package ebpfcommon | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setIntegrity(t *testing.T, path, text string) { | ||
err := os.WriteFile(path, []byte(text), 0644) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func setNotReadable(t *testing.T, path string) { | ||
err := os.Chmod(path, 000) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestLockdownParsing(t *testing.T) { | ||
noFile, err := os.CreateTemp("", "not_existent_fake_lockdown") | ||
assert.NoError(t, err) | ||
notPath, err := filepath.Abs(noFile.Name()) | ||
assert.NoError(t, err) | ||
noFile.Close() | ||
os.Remove(noFile.Name()) | ||
|
||
// Setup for testing file that doesn't exist | ||
lockdownPath = notPath | ||
assert.Equal(t, KernelLockdownNone, KernelLockdownMode()) | ||
|
||
tempFile, err := os.CreateTemp("", "fake_lockdown") | ||
assert.NoError(t, err) | ||
path, err := filepath.Abs(tempFile.Name()) | ||
assert.NoError(t, err) | ||
tempFile.Close() | ||
|
||
defer os.Remove(tempFile.Name()) | ||
// Setup for testing | ||
lockdownPath = path | ||
|
||
setIntegrity(t, path, "none [integrity] confidentiality\n") | ||
assert.Equal(t, KernelLockdownIntegrity, KernelLockdownMode()) | ||
|
||
setIntegrity(t, path, "[none] integrity confidentiality\n") | ||
assert.Equal(t, KernelLockdownNone, KernelLockdownMode()) | ||
|
||
setIntegrity(t, path, "none integrity [confidentiality]\n") | ||
assert.Equal(t, KernelLockdownConfidentiality, KernelLockdownMode()) | ||
|
||
setIntegrity(t, path, "whatever\n") | ||
assert.Equal(t, KernelLockdownOther, KernelLockdownMode()) | ||
|
||
setIntegrity(t, path, "") | ||
assert.Equal(t, KernelLockdownIntegrity, KernelLockdownMode()) | ||
|
||
setIntegrity(t, path, "[none] integrity confidentiality\n") | ||
setNotReadable(t, path) | ||
assert.Equal(t, KernelLockdownIntegrity, KernelLockdownMode()) | ||
} |