diff --git a/bin/omamobile b/bin/omamobile index 97cb3c100..30882afff 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -514,6 +514,11 @@ cmd_initramfs() { "$CLANG_DIR/bin/clang" --target=aarch64-linux-musl --sysroot="$sysroot" -fuse-ld=lld -static -nostdlib -O2 \ -o "$stage/usr/local/bin/reboot-to" "$sysroot/usr/lib/crt1.o" "$sysroot/usr/lib/crti.o" \ "$reboot_src" -L"$sysroot/usr/lib" -lc "$builtins" "$sysroot/usr/lib/crtn.o" + if [[ -n ${DEV_INITRAMFS_MODULES:-} ]]; then + "$CLANG_DIR/bin/clang" --target=aarch64-linux-musl --sysroot="$sysroot" -fuse-ld=lld -static -nostdlib -O2 \ + -o "$stage/usr/local/bin/module-load-once" "$sysroot/usr/lib/crt1.o" "$sysroot/usr/lib/crti.o" \ + "$ROOT/tools/module-load-once.c" -L"$sysroot/usr/lib" -lc "$builtins" "$sysroot/usr/lib/crtn.o" + fi # A/B slot metadata in devinfo (Apache-2.0 port of Google's boot HAL). Not called by init yet. "$CLANG_DIR/bin/clang" --target=aarch64-linux-musl --sysroot="$sysroot" -fuse-ld=lld -static -nostdlib -O2 \ -o "$stage/usr/local/bin/omamobile-bootctl" "$sysroot/usr/lib/crt1.o" "$sysroot/usr/lib/crti.o" \ @@ -535,6 +540,14 @@ cmd_initramfs() { "$sysroot/usr/lib/crt1.o" "$sysroot/usr/lib/crti.o" \ "$reboot_src" "$sysroot/usr/lib/libc.a" "$sysroot/usr/lib/crtn.o" \ || die "reboot-to failed to link" + if [[ -n ${DEV_INITRAMFS_MODULES:-} ]]; then + clang --target=aarch64-linux-musl --sysroot="$sysroot" -nostdinc -isystem "$sysroot/usr/include" \ + -static -nostdlib -O2 -fno-stack-protector -fno-pie -fuse-ld=lld \ + -o "$stage/usr/local/bin/module-load-once" \ + "$sysroot/usr/lib/crt1.o" "$sysroot/usr/lib/crti.o" \ + "$ROOT/tools/module-load-once.c" "$sysroot/usr/lib/libc.a" "$sysroot/usr/lib/crtn.o" \ + || die "module-load-once failed to link" + fi say "No $DEV_PACKAGE bootctl; built freestanding devmem and musl reboot-to" fi mkdir -p "$stage/root/.ssh" diff --git a/initramfs/init b/initramfs/init index 36cd2d946..6e5fe74a2 100755 --- a/initramfs/init +++ b/initramfs/init @@ -167,16 +167,18 @@ board_matches() { return 0 } -# Modules this image carries (the descriptor's INITRAMFS_MODULES), insmodded +# Modules this image carries (the descriptor's INITRAMFS_MODULES), loaded # in the packed order before any other step: on raven the PMIC regulator # driver the simple-framebuffer waits for, and the MAX77759 nvmem that ABL # reads the reboot reason from. They were built for this image's board. load_modules() { - local m + local m error [ -s /lib/modules/omamobile/order ] || return 0 board_matches "initramfs modules" || return 0 + # BusyBox insmod may retry initialization after finit_module fails. + # These modules can already have touched hardware before returning an error. while read -r m; do - insmod "/lib/modules/omamobile/$m.ko" 2>/dev/null || say "insmod $m failed" + error=$(/usr/local/bin/module-load-once "/lib/modules/omamobile/$m.ko" 2>&1) || say "module-load-once $m failed: $error" done < /lib/modules/omamobile/order } load_modules diff --git a/tests/all b/tests/all index abcf72172..b406d501c 100755 --- a/tests/all +++ b/tests/all @@ -40,3 +40,5 @@ fi echo "# tests/phone-ssh" tests/phone-ssh +echo "# tests/module-load-once" +tests/module-load-once diff --git a/tests/initramfs-steps b/tests/initramfs-steps index bec6b7f52..c45c93588 100755 --- a/tests/initramfs-steps +++ b/tests/initramfs-steps @@ -68,10 +68,12 @@ ln -s "$t/i2c/platform/soc@0/10d60000.i2c/i2c-6" "$t/devices/i2c-6" sed -e "s|/etc/omamobile-|$t/etc/omamobile-|g" \ -e "s|/proc/device-tree/compatible|$t/compatible|" \ -e "s|/lib/modules/omamobile|$t/mod|g" \ + -e "s|/usr/local/bin/module-load-once|$t/bin/module-load-once|g" \ -e "s|/sys/bus/i2c/devices|$t/devices|" \ -e 's|( while sleep 2; do usb_switch quiet; done ) &|:|' \ "$t/fn.sh" > "$t/fn-test.sh" printf '#!/bin/sh\necho "insmod $*" >> "%s/hw.log"\n' "$t" > "$t/bin/insmod" +printf '#!/bin/sh\necho "module-load-once $*" >> "%s/hw.log"\n[ "${MODULE_LOAD_STATUS:-0}" = 0 ] || echo "finit_module: simulated failure" >&2\nexit "${MODULE_LOAD_STATUS:-0}"\n' "$t" > "$t/bin/module-load-once" printf '#!/bin/sh\necho "i2cget $*" >> "%s/hw.log"\ncat "%s/reg"\n' "$t" "$t" > "$t/bin/i2cget" printf '#!/bin/sh\necho "i2cset $*" >> "%s/hw.log"\necho 0x09 > "%s/reg"\n' "$t" "$t" > "$t/bin/i2cset" printf '#!/bin/sh\nexit 0\n' > "$t/bin/sleep" @@ -85,7 +87,17 @@ run() { # $1: the step printf 'sec-acpm\nmax77759\n' > "$t/mod/order" printf 'google,gs101-raven\n' > "$t/etc/omamobile-compatible" run load_modules -[[ $(cat "$t/hw.log") == "insmod $t/mod/sec-acpm.ko"$'\n'"insmod $t/mod/max77759.ko" ]] || fail "a matching board loads its modules in order" +[[ $(cat "$t/hw.log") == "module-load-once $t/mod/sec-acpm.ko"$'\n'"module-load-once $t/mod/max77759.ko" ]] || fail "a matching board loads its modules in order" +# A failed helper must not fall back to BusyBox insmod or retry the module. +MODULE_LOAD_STATUS=1 run load_modules +[[ $(cat "$t/hw.log") == "module-load-once $t/mod/sec-acpm.ko"$'\n'"module-load-once $t/mod/max77759.ko" ]] || fail "failed loads are attempted only once, in order" +[[ $(cat "$t/said") == "module-load-once sec-acpm failed: finit_module: simulated failure"$'\n'"module-load-once max77759 failed: finit_module: simulated failure" ]] || fail "each failed load is reported" +# A missing helper is also a failure, never permission to use insmod. +mv "$t/bin/module-load-once" "$t/bin/module-load-once.saved" +run load_modules +[[ ! -e $t/hw.log ]] || fail "missing helper fell back to insmod" +[[ $(wc -l < "$t/said") == 2 ]] || fail "missing helper failures are reported" +mv "$t/bin/module-load-once.saved" "$t/bin/module-load-once" printf 'google,gs201-cheetah\n' > "$t/etc/omamobile-compatible" run load_modules [[ ! -e $t/hw.log ]] || fail "another board's image loaded a module" diff --git a/tests/module-load-once b/tests/module-load-once new file mode 100755 index 000000000..ab8f0178d --- /dev/null +++ b/tests/module-load-once @@ -0,0 +1,92 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Exercise the real loader with the module syscall intercepted. No modules load. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +scratch=$(mktemp -d) +trap 'rm -rf "$scratch"' EXIT +cat >"$scratch/syscall.c" <<'C' +#include +#include +#include +#include +#include +#include +#include +#include + +long __wrap_syscall(long number, ...) +{ + va_list ap; + int fd, flags; + const char *parameters; + char byte; + FILE *log; + + assert(number == SYS_finit_module); + va_start(ap, number); + fd = va_arg(ap, int); + parameters = va_arg(ap, const char *); + flags = va_arg(ap, int); + va_end(ap); + assert(fcntl(fd, F_GETFD) & FD_CLOEXEC); + assert(read(fd, &byte, 1) == 1 && byte == 'x'); + assert(flags == 0); + log = fopen(getenv("CALL_LOG"), "a"); + assert(log); + fprintf(log, "%s\n", parameters); + assert(fclose(log) == 0); + errno = atoi(getenv("LOAD_ERRNO")); + return errno ? -1 : 0; +} + +int __real_close(int fd); +int __wrap_close(int fd) +{ + int result = __real_close(fd); + errno = EBADF; /* Closing must not hide the loading error. */ + return result; +} +C +${CC:-cc} -Wall -Wextra -Werror tools/module-load-once.c "$scratch/syscall.c" \ + -Wl,--wrap=syscall,--wrap=close -o "$scratch/module-load-once" +python3 - "$scratch" <<'PY' +import errno +import os +from pathlib import Path +import subprocess +import sys + +work = Path(sys.argv[1]) +module = work / 'test.ko' +module.write_bytes(b'x: deliberately not a kernel module') +log = work / 'calls' + +def run(args, error=0): + log.unlink(missing_ok=True) + env = dict(os.environ, CALL_LOG=str(log), LOAD_ERRNO=str(error), LC_ALL='C') + result = subprocess.run([str(work / 'module-load-once'), *map(str, args)], + env=env, capture_output=True, text=True, timeout=2) + calls = log.read_text().splitlines() if log.exists() else [] + return result, calls + +for parameters in [[], ['count=1 label="two words"']]: + for error in [0, errno.EIO, errno.ENOSYS, errno.EOPNOTSUPP]: + result, calls = run([module, *parameters], error) + assert result.returncode == bool(error), result + assert calls == [parameters[0] if parameters else ''], calls + if error: + assert os.strerror(error) in result.stderr, result.stderr + +empty = work / 'empty.ko' +empty.touch() +fifo = work / 'fifo.ko' +os.mkfifo(fifo) +for path in [work / 'missing.ko', empty, work, fifo]: + result, calls = run([path]) + assert result.returncode == 1 and calls == [], (path, result, calls) +for args in [[], [module, 'one', 'two']]: + result, calls = run(args) + assert result.returncode == 2 and calls == [], (result, calls) +print('ok - module loader calls once, preserves parameters/errors and rejects invalid input without loading') +PY diff --git a/tools/module-load-once.c b/tools/module-load-once.c new file mode 100644 index 000000000..dd92cc25d --- /dev/null +++ b/tools/module-load-once.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: MIT */ +/* Load an uncompressed module with one finit_module call and no fallback. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + struct stat info; + int fd, result, saved_errno; + + if (argc < 2 || argc > 3) { + fputs("Usage: module-load-once MODULE.ko [PARAMETERS]\n", stderr); + return 2; + } + fd = open(argv[1], O_RDONLY | O_CLOEXEC | O_NONBLOCK); + if (fd < 0) { + perror("open module"); + return 1; + } + if (fstat(fd, &info) || !S_ISREG(info.st_mode) || info.st_size <= 0) { + fputs("Module must be a nonempty regular file\n", stderr); + close(fd); + return 1; + } + result = syscall(SYS_finit_module, fd, argc == 3 ? argv[2] : "", 0); + saved_errno = errno; + close(fd); + if (result) { + errno = saved_errno; + perror("finit_module (not retried)"); + return 1; + } + return 0; +}