-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
vminfo.go
166 lines (146 loc) · 4.22 KB
/
vminfo.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
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// Package vminfo extracts information about the target VM.
// The package itself runs on the host, which may be a different OS/arch.
// User of the package first requests set of files that needs to be fetched from the VM
// and set of test programs that needs to be executed in the VM (Checker.RequiredThings),
// then fetches these files and executes test programs, and calls Checker.MachineInfo
// to parse the files and extract information about the VM, and optionally calls
// Checker.Check to obtain list of enabled/disabled syscalls.
// The information includes information about kernel modules and OS-specific info
// (for Linux that includes things like parsed /proc/cpuinfo).
package vminfo
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"strings"
"github.com/google/syzkaller/pkg/cover"
"github.com/google/syzkaller/pkg/flatrpc"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/rpctype"
"github.com/google/syzkaller/prog"
"github.com/google/syzkaller/sys/targets"
)
type Checker struct {
checker
checkContext *checkContext
}
func New(cfg *mgrconfig.Config) *Checker {
var impl checker
switch cfg.TargetOS {
case targets.Linux:
impl = new(linux)
case targets.NetBSD:
impl = new(netbsd)
case targets.OpenBSD:
impl = new(openbsd)
default:
impl = new(stub)
}
return &Checker{
checker: impl,
checkContext: newCheckContext(cfg, impl),
}
}
func (checker *Checker) MachineInfo(fileInfos []flatrpc.FileInfo) ([]cover.KernelModule, []byte, error) {
files := createVirtualFilesystem(fileInfos)
modules, err := checker.parseModules(files)
if err != nil {
return nil, nil, err
}
info := new(bytes.Buffer)
tmp := new(bytes.Buffer)
for _, fn := range checker.machineInfos() {
tmp.Reset()
name, err := fn(files, tmp)
if err != nil {
if !os.IsNotExist(err) {
return nil, nil, err
}
continue
}
if tmp.Len() == 0 {
continue
}
fmt.Fprintf(info, "[%v]\n%s\n%v\n\n", name, tmp.Bytes(), strings.Repeat("-", 80))
}
return modules, info.Bytes(), nil
}
func (checker *Checker) StartCheck() ([]string, []rpctype.ExecutionRequest) {
return checker.checkFiles(), checker.checkContext.startCheck()
}
func (checker *Checker) FinishCheck(files []flatrpc.FileInfo, progs []rpctype.ExecutionResult,
featureInfos []flatrpc.FeatureInfo) (map[*prog.Syscall]bool, map[*prog.Syscall]string, Features, error) {
ctx := checker.checkContext
checker.checkContext = nil
return ctx.finishCheck(files, progs, featureInfos)
}
type machineInfoFunc func(files filesystem, w io.Writer) (string, error)
type checker interface {
RequiredFiles() []string
checkFiles() []string
parseModules(files filesystem) ([]cover.KernelModule, error)
machineInfos() []machineInfoFunc
syscallCheck(*checkContext, *prog.Syscall) string
}
type filesystem map[string]flatrpc.FileInfo
func createVirtualFilesystem(fileInfos []flatrpc.FileInfo) filesystem {
files := make(filesystem)
for _, file := range fileInfos {
if file.Exists {
files[file.Name] = file
}
}
return files
}
func (files filesystem) ReadFile(name string) ([]byte, error) {
file, ok := files[name]
if !ok {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
if file.Error != "" {
return nil, errors.New(file.Error)
}
return file.Data, nil
}
func (files filesystem) ReadDir(dir string) []string {
var res []string
dedup := make(map[string]bool)
for _, file := range files {
if len(file.Name) < len(dir)+2 ||
!strings.HasPrefix(file.Name, dir) ||
file.Name[len(dir)] != '/' {
continue
}
name := file.Name[len(dir)+1:]
if slash := strings.Index(name, "/"); slash != -1 {
name = name[:slash]
}
if dedup[name] {
continue
}
dedup[name] = true
res = append(res, name)
}
return res
}
type stub int
func (stub) RequiredFiles() []string {
return nil
}
func (stub) checkFiles() []string {
return nil
}
func (stub) parseModules(files filesystem) ([]cover.KernelModule, error) {
return nil, nil
}
func (stub) machineInfos() []machineInfoFunc {
return nil
}
func (stub) syscallCheck(*checkContext, *prog.Syscall) string {
return ""
}