2018-09-18 21:45:21 +00:00
|
|
|
// Copyright 2018 Mikhail Klementev. All rights reserved.
|
|
|
|
// Use of this source code is governed by a GPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package qemukernel
|
|
|
|
|
|
|
|
import (
|
2018-09-22 12:16:42 +00:00
|
|
|
"crypto/sha512"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-09-22 12:44:34 +00:00
|
|
|
"math/rand"
|
2018-09-19 06:13:28 +00:00
|
|
|
"net"
|
2018-09-22 12:44:34 +00:00
|
|
|
"os"
|
2018-09-19 07:00:12 +00:00
|
|
|
"strings"
|
2018-09-18 21:45:21 +00:00
|
|
|
"testing"
|
2018-09-22 07:28:08 +00:00
|
|
|
"time"
|
2018-09-18 21:45:21 +00:00
|
|
|
)
|
|
|
|
|
2018-09-22 12:44:34 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:45:21 +00:00
|
|
|
func TestQemuSystemNew_InvalidKernelPath(t *testing.T) {
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{Name: "Invalid", KernelPath: "/invalid/path"}
|
2018-09-19 06:13:28 +00:00
|
|
|
if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err == nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemNew_InvalidQemuArch(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2018-09-19 06:13:28 +00:00
|
|
|
if _, err := NewQemuSystem(unsupported, kernel, "/bin/sh"); err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2018-09-19 06:13:28 +00:00
|
|
|
if _, err := NewQemuSystem(X86_64, kernel, "/invalid/path"); err == nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemNew(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2018-09-19 06:13:28 +00:00
|
|
|
if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err != nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemStart(t *testing.T) {
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz}
|
2018-09-19 06:13:28 +00:00
|
|
|
qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh")
|
2018-09-18 21:45:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = qemu.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu.Stop()
|
|
|
|
}
|
2018-09-19 06:13:28 +00:00
|
|
|
|
2018-09-22 12:59:01 +00:00
|
|
|
func TestGetFreeAddrPort(t *testing.T) {
|
|
|
|
addrPort := getFreeAddrPort()
|
|
|
|
ln, err := net.Listen("tcp", addrPort)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ln.Close()
|
|
|
|
}
|
|
|
|
|
2018-09-22 07:28:08 +00:00
|
|
|
func TestQemuSystemStart_Timeout(t *testing.T) {
|
2018-09-22 12:59:01 +00:00
|
|
|
t.Parallel()
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz}
|
2018-09-22 07:28:08 +00:00
|
|
|
qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu.Timeout = time.Second
|
|
|
|
|
|
|
|
if err = qemu.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
if !qemu.Died {
|
|
|
|
t.Fatal("qemu does not died :c")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !qemu.KilledByTimeout {
|
|
|
|
t.Fatal("qemu died not because of timeout O_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 14:00:31 +00:00
|
|
|
func startTestQemu(t *testing.T, timeout time.Duration) (q *QemuSystem, err error) {
|
2018-09-22 12:59:01 +00:00
|
|
|
t.Parallel()
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{
|
|
|
|
Name: "Test kernel",
|
|
|
|
KernelPath: testConfigVmlinuz,
|
|
|
|
InitrdPath: testConfigInitrd,
|
|
|
|
}
|
2018-09-21 01:00:17 +00:00
|
|
|
q, err = NewQemuSystem(X86_64, kernel, testConfigRootfs)
|
2018-09-19 07:00:12 +00:00
|
|
|
if err != nil {
|
2018-09-20 23:45:46 +00:00
|
|
|
return
|
2018-09-19 07:00:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 14:00:31 +00:00
|
|
|
if timeout != 0 {
|
|
|
|
q.Timeout = timeout
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:46 +00:00
|
|
|
if err = q.Start(); err != nil {
|
|
|
|
return
|
2018-09-19 07:00:12 +00:00
|
|
|
}
|
2018-09-20 23:45:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemCommand(t *testing.T) {
|
2018-10-06 14:00:31 +00:00
|
|
|
qemu, err := startTestQemu(t, 0)
|
2018-09-22 10:34:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-19 07:00:12 +00:00
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
output, err := qemu.Command("root", "cat /etc/shadow")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "root::") {
|
|
|
|
t.Fatal("Wrong output from `cat /etc/shadow` by root")
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err = qemu.Command("user", "cat /etc/passwd")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "root:x:0:0:root:/root:/bin/bash") {
|
|
|
|
t.Fatal("Wrong output from `cat /etc/passwd` by user")
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err = qemu.Command("user", "cat /etc/shadow")
|
|
|
|
if err == nil { // unsucessful is good because user must not read /etc/shadow
|
|
|
|
t.Fatal("User have rights for /etc/shadow. WAT?!")
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 12:16:42 +00:00
|
|
|
|
|
|
|
func TestQemuSystemCopyFile(t *testing.T) {
|
2018-10-06 14:00:31 +00:00
|
|
|
qemu, err := startTestQemu(t, 0)
|
2018-09-22 12:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
localPath := "/bin/sh"
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sha_local := fmt.Sprintf("%x", sha512.Sum512(content))
|
|
|
|
|
|
|
|
err = qemu.CopyFile("user", localPath, "/tmp/test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sha_remote, err := qemu.Command("user", "sha512sum /tmp/test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sha_remote = strings.Split(sha_remote, " ")[0]
|
|
|
|
|
|
|
|
if sha_local != sha_remote {
|
|
|
|
t.Fatal(fmt.Sprintf("Broken file (%s instead of %s)", sha_remote, sha_local))
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 12:44:34 +00:00
|
|
|
|
|
|
|
func TestQemuSystemCopyAndRun(t *testing.T) {
|
2018-10-06 14:00:31 +00:00
|
|
|
qemu, err := startTestQemu(t, 0)
|
2018-09-22 12:44:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
randStr := fmt.Sprintf("%d", rand.Int())
|
|
|
|
content := []byte("#!/bin/sh\n echo -n " + randStr + "\n")
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "executable")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
if _, err := tmpfile.Write(content); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := tmpfile.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := qemu.CopyAndRun("user", tmpfile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if output != randStr {
|
|
|
|
t.Fatal("Wrong output from copyied executable (" + output + "," + randStr + ")")
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 13:18:25 +00:00
|
|
|
|
|
|
|
func TestQemuSystemCopyAndInsmod(t *testing.T) {
|
2018-10-06 14:00:31 +00:00
|
|
|
qemu, err := startTestQemu(t, 0)
|
2018-09-22 13:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
lsmodBefore, err := qemu.Command("root", "lsmod | wc -l")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = qemu.CopyAndInsmod(testConfigSampleKo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lsmodAfter, err := qemu.Command("root", "lsmod | wc -l")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lsmodBefore == lsmodAfter {
|
|
|
|
t.Fatal("insmod returns ok but there is no new kernel modules")
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 14:00:31 +00:00
|
|
|
|
2018-10-06 14:00:48 +00:00
|
|
|
func TestQemuSystemKernelPanic(t *testing.T) {
|
|
|
|
qemu, err := startTestQemu(t, time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
// Enable sysrq
|
|
|
|
_, err = qemu.Command("root", "echo 1 > /proc/sys/kernel/sysrq")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger kernel panic
|
|
|
|
err = qemu.AsyncCommand("root", "sleep 1s && echo c > /proc/sysrq-trigger")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for panic watcher timeout
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
if qemu.KilledByTimeout {
|
|
|
|
t.Fatal("qemu is killed by timeout, not because of panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !qemu.Died {
|
|
|
|
t.Fatal("qemu is not killed after kernel panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !qemu.KernelPanic {
|
|
|
|
t.Fatal("qemu is died but there's no information about panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 14:00:31 +00:00
|
|
|
func TestQemuSystemRun(t *testing.T) {
|
|
|
|
qemu, err := startTestQemu(t, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer qemu.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := qemu.Command("root", "echo")
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
err = qemu.AsyncCommand("root", "sleep 10s")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if time.Since(start) > time.Second {
|
|
|
|
t.Fatalf("qemu.Run does not async (waited %s)", +time.Since(start))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|