etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

init (3573B)

      1 #!/bin/sh
      2 #
      3 # /init — minimal initramfs init for the usbipd fuzzing VM.
      4 #
      5 # Goals:
      6 #   1. Mount virtual filesystems
      7 #   2. Configure loopback + eth0 networking (QEMU user networking)
      8 #   3. Load USB/IP kernel modules
      9 #   4. Run usbipd in a tight watchdog loop (restart immediately on crash)
     10 #
     11 # Boot time target: <2 seconds from kernel entry to usbipd accepting connections.
     12 # This script MUST NOT call any program that isn't in the initramfs.
     13 
     14 set -e
     15 
     16 # ── virtual filesystems ────────────────────────────────────────────────────
     17 mount -t proc     proc    /proc
     18 mount -t sysfs    sysfs   /sys
     19 mount -t devtmpfs devtmpfs /dev
     20 mkdir -p /dev/pts
     21 mount -t devpts   devpts  /dev/pts
     22 mount -t tmpfs    tmpfs   /tmp
     23 
     24 # debugfs: required for KCOV (/sys/kernel/debug/kcov)
     25 mount -t debugfs  debugfs /sys/kernel/debug 2>/dev/null || true
     26 
     27 # suppress most kernel messages on the console
     28 echo 1 > /proc/sys/kernel/printk
     29 
     30 # KASAN: make KASAN reports immediately panic (and reboot) rather than
     31 # continuing with corrupted state. AFL++ detects the reboot as ECONNRESET.
     32 echo 1 > /proc/sys/kernel/panic_on_oops
     33 echo 1 > /proc/sys/kernel/panic
     34 
     35 # ── networking ─────────────────────────────────────────────────────────────
     36 # QEMU user-mode networking gives us 10.0.2.15/24, gateway 10.0.2.2
     37 ip link set lo   up
     38 ip link set eth0 up
     39 ip addr add 10.0.2.15/24 dev eth0
     40 ip route add default via 10.0.2.2 dev eth0
     41 
     42 # ── USB/IP kernel modules ──────────────────────────────────────────────────
     43 modprobe usbip-core 2>/dev/null  || true
     44 modprobe usbip-host 2>/dev/null  || true
     45 modprobe vhci-hcd  2>/dev/null   || true
     46 
     47 # ── best-effort auto export for stub-side fuzzing ──────────────────────────
     48 # fuzz_stub_client expects OP_REQ_IMPORT busid=1-1 by default. In minimal VMs
     49 # there may be no exportable USB devices; when one exists, bind it automatically.
     50 USBIP_BIN=""
     51 if [ -x /usr/sbin/usbip ]; then
     52     USBIP_BIN=/usr/sbin/usbip
     53 elif [ -x /usr/bin/usbip ]; then
     54     USBIP_BIN=/usr/bin/usbip
     55 fi
     56 
     57 if [ -n "$USBIP_BIN" ]; then
     58     BUSID="$($USBIP_BIN list -l 2>/dev/null | sed -n 's/^- busid \\([^ ]*\\) .*/\\1/p' | head -n 1)"
     59     if [ -n "$BUSID" ]; then
     60         $USBIP_BIN bind -b "$BUSID" >/dev/null 2>&1 || true
     61         echo "usbip-fuzz: auto-bound export busid=$BUSID"
     62     else
     63         echo "usbip-fuzz: no exportable USB device found (stub fuzzing may idle)"
     64     fi
     65 else
     66     echo "usbip-fuzz: usbip tool missing in initramfs (stub fuzzing may idle)"
     67 fi
     68 
     69 # ── usbipd watchdog loop ───────────────────────────────────────────────────
     70 # usbipd runs in foreground mode (no -D flag).  When it crashes or exits the
     71 # loop restarts it immediately — this keeps the fuzzer's "connect and send"
     72 # flow always finding a live daemon.
     73 #
     74 # Crash recovery time = time for this loop to spin + usbipd startup time.
     75 # On a minimal initramfs that's ~1-5 ms, keeping throughput high.
     76 
     77 echo "usbip-fuzz: starting usbipd watchdog"
     78 
     79 while true; do
     80     /usr/sbin/usbipd 2>/dev/null
     81     # usbipd exited (crash / clean exit) — restart without any delay
     82 done
     83 
     84 # unreachable — kernel will panic if this exits