-
Notifications
You must be signed in to change notification settings - Fork 10
/
detector_linux.go
108 lines (94 loc) · 3.37 KB
/
detector_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2024 Google LLC
//
// 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
// Package etcpasswdpermissions implements a detector for the "Ensure permissions on /etc/passwd- are configured" CIS check.
package etcpasswdpermissions
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"syscall"
"github.com/google/osv-scalibr/detector"
"github.com/google/osv-scalibr/inventoryindex"
)
// Detector is a SCALIBR Detector for the CIS check "Ensure permissions on /etc/passwd- are configured"
// from the CIS Distribution Independent Linux benchmarks.
type Detector struct{}
// Name of the detector.
func (Detector) Name() string { return "cis/generic_linux/etcpasswdpermissions" }
// Version of the detector.
func (Detector) Version() int { return 0 }
// RequiredExtractors returns an empty list as there are no dependencies.
func (Detector) RequiredExtractors() []string { return []string{} }
// Scan starts the scan.
func (d Detector) Scan(ctx context.Context, scanRoot string, ix *inventoryindex.InventoryIndex) ([]*detector.Finding, error) {
return d.ScanFS(ctx, os.DirFS(scanRoot), ix)
}
// ScanFS starts the scan from a pseudo-filesystem.
func (Detector) ScanFS(ctx context.Context, fs fs.FS, ix *inventoryindex.InventoryIndex) ([]*detector.Finding, error) {
f, err := fs.Open("etc/passwd")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// File doesn't exist, check not applicable.
return nil, nil
}
return nil, err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return nil, err
}
problems := ""
if info.Mode().Perm() != 0644 {
problems = fmt.Sprintf("file permissions %03o, expected 644\n", info.Mode().Perm())
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return nil, fmt.Errorf("failed to get file ownership info")
}
if stat.Uid != 0 {
problems += fmt.Sprintf("file owner %d, expected 0/root\n", stat.Uid)
}
if stat.Gid != 0 {
problems += fmt.Sprintf("file group %d, expected 0/root\n", stat.Gid)
}
if len(problems) == 0 {
return nil, nil
}
title := "Ensure permissions on /etc/passwd are configured"
description := "The /etc/passwd file contains user account information that " +
"is used by many system utilities and therefore must be readable for these " +
"utilities to operate."
recommendation := "Run the following command to set permissions on /etc/passwd :\n" +
"# chown root:root /etc/passwd\n" +
"# chmod 644 /etc/passwd"
return []*detector.Finding{&detector.Finding{
Adv: &detector.Advisory{
ID: &detector.AdvisoryID{
Publisher: "CIS",
Reference: "etc-passwd-permissions",
},
Type: detector.TypeCISFinding,
Title: title,
Description: description,
Recommendation: recommendation,
Sev: &detector.Severity{Severity: detector.SeverityMinimal},
},
Target: &detector.TargetDetails{Location: []string{"/etc/passwd"}},
Extra: problems,
}}, nil
}