From 4cf3f9b3f15d248d052cd369d23308c953d657d9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 7 May 2024 16:37:30 +0200 Subject: executor: consistently fail on feature setup Currently we fail in some cases, but ignore errors in other cases. Consistently fail when feature setup fails. This will be required for relying on setup failure to detect feature presence. --- executor/common_linux.h | 83 ++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) (limited to 'executor/common_linux.h') diff --git a/executor/common_linux.h b/executor/common_linux.h index 93ecef654..6b5fcad71 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -1106,25 +1106,19 @@ static void initialize_wifi_devices(void) return; #endif int rfkill = open("/dev/rfkill", O_RDWR); - if (rfkill == -1) { - if (errno != ENOENT && errno != EACCES) - fail("open(/dev/rfkill) failed"); - } else { - struct rfkill_event event = {0}; - event.type = RFKILL_TYPE_ALL; - event.op = RFKILL_OP_CHANGE_ALL; - if (write(rfkill, &event, sizeof(event)) != (ssize_t)(sizeof(event))) - fail("write(/dev/rfkill) failed"); - close(rfkill); - } + if (rfkill == -1) + fail("open(/dev/rfkill) failed"); + struct rfkill_event event = {0}; + event.type = RFKILL_TYPE_ALL; + event.op = RFKILL_OP_CHANGE_ALL; + if (write(rfkill, &event, sizeof(event)) != (ssize_t)(sizeof(event))) + fail("write(/dev/rfkill) failed"); + close(rfkill); uint8 mac_addr[6] = WIFI_MAC_BASE; int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); - if (sock < 0) { - debug("initialize_wifi_devices: failed to create socket (%d)\n", errno); - return; - } - + if (sock < 0) + fail("initialize_wifi_devices: failed to create socket"); int hwsim_family_id = netlink_query_family_id(&nlmsg, sock, "MAC80211_HWSIM", true); int nl80211_family_id = netlink_query_family_id(&nlmsg, sock, "nl80211", true); uint8 ssid[] = WIFI_IBSS_SSID; @@ -1467,20 +1461,16 @@ static void netlink_nicvf_setup(void) sprintf(cmdline, "nsenter -t 1 -n ip link set %s netns %d", vf_intf.pass_thru_intf, getpid()); if (runcmdline(cmdline)) - return; - + failmsg("failed to run command", "%s", cmdline); sprintf(cmdline, "ip a s %s", vf_intf.pass_thru_intf); if (runcmdline(cmdline)) - return; - + failmsg("failed to run command", "%s", cmdline); sprintf(cmdline, "ip link set %s down", vf_intf.pass_thru_intf); if (runcmdline(cmdline)) - return; - + failmsg("failed to run command", "%s", cmdline); sprintf(cmdline, "ip link set %s name nicvf0", vf_intf.pass_thru_intf); if (runcmdline(cmdline)) - return; - + failmsg("failed to run command", "%s", cmdline); debug("nicvf0 VF pass-through setup complete.\n"); } #endif // SYZ_NIC_VF @@ -4849,6 +4839,16 @@ static void close_fds() static void setup_fault() { + int fd = open("/proc/self/make-it-fail", O_WRONLY); + if (fd == -1) + fail("CONFIG_FAULT_INJECTION is not enabled"); + close(fd); + + fd = open("/proc/thread-self/fail-nth", O_WRONLY); + if (fd == -1) + fail("kernel does not have systematic fault injection support"); + close(fd); + static struct { const char* file; const char* val; @@ -4884,6 +4884,12 @@ static void setup_fault() static void setup_leak() { + if (!write_file(KMEMLEAK_FILE, "scan=off")) { + if (errno == EBUSY) + fail("KMEMLEAK disabled: increase CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE" + " or unset CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF"); + fail("failed to write(kmemleak, \"scan=off\")"); + } // Flush boot leaks. if (!write_file(KMEMLEAK_FILE, "scan")) fail("failed to write(kmemleak, \"scan\")"); @@ -4983,8 +4989,9 @@ static void setup_binfmt_misc() if (mount(0, "/proc/sys/fs/binfmt_misc", "binfmt_misc", 0, 0)) { debug("mount(binfmt_misc) failed: %d\n", errno); } - write_file("/proc/sys/fs/binfmt_misc/register", ":syz0:M:0:\x01::./file0:"); - write_file("/proc/sys/fs/binfmt_misc/register", ":syz1:M:1:\x02::./file0:POC"); + if (!write_file("/proc/sys/fs/binfmt_misc/register", ":syz0:M:0:\x01::./file0:") || + !write_file("/proc/sys/fs/binfmt_misc/register", ":syz1:M:1:\x02::./file0:POC")) + fail("write(/proc/sys/fs/binfmt_misc/register) failed"); } #endif @@ -4994,7 +5001,7 @@ static void setup_binfmt_misc() static void setup_kcsan() { if (!write_file(KCSAN_DEBUGFS_FILE, "on")) - fail("failed to enable KCSAN"); + fail("write(/sys/kernel/debug/kcsan, on) failed"); } #if SYZ_EXECUTOR // currently only used by executor @@ -5122,18 +5129,16 @@ static void setup_802154() netlink_attr(&nlmsg, NL802154_ATTR_IFINDEX, &ifindex, sizeof(ifindex)); netlink_attr(&nlmsg, NL802154_ATTR_SHORT_ADDR, &shortaddr, sizeof(shortaddr)); int err = netlink_send(&nlmsg, sock_generic); - if (err < 0) { - debug("NL802154_CMD_SET_SHORT_ADDR failed: %s\n", strerror(errno)); - } + if (err < 0) + fail("NL802154_CMD_SET_SHORT_ADDR failed"); netlink_device_change(&nlmsg, sock_route, devname, true, 0, &hwaddr, sizeof(hwaddr), 0); if (i == 0) { netlink_add_device_impl(&nlmsg, "lowpan", "lowpan0", false); netlink_done(&nlmsg); netlink_attr(&nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex)); int err = netlink_send(&nlmsg, sock_route); - if (err < 0) { - debug("netlink: adding device lowpan0 type lowpan link wpan0: %s\n", strerror(errno)); - } + if (err < 0) + fail("netlink: adding device lowpan0 type lowpan link wpan0"); } } close(sock_route); @@ -5695,24 +5700,18 @@ static void setup_swap() unlink(SWAP_FILE); // Zero-fill the file. int fd = open(SWAP_FILE, O_CREAT | O_WRONLY | O_CLOEXEC, 0600); - if (fd == -1) { + if (fd == -1) failmsg("swap file open failed", "file: %s", SWAP_FILE); - return; - } // We cannot do ftruncate -- swapon complains about this. Do fallocate instead. fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); close(fd); // Set up the swap file. char cmdline[64]; sprintf(cmdline, "mkswap %s", SWAP_FILE); - if (runcmdline(cmdline)) { + if (runcmdline(cmdline)) fail("mkswap failed"); - return; - } - if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) { + if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) failmsg("swapon failed", "file: %s", SWAP_FILE); - return; - } } #endif -- cgit mrf-deployment