|
| 1 | +import os |
| 2 | +import json |
| 3 | +import time |
| 4 | +import shutil |
| 5 | + |
| 6 | +from avocado.utils import software_manager |
| 7 | +from avocado.utils import process |
| 8 | + |
| 9 | +from virttest import utils_misc |
| 10 | +from virttest import ssh_key |
| 11 | +from virttest import storage |
| 12 | +from virttest import data_dir |
| 13 | + |
| 14 | + |
| 15 | +def run(test, params, env): |
| 16 | + """ |
| 17 | + Setup and run syzkaller (https://github.com/google/syzkaller) |
| 18 | + 1. Install/Setup syzkaller in host |
| 19 | + 2. Setup Guest for passwordless ssh from host |
| 20 | + 3. Prepare and compile Guest kernel |
| 21 | + 4. Prepare syzkaller config with qemu params and guest params |
| 22 | + 5. Start sykaller with above config and run for specified time(test_timeout) |
| 23 | + 6. Test fails out incase of any host issues |
| 24 | + """ |
| 25 | + start_time = time.time() |
| 26 | + # Step 1: Install/Setup syzkaller in host |
| 27 | + sm = software_manager.SoftwareManager() |
| 28 | + if not sm.check_installed("go") and not sm.install("go"): |
| 29 | + test.cancel("golang package install failed") |
| 30 | + home = os.environ["HOME"] |
| 31 | + if not ("goroot/bin" in os.environ["PATH"] and "go/bin" in os.environ["PATH"]): |
| 32 | + process.run('echo "PATH=%s/goroot/bin:%s/go/bin:$PATH" >> %s/.bashrc' % (home, home, home), shell=True) |
| 33 | + process.run("source %s/.bashrc" % home, shell=True) |
| 34 | + process.run("go get -u -d github.com/google/syzkaller/...", shell=True) |
| 35 | + process.run("cd %s/go/src/github.com/google/syzkaller;make" % home, shell=True) |
| 36 | + syzkaller_path = "%s/go/src/github.com/google/syzkaller" % home |
| 37 | + |
| 38 | + # Step 2: Setup Guest for passwordless ssh from host |
| 39 | + vm = env.get_vm(params["main_vm"]) |
| 40 | + session = vm.wait_for_login() |
| 41 | + ssh_key.setup_ssh_key(vm.get_address(), |
| 42 | + params.get("username"), |
| 43 | + params.get("password")) |
| 44 | + session.close() |
| 45 | + vm.destroy() |
| 46 | + |
| 47 | + # Step 3: Prepare Guest kernel |
| 48 | + guest_kernel_repo = params.get("syz_kernel_repo") |
| 49 | + guest_kernel_branch = params.get("syz_kernel_branch") |
| 50 | + guest_kernel_config = params.get("syz_kernel_config") |
| 51 | + guest_kernel_build_path = utils_misc.get_path(test.debugdir, "linux") |
| 52 | + process.run("git clone --depth 1 %s -b %s %s" % (guest_kernel_repo, guest_kernel_branch, guest_kernel_build_path), shell=True) |
| 53 | + process.run("cd %s;git log -1;make %s" % (guest_kernel_build_path, guest_kernel_config), shell=True) |
| 54 | + process.run('cd %s; echo "CONFIG_KCOV=y\nCONFIG_GCC_PLUGINS=y" >> .config; make olddefconfig' % guest_kernel_build_path, shell=True) |
| 55 | + process.run("cd %s;make -j 40" % guest_kernel_build_path, shell=True) |
| 56 | + |
| 57 | + # Step 4: Prepare syzkaller config with qemu params and guest params |
| 58 | + syz_config_path = utils_misc.get_path(test.debugdir, "syzkaller_config") |
| 59 | + os.makedirs("%s/syzkaller" % test.debugdir) |
| 60 | + workdir = "%s/syzkaller" % test.debugdir |
| 61 | + sshkey = "%s/.ssh/id_rsa" % os.environ["HOME"] |
| 62 | + kernel_path = "%s/vmlinux" % guest_kernel_build_path |
| 63 | + |
| 64 | + vm_config = { |
| 65 | + "count": int(params.get("syz_count")), |
| 66 | + "cpu": int(params.get("smp")), |
| 67 | + "mem": int(params.get("mem")), |
| 68 | + "kernel": kernel_path, |
| 69 | + "cmdline": params.get("kernel_args"), |
| 70 | + "qemu_args": params.get("syz_qemu_args") |
| 71 | + } |
| 72 | + |
| 73 | + syz_config = { |
| 74 | + 'target': params.get("syz_target"), |
| 75 | + 'workdir': workdir, |
| 76 | + "http": params.get("syz_http"), |
| 77 | + "image": storage.get_image_filename(params, data_dir.get_data_dir()), |
| 78 | + "syzkaller": syzkaller_path, |
| 79 | + "procs": int(params.get("syz_procs")), |
| 80 | + "type": "qemu", |
| 81 | + "sshkey": sshkey, |
| 82 | + "vm": vm_config |
| 83 | + } |
| 84 | + try: |
| 85 | + with open(syz_config_path, "w") as fp: |
| 86 | + json.dump(syz_config, fp) |
| 87 | + except IOError as err: |
| 88 | + test.error("Unable to update syzkaller config: %s", err) |
| 89 | + end_time = time.time() |
| 90 | + # Step 5: Start sykaller config with specified time |
| 91 | + # Let's calculate the syzkaller timeout from |
| 92 | + # test timeout excluding current elapsed time + buffer |
| 93 | + testtimeout = int(params.get("test_timeout")) - (int(end_time - start_time) + 10) |
| 94 | + cmd = "%s/bin/syz-manager -config %s %s" % (syzkaller_path, syz_config_path, params.get("syz_cmd_params")) |
| 95 | + process.run(cmd, timeout=testtimeout, |
| 96 | + allow_output_check="combined", |
| 97 | + ignore_status=True, shell=True) |
| 98 | + # Let's delete linux kernel folder from test-results as it would |
| 99 | + # consume lot of space and test log have all the information about |
| 100 | + # it incase to retrieve it back. |
| 101 | + if os.path.isdir(guest_kernel_build_path): |
| 102 | + shutil.rmtree(guest_kernel_build_path) |
0 commit comments