From 7009aebcd4c978e0f9d7cbb1f45c482104ff3019 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 16 Apr 2024 10:04:45 +0200 Subject: pkg/vminfo: add package This moves significant part of logic from the target to host (#1541), eventually this will allow us to switch target code from Go to C++. Currnetly syz-fuzzer parses a number of system files (/proc/cpuinfo) in non-trivial ways and passes that info to the host. This is problematic to recreate in C++. So instead make the fuzzer part as simple as possible: now it merely reads the gives set of files and returns contents. The rest of the parsing happens on the host (the new vminfo package). 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 (Checker.RequiredFiles), then fetches these files, and calls Checker.MachineInfo to parse the files and extract information about the VM. The information includes information about kernel modules and OS-specific info (for Linux that includes things like parsed /proc/cpuinfo). This also requires changing RPC flow between fuzzer and manager. Currently, Check call is optional and happens only for first VMs. With this change Check is always done because we need to return contents of the requested files always. The plan is to switch the rest of the pkg/host package to this scheme later: instead of some complex custom logic, we need to express it as some simple operations on the target (checking file presence, etc), and the rest of the logic on the host. --- pkg/host/machine_info.go | 82 ++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 37 deletions(-) (limited to 'pkg/host/machine_info.go') diff --git a/pkg/host/machine_info.go b/pkg/host/machine_info.go index 3c04becab..c32a0cf3b 100644 --- a/pkg/host/machine_info.go +++ b/pkg/host/machine_info.go @@ -4,40 +4,11 @@ package host import ( - "bytes" - "fmt" "os" + "path/filepath" "strings" ) -func CollectMachineInfo() ([]byte, error) { - buf := new(bytes.Buffer) - for _, pair := range machineInfoFuncs { - pos0 := buf.Len() - fmt.Fprintf(buf, "[%s]\n", pair.name) - pos1 := buf.Len() - err := pair.fn(buf) - if err != nil { - if !os.IsNotExist(err) { - return nil, err - } - } - if buf.Len() == pos1 { - buf.Truncate(pos0) - continue - } - fmt.Fprintf(buf, "\n%v\n\n", strings.Repeat("-", 80)) - } - return buf.Bytes(), nil -} - -func CollectModulesInfo() ([]KernelModule, error) { - if machineModulesInfo == nil { - return nil, nil - } - return machineModulesInfo() -} - func CollectGlobsInfo(globs map[string]bool) (map[string][]string, error) { if machineGlobsInfo == nil { return nil, nil @@ -45,17 +16,54 @@ func CollectGlobsInfo(globs map[string]bool) (map[string][]string, error) { return machineGlobsInfo(globs) } -var machineInfoFuncs []machineInfoFunc -var machineModulesInfo func() ([]KernelModule, error) var machineGlobsInfo func(map[string]bool) (map[string][]string, error) -type machineInfoFunc struct { - name string - fn func(*bytes.Buffer) error -} - type KernelModule struct { Name string `json:"Name"` Addr uint64 `json:"Addr"` Size uint64 `json:"Size"` } + +type FileInfo struct { + Name string + Exists bool + Error string + Data []byte +} + +func ReadFiles(files []string) []FileInfo { + var res []FileInfo + for _, glob := range files { + glob = filepath.FromSlash(glob) + if !strings.Contains(glob, "*") { + res = append(res, readFile(glob)) + continue + } + matches, err := filepath.Glob(glob) + if err != nil { + res = append(res, FileInfo{ + Name: glob, + Error: err.Error(), + }) + continue + } + for _, file := range matches { + res = append(res, readFile(file)) + } + } + return res +} + +func readFile(file string) FileInfo { + data, err := os.ReadFile(file) + exists, errStr := true, "" + if err != nil { + exists, errStr = os.IsNotExist(err), err.Error() + } + return FileInfo{ + Name: file, + Exists: exists, + Error: errStr, + Data: data, + } +} -- cgit mrf-deployment