From 584d33ebfef93b91cc734d38c692802ec1e22ea0 Mon Sep 17 00:00:00 2001 From: Marsel Nenaj <201385099+marselnenaj@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:37:00 +0100 Subject: [PATCH 1/3] omamobile-modem: allow a device-provided ordered link stack Keep the gs201 default while allowing a device package to select its PHY and host modules. Refuse missing or mismatched parameters on already loaded modules before the inhibitor, persistent latch or any new load. Integrate synthetic parameter/order tests into the package suite and advance the package version. Co-Authored-By: OpenAI Codex --- .../omamobile-modem/lib/omamobile-modem-start | 40 ++++++++++-- packages/omamobile-modem/test/all | 7 +- packages/omamobile-modem/test/test_link.py | 65 +++++++++++++++++++ packages/omamobile-modem/version | 2 +- 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 packages/omamobile-modem/test/test_link.py diff --git a/packages/omamobile-modem/lib/omamobile-modem-start b/packages/omamobile-modem/lib/omamobile-modem-start index aea70393d..c2c534730 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-start +++ b/packages/omamobile-modem/lib/omamobile-modem-start @@ -151,14 +151,37 @@ if [[ ${OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS:-} == 1 ]]; then exit 0 fi -# The link driver must allow hardware access, or cbd's power-on is refused -# (-EPERM). allow_hw is fixed at load time: a copy loaded without it (by -# hand, M3.3-B) needs a reboot. -if [[ -d /sys/module/pcie_gs201_cp ]]; then - [[ $(cat /sys/module/pcie_gs201_cp/parameters/allow_hw) == Y ]] || - { say "pcie_gs201_cp is loaded without allow_hw=1: reboot first"; refuse "Off: reboot needed"; } +# The link's modules and their parameters, in load order, one "module +# key=value..." entry each: the board's (its device package installs +# $LIB/modem/link.conf, a bash fragment setting LINK_MODULES), or gs201's +# one driver. A device package may select a separate PHY and PCIe host. +LINK_MODULES=("pcie-gs201-cp allow_hw=1") +if [[ -r $LIB/modem/link.conf ]]; then + # shellcheck source=/dev/null + . "$LIB/modem/link.conf" fi +# The link driver must allow hardware access, or cbd's power-on is refused +# (-EPERM), and parameters are fixed at load time: a copy already loaded +# with other values (by hand, M3.3-B, or an identity-gate test this boot) +# needs a reboot. Booleans read back as Y/N. +link_param_ok() { + local mod=$1 kv=$2 key val have + key=${kv%%=*} val=${kv#*=} + [[ -r /sys/module/$mod/parameters/$key ]] || return 1 + have=$(cat "/sys/module/$mod/parameters/$key") + [[ $have == "$val" ]] || { [[ $val == 1 && $have == Y ]]; } || { [[ $val == 0 && $have == N ]]; } +} +for entry in "${LINK_MODULES[@]}"; do + read -r -a words <<< "$entry" + mod=${words[0]//-/_} + [[ -d /sys/module/$mod ]] || continue + for kv in "${words[@]:1}"; do + link_param_ok "$mod" "$kv" || + { say "$mod is loaded without $kv: reboot first"; refuse "Off: reboot needed"; } + done +done + # The sleep inhibitor before the drivers: the unit requires and orders after # omamobile-modem-inhibit.service, but a Type=simple unit is "started" at # its fork, so give the lock a moment to appear, and refuse without it. @@ -178,7 +201,10 @@ sync # CPIF and the regions are blacklisted by the device's modem policy (the # package providing omamobile-modem-policy): only this loads them. Never # unload cpif (it is permanent by design). -modprobe pcie-gs201-cp allow_hw=1 +for entry in "${LINK_MODULES[@]}"; do + read -r -a words <<< "$entry" + modprobe "${words[@]}" +done modprobe shm_ipc modprobe cpif # The link to the modem with ASPM off, as every link test so far ran diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 70f5ae26d..ac8ed7361 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -16,6 +16,7 @@ if command -v shellcheck >/dev/null; then shellcheck -S warning lib/omamobile-modem-start install || fail "shellcheck" fi pass "scripts parse" +python3 test/test_link.py || fail "device link module parameters and load order" # spawn_optional keeps its job alive whatever the daemon's exit: run the # function from modem.conf under the library's errexit and pipefail with a @@ -165,7 +166,7 @@ s=lib/omamobile-modem-start lineof() { grep -n "$1" $s | head -1 | cut -d: -f1; } l_prov_refuse=$(lineof "isn't provisioned"); l_mark_refuse=$(lineof 'never reached ONLINE; skipping'); l_up_refuse=$(lineof 'refuse_unclean$') l_inhibit=$(lineof 'no sleep inhibitor held'); l_latch=$(lineof '> "\$MARK"'); l_latch_sync=$(awk -v n="$l_latch" 'NR == n + 1' $s) -l_modprobe=$(lineof '^modprobe '); l_check=$(lineof 'omamobile-vendor-chroot" modem check'); l_rm=$(lineof '^ rm -f "\$MARK"') +l_modprobe=$(lineof '^[[:space:]]*modprobe '); l_check=$(lineof 'omamobile-vendor-chroot" modem check'); l_rm=$(lineof '^ rm -f "\$MARK"') l_ready=$(lineof 'systemd-notify --ready'); l_exec=$(lineof 'exec "\$LIB/omamobile-vendor-chroot" modem start') l_reason_rm=$(lineof '^rm -f "\$REFUSED"') for v in l_prov_refuse l_mark_refuse l_up_refuse l_inhibit l_reason_rm l_latch l_modprobe l_check l_rm l_ready l_exec; do @@ -175,7 +176,7 @@ done [[ $l_prov_refuse -lt $l_mark_refuse && $l_mark_refuse -lt $l_up_refuse && $l_up_refuse -lt $l_inhibit && $l_inhibit -lt $l_reason_rm && $l_reason_rm -lt $l_latch && $l_latch -lt $l_modprobe && $l_modprobe -lt $l_check && $l_check -lt $l_rm && $l_rm -lt $l_ready && $l_ready -lt $l_exec ]] || fail "start script order: refuse($l_prov_refuse,$l_mark_refuse,$l_up_refuse) inhibit($l_inhibit) reason-rm($l_reason_rm) latch($l_latch) modprobe($l_modprobe) check($l_check) rm($l_rm) ready($l_ready) exec($l_exec)" -[[ $(grep -c '^modprobe ' $s) -eq 3 && $(grep -c 'modprobe' <(sed -n "1,${l_latch}p" $s) ) -eq 0 ]] || +[[ $(grep -c '^[[:space:]]*modprobe ' $s) -eq 3 && $(grep -c 'modprobe' <(sed -n "1,${l_latch}p" $s) ) -eq 0 ]] || fail "no modprobe before the latch" grep -q 'OMAMOBILE_MODEM_MARK=\$MARK OMAMOBILE_MODEM_UP_MARK=\${INVOCATION_ID:+\$UP} exec' $s || fail "both markers go to the profile's watcher (the modem-up one only under the unit)" grep -q "OMAMOBILE_MODEM_UP_MARK" $cp || fail "the watcher writes the modem-up marker" @@ -201,7 +202,7 @@ past() { OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS=1 start "$@"; } stop_line='past the marker checks, stopping here (test only)' ! grep -rq 'OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS' systemd install || fail "the test stop point is set by no unit or install step" l_stop=$(lineof 'OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS:-') -[[ $l_up_refuse -lt $l_stop && $l_stop -lt $(lineof '/sys/module/pcie_gs201_cp') && $l_stop -lt $l_inhibit && $l_stop -lt $l_latch ]] || +[[ $l_up_refuse -lt $l_stop && $l_stop -lt $(lineof '^link_param_ok()') && $l_stop -lt $l_inhibit && $l_stop -lt $l_latch ]] || fail "the test stop point sits after the marker decision and before the sysfs read, the inhibitor and the latch (stop=$l_stop)" # Not provisioned (a fresh install before tools/phone-provision): one line # per missing file, no latch, the reason says so. The unit itself is skipped diff --git a/packages/omamobile-modem/test/test_link.py b/packages/omamobile-modem/test/test_link.py new file mode 100644 index 000000000..7c26739a4 --- /dev/null +++ b/packages/omamobile-modem/test/test_link.py @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: MIT +"""Run the starter's link checks with synthetic sysfs and inert module loads.""" +from pathlib import Path +import re +import subprocess +import tempfile +import unittest + +SOURCE = (Path(__file__).resolve().parents[1] / 'lib/omamobile-modem-start').read_text() + + +class LinkTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.function = re.search(r'^link_param_ok\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + self.function = self.function.replace('/sys/module/', str(self.root) + '/') + + def run_value(self, value, wanted): + parameter = self.root / 'test/parameters/flag' + parameter.parent.mkdir(parents=True, exist_ok=True) + if value is not None: + parameter.write_text(value + '\n') + return subprocess.run(['bash', '-c', 'set -euo pipefail\n' + self.function + + '\nlink_param_ok test flag=' + wanted], capture_output=True).returncode + + def test_missing_parameter_refuses(self): + self.assertNotEqual(self.run_value(None, '1'), 0) + + def test_boolean_and_literal_values(self): + for value, wanted in [('Y', '1'), ('N', '0'), ('1', '1'), ('0', '0'), ('42', '42')]: + self.assertEqual(self.run_value(value, wanted), 0) + + def test_mismatched_values_refuse(self): + for value, wanted in [('N', '1'), ('Y', '0'), ('unknown', '1')]: + self.assertNotEqual(self.run_value(value, wanted), 0) + + def test_device_modules_load_in_order_with_arguments(self): + loop = re.search(r'for entry in "\$\{LINK_MODULES\[@\]\}"; do\n read -r -a words <<< "\$entry"\n modprobe "\$\{words\[@\]\}"\ndone', SOURCE).group() + script = ('LINK_MODULES=("first allow_hw=1" "second cpif=1")\n' + 'modprobe() { printf "%s " "$@"; printf "\\n"; }\n' + loop) + result = subprocess.run(['bash', '-c', script], text=True, capture_output=True, check=True) + self.assertEqual(result.stdout, 'first allow_hw=1 \nsecond cpif=1 \n') + + def test_default_keeps_existing_gs201_parameters(self): + self.assertIn('LINK_MODULES=("pcie-gs201-cp allow_hw=1")', SOURCE) + + def test_loaded_module_mismatch_refuses_before_loading(self): + block = SOURCE[SOURCE.index('link_param_ok() {'):SOURCE.index('# The sleep inhibitor before the drivers:')] + block = block.replace('/sys/module/', str(self.root) + '/') + parameter = self.root / 'second/parameters/cpif' + parameter.parent.mkdir(parents=True) + for value in [None, 'N', 'Y']: + if value is not None: + parameter.write_text(value + '\n') + script = ('set -euo pipefail\nLINK_MODULES=("first allow_hw=1" "second cpif=1")\n' + 'say() { :; }; refuse() { exit 7; }\n' + block + '\necho load-reached\n') + result = subprocess.run(['bash', '-c', script], text=True, capture_output=True) + self.assertEqual(result.returncode, 0 if value == 'Y' else 7) + self.assertEqual('load-reached' in result.stdout, value == 'Y') + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index e8e277f2f..04c5555c6 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.16 +0.1.17 From e6f0aa35a5e61c3f560c84f26b7651a4bdbb16c8 Mon Sep 17 00:00:00 2001 From: Marsel Nenaj <201385099+marselnenaj@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:58:00 +0100 Subject: [PATCH 2/3] omamobile-modem: parse device link configuration as data Do not source a device fragment as root before the bring-up latch: it could replace the marker variables or refusal function. Accept only a strict ordered module/key=value list, rejecting shell syntax before parameter checks, the inhibitor, the persistent latch or any module load. Use /usr/lib/omamobile-modem/link.conf outside the vendor provisioning tree. Stage its package-owned directory and document the device-package contract. Name the Pixel 9 Pro XL's PHY/host ordering requirement in the starter comment. Keep the gs201 default and leave version allocation to the maintainer. The complete package suite passes, including 13 actual source/guard tests. A source-evaluation negative control creates the inert marker and fails the same command-substitution assertion that the data parser passes. Co-Authored-By: Codex --- packages/omamobile-modem/README.md | 33 +++++ packages/omamobile-modem/install | 4 + .../omamobile-modem/lib/omamobile-modem-start | 30 +++-- packages/omamobile-modem/test/all | 4 +- packages/omamobile-modem/test/test_link.py | 119 ++++++++++++++++++ 5 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 packages/omamobile-modem/README.md diff --git a/packages/omamobile-modem/README.md b/packages/omamobile-modem/README.md new file mode 100644 index 000000000..f07064c30 --- /dev/null +++ b/packages/omamobile-modem/README.md @@ -0,0 +1,33 @@ + +# Modem link modules + +The default link remains `modprobe pcie-gs201-cp allow_hw=1` when no device +configuration exists. The Pixel 9 Pro XL needs a separate PHY before its PCIe +host; its device package may supply the ordered list as +`/usr/lib/omamobile-modem/link.conf`. This directory is staged by this package +and is separate from extracted vendor files. This package does not install +link.conf or enable another device's drivers. The device package owns its file. + +The file is data, never a shell fragment. Each nonempty, noncomment line is: + +``` +module key=value key=value +``` + +A module starts with an ASCII letter and contains only letters, digits, +underscores and hyphens. Parameter names start with a letter or underscore; +remaining characters are letters, digits or underscores. Values are nonempty +and contain only letters, digits, underscore, period, comma, colon, plus, +slash or hyphen. Arguments are separated by spaces or tabs; a module without +parameters is valid. Blank lines and whole-line comments are allowed. Shell +quotes, substitutions, assignments, functions, options and inline comments +are refused, as are empty configurations, symlinks and unreadable/nonregular +files. A missing file preserves the default; a present invalid file refuses. + +All entries are validated before checking already-loaded parameters, checking +the sleep inhibitor, creating the bring-up latch or loading modules. Missing +or mismatched parameters on loaded modules still require a reboot; the +configuration cannot reassign MARK/HOLD or redefine refuse. The normal marker, +unclean-stop and hold decisions remain unchanged. Device-specific configuration +does not authorize starting a modem or replace its watchdog/backup/recovery +qualification. The maintainer allocates the package version at merge. diff --git a/packages/omamobile-modem/install b/packages/omamobile-modem/install index 91a1006d4..ebf06f7e1 100755 --- a/packages/omamobile-modem/install +++ b/packages/omamobile-modem/install @@ -12,6 +12,9 @@ set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")" d=${1:?Usage: ./install DESTDIR} [[ $d == /* && $d != "/" ]] || { echo "Use an absolute staging directory" >&2; exit 1; } +# Device packages may install a data-only link.conf here. Vendor extraction +# never populates this directory; no config is needed for the gs201 default. +install -dm755 "$d/usr/lib/omamobile-modem" v=$d/usr/lib/omamobile-vendor install -Dm755 lib/omamobile-modem-start "$v/omamobile-modem-start" install -Dm755 lib/omamobile-modem-cp "$v/omamobile-modem-cp" @@ -25,5 +28,6 @@ for u in omamobile-modem.service omamobile-modem-inhibit.service omamobile-modem done install -Dm644 udev/60-omamobile-modem-nv-ro.rules "$d/usr/lib/udev/rules.d/60-omamobile-modem-nv-ro.rules" install -Dm644 udev/61-omamobile-vendor-udisks-ignore.rules "$d/usr/lib/udev/rules.d/61-omamobile-vendor-udisks-ignore.rules" +install -Dm644 README.md "$d/usr/share/doc/omamobile-modem/README.md" install -Dm644 LICENSE "$d/usr/share/licenses/omamobile-modem/LICENSE" install -Dm644 version "$d/usr/share/omamobile/modem/version" diff --git a/packages/omamobile-modem/lib/omamobile-modem-start b/packages/omamobile-modem/lib/omamobile-modem-start index c2c534730..fbf956056 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-start +++ b/packages/omamobile-modem/lib/omamobile-modem-start @@ -151,19 +151,31 @@ if [[ ${OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS:-} == 1 ]]; then exit 0 fi -# The link's modules and their parameters, in load order, one "module -# key=value..." entry each: the board's (its device package installs -# $LIB/modem/link.conf, a bash fragment setting LINK_MODULES), or gs201's -# one driver. A device package may select a separate PHY and PCIe host. +# The Pixel 9 Pro XL needs its PHY loaded before its PCIe host. Device +# packages can provide this ordered, data-only list outside vendor extraction. +# Without a file, retain the Pixel 7 Pro's existing gs201 invocation. +LINK_CONFIG=/usr/lib/omamobile-modem/link.conf LINK_MODULES=("pcie-gs201-cp allow_hw=1") -if [[ -r $LIB/modem/link.conf ]]; then - # shellcheck source=/dev/null - . "$LIB/modem/link.conf" -fi +read_link_config() { + local path=$1 entry LC_ALL=C + [[ -e $path || -L $path ]] || return 0 + [[ -f $path && -r $path && ! -L $path ]] || refuse "Off: invalid link configuration" + LINK_MODULES=() + while IFS= read -r entry || [[ -n $entry ]]; do + [[ $entry =~ ^[[:blank:]]*$ || $entry =~ ^[[:blank:]]*# ]] && continue + # A module followed by zero or more key=value arguments. No shell syntax, + # quotes, substitutions, options or inline comments are accepted. + [[ $entry =~ ^[a-zA-Z][a-zA-Z0-9_-]*([[:blank:]]+[a-zA-Z_][a-zA-Z0-9_]*=[a-zA-Z0-9_.,:+/-]+)*[[:blank:]]*$ ]] || + refuse "Off: invalid link configuration" + LINK_MODULES+=("$entry") + done < "$path" + ((${#LINK_MODULES[@]})) || refuse "Off: empty link configuration" +} +read_link_config "$LINK_CONFIG" # The link driver must allow hardware access, or cbd's power-on is refused # (-EPERM), and parameters are fixed at load time: a copy already loaded -# with other values (by hand, M3.3-B, or an identity-gate test this boot) +# with other values (by hand, M3.3-B, or a hardware-disabled test this boot) # needs a reboot. Booleans read back as Y/N. link_param_ok() { local mod=$1 kv=$2 key val have diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index ac8ed7361..141aef911 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -85,7 +85,9 @@ pass "NV checksums" stage=$t/stage ./install "$stage" -for f in usr/lib/omamobile-vendor/omamobile-modem-start usr/lib/omamobile-vendor/omamobile-modem-cp \ +[[ -d $stage/usr/lib/omamobile-modem ]] || fail "package-owned link configuration directory" +[[ ! -e $stage/usr/lib/omamobile-modem/link.conf ]] || fail "default needs no device link configuration" +for f in usr/share/doc/omamobile-modem/README.md usr/lib/omamobile-vendor/omamobile-modem-start usr/lib/omamobile-vendor/omamobile-modem-cp \ usr/lib/omamobile-vendor/profiles/modem.conf usr/lib/omamobile-vendor/modem/props.conf \ usr/lib/systemd/system/omamobile-modem.service usr/lib/udev/rules.d/60-omamobile-modem-nv-ro.rules \ usr/lib/omamobile-vendor/omamobile-modem-nv-overlay usr/bin/omamobile-modem \ diff --git a/packages/omamobile-modem/test/test_link.py b/packages/omamobile-modem/test/test_link.py index 7c26739a4..d1ba2b8f2 100644 --- a/packages/omamobile-modem/test/test_link.py +++ b/packages/omamobile-modem/test/test_link.py @@ -2,6 +2,7 @@ """Run the starter's link checks with synthetic sysfs and inert module loads.""" from pathlib import Path import re +import os import subprocess import tempfile import unittest @@ -60,6 +61,124 @@ def test_loaded_module_mismatch_refuses_before_loading(self): self.assertEqual(result.returncode, 0 if value == 'Y' else 7) self.assertEqual('load-reached' in result.stdout, value == 'Y') + def run_starter(self, config, loaded=None): + """Run actual starter with only paths redirected and inert external tools.""" + config_path = self.root / 'link.conf' + if config is not None: + config_path.write_text(config) + vendor = self.root / 'vendor' + vendor.mkdir() + profile = vendor / 'omamobile-vendor-chroot' + profile.write_text('#!/bin/bash\necho profile-$2 >> "$TRACE"\nexit 1\n') + profile.chmod(0o700) + sysfs = self.root / 'sys' + sysfs.mkdir() + if loaded is not None: + parameter = sysfs / 'second/parameters/cpif' + parameter.parent.mkdir(parents=True) + if loaded != 'missing': + parameter.write_text(loaded + '\n') + tools = self.root / 'bin' + tools.mkdir() + for name, body in { + 'systemd-inhibit': 'echo inhibitor >> "$TRACE"; echo "omamobile-modem held"', + 'sync': ':', + 'modprobe': 'test -f "$OMAMOBILE_MODEM_LATCH" || exit 23; echo "load:$*" >> "$TRACE"', + }.items(): + tool = tools / name + tool.write_text('#!/bin/bash\n' + body + '\n') + tool.chmod(0o700) + provision = self.root / 'provisioned' + provision.touch() + starter = self.root / 'starter' + starter.write_text(SOURCE.replace('LIB=/usr/lib/omamobile-vendor', + 'LIB=' + str(vendor)).replace( + 'LINK_CONFIG=/usr/lib/omamobile-modem/link.conf', + 'LINK_CONFIG=' + str(config_path)).replace( + '/sys/module/', str(sysfs) + '/')) + trace = self.root / 'trace' + env = os.environ.copy() + env.update(PATH=str(tools) + ':' + env['PATH'], TRACE=str(trace), + OMAMOBILE_MODEM_LATCH=str(self.root / 'latch'), + OMAMOBILE_MODEM_UP_MARK=str(self.root / 'up'), + OMAMOBILE_MODEM_HOLD=str(self.root / 'hold'), + OMAMOBILE_MODEM_REFUSED=str(self.root / 'refused'), + OMAMOBILE_MODEM_CHROOT=str(provision), + OMAMOBILE_MODEM_NV_SUMS=str(provision), + OMAMOBILE_MODEM_PROPS=str(provision)) + env.pop('OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS', None) + result = subprocess.run(['bash', str(starter)], env=env, + capture_output=True, text=True, timeout=5) + return result, trace.read_text().splitlines() if trace.exists() else [] + + def test_data_list_reaches_inhibitor_then_ordered_loads_with_latch(self): + result, trace = self.run_starter('# device list\nfirst allow_hw=1\nsecond cpif=1', 'Y') + self.assertEqual(result.returncode, 1) # inert profile check deliberately refuses + self.assertEqual(trace, ['inhibitor', 'inhibitor', 'load:first allow_hw=1', + 'load:second cpif=1', 'load:shm_ipc', 'load:cpif', 'profile-check']) + + def test_no_config_preserves_real_default_load(self): + result, trace = self.run_starter(None) + self.assertEqual(result.returncode, 1) + self.assertEqual(trace[2], 'load:pcie-gs201-cp allow_hw=1') + + def test_data_substitution_refuses_without_executing_marker(self): + marker = self.root / 'injected' + result, trace = self.run_starter('first allow_hw=$(touch ' + str(marker) + ')\n') + self.assertFalse(marker.exists()) + self.assertEqual(result.returncode, 1) + self.assertEqual(trace, []) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), + 'Off: invalid link configuration') + + def test_invalid_data_cannot_override_guards(self): + # Execute the actual parser once per hostile data sample, never source it. + function = re.search(r'^read_link_config\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + for text in ['MARK=/wrong', 'HOLD=/wrong', 'refuse() { :; }', + 'first a=`true`', 'first a="1"', '-r first', + 'first a=1; true', 'first a=', 'first ../a=1', + 'first a=1 # inline', '', '# comment only\n']: + path = self.root / 'invalid.conf' + path.write_text(text) + result = subprocess.run(['bash', '-c', + 'set -euo pipefail\nrefuse() { exit 7; }\n' + function + + '\nread_link_config "$1"\necho accepted', 'test', str(path)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 7, text) + self.assertNotIn('accepted', result.stdout) + + def test_full_starter_loaded_parameter_refuses_before_inhibitor_or_latch(self): + result, trace = self.run_starter('first allow_hw=1\nsecond cpif=1\n', 'missing') + self.assertEqual(result.returncode, 1) + self.assertEqual(trace, []) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), 'Off: reboot needed') + + def test_parser_accepts_only_named_module_arguments_and_refuses_symlink(self): + function = re.search(r'^read_link_config\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + path = self.root / 'valid.conf' + path.write_text('first\nsecond\tflag=1,2 value=a/b:c+_-.\n') + script = ('set -euo pipefail\nrefuse() { exit 7; }\n' + function + + '\nread_link_config "$1"\nprintf "%s\\n" "${LINK_MODULES[@]}"') + result = subprocess.run(['bash', '-c', script, 'test', str(path)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout.splitlines(), ['first', 'second\tflag=1,2 value=a/b:c+_-.']) + link = self.root / 'alias.conf' + link.symlink_to(path) + result = subprocess.run(['bash', '-c', script, 'test', str(link)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 7) + + def test_config_read_precedes_parameter_check_inhibitor_and_latch(self): + self.assertLess(SOURCE.index('read_link_config "$LINK_CONFIG"'), + SOURCE.index('link_param_ok() {')) + self.assertLess(SOURCE.index('link_param_ok() {'), + SOURCE.index('# The sleep inhibitor before the drivers:')) + self.assertLess(SOURCE.index('# The sleep inhibitor before the drivers:'), + SOURCE.index("printf '%s boot %s")) + if __name__ == '__main__': unittest.main() From 778d44bc1dcd62d5ce178a88aea89030d876160b Mon Sep 17 00:00:00 2001 From: Marsel Nenaj <201385099+marselnenaj@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:02:24 +0100 Subject: [PATCH 3/3] omamobile-modem: reject binary link configuration before parsing Bash read discards NUL bytes, so a malformed module or parameter could become a valid entry before the strict line pattern sees it. Reject non-ASCII-text bytes in C locale with grep's binary handling disabled, before reading lines. Read both passes through the same opened config file and fail on scan errors. Actual full-starter tests reject NUL-only input and NUL in module/parameter names before the inhibitor, latch or any module load. Removing the byte filter restores the inert load trace and fails the same assertion. The full package suite passes with all sixteen link tests. Co-Authored-By: Codex --- packages/omamobile-modem/README.md | 3 ++- .../omamobile-modem/lib/omamobile-modem-start | 13 +++++++++-- packages/omamobile-modem/test/test_link.py | 22 ++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/omamobile-modem/README.md b/packages/omamobile-modem/README.md index f07064c30..30405c55d 100644 --- a/packages/omamobile-modem/README.md +++ b/packages/omamobile-modem/README.md @@ -22,7 +22,8 @@ slash or hyphen. Arguments are separated by spaces or tabs; a module without parameters is valid. Blank lines and whole-line comments are allowed. Shell quotes, substitutions, assignments, functions, options and inline comments are refused, as are empty configurations, symlinks and unreadable/nonregular -files. A missing file preserves the default; a present invalid file refuses. +files. A raw ASCII-text check rejects NUL and other non-text bytes before +Bash reads lines; both passes use the same opened file. A missing file preserves the default; a present invalid file refuses. All entries are validated before checking already-loaded parameters, checking the sleep inhibitor, creating the bring-up latch or loading modules. Missing diff --git a/packages/omamobile-modem/lib/omamobile-modem-start b/packages/omamobile-modem/lib/omamobile-modem-start index fbf956056..8b90ad187 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-start +++ b/packages/omamobile-modem/lib/omamobile-modem-start @@ -157,9 +157,17 @@ fi LINK_CONFIG=/usr/lib/omamobile-modem/link.conf LINK_MODULES=("pcie-gs201-cp allow_hw=1") read_link_config() { - local path=$1 entry LC_ALL=C + local path=$1 entry config_fd LC_ALL=C [[ -e $path || -L $path ]] || return 0 [[ -f $path && -r $path && ! -L $path ]] || refuse "Off: invalid link configuration" + # Bash read discards NUL bytes. Reject binary/non-ASCII bytes first, + # through the same opened file, before read can normalize their contents. + exec {config_fd}< "$path" || refuse "Off: invalid link configuration" + if LC_ALL=C grep -aq '[^[:print:][:blank:]]' "/proc/self/fd/$config_fd"; then + refuse "Off: invalid link configuration" + else + [[ $? == 1 ]] || refuse "Off: invalid link configuration" + fi LINK_MODULES=() while IFS= read -r entry || [[ -n $entry ]]; do [[ $entry =~ ^[[:blank:]]*$ || $entry =~ ^[[:blank:]]*# ]] && continue @@ -168,7 +176,8 @@ read_link_config() { [[ $entry =~ ^[a-zA-Z][a-zA-Z0-9_-]*([[:blank:]]+[a-zA-Z_][a-zA-Z0-9_]*=[a-zA-Z0-9_.,:+/-]+)*[[:blank:]]*$ ]] || refuse "Off: invalid link configuration" LINK_MODULES+=("$entry") - done < "$path" + done <&"$config_fd" + exec {config_fd}<&- ((${#LINK_MODULES[@]})) || refuse "Off: empty link configuration" } read_link_config "$LINK_CONFIG" diff --git a/packages/omamobile-modem/test/test_link.py b/packages/omamobile-modem/test/test_link.py index d1ba2b8f2..c7893dd42 100644 --- a/packages/omamobile-modem/test/test_link.py +++ b/packages/omamobile-modem/test/test_link.py @@ -65,7 +65,10 @@ def run_starter(self, config, loaded=None): """Run actual starter with only paths redirected and inert external tools.""" config_path = self.root / 'link.conf' if config is not None: - config_path.write_text(config) + if isinstance(config, bytes): + config_path.write_bytes(config) + else: + config_path.write_text(config) vendor = self.root / 'vendor' vendor.mkdir() profile = vendor / 'omamobile-vendor-chroot' @@ -132,6 +135,23 @@ def test_data_substitution_refuses_without_executing_marker(self): self.assertEqual((self.root / 'refused').read_text().strip(), 'Off: invalid link configuration') + def assert_binary_config_refused(self, raw): + result, trace = self.run_starter(raw) + self.assertEqual(trace, []) + self.assertEqual(result.returncode, 1) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), + 'Off: invalid link configuration') + + def test_nul_inside_module_cannot_normalize_to_valid_load(self): + self.assert_binary_config_refused(b'fi\x00rst allow_hw=1\n') + + def test_nul_inside_parameter_cannot_normalize_to_valid_load(self): + self.assert_binary_config_refused(b'first allow_hw=\x001\n') + + def test_nul_only_configuration_is_invalid(self): + self.assert_binary_config_refused(b'\x00') + def test_invalid_data_cannot_override_guards(self): # Execute the actual parser once per hostile data sample, never source it. function = re.search(r'^read_link_config\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group()