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/vminfo/linux.go | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 pkg/vminfo/linux.go (limited to 'pkg/vminfo/linux.go') diff --git a/pkg/vminfo/linux.go b/pkg/vminfo/linux.go new file mode 100644 index 000000000..c9cd1a3db --- /dev/null +++ b/pkg/vminfo/linux.go @@ -0,0 +1,148 @@ +// 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 + +import ( + "bufio" + "bytes" + "fmt" + "io" + "path" + "regexp" + "strconv" + "strings" + + "github.com/google/syzkaller/pkg/host" +) + +type linux int + +func (linux) RequiredFiles() []string { + return []string{ + "/proc/cpuinfo", + "/proc/modules", + "/sys/module/*/sections/.text", + "/sys/module/kvm*/parameters/*", + } +} + +func (linux) machineInfos() []machineInfoFunc { + return []machineInfoFunc{ + linuxReadCPUInfo, + linuxReadKVMInfo, + } +} + +func (linux) parseModules(files filesystem) ([]host.KernelModule, error) { + var modules []host.KernelModule + re := regexp.MustCompile(`(\w+) ([0-9]+) .*(0[x|X][a-fA-F0-9]+)[^\n]*`) + modulesText, _ := files.ReadFile("/proc/modules") + for _, match := range re.FindAllSubmatch(modulesText, -1) { + name := string(match[1]) + modAddr, err := strconv.ParseUint(string(match[3]), 0, 64) + if err != nil { + // /proc/modules is broken, bail out. + return nil, fmt.Errorf("module %v address parsing error: %w", name, err) + } + textAddr, err := linuxModuleTextAddr(files, name) + if err != nil { + // Module address unavailable, .text is probably 0. Skip this module. + continue + } + modSize, err := strconv.ParseUint(string(match[2]), 0, 64) + if err != nil { + // /proc/modules is broken, bail out. + return nil, fmt.Errorf("module %v size parsing error: %w", name, err) + } + offset := modAddr - textAddr + modules = append(modules, host.KernelModule{ + Name: name, + Addr: textAddr, + Size: modSize - offset, + }) + } + return modules, nil +} + +func linuxModuleTextAddr(files filesystem, module string) (uint64, error) { + data, err := files.ReadFile("/sys/module/" + module + "/sections/.text") + if err != nil { + return 0, fmt.Errorf("could not read module %v .text address file: %w", module, err) + } + addrString := strings.TrimSpace(string(data)) + addr, err := strconv.ParseUint(addrString, 0, 64) + if err != nil { + return 0, fmt.Errorf("address parsing error in %v: %w", module, err) + } + return addr, nil +} + +func linuxReadCPUInfo(files filesystem, w io.Writer) (string, error) { + data, err := files.ReadFile("/proc/cpuinfo") + if err != nil { + return "", fmt.Errorf("error reading CPU info:: %w", err) + } + + keyIndices := make(map[string]int) + type keyValues struct { + key string + values []string + } + var info []keyValues + for s := bufio.NewScanner(bytes.NewReader(data)); s.Scan(); { + splitted := strings.Split(s.Text(), ":") + if len(splitted) != 2 { + continue + } + key := strings.TrimSpace(splitted[0]) + val := strings.TrimSpace(splitted[1]) + if idx, ok := keyIndices[key]; !ok { + idx = len(keyIndices) + keyIndices[key] = idx + info = append(info, keyValues{key, []string{val}}) + } else { + info[idx].values = append(info[idx].values, val) + } + } + + for _, kv := range info { + // It is guaranteed that len(vals) >= 1 + key := kv.key + vals := kv.values + if allEqual(vals) { + fmt.Fprintf(w, "%-20s: %s\n", key, vals[0]) + } else { + fmt.Fprintf(w, "%-20s: %s\n", key, strings.Join(vals, ", ")) + } + } + return "CPU Info", nil +} + +func allEqual(slice []string) bool { + for i := 1; i < len(slice); i++ { + if slice[i] != slice[0] { + return false + } + } + return true +} + +func linuxReadKVMInfo(files filesystem, w io.Writer) (string, error) { + for _, module := range files.ReadDir("/sys/module") { + if !strings.HasPrefix(module, "kvm") { + continue + } + paramPath := path.Join("/sys", "module", module, "parameters") + fmt.Fprintf(w, "/sys/module/%s:\n", module) + for _, param := range files.ReadDir(paramPath) { + data, err := files.ReadFile(path.Join(paramPath, param)) + if err != nil { + return "", fmt.Errorf("error reading KVM info: %w", err) + } + fmt.Fprintf(w, "\t%s: %s", param, data) + } + w.Write([]byte{'\n'}) + } + return "KVM", nil +} -- cgit mrf-deployment