Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Audit Mode 0000 bug #15759

Merged
merged 4 commits into from Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion builtin/audit/file/backend.go
Expand Up @@ -78,9 +78,21 @@ func Factory(ctx context.Context, conf *audit.BackendConfig) (audit.Backend, err
if err != nil {
return nil, err
}
if m != 0 {
switch m {
case 0:
// if mode is 0000, then do not modify file mode
if path != "stdout" && path != "discard" {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
mode = fileInfo.Mode()
}
default:
mode = os.FileMode(m)

}

}

b := &Backend{
Expand Down
40 changes: 40 additions & 0 deletions builtin/audit/file/backend_test.go
Expand Up @@ -93,6 +93,46 @@ func TestAuditFile_fileModeExisting(t *testing.T) {
}
}

func TestAuditFile_fileMode0000(t *testing.T) {
f, err := ioutil.TempFile("", "test")
if err != nil {
t.Fatalf("Failure to create test file.")
akshya96 marked this conversation as resolved.
Show resolved Hide resolved
}
defer os.Remove(f.Name())

err = os.Chmod(f.Name(), 0o777)
if err != nil {
t.Fatalf("Failure to chmod temp file for testing.")
akshya96 marked this conversation as resolved.
Show resolved Hide resolved
}

err = f.Close()
if err != nil {
t.Fatalf("Failure to close temp file for test.")
akshya96 marked this conversation as resolved.
Show resolved Hide resolved
}

config := map[string]string{
"path": f.Name(),
"mode": "0000",
}

_, err = Factory(context.Background(), &audit.BackendConfig{
Config: config,
SaltConfig: &salt.Config{},
SaltView: &logical.InmemStorage{},
})
if err != nil {
t.Fatal(err)
}

info, err := os.Stat(f.Name())
if err != nil {
t.Fatalf("cannot retrieve file mode from `Stat`")
akshya96 marked this conversation as resolved.
Show resolved Hide resolved
}
if info.Mode() != os.FileMode(0o777) {
t.Fatalf("File mode does not match.")
}
}

func BenchmarkAuditFile_request(b *testing.B) {
config := map[string]string{
"path": "/dev/null",
Expand Down
3 changes: 3 additions & 0 deletions changelog/15759.txt
@@ -0,0 +1,3 @@
```release-note:bug
core: Prevent changing file permissions of audit logs when mode 0000 is used.
```