2015-12-27 11:20:00 +00:00
|
|
|
// Copyright 2015 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 host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-16 10:18:50 +00:00
|
|
|
"fmt"
|
2015-12-27 11:20:00 +00:00
|
|
|
"io/ioutil"
|
2018-06-12 12:05:02 +00:00
|
|
|
"os"
|
2018-07-13 10:46:32 +00:00
|
|
|
"os/exec"
|
2019-01-04 18:56:16 +00:00
|
|
|
"regexp"
|
2017-01-08 16:20:32 +00:00
|
|
|
"runtime"
|
2016-01-25 18:08:17 +00:00
|
|
|
"strconv"
|
2015-12-27 11:20:00 +00:00
|
|
|
"strings"
|
2018-04-06 16:46:49 +00:00
|
|
|
"sync"
|
2015-12-27 11:20:00 +00:00
|
|
|
"syscall"
|
2018-06-12 12:05:02 +00:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
2015-12-27 11:20:00 +00:00
|
|
|
|
2019-01-04 18:56:16 +00:00
|
|
|
"github.com/google/syzkaller/pkg/log"
|
2017-06-17 10:23:52 +00:00
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2017-09-05 11:31:14 +00:00
|
|
|
"github.com/google/syzkaller/prog"
|
2018-06-12 12:05:02 +00:00
|
|
|
"github.com/google/syzkaller/sys/linux"
|
2015-12-27 11:20:00 +00:00
|
|
|
)
|
|
|
|
|
2019-01-16 13:50:43 +00:00
|
|
|
type KcovRemoteArg struct {
|
|
|
|
TraceMode uint32
|
|
|
|
AreaSize uint32
|
|
|
|
NumHandles uint32
|
|
|
|
CommonHandle uint64
|
|
|
|
// Handles []uint64 goes here.
|
|
|
|
}
|
|
|
|
|
2019-01-04 18:56:16 +00:00
|
|
|
func isSupported(c *prog.Syscall, target *prog.Target, sandbox string) (bool, string) {
|
|
|
|
log.Logf(2, "checking support for %v", c.Name)
|
2018-06-18 17:45:45 +00:00
|
|
|
if strings.HasPrefix(c.CallName, "syz_") {
|
|
|
|
return isSupportedSyzkall(sandbox, c)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(c.Name, "socket$") ||
|
|
|
|
strings.HasPrefix(c.Name, "socketpair$") {
|
|
|
|
return isSupportedSocket(c)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(c.Name, "openat$") {
|
|
|
|
return isSupportedOpenAt(c)
|
|
|
|
}
|
2018-07-10 14:18:45 +00:00
|
|
|
if strings.HasPrefix(c.Name, "mount$") {
|
|
|
|
return isSupportedMount(c, sandbox)
|
|
|
|
}
|
2018-08-29 19:35:25 +00:00
|
|
|
if c.Name == "ioctl$EXT4_IOC_SHUTDOWN" && sandbox == "none" {
|
|
|
|
// Don't shutdown root filesystem.
|
|
|
|
return false, "unsafe with sandbox=none"
|
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
// There are 3 possible strategies for detecting supported syscalls:
|
2017-09-05 11:31:14 +00:00
|
|
|
// 1. Executes all syscalls with presumably invalid arguments and check for ENOprog.
|
2015-12-27 11:20:00 +00:00
|
|
|
// But not all syscalls are safe to execute. For example, pause will hang,
|
|
|
|
// while setpgrp will push the process into own process group.
|
|
|
|
// 2. Check presence of /sys/kernel/debug/tracing/events/syscalls/sys_enter_* files.
|
|
|
|
// This requires root and CONFIG_FTRACE_SYSCALLS. Also it lies for some syscalls.
|
|
|
|
// For example, on x86_64 it says that sendfile is not present (only sendfile64).
|
|
|
|
// 3. Check sys_syscallname in /proc/kallsyms.
|
2018-06-18 17:45:45 +00:00
|
|
|
// Requires CONFIG_KALLSYMS.
|
|
|
|
// Kallsyms seems to be the most reliable and fast. That's what we use first.
|
|
|
|
// If kallsyms is not present, we fallback to execution of syscalls.
|
2018-04-06 16:46:49 +00:00
|
|
|
kallsymsOnce.Do(func() {
|
2019-01-04 18:56:16 +00:00
|
|
|
kallsyms, _ := ioutil.ReadFile("/proc/kallsyms")
|
|
|
|
if len(kallsyms) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var re *regexp.Regexp
|
|
|
|
switch target.Arch {
|
|
|
|
case "386", "amd64":
|
2019-01-08 18:36:52 +00:00
|
|
|
re = regexp.MustCompile(` T (__ia32_|__x64_)?sys_([^\n]+)\n`)
|
2019-01-04 18:56:16 +00:00
|
|
|
case "arm64":
|
2019-01-08 18:36:52 +00:00
|
|
|
re = regexp.MustCompile(` T (__arm64_)?sys_([^\n]+)\n`)
|
2019-01-11 06:20:04 +00:00
|
|
|
case "ppc64le":
|
|
|
|
re = regexp.MustCompile(` T ()?sys_([^\n]+)\n`)
|
2019-01-04 18:56:16 +00:00
|
|
|
default:
|
|
|
|
panic("unsupported arch for kallsyms parsing")
|
|
|
|
}
|
|
|
|
matches := re.FindAllSubmatch(kallsyms, -1)
|
|
|
|
for _, m := range matches {
|
|
|
|
name := string(m[2])
|
|
|
|
log.Logf(2, "found in kallsyms: %v", name)
|
|
|
|
kallsymsSyscallSet[name] = true
|
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
})
|
2019-01-04 18:56:16 +00:00
|
|
|
if !testFallback && len(kallsymsSyscallSet) != 0 {
|
|
|
|
r, v := isSupportedKallsyms(c)
|
|
|
|
return r, v
|
2015-12-28 09:45:30 +00:00
|
|
|
}
|
2018-06-18 17:45:45 +00:00
|
|
|
return isSupportedTrial(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSupportedKallsyms(c *prog.Syscall) (bool, string) {
|
2017-11-20 16:23:24 +00:00
|
|
|
name := c.CallName
|
2019-01-04 18:56:16 +00:00
|
|
|
if newname := kallsymsRenameMap[name]; newname != "" {
|
2017-11-20 16:23:24 +00:00
|
|
|
name = newname
|
|
|
|
}
|
2019-01-04 18:56:16 +00:00
|
|
|
if !kallsymsSyscallSet[name] {
|
2018-04-06 16:46:49 +00:00
|
|
|
return false, fmt.Sprintf("sys_%v is not present in /proc/kallsyms", name)
|
|
|
|
}
|
|
|
|
return true, ""
|
2017-11-20 16:23:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 17:45:45 +00:00
|
|
|
func isSupportedTrial(c *prog.Syscall) (bool, string) {
|
|
|
|
switch c.CallName {
|
|
|
|
// These known to cause hangs.
|
|
|
|
case "exit", "pause":
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
trialMu.Lock()
|
|
|
|
defer trialMu.Unlock()
|
|
|
|
if res, ok := trialSupported[c.NR]; ok {
|
|
|
|
return res, "ENOSYS"
|
|
|
|
}
|
|
|
|
cmd := osutil.Command(os.Args[0])
|
|
|
|
cmd.Env = []string{fmt.Sprintf("SYZ_TRIAL_TEST=%v", c.NR)}
|
|
|
|
_, err := osutil.Run(10*time.Second, cmd)
|
|
|
|
res := err != nil
|
|
|
|
trialSupported[c.NR] = res
|
|
|
|
return res, "ENOSYS"
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
str := os.Getenv("SYZ_TRIAL_TEST")
|
|
|
|
if str == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nr, err := strconv.Atoi(str)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
arg := ^uintptr(0) - 1e4 // something as invalid as possible
|
|
|
|
_, _, err = syscall.Syscall6(uintptr(nr), arg, arg, arg, arg, arg, arg)
|
|
|
|
if err == syscall.ENOSYS {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-11-20 16:23:24 +00:00
|
|
|
// Some syscall names diverge in __NR_* consts and kallsyms.
|
|
|
|
// umount2 is renamed to umount in arch/x86/entry/syscalls/syscall_64.tbl.
|
|
|
|
// Where umount is renamed to oldumount is unclear.
|
2018-04-06 16:46:49 +00:00
|
|
|
var (
|
2019-01-04 18:56:16 +00:00
|
|
|
kallsymsOnce sync.Once
|
|
|
|
kallsymsSyscallSet = make(map[string]bool)
|
|
|
|
kallsymsRenameMap = map[string]string{
|
2018-04-06 16:46:49 +00:00
|
|
|
"umount": "oldumount",
|
|
|
|
"umount2": "umount",
|
|
|
|
}
|
2018-06-18 17:45:50 +00:00
|
|
|
trialMu sync.Mutex
|
|
|
|
trialSupported = make(map[uint64]bool)
|
|
|
|
filesystems []byte
|
|
|
|
filesystemsOnce sync.Once
|
2018-04-06 16:46:49 +00:00
|
|
|
)
|
2015-12-28 09:45:30 +00:00
|
|
|
|
2018-08-02 14:55:29 +00:00
|
|
|
// The function is lengthy as it handles all pseudo-syscalls,
|
|
|
|
// but it does not seem to cause comprehension problems as there is no shared state.
|
|
|
|
// Splitting this per-syscall will only increase code size.
|
|
|
|
// nolint: gocyclo
|
2018-04-06 16:46:49 +00:00
|
|
|
func isSupportedSyzkall(sandbox string, c *prog.Syscall) (bool, string) {
|
2015-12-27 11:20:00 +00:00
|
|
|
switch c.CallName {
|
2016-01-13 17:57:12 +00:00
|
|
|
case "syz_open_dev":
|
2017-09-05 11:31:14 +00:00
|
|
|
if _, ok := c.Args[0].(*prog.ConstType); ok {
|
2017-02-10 12:54:59 +00:00
|
|
|
// This is for syz_open_dev$char/block.
|
|
|
|
// They are currently commented out, but in case one enables them.
|
2018-04-06 16:46:49 +00:00
|
|
|
return true, ""
|
2017-02-10 12:54:59 +00:00
|
|
|
}
|
2016-10-31 21:15:13 +00:00
|
|
|
fname, ok := extractStringConst(c.Args[0])
|
2016-01-13 17:57:12 +00:00
|
|
|
if !ok {
|
|
|
|
panic("first open arg is not a pointer to string const")
|
|
|
|
}
|
2016-01-25 18:08:17 +00:00
|
|
|
var check func(dev string) bool
|
|
|
|
check = func(dev string) bool {
|
|
|
|
if !strings.Contains(dev, "#") {
|
2018-07-10 11:38:27 +00:00
|
|
|
// Note: don't try to open them all, some can hang (e.g. /dev/snd/pcmC#D#p).
|
2018-04-08 11:38:07 +00:00
|
|
|
return osutil.IsExist(dev)
|
2016-01-25 18:08:17 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if check(strings.Replace(dev, "#", strconv.Itoa(i), 1)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
if !check(fname) {
|
|
|
|
return false, fmt.Sprintf("file %v does not exist", fname)
|
|
|
|
}
|
2018-04-08 11:38:07 +00:00
|
|
|
return onlySandboxNoneOrNamespace(sandbox)
|
2017-11-27 08:08:59 +00:00
|
|
|
case "syz_open_procfs":
|
2018-04-06 16:46:49 +00:00
|
|
|
return true, ""
|
2016-01-13 17:57:12 +00:00
|
|
|
case "syz_open_pts":
|
2018-04-06 16:46:49 +00:00
|
|
|
return true, ""
|
sys, executor: extract tcp sequence numbers from /dev/net/tun
This commit adds a new pseudo syscall syz_extract_tcp_res, that reads
a packet from /dev/net/tun and extracts tcp sequence numbers to be used
in subsequent packets.
As a result this syzkaller program:
mmap(&(0x7f0000000000/0x10000)=nil, (0x10000), 0x3, 0x32, 0xffffffffffffffff, 0x0)
r0 = socket$inet_tcp(0x2, 0x1, 0x0)
bind$inet(r0, &(0x7f0000001000)={0x2, 0x0, @empty=0x0, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]}, 0x10)
listen(r0, 0x5)
syz_emit_ethernet(0x36, &(0x7f0000002000)={@local={[0xaa, 0xaa, 0xaa, 0xaa, 0xaa], 0x0}, @random="4c6112cc15d8", [], {{0x800, @ipv4={{0x5, 0x4, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x6, 0x0, @remote={0xac, 0x14, 0x0, 0xbb}, @local={0xac, 0x14, 0x0, 0xaa}, {[]}}, @tcp={{0x1, 0x0, 0x42424242, 0x42424242, 0x0, 0x0, 0x5, 0x2, 0x0, 0x0, 0x0, {[]}}, {""}}}}}})
syz_extract_tcp_res(&(0x7f0000003000)={<r1=>0x42424242, <r2=>0x42424242}, 0x1, 0x0)
syz_emit_ethernet(0x38, &(0x7f0000004000)={@local={[0xaa, 0xaa, 0xaa, 0xaa, 0xaa], 0x0}, @remote={[0xbb, 0xbb, 0xbb, 0xbb, 0xbb], 0x0}, [], {{0x800, @ipv4={{0x5, 0x4, 0x0, 0x0, 0x2a, 0x0, 0x0, 0x0, 0x6, 0x0, @remote={0xac, 0x14, 0x0, 0xbb}, @local={0xac, 0x14, 0x0, 0xaa}, {[]}}, @tcp={{0x1, 0x0, r2, r1, 0x0, 0x0, 0x5, 0x10, 0x0, 0x0, 0x0, {[]}}, {"0c10"}}}}}})
r3 = accept$inet(r0, &(0x7f0000005000)={0x0, 0x0, @multicast1=0x0, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]}, &(0x7f0000006000)=0x10)
established a TCP connection:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:20000 0.0.0.0:* LISTEN 5477/a.out
tcp 2 0 172.20.0.170:20000 172.20.0.187:20001 ESTABLISHED 5477/a.out
Similar program for IPv6:
mmap(&(0x7f0000000000/0x10000)=nil, (0x10000), 0x3, 0x32, 0xffffffffffffffff, 0x0)
r0 = socket$inet6_tcp(0xa, 0x1, 0x0)
bind$inet6(r0, &(0x7f0000000000)={0xa, 0x1, 0x0, @empty={[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]}, 0x0}, 0x1c)
listen(r0, 0x5)
syz_emit_ethernet(0x4a, &(0x7f0000001000)={@local={[0xaa, 0xaa, 0xaa, 0xaa, 0xaa], 0x0}, @random="de895db1468d", [], {{0x86dd, @ipv6={0x0, 0x6, "a228af", 0x14, 0x6, 0x0, @remote={0xfe, 0x80, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], 0x0, 0xbb}, @local={0xfe, 0x80, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], 0x0, 0xaa}, {[], @tcp={{0x0, 0x1, 0x42424242, 0x42424242, 0x0, 0x0, 0x5, 0x2, 0x0, 0x0, 0x0, {[]}}, {""}}}}}}})
syz_extract_tcp_res(&(0x7f0000002000)={<r1=>0x42424242, <r2=>0x42424242}, 0x1, 0x0)
syz_emit_ethernet(0x4a, &(0x7f0000003000)={@local={[0xaa, 0xaa, 0xaa, 0xaa, 0xaa], 0x0}, @random="de895db1468d", [], {{0x86dd, @ipv6={0x0, 0x6, "a228af", 0x14, 0x6, 0x0, @remote={0xfe, 0x80, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], 0x0, 0xbb}, @local={0xfe, 0x80, [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], 0x0, 0xaa}, {[], @tcp={{0x0, 0x1, r2, r1, 0x0, 0x0, 0x5, 0x10, 0x0, 0x0, 0x0, {[]}}, {""}}}}}}})
r3 = accept$inet6(r0, &(0x7f0000004000)={0x0, 0x0, 0x0, @empty={[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]}, 0x0}, &(0x7f0000005000)=0x1c)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::20001 :::* LISTEN 5527/a.out
tcp6 0 0 fe80::aa:20001 fe80::bb:20000 ESTABLISHED 5527/a.out
2017-05-16 14:14:58 +00:00
|
|
|
case "syz_emit_ethernet", "syz_extract_tcp_res":
|
2018-06-12 12:05:02 +00:00
|
|
|
reason := checkNetworkInjection()
|
|
|
|
return reason == "", reason
|
2017-01-08 16:20:32 +00:00
|
|
|
case "syz_kvm_setup_cpu":
|
|
|
|
switch c.Name {
|
|
|
|
case "syz_kvm_setup_cpu$x86":
|
2018-04-06 16:46:49 +00:00
|
|
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" {
|
|
|
|
return true, ""
|
|
|
|
}
|
2017-01-12 10:57:17 +00:00
|
|
|
case "syz_kvm_setup_cpu$arm64":
|
2018-04-06 16:46:49 +00:00
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return true, ""
|
|
|
|
}
|
2017-01-08 16:20:32 +00:00
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
return false, "unsupported arch"
|
2018-03-05 11:07:59 +00:00
|
|
|
case "syz_init_net_socket":
|
|
|
|
// Unfortunately this only works with sandbox none at the moment.
|
|
|
|
// The problem is that setns of a network namespace requires CAP_SYS_ADMIN
|
|
|
|
// in the target namespace, and we've lost all privs in the init namespace
|
|
|
|
// during creation of a user namespace.
|
2018-04-08 11:38:07 +00:00
|
|
|
if ok, reason := onlySandboxNone(sandbox); !ok {
|
|
|
|
return false, reason
|
2018-03-05 11:07:59 +00:00
|
|
|
}
|
|
|
|
return isSupportedSocket(c)
|
2018-03-21 11:18:36 +00:00
|
|
|
case "syz_genetlink_get_family_id":
|
2018-04-06 16:46:49 +00:00
|
|
|
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_GENERIC)
|
2018-03-21 11:18:36 +00:00
|
|
|
if fd == -1 {
|
2018-04-06 16:46:49 +00:00
|
|
|
return false, fmt.Sprintf("socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC) failed: %v", err)
|
2018-03-21 11:18:36 +00:00
|
|
|
}
|
|
|
|
syscall.Close(fd)
|
2018-04-06 16:46:49 +00:00
|
|
|
return true, ""
|
2018-03-28 12:42:02 +00:00
|
|
|
case "syz_mount_image":
|
2018-06-18 17:45:50 +00:00
|
|
|
if ok, reason := onlySandboxNone(sandbox); !ok {
|
|
|
|
return ok, reason
|
|
|
|
}
|
|
|
|
fstype, ok := extractStringConst(c.Args[0])
|
|
|
|
if !ok {
|
|
|
|
panic("syz_mount_image arg is not string")
|
|
|
|
}
|
2018-07-10 14:18:45 +00:00
|
|
|
return isSupportedFilesystem(fstype)
|
2018-04-01 16:29:56 +00:00
|
|
|
case "syz_read_part_table":
|
2018-04-08 11:38:07 +00:00
|
|
|
return onlySandboxNone(sandbox)
|
2018-08-31 04:10:38 +00:00
|
|
|
case "syz_execute_func":
|
|
|
|
return true, ""
|
2015-12-27 11:20:00 +00:00
|
|
|
}
|
2017-01-08 16:20:32 +00:00
|
|
|
panic("unknown syzkall: " + c.Name)
|
2015-12-27 11:20:00 +00:00
|
|
|
}
|
2015-12-28 09:45:30 +00:00
|
|
|
|
2018-04-08 11:38:07 +00:00
|
|
|
func onlySandboxNone(sandbox string) (bool, string) {
|
|
|
|
if syscall.Getuid() != 0 || sandbox != "none" {
|
|
|
|
return false, "only supported under root with sandbox=none"
|
|
|
|
}
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func onlySandboxNoneOrNamespace(sandbox string) (bool, string) {
|
|
|
|
if syscall.Getuid() != 0 || sandbox == "setuid" {
|
|
|
|
return false, "only supported under root with sandbox=none/namespace"
|
|
|
|
}
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:46:49 +00:00
|
|
|
func isSupportedSocket(c *prog.Syscall) (bool, string) {
|
2017-09-05 11:31:14 +00:00
|
|
|
af, ok := c.Args[0].(*prog.ConstType)
|
2015-12-28 09:45:30 +00:00
|
|
|
if !ok {
|
|
|
|
panic("socket family is not const")
|
|
|
|
}
|
|
|
|
fd, err := syscall.Socket(int(af.Val), 0, 0)
|
|
|
|
if fd != -1 {
|
|
|
|
syscall.Close(fd)
|
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
if err == syscall.ENOSYS {
|
|
|
|
return false, "socket syscall returns ENOSYS"
|
|
|
|
}
|
|
|
|
if err == syscall.EAFNOSUPPORT {
|
|
|
|
return false, "socket family is not supported (EAFNOSUPPORT)"
|
|
|
|
}
|
2018-06-18 17:45:45 +00:00
|
|
|
proto, ok := c.Args[2].(*prog.ConstType)
|
|
|
|
if !ok {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
var typ uint64
|
|
|
|
if arg, ok := c.Args[1].(*prog.ConstType); ok {
|
|
|
|
typ = arg.Val
|
|
|
|
} else if arg, ok := c.Args[1].(*prog.FlagsType); ok {
|
|
|
|
typ = arg.Vals[0]
|
|
|
|
} else {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
fd, err = syscall.Socket(int(af.Val), int(typ), int(proto.Val))
|
|
|
|
if fd != -1 {
|
|
|
|
syscall.Close(fd)
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
return false, err.Error()
|
2015-12-28 09:45:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:46:49 +00:00
|
|
|
func isSupportedOpenAt(c *prog.Syscall) (bool, string) {
|
2017-01-05 14:13:12 +00:00
|
|
|
fname, ok := extractStringConst(c.Args[1])
|
2018-03-22 12:24:02 +00:00
|
|
|
if !ok || len(fname) == 0 || fname[0] != '/' {
|
2018-04-06 16:46:49 +00:00
|
|
|
return true, ""
|
2017-01-05 14:13:12 +00:00
|
|
|
}
|
|
|
|
fd, err := syscall.Open(fname, syscall.O_RDONLY, 0)
|
|
|
|
if fd != -1 {
|
|
|
|
syscall.Close(fd)
|
|
|
|
}
|
2018-04-06 16:46:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Sprintf("open(%v) failed: %v", fname, err)
|
|
|
|
}
|
|
|
|
return true, ""
|
2017-01-05 14:13:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 14:18:45 +00:00
|
|
|
func isSupportedMount(c *prog.Syscall, sandbox string) (bool, string) {
|
|
|
|
fstype, ok := extractStringConst(c.Args[2])
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("%v: filesystem is not string const", c.Name))
|
|
|
|
}
|
|
|
|
if ok, reason := isSupportedFilesystem(fstype); !ok {
|
|
|
|
return ok, reason
|
|
|
|
}
|
|
|
|
switch fstype {
|
|
|
|
case "fuse", "fuseblk":
|
|
|
|
if err := osutil.IsAccessible("/dev/fuse"); err != nil {
|
|
|
|
return false, err.Error()
|
|
|
|
}
|
|
|
|
return onlySandboxNoneOrNamespace(sandbox)
|
|
|
|
default:
|
|
|
|
return onlySandboxNone(sandbox)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSupportedFilesystem(fstype string) (bool, string) {
|
|
|
|
filesystemsOnce.Do(func() {
|
|
|
|
filesystems, _ = ioutil.ReadFile("/proc/filesystems")
|
|
|
|
})
|
|
|
|
if !bytes.Contains(filesystems, []byte("\t"+fstype+"\n")) {
|
|
|
|
return false, fmt.Sprintf("/proc/filesystems does not contain %v", fstype)
|
|
|
|
}
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2017-09-05 11:31:14 +00:00
|
|
|
func extractStringConst(typ prog.Type) (string, bool) {
|
|
|
|
ptr, ok := typ.(*prog.PtrType)
|
2016-10-31 21:15:13 +00:00
|
|
|
if !ok {
|
|
|
|
panic("first open arg is not a pointer to string const")
|
|
|
|
}
|
2017-09-05 11:31:14 +00:00
|
|
|
str, ok := ptr.Type.(*prog.BufferType)
|
2018-06-18 17:45:50 +00:00
|
|
|
if !ok || str.Kind != prog.BufferString || len(str.Values) == 0 {
|
2016-10-31 21:15:13 +00:00
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
v := str.Values[0]
|
2018-06-18 17:45:50 +00:00
|
|
|
for len(v) != 0 && v[len(v)-1] == 0 {
|
|
|
|
v = v[:len(v)-1] // string terminating \x00
|
|
|
|
}
|
2016-10-31 21:15:13 +00:00
|
|
|
return v, true
|
|
|
|
}
|
2017-10-16 10:18:50 +00:00
|
|
|
|
2018-06-12 12:05:02 +00:00
|
|
|
func init() {
|
|
|
|
checkFeature[FeatureCoverage] = checkCoverage
|
|
|
|
checkFeature[FeatureComparisons] = checkComparisons
|
2019-01-16 13:50:43 +00:00
|
|
|
checkFeature[FeatureExtraCoverage] = checkExtraCoverage
|
2018-06-12 12:05:02 +00:00
|
|
|
checkFeature[FeatureSandboxSetuid] = unconditionallyEnabled
|
|
|
|
checkFeature[FeatureSandboxNamespace] = checkSandboxNamespace
|
2018-09-17 09:33:11 +00:00
|
|
|
checkFeature[FeatureSandboxAndroidUntrustedApp] = checkSandboxAndroidUntrustedApp
|
2018-06-12 12:05:02 +00:00
|
|
|
checkFeature[FeatureFaultInjection] = checkFaultInjection
|
|
|
|
setupFeature[FeatureFaultInjection] = setupFaultInjection
|
|
|
|
checkFeature[FeatureLeakChecking] = checkLeakChecking
|
|
|
|
setupFeature[FeatureLeakChecking] = setupLeakChecking
|
|
|
|
callbFeature[FeatureLeakChecking] = callbackLeakChecking
|
|
|
|
checkFeature[FeatureNetworkInjection] = checkNetworkInjection
|
2018-07-13 10:46:32 +00:00
|
|
|
checkFeature[FeatureNetworkDevices] = checkNetworkDevices
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkCoverage() string {
|
2018-06-12 18:10:58 +00:00
|
|
|
if reason := checkDebugFS(); reason != "" {
|
|
|
|
return reason
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
if !osutil.IsExist("/sys/kernel/debug/kcov") {
|
|
|
|
return "CONFIG_KCOV is not enabled"
|
|
|
|
}
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/sys/kernel/debug/kcov"); err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-08-01 11:19:54 +00:00
|
|
|
func checkComparisons() (reason string) {
|
2019-01-16 13:50:43 +00:00
|
|
|
return checkCoverageFeature(FeatureComparisons)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkExtraCoverage() (reason string) {
|
|
|
|
return checkCoverageFeature(FeatureExtraCoverage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCoverageFeature(feature int) (reason string) {
|
2018-08-01 11:19:54 +00:00
|
|
|
if reason = checkDebugFS(); reason != "" {
|
2018-06-12 18:10:58 +00:00
|
|
|
return reason
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
// TODO(dvyukov): this should run under target arch.
|
|
|
|
// E.g. KCOV ioctls were initially not supported on 386 (missing compat_ioctl),
|
|
|
|
// and a 386 executor won't be able to use them, but an amd64 fuzzer will be.
|
|
|
|
fd, err := syscall.Open("/sys/kernel/debug/kcov", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "CONFIG_KCOV is not enabled"
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
// Trigger host target lazy initialization, it will fill linux.KCOV_INIT_TRACE.
|
|
|
|
// It's all wrong and needs to be refactored.
|
|
|
|
if _, err := prog.GetTarget(runtime.GOOS, runtime.GOARCH); err != nil {
|
|
|
|
return fmt.Sprintf("failed to get target: %v", err)
|
|
|
|
}
|
|
|
|
coverSize := uintptr(64 << 10)
|
|
|
|
_, _, errno := syscall.Syscall(
|
|
|
|
syscall.SYS_IOCTL, uintptr(fd), linux.KCOV_INIT_TRACE, coverSize)
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Sprintf("ioctl(KCOV_INIT_TRACE) failed: %v", errno)
|
|
|
|
}
|
|
|
|
mem, err := syscall.Mmap(fd, 0, int(coverSize*unsafe.Sizeof(uintptr(0))),
|
|
|
|
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf("KCOV mmap failed: %v", err)
|
|
|
|
}
|
2018-08-01 11:19:54 +00:00
|
|
|
defer func() {
|
|
|
|
if err := syscall.Munmap(mem); err != nil {
|
|
|
|
reason = fmt.Sprintf("munmap failed: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-01-16 13:50:43 +00:00
|
|
|
switch feature {
|
|
|
|
case FeatureComparisons:
|
|
|
|
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL,
|
|
|
|
uintptr(fd), linux.KCOV_ENABLE, linux.KCOV_TRACE_CMP)
|
|
|
|
if errno != 0 {
|
|
|
|
if errno == 524 { // ENOTSUPP
|
|
|
|
return "CONFIG_KCOV_ENABLE_COMPARISONS is not enabled"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("ioctl(KCOV_TRACE_CMP) failed: %v", errno)
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
2019-01-16 13:50:43 +00:00
|
|
|
case FeatureExtraCoverage:
|
|
|
|
arg := KcovRemoteArg{
|
2019-01-17 13:53:44 +00:00
|
|
|
TraceMode: uint32(linux.KCOV_TRACE_PC),
|
2019-01-16 13:50:43 +00:00
|
|
|
AreaSize: uint32(coverSize * unsafe.Sizeof(uintptr(0))),
|
|
|
|
NumHandles: 0,
|
|
|
|
CommonHandle: 0,
|
|
|
|
}
|
|
|
|
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL,
|
|
|
|
uintptr(fd), linux.KCOV_REMOTE_ENABLE, uintptr(unsafe.Pointer(&arg)))
|
|
|
|
if errno != 0 {
|
|
|
|
if errno == 25 { // ENOTTY
|
|
|
|
return "extra coverage is not supported by the kernel"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("ioctl(KCOV_REMOTE_ENABLE) failed: %v", errno)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unknown feature in checkCoverageFeature")
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
2018-08-01 11:19:54 +00:00
|
|
|
defer func() {
|
|
|
|
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), linux.KCOV_DISABLE, 0)
|
|
|
|
if errno != 0 {
|
|
|
|
reason = fmt.Sprintf("ioctl(KCOV_DISABLE) failed: %v", errno)
|
|
|
|
}
|
|
|
|
}()
|
2018-06-12 12:05:02 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFaultInjection() string {
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/proc/self/make-it-fail"); err != nil {
|
2018-06-12 12:05:02 +00:00
|
|
|
return "CONFIG_FAULT_INJECTION is not enabled"
|
|
|
|
}
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/proc/thread-self/fail-nth"); err != nil {
|
2018-06-12 12:05:02 +00:00
|
|
|
return "kernel does not have systematic fault injection support"
|
|
|
|
}
|
2018-06-12 18:10:58 +00:00
|
|
|
if reason := checkDebugFS(); reason != "" {
|
|
|
|
return reason
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/sys/kernel/debug/failslab/ignore-gfp-wait"); err != nil {
|
2019-02-11 17:35:46 +00:00
|
|
|
return "CONFIG_FAULT_INJECTION_DEBUG_FS or CONFIG_FAILSLAB are not enabled"
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupFaultInjection() error {
|
2019-02-11 17:35:46 +00:00
|
|
|
// Note: these files are also hardcoded in pkg/csource/csource.go.
|
2017-10-16 10:18:50 +00:00
|
|
|
if err := osutil.WriteFile("/sys/kernel/debug/failslab/ignore-gfp-wait", []byte("N")); err != nil {
|
2018-04-27 13:43:29 +00:00
|
|
|
return fmt.Errorf("failed to write /failslab/ignore-gfp-wait: %v", err)
|
2017-10-16 10:18:50 +00:00
|
|
|
}
|
2019-02-11 17:35:46 +00:00
|
|
|
// These are enabled by separate configs (e.g. CONFIG_FAIL_FUTEX)
|
|
|
|
// and we did not check all of them in checkFaultInjection, so we ignore errors.
|
2017-10-16 10:18:50 +00:00
|
|
|
if err := osutil.WriteFile("/sys/kernel/debug/fail_futex/ignore-private", []byte("N")); err != nil {
|
2019-02-11 17:35:46 +00:00
|
|
|
log.Logf(0, "failed to write /sys/kernel/debug/fail_futex/ignore-private: %v", err)
|
2018-04-27 13:43:29 +00:00
|
|
|
}
|
|
|
|
if err := osutil.WriteFile("/sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem", []byte("N")); err != nil {
|
2019-02-11 17:35:46 +00:00
|
|
|
log.Logf(0, "failed to write /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem: %v", err)
|
2018-04-27 13:43:29 +00:00
|
|
|
}
|
|
|
|
if err := osutil.WriteFile("/sys/kernel/debug/fail_page_alloc/ignore-gfp-wait", []byte("N")); err != nil {
|
2019-02-11 17:35:46 +00:00
|
|
|
log.Logf(0, "failed to write /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait: %v", err)
|
2018-04-27 13:43:29 +00:00
|
|
|
}
|
|
|
|
if err := osutil.WriteFile("/sys/kernel/debug/fail_page_alloc/min-order", []byte("0")); err != nil {
|
2019-02-11 17:35:46 +00:00
|
|
|
log.Logf(0, "failed to write /sys/kernel/debug/fail_page_alloc/min-order: %v", err)
|
2017-10-16 10:18:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
|
|
|
|
func checkLeakChecking() string {
|
2018-06-12 18:10:58 +00:00
|
|
|
if reason := checkDebugFS(); reason != "" {
|
|
|
|
return reason
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
2018-10-10 13:38:06 +00:00
|
|
|
fd, err := syscall.Open("/sys/kernel/debug/kmemleak", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
2018-06-12 12:05:02 +00:00
|
|
|
return "CONFIG_DEBUG_KMEMLEAK is not enabled"
|
|
|
|
}
|
2018-10-10 13:38:06 +00:00
|
|
|
defer syscall.Close(fd)
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan=off")); err != nil {
|
|
|
|
if err == syscall.EBUSY {
|
|
|
|
return "KMEMLEAK disabled: increase CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE or unset CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/sys/kernel/debug/kmemleak write failed: %v", err)
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupLeakChecking() error {
|
|
|
|
fd, err := syscall.Open("/sys/kernel/debug/kmemleak", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open /sys/kernel/debug/kmemleak: %v", err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
// Flush boot leaks.
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan")); err != nil {
|
|
|
|
return fmt.Errorf("write(kmemleak, scan) failed: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second) // account for MSECS_MIN_AGE
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan")); err != nil {
|
|
|
|
return fmt.Errorf("write(kmemleak, scan) failed: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := syscall.Write(fd, []byte("clear")); err != nil {
|
|
|
|
return fmt.Errorf("write(kmemleak, clear) failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 12:25:01 +00:00
|
|
|
func callbackLeakChecking(leakFrames [][]byte) {
|
2018-06-12 12:05:02 +00:00
|
|
|
start := time.Now()
|
|
|
|
fd, err := syscall.Open("/sys/kernel/debug/kmemleak", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
// KMEMLEAK has false positives. To mitigate most of them, it checksums
|
|
|
|
// potentially leaked objects, and reports them only on the next scan
|
|
|
|
// iff the checksum does not change. Because of that we do the following
|
|
|
|
// intricate dance:
|
|
|
|
// Scan, sleep, scan again. At this point we can get some leaks.
|
|
|
|
// If there are leaks, we sleep and scan again, this can remove
|
|
|
|
// false leaks. Then, read kmemleak again. If we get leaks now, then
|
|
|
|
// hopefully these are true positives during the previous testing cycle.
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan")); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
// Account for MSECS_MIN_AGE
|
|
|
|
// (1 second less because scanning will take at least a second).
|
|
|
|
for time.Since(start) < 4*time.Second {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan")); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
buf := make([]byte, 128<<10)
|
|
|
|
n, err := syscall.Read(fd, buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if n != 0 {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if _, err := syscall.Write(fd, []byte("scan")); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-09-28 12:25:01 +00:00
|
|
|
if _, err := syscall.Seek(fd, 0, 0); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
n, err := syscall.Read(fd, buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
nleaks := 0
|
2018-09-28 12:25:01 +00:00
|
|
|
nextLeak:
|
2018-06-12 12:05:02 +00:00
|
|
|
for buf = buf[:n]; len(buf) != 0; {
|
|
|
|
end := bytes.Index(buf[1:], []byte("unreferenced object"))
|
|
|
|
if end != -1 {
|
|
|
|
end++
|
|
|
|
} else {
|
|
|
|
end = len(buf)
|
|
|
|
}
|
|
|
|
report := buf[:end]
|
|
|
|
buf = buf[end:]
|
2018-09-28 12:25:01 +00:00
|
|
|
for _, frame := range leakFrames {
|
|
|
|
if bytes.Contains(report, frame) {
|
|
|
|
continue nextLeak
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
// BUG in output should be recognized by manager.
|
|
|
|
fmt.Printf("BUG: memory leak\n%s\n", report)
|
|
|
|
nleaks++
|
|
|
|
}
|
|
|
|
if nleaks != 0 {
|
2018-09-28 12:25:01 +00:00
|
|
|
// If we exit right away, dying executors will dump lots of garbage to console.
|
|
|
|
time.Sleep(time.Hour)
|
2018-06-12 12:05:02 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := syscall.Write(fd, []byte("clear")); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSandboxNamespace() string {
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/proc/self/ns/user"); err != nil {
|
|
|
|
return err.Error()
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:33:11 +00:00
|
|
|
func checkSandboxAndroidUntrustedApp() string {
|
|
|
|
if err := osutil.IsAccessible("/sys/fs/selinux/policy"); err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-06-12 12:05:02 +00:00
|
|
|
func checkNetworkInjection() string {
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/dev/net/tun"); err != nil {
|
|
|
|
return err.Error()
|
2018-06-12 12:05:02 +00:00
|
|
|
}
|
2018-07-13 10:46:32 +00:00
|
|
|
return checkNetworkDevices()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNetworkDevices() string {
|
|
|
|
if _, err := exec.LookPath("ip"); err != nil {
|
|
|
|
return "ip command is not found"
|
|
|
|
}
|
2018-06-12 12:05:02 +00:00
|
|
|
return ""
|
|
|
|
}
|
2018-06-12 18:10:58 +00:00
|
|
|
|
|
|
|
func checkDebugFS() string {
|
2018-07-10 11:38:27 +00:00
|
|
|
if err := osutil.IsAccessible("/sys/kernel/debug"); err != nil {
|
2018-06-12 18:10:58 +00:00
|
|
|
return "debugfs is not enabled or not mounted"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|