-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathvm.go
229 lines (196 loc) · 5.2 KB
/
vm.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package walker
import (
"bytes"
"io"
"io/fs"
"path/filepath"
"strings"
"github.com/masahiro331/go-disk"
"github.com/masahiro331/go-disk/gpt"
"github.com/masahiro331/go-disk/mbr"
"github.com/masahiro331/go-disk/types"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/trivy/pkg/fanal/vm/filesystem"
"github.com/aquasecurity/trivy/pkg/log"
)
var requiredDiskName = []string{
"Linux", // AmazonLinux image name
"p.lxroot", // SLES image name
"primary", // Common image name
"0", // Common image name
"1", // Common image name
"2", // Common image name
"3", // Common image name
}
func AppendPermitDiskName(s ...string) {
requiredDiskName = append(requiredDiskName, s...)
}
type VM struct {
walker
threshold int64
analyzeFn WalkFunc
}
func NewVM(skipFiles, skipDirs []string, slow bool) VM {
threshold := defaultSizeThreshold
if slow {
threshold = slowSizeThreshold
}
return VM{
walker: newWalker(skipFiles, skipDirs, slow),
threshold: threshold,
}
}
func (w *VM) Walk(vreader *io.SectionReader, root string, fn WalkFunc) error {
// This function will be called on each file.
w.analyzeFn = fn
driver, err := disk.NewDriver(vreader)
if err != nil {
return xerrors.Errorf("failed to new disk driver: %w", err)
}
for {
partition, err := driver.Next()
if err != nil {
if err == io.EOF {
break
}
return xerrors.Errorf("failed to get a next partition: %w", err)
}
// skip boot partition
if shouldSkip(partition) {
continue
}
// Walk each partition
if err = w.diskWalk(root, partition); err != nil {
log.Logger.Warnf("Partition error: %s", err.Error())
}
}
return nil
}
// Inject disk partitioning processes from externally with diskWalk.
func (w *VM) diskWalk(root string, partition types.Partition) error {
log.Logger.Debugf("Found partition: %s", partition.Name())
sr := partition.GetSectionReader()
// Trivy does not support LVM scanning. It is skipped at the moment.
foundLVM, err := w.detectLVM(sr)
if err != nil {
return xerrors.Errorf("LVM detection error: %w", err)
} else if foundLVM {
log.Logger.Errorf("LVM is not supported, skip %s.img", partition.Name())
return nil
}
// Auto-detect filesystem such as ext4 and xfs
fsys, clean, err := filesystem.New(sr)
if err != nil {
return xerrors.Errorf("filesystem error: %w", err)
}
defer clean()
err = fs.WalkDir(fsys, root, func(path string, d fs.DirEntry, err error) error {
// Walk filesystem
return w.fsWalk(fsys, path, d, err)
})
if err != nil {
return xerrors.Errorf("filesystem walk error: %w", err)
}
return nil
}
func (w *VM) fsWalk(fsys fs.FS, path string, d fs.DirEntry, err error) error {
if err != nil {
return xerrors.Errorf("fs.Walk error: %w", err)
}
fi, err := d.Info()
if err != nil {
return xerrors.Errorf("dir entry info error: %w", err)
}
pathName := strings.TrimPrefix(filepath.Clean(path), "/")
if fi.IsDir() {
if w.shouldSkipDir(pathName) {
return filepath.SkipDir
}
return nil
} else if !fi.Mode().IsRegular() {
return nil
} else if w.shouldSkipFile(pathName) {
return nil
} else if fi.Mode()&0x1000 == 0x1000 ||
fi.Mode()&0x2000 == 0x2000 ||
fi.Mode()&0x6000 == 0x6000 ||
fi.Mode()&0xA000 == 0xA000 ||
fi.Mode()&0xc000 == 0xc000 {
// 0x1000: S_IFIFO (FIFO)
// 0x2000: S_IFCHR (Character device)
// 0x6000: S_IFBLK (Block device)
// 0xA000: S_IFLNK (Symbolic link)
// 0xC000: S_IFSOCK (Socket)
return nil
}
cvf := newCachedVMFile(fsys, pathName, w.threshold)
defer cvf.Clean()
if err = w.analyzeFn(path, fi, cvf.Open); err != nil {
return xerrors.Errorf("failed to analyze file: %w", err)
}
return nil
}
type cachedVMFile struct {
fs fs.FS
filePath string
threshold int64
cf *cachedFile
}
func newCachedVMFile(fsys fs.FS, filePath string, threshold int64) *cachedVMFile {
return &cachedVMFile{fs: fsys, filePath: filePath, threshold: threshold}
}
func (cvf *cachedVMFile) Open() (dio.ReadSeekCloserAt, error) {
if cvf.cf != nil {
return cvf.cf.Open()
}
f, err := cvf.fs.Open(cvf.filePath)
if err != nil {
return nil, xerrors.Errorf("file open error: %w", err)
}
fi, err := f.Stat()
if err != nil {
return nil, xerrors.Errorf("file stat error: %w", err)
}
cvf.cf = newCachedFile(fi.Size(), f, cvf.threshold)
return cvf.cf.Open()
}
func (cvf *cachedVMFile) Clean() error {
if cvf.cf == nil {
return nil
}
return cvf.cf.Clean()
}
func (w *VM) detectLVM(sr io.SectionReader) (bool, error) {
buf := make([]byte, 512)
_, err := sr.ReadAt(buf, 512)
if err != nil {
return false, xerrors.Errorf("read header block error: %w", err)
}
_, err = sr.Seek(0, io.SeekStart)
if err != nil {
return false, xerrors.Errorf("seek error: %w", err)
}
// LABELONE is LVM signature
if string(buf[:8]) == "LABELONE" {
return true, nil
}
return false, nil
}
func shouldSkip(partition types.Partition) bool {
// skip empty partition
if bytes.Equal(partition.GetType(), []byte{0x00}) {
return true
}
if !slices.Contains(requiredDiskName, partition.Name()) {
return true
}
switch p := partition.(type) {
case *gpt.PartitionEntry:
return p.Bootable()
case *mbr.Partition:
return false
}
return false
}