2018-09-18 21:45:21 +00:00
|
|
|
// Copyright 2018 Mikhail Klementev. All rights reserved.
|
2018-10-08 20:51:32 +00:00
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
2018-09-18 21:45:21 +00:00
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
package qemu
|
2018-09-18 21:45:21 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemNew_InvalidKernelPath(t *testing.T) {
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{Name: "Invalid", KernelPath: "/invalid/path"}
|
2019-08-17 09:05:06 +00:00
|
|
|
if _, err := NewSystem(X86_64, kernel, "/bin/sh"); err == nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemNew_InvalidQemuArch(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2019-08-17 09:05:06 +00:00
|
|
|
if _, err := NewSystem(unsupported, kernel, "/bin/sh"); err == nil {
|
2018-09-19 06:13:28 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemNew_InvalidQemuDrivePath(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2019-08-17 09:05:06 +00:00
|
|
|
if _, err := NewSystem(X86_64, kernel, "/invalid/path"); err == nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemNew(t *testing.T) {
|
2018-09-22 14:00:49 +00:00
|
|
|
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
|
2019-08-17 09:05:06 +00:00
|
|
|
if _, err := NewSystem(X86_64, kernel, "/bin/sh"); err != nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemStart(t *testing.T) {
|
2018-09-22 10:34:43 +00:00
|
|
|
kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz}
|
2019-08-17 09:05:06 +00:00
|
|
|
q, err := NewSystem(X86_64, kernel, "/bin/sh")
|
2018-09-18 21:45:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if err = q.Start(); err != nil {
|
2018-09-18 21:45:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
q.Stop()
|
2018-09-18 21:45:21 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemStart_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}
|
2019-08-17 09:05:06 +00:00
|
|
|
q, err := NewSystem(X86_64, kernel, "/bin/sh")
|
2018-09-22 07:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
q.Timeout = time.Second
|
2018-09-22 07:28:08 +00:00
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if err = q.Start(); err != nil {
|
2018-09-22 07:28:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if !q.Died {
|
2018-09-22 07:28:08 +00:00
|
|
|
t.Fatal("qemu does not died :c")
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if !q.KilledByTimeout {
|
2018-09-22 07:28:08 +00:00
|
|
|
t.Fatal("qemu died not because of timeout O_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func startTestQemu(t *testing.T, timeout time.Duration) (q *System, 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,
|
|
|
|
}
|
2019-08-17 09:05:06 +00:00
|
|
|
q, err = NewSystem(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
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemCommand(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 0)
|
2018-09-22 10:34:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-09-19 07:00:12 +00:00
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
output, err := q.Command("root", "cat /etc/shadow")
|
2018-09-19 07:00:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "root::") {
|
|
|
|
t.Fatal("Wrong output from `cat /etc/shadow` by root")
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
output, err = q.Command("user", "cat /etc/passwd")
|
2018-09-19 07:00:12 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
_, err = q.Command("user", "cat /etc/shadow")
|
2018-12-10 02:37:40 +00:00
|
|
|
// unsuccessful is good because user must not read /etc/shadow
|
|
|
|
if err == nil {
|
2018-09-19 07:00:12 +00:00
|
|
|
t.Fatal("User have rights for /etc/shadow. WAT?!")
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 12:16:42 +00:00
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemCopyFile(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 0)
|
2018-09-22 12:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-09-22 12:16:42 +00:00
|
|
|
|
|
|
|
localPath := "/bin/sh"
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 02:37:40 +00:00
|
|
|
shaLocal := fmt.Sprintf("%x", sha512.Sum512(content))
|
2018-09-22 12:16:42 +00:00
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
err = q.CopyFile("user", localPath, "/tmp/test")
|
2018-09-22 12:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
shaRemote, err := q.Command("user", "sha512sum /tmp/test")
|
2018-09-22 12:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-10 02:37:40 +00:00
|
|
|
shaRemote = strings.Split(shaRemote, " ")[0]
|
2018-09-22 12:16:42 +00:00
|
|
|
|
2018-12-10 02:37:40 +00:00
|
|
|
if shaLocal != shaRemote {
|
|
|
|
t.Fatal(fmt.Sprintf("Broken file (%s instead of %s)",
|
|
|
|
shaRemote, shaLocal))
|
2018-09-22 12:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 12:44:34 +00:00
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemCopyAndRun(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 0)
|
2018-09-22 12:44:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-09-22 12:44:34 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
output, err := q.CopyAndRun("user", tmpfile.Name())
|
2018-09-22 12:44:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if output != randStr {
|
2018-12-10 02:37:40 +00:00
|
|
|
t.Fatal("Wrong output from copyied executable (" +
|
|
|
|
output + "," + randStr + ")")
|
2018-09-22 12:44:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 13:18:25 +00:00
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemCopyAndInsmod(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 0)
|
2018-09-22 13:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-09-22 13:18:25 +00:00
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
lsmodBefore, err := q.Command("root", "lsmod | wc -l")
|
2018-09-22 13:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
_, err = q.CopyAndInsmod(testConfigSampleKo)
|
2018-09-22 13:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
lsmodAfter, err := q.Command("root", "lsmod | wc -l")
|
2018-09-22 13:18:25 +00:00
|
|
|
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
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemKernelPanic(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 5*time.Minute)
|
2018-10-06 14:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-10-06 14:00:48 +00:00
|
|
|
|
|
|
|
// Enable sysrq
|
2019-02-02 21:33:27 +00:00
|
|
|
_, err = q.Command("root", "echo 1 > /proc/sys/kernel/sysrq")
|
2018-10-06 14:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger kernel panic
|
2019-02-02 21:33:27 +00:00
|
|
|
err = q.AsyncCommand("root", "sleep 1s && echo c > /proc/sysrq-trigger")
|
2018-10-06 14:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for panic watcher timeout
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if q.KilledByTimeout {
|
2018-10-06 14:00:48 +00:00
|
|
|
t.Fatal("qemu is killed by timeout, not because of panic")
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if !q.Died {
|
2018-10-06 14:00:48 +00:00
|
|
|
t.Fatal("qemu is not killed after kernel panic")
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:33:27 +00:00
|
|
|
if !q.KernelPanic {
|
2018-10-06 14:00:48 +00:00
|
|
|
t.Fatal("qemu is died but there's no information about panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemRun(t *testing.T) {
|
2019-02-02 21:33:27 +00:00
|
|
|
q, err := startTestQemu(t, 0)
|
2018-10-06 14:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 21:33:27 +00:00
|
|
|
defer q.Stop()
|
2018-10-06 14:00:31 +00:00
|
|
|
|
|
|
|
for {
|
2019-02-02 21:33:27 +00:00
|
|
|
_, err := q.Command("root", "echo")
|
2018-10-06 14:00:31 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
2019-02-02 21:33:27 +00:00
|
|
|
err = q.AsyncCommand("root", "sleep 1m")
|
2018-10-06 14:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-02 00:09:35 +00:00
|
|
|
if time.Since(start) > 10*time.Second {
|
2019-02-02 21:33:27 +00:00
|
|
|
t.Fatalf("q.AsyncCommand does not async (waited %s)",
|
2018-12-02 00:11:06 +00:00
|
|
|
time.Since(start))
|
2018-10-06 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-25 13:12:06 +00:00
|
|
|
|
|
|
|
func openedPort(port int) bool {
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-17 09:05:06 +00:00
|
|
|
func TestSystemDebug(t *testing.T) {
|
2018-11-25 13:12:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
kernel := Kernel{
|
|
|
|
KernelPath: testConfigVmlinuz,
|
|
|
|
InitrdPath: testConfigInitrd,
|
|
|
|
}
|
2019-08-17 09:05:06 +00:00
|
|
|
q, err := NewSystem(X86_64, kernel, testConfigRootfs)
|
2018-11-25 13:12:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
port := 45256
|
|
|
|
|
|
|
|
q.Debug(fmt.Sprintf("tcp::%d", port))
|
|
|
|
|
|
|
|
if openedPort(port) {
|
|
|
|
t.Fatal("Port opened before qemu starts")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = q.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer q.Stop()
|
|
|
|
|
2018-11-25 14:43:58 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-11-25 13:12:06 +00:00
|
|
|
if !openedPort(port) {
|
|
|
|
t.Fatal("Qemu debug port does not opened")
|
|
|
|
}
|
|
|
|
|
|
|
|
q.Stop()
|
|
|
|
|
|
|
|
if openedPort(port) {
|
|
|
|
t.Fatal("Qemu listens after die")
|
|
|
|
}
|
|
|
|
}
|