From 376451c5eee60f05cd34dddbb00255e97bbcb4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdurrahman=20Kaan=20Dike=C3=A7?= Date: Mon, 12 Oct 2026 15:34:11 +0300 Subject: [PATCH] SMS direct delivery: no header or text in the log, no links followed; FINDINGS 48 mu300-atd logs one fixed word per +CMT frame ("+CMT (PDU spooled)" / "+CMT (not spooled)"), never the header (the sender, in text mode) nor the second line, which the spooler takes only as hexadecimal; a text-mode frame reaches nothing. import-pdu, the spooler and the daemon's spool loop refuse symbolic links and directories that are not ours (own_dir/own_file, the lock rule of mu300-update), make the directories mode 700 and write through mktemp and rename only. The periodic CNMI check looks at mt alone (the U30 Air answers 3,2,1,0,1 for 3,2,2,0,1), each acknowledged message is logged. Tests for the links and the text-mode frame; FINDINGS 48 with the device run (direct and forced fallback) and why Ubuntu keeps polling. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VVN2zDgf76kMDVSuWhHPUE --- docs/FINDINGS.md | 88 ++++++++++++++++++ openwrt/luci-overlay/opt/mu300/bin/mu300-sms | 43 ++++++--- openwrt/luci-overlay/opt/mu300/bin/mu300-smsd | 15 +++- rootfs/overlay/opt/mu300/bin/mu300-atd | 16 ++-- rootfs/overlay/opt/mu300/bin/mu300-cmt-spool | 21 ++++- rootfs/overlay/opt/mu300/bin/sms | 3 + tests/test_device_scripts.py | 34 +++++-- tests/test_sms_pool.py | 90 ++++++++++++++++++- 8 files changed, 279 insertions(+), 31 deletions(-) diff --git a/docs/FINDINGS.md b/docs/FINDINGS.md index f9179b6a..81c6b788 100644 --- a/docs/FINDINGS.md +++ b/docs/FINDINGS.md @@ -109,6 +109,10 @@ what replaced it. The table below gives each section's state. | 42 | No access point on a DFS channel (user report, 2026-10-11) | current | | 43 | adb over USB to Linux: adbd in the gadget, and what FunctionFS does to the rest of it | current | | 44 | Network namespaces on 5.4: the vendor's fragment sysctl, torn down by the wrong namespace (#103) | current (fixed) | +| 45 | `rmmod sprd_wlan_combo` panicked in `__run_timers` (issue #146) | current | +| 46 | SMS sends failed with IMS "Busy Everywhere" until the SMSC was written back; the panel lost slow sends (#176) | current (fixed) | +| 47 | A system the Magisk zip added beside another had a password nobody saw (#167) | current (fixed) | +| 48 | SMS delivered directly (+CMT), acknowledged only once the pool has it; the SIM path stays as the fallback | current | ## Hardware and firmware facts @@ -3887,3 +3891,87 @@ plan, the closing message and the note in `/data/adb/mu300-linux-password.txt` s Device test: a zip install of `openwrt` on a board that has Ubuntu must end with the `root: none yet` line, the file must note it under `user: root`, and `ssh root@192.168.77.1` into the booted OpenWrt must log in with no password; LuCI's login on openwrt-luci likewise, and `passwd` there must take. + +### 48. SMS delivered directly (+CMT), acknowledged only once the pool has it; the SIM path stays as the fallback + +Until v2026.10.22 the panel's SMS pool (openwrt-luci: mu300-smsd, mu300-sms, ยง46) learned of a new message the +roundabout way: the modem stored it on the SIM and said `+CMTI: "SM",n` on nr0, mu300-smsd saw the line in +mu300-atd's URC log and ran `mu300-sms sync` (`AT+CMGL=4`, decode, pool, `AT+CMGD` per slot). Two writes to the SIM +per message, a listing of the whole store, and the SIM as the only safe copy until the pool had it. kanoqwq's fork +delivers directly instead (GSM 27.005 `+CNMI` with mt=2: the modem forwards the SMS-DELIVER as a `+CMT:` frame and +waits for `AT+CNMA`); this is that mechanism, ported into this tree's layout. + +**Mechanism.** mu300-smsd starts as before: `AT+CNMI=2,1,0,0,0`, a first `sync` (what the SIM holds), then +`AT+CMGF=0`, `AT+CSMS=1`, `AT+CNMI=3,2,2,0,1`. If any of the three is not `OK` (or `MU300_SMS_DIRECT=0` says not to +ask), `AT+CNMI=2,1,0,0,0` is asked again and only the +CMTI path runs, exactly as before. With direct delivery on +(`/run/mu300-smsd.direct` exists) every new message comes as two lines on nr0, where +CMTI came too: `+CMT: ,` +and the PDU. mu300-atd's nr0 drainer (and its drain and collect on the command channel, should a frame come there) +hands both lines to `mu300-cmt-spool` on its **stdin** - never argv, which is public - and that writes the PDU as +`$POOL/incoming/cmt-..ready` via mktemp and rename, fsyncing the file and the directory (busybox +`fsync`, or coreutils `sync FILE`; never a global sync). The URC log gets one fixed word, `+CMT (PDU spooled)` or +`+CMT (not spooled)`: not the header, which in text mode would carry the sender, and never the second line. A frame +inside a reply is taken out of it (a client parsing `AT+CMGL` would read the PDU line as one of its own). Every 5 s +mu300-smsd pools each `*.ready` with `mu300-sms import-pdu FILE` and **only then** sends `AT+CNMA=1`; an import or an +acknowledgement that fails ends the round and is retried, three failed rounds in a row set the files aside as +`*.failed` and fall back to the SIM path. `import-pdu` refuses anything the codec makes no message of, then parks the +PDU in `$POOL/parts` (mktemp, rename, fsync) and absorbs the parked parts with the same decoding and grouping a SIM +listing goes through, so a long message's parts wait for each other there as they did on the SIM, with the same +`MU300_SMS_PARTIAL_WAIT`; the periodic sync lists the SIM and the parked parts together, so a part whose siblings +never come is pooled after the hour, and the parts of one message join across the SIM (a class 2 part) and the air. +The pool's dedupe (fingerprint, tombstones) applies: a message the modem delivers again after a lost acknowledgement +is not pooled twice, and one deleted from the pool stays deleted. The daemon exports `MU300_AT_LOCK_WAIT=8`, so a +client stuck on the AT channel cannot hold the receiver behind mu300-at's 90 s default. Everything that writes as root +into the pool (`import-pdu`, the spooler, the daemon's spool loop) refuses symbolic links and directories that are not +ours (`own_dir`/`own_file`, mu300-update's lock rule) and makes the directories mode 700. + +**What the modem does (U30 Air, Vodafone TR).** `AT+CNMI=?` gives `(0-3),(0-3),(0-3),(0-2),(0-1)`, `AT+CSMS=?` +`(0-1)`. It answers `OK` to `AT+CNMI=3,2,2,0,1` and then reports `+CNMI: 3,2,1,0,1` - it keeps a bm of its own, so +the daemon's periodic check (every full sync, after which it re-applies the setting) looks at mt alone. ds=0 rather +than the fork's ds=1 on purpose: nothing here asks for status reports (no SRR bit), and a report routed to the TE +would need an acknowledgement of its own, which is the one thing that makes the modem drop the routing by itself +(27.005: no CNMA in time, mt and ds back to 0). A two-part reply from 7000 came as two frames (`+CMT: ,157` then +`+CMT: ,75`, three seconds apart), the second only after the first was acknowledged; the SIM/ME store stayed at +`"ME",0,0`. The modem held an unacknowledged message for two minutes without dropping the routing (measured by +accident: the daemon was stopped between the frame and its import, the spool kept both PDUs, the restarted daemon +imported and acknowledged them and `+CNMI?` still said `3,2,1,0,1`). + +**Device test** (`mu300-sms send 7000 KALAN`, the line answers within seconds): + +``` +daemon.info mu300-smsd[9008]: sync: 0 new, 0 on the SIM, 42 in the pool, 0 moved off the SIM +daemon.err mu300-smsd[9008]: mu300-smsd: direct delivery is on (+CMT frames into /etc/mu300/sms-pool/incoming) +daemon.info mu300-smsd[9008]: import: 0 new, 43 in the pool, 1 waiting for its other parts +daemon.err mu300-smsd[9008]: mu300-smsd: pooled and acknowledged cmt-1791808049.ePLCmA.ready +daemon.info mu300-smsd[9008]: new: 000049 from 7000 2026-10-12 15:27:29 +daemon.info mu300-smsd[9008]: import: 1 new, 44 in the pool +daemon.err mu300-smsd[9008]: mu300-smsd: pooled and acknowledged cmt-1791808053.iFAgce.ready +# /run/mu300-at/urc/stty_nr0.log: +CMT (PDU spooled) (twice; no PDU, no header fields) +# AT+CPMS? -> +CPMS: "ME",0,0,"ME",0,0,"ME",0,0 AT+CNMI? -> +CNMI: 3,2,1,0,1 +# mu300-sms show 49 -> sim index: not on the SIM; ubus call mu300dash sms_list lists 000049 (the panel's page) +``` + +The fallback, forced with `MU300_SMS_DIRECT=0`: `the modem did not take direct delivery (or MU300_SMS_DIRECT=0); new +messages are stored on the SIM and announced (+CMTI)`, `+CNMI: 2,1,0,0,0`, and the same send answered through the old +path - `+CMTI: "SM",2` in the URC log, `new: 000053 from 7000`, `sync: 1 new, 1 on the SIM, 48 in the pool, 2 moved +off the SIM`, `sim index: 1,2`, the SIM empty again afterwards. The service restarted went back to direct delivery. + +**Tests** (`tests/test_sms_pool.py`, `tests/test_device_scripts.py Atd`): `import-pdu` pools a message with no AT +command, is idempotent, honours tombstones, joins parts and leaves the waiting ones parked, pools a stale part by a +sync, joins a SIM part with an air part, refuses non-hex/odd/undecodable/overlong PDUs and every link (a spool entry, +the parts directory, the pool), fsyncs the parked part before the pool is touched; the spooler writes mode 600 into a +700 directory with the two fsyncs and nothing else, refuses bad frames and linked directories; the daemon (Linux +only: it tells a live holder by `/proc`) asks for direct delivery after the first sync, acknowledges after the import +and in spool order, falls back on each refusal and on `MU300_SMS_DIRECT=0` with +CMTI still working, never +acknowledges a failed import, gives up after three rounds, keeps the file when the ack is refused, puts the routing +back when `+CNMI?` says the modem dropped it, sets a linked spool entry aside; mu300-atd on a pty spools a frame from +nr0, from inside a reply (kept out of the answer), from a drain, logs the fixed word only, drops a text-mode frame +(sender and text reach nothing), and goes on without the spooler. + +**Ubuntu.** mu300-atd and the spooler are on every system, so a `+CMT` frame there is spooled too and never logged - +but nothing on Ubuntu asks for direct delivery, and that is deliberate: the system's SMS tool, `sms`, has no daemon +and no pool; `sms watch` polls the store (`AT+CPMS?`/`AT+CMGL`), which cannot miss a message, and `sms list`/`read` +read the SIM. A message routed to the TE needs something running to acknowledge it, or the modem drops the routing +and stores the next ones unannounced; a daemon with a pool is what openwrt-luci has and Ubuntu does not. So on Ubuntu +the modem stays in its storage mode (the fork's smsd is openwrt-luci only here as well), and a PDU that ever lands in +`/etc/mu300/sms-pool/incoming` there (someone set `AT+CNMI` mt=2 by hand) is kept, unacknowledged, for an +`mu300-sms import-pdu` that does not exist on that system - documented rather than half-built. diff --git a/openwrt/luci-overlay/opt/mu300/bin/mu300-sms b/openwrt/luci-overlay/opt/mu300/bin/mu300-sms index 42b6a518..8ec99b9b 100755 --- a/openwrt/luci-overlay/opt/mu300/bin/mu300-sms +++ b/openwrt/luci-overlay/opt/mu300/bin/mu300-sms @@ -313,10 +313,9 @@ sim_raw() { # the SIM's slots, one "index:stat PDU" per line (and a copy /^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]+$/ { if (idx != "") { print idx "\t" $0; idx = "" } } ' | { if [ -n "${SIM_RAW:-}" ]; then tee "$SIM_RAW"; else cat; fi; } } -parts_raw() { # the parked PDUs, "p:0 PDU" per file +parts_raw() { # the parked PDUs, "p:0 PDU" per file (a regular file of this directory, never a link) for _p in "$PARTS"/p*; do - case $_p in *.part) continue ;; esac # a write in progress (or one a crash left behind) - [ -f "$_p" ] || continue + [ -f "$_p" ] && [ ! -L "$_p" ] || continue _pdu=$(head -n 1 "$_p" 2>/dev/null) case $_pdu in ''|*[!0-9A-Fa-f]*) continue ;; esac printf '%s:0\t%s\n' "${_p##*/}" "$_pdu" @@ -551,13 +550,32 @@ cmd_sync() { absorb sim } +# own_dir DIR: DIR is a directory of ours, mode 700, and no symbolic link (made so when it is missing); else false. +# The same rule as mu300-update's lock directory: import-pdu runs as root in a directory tree, and follows no name +# somebody else may have planted there. +own_dir() { + [ ! -L "$1" ] || return 1 + [ -e "$1" ] || ( umask 077; mkdir "$1" ) 2>/dev/null || [ -d "$1" ] || return 1 + [ ! -L "$1" ] && [ -d "$1" ] || return 1 + _ls=$(ls -ldn "$1" 2>/dev/null) + [ "$(printf '%s\n' "$_ls" | awk '{print $3}')" = "$(id -u)" ] || return 1 + case $_ls in drwx------*) ;; *) chmod 700 "$1" 2>/dev/null || return 1 ;; esac +} +# own_file FILE: a regular file of ours, no symbolic link +own_file() { + [ ! -L "$1" ] && [ -f "$1" ] || return 1 + [ "$(ls -ldn "$1" 2>/dev/null | awk '{print $3}')" = "$(id -u)" ] +} + # import-pdu FILE: a message the modem delivered directly, as mu300-cmt-spool wrote it (one line, the PDU in # hexadecimal). Refused (exit 1, nothing written) unless the codec makes a message of it; then parked in $PARTS on the # disk, and the parked parts absorbed into the pool. Exit 0 means the pool (or $PARTS, for a part whose siblings are # still to come) holds it on the disk: mu300-smsd acknowledges the message to the network only then. +# FILE must be a regular file of ours, the pool and $PARTS directories of ours (own_file, own_dir): no link anywhere. cmd_import_pdu() { _file=${1:-} - [ -n "$_file" ] && [ -f "$_file" ] || die "import-pdu needs a file holding one PDU" + [ -n "$_file" ] || die "import-pdu needs a file holding one PDU" + own_file "$_file" || die "$_file is not a regular file of ours (or a symbolic link)" [ -f "$AWK" ] || die "$AWK is missing" _pdu=$(head -n 1 "$_file" 2>/dev/null | tr -d '\r') case $_pdu in ''|*[!0-9A-Fa-f]*) die "$_file does not hold a hexadecimal PDU" ;; esac @@ -565,14 +583,17 @@ cmd_import_pdu() { # decodable before it is parked: a PDU the codec makes nothing of would sit in $PARTS for ever _rec=$(printf 'p:0\t%s\n' "$_pdu" | awk -v mode=decode -F'\t' -f "$AWK" 2>/dev/null) [ -n "$_rec" ] || die "the PDU in $_file cannot be decoded" + own_dir "$POOL" || die "$POOL is not a directory of ours (or a symbolic link)" + own_dir "$PARTS" || die "$PARTS is not a directory of ours (or a symbolic link)" pool_init - mkdir -p "$PARTS" 2>/dev/null || die "cannot create $PARTS" - # under a name of its own (the second, the pid), written aside and renamed, the file and the directory entry on - # the disk: from here on the message is safe whatever happens to the pool - _name=p$(date +%s)-$$ - { printf '%s\n' "$_pdu" > "$PARTS/$_name.part" && durable "$PARTS/$_name.part" && - mv "$PARTS/$_name.part" "$PARTS/$_name" && durable "$PARTS"; } || { - rm -f "$PARTS/$_name.part"; die "could not write into $PARTS"; } + # made by mktemp inside $PARTS (a dotfile: not one of the parts yet), the file and then its final name (the second, + # the pid: nothing can wait there, and a link there is refused) on the disk: from here on the message is safe + # whatever happens to the pool + _name=$PARTS/p$(date +%s)-$$ + _tmp=$(umask 077; mktemp "$PARTS/.part.XXXXXX") || die "cannot write into $PARTS" + { printf '%s\n' "$_pdu" > "$_tmp" && durable "$_tmp" && [ ! -L "$_name" ] && [ ! -e "$_name" ] && + mv "$_tmp" "$_name" && durable "$PARTS"; } || { + rm -f "$_tmp"; die "could not write into $PARTS"; } absorb parts } diff --git a/openwrt/luci-overlay/opt/mu300/bin/mu300-smsd b/openwrt/luci-overlay/opt/mu300/bin/mu300-smsd index cfc8108e..c8397c2f 100755 --- a/openwrt/luci-overlay/opt/mu300/bin/mu300-smsd +++ b/openwrt/luci-overlay/opt/mu300/bin/mu300-smsd @@ -118,10 +118,17 @@ direct_on || direct_off "the modem did not take direct delivery (or MU300_SMS_DI # acknowledgement comes (it forwards no other meanwhile). process_spool() { for f in "$SPOOL"/*.ready; do - [ -f "$f" ] || continue + [ -e "$f" ] || [ -L "$f" ] || continue + # the spooler writes regular files; a link here is somebody else's, and is never followed + if [ -L "$f" ] || [ ! -f "$f" ]; then + echo "mu300-smsd: $f is not a regular file; set aside" >&2 + mv "$f" "${f%.ready}.failed" 2>/dev/null || rm -f "$f" + continue + fi "$SMS" import-pdu "$f" || { echo "mu300-smsd: could not pool $f" >&2; return 1; } at_ok 'AT+CNMA=1' || { echo "mu300-smsd: the modem did not take the acknowledgement of $f" >&2; return 1; } rm -f "$f" + echo "mu300-smsd: pooled and acknowledged ${f##*/}" >&2 done return 0 } @@ -163,9 +170,11 @@ while :; do elapsed=0 "$SMS" sync >/dev/null || true # the modem drops direct routing by itself (mt=0: no indication at all) after an acknowledgement that never - # came; the periodic sync above still finds such a message, and the routing is put back here + # came; the periodic sync above still finds such a message, and the routing is put back here. Only mt is + # looked at: the modem says OK and then keeps a setting of its own for the rest (measured on the U30 Air: + # "3,2,1,0,1" for the "3,2,2,0,1" it was asked) if [ "$direct" = 1 ]; then - mu300-at -t 5 'AT+CNMI?' 2>/dev/null | grep -q "^+CNMI: ${DIRECT_CNMI#AT+CNMI=}" || + mu300-at -t 5 'AT+CNMI?' 2>/dev/null | grep -q '^+CNMI: [0-9],2,' || direct_on || direct_off "the modem dropped direct delivery and did not take it again" fi fi diff --git a/rootfs/overlay/opt/mu300/bin/mu300-atd b/rootfs/overlay/opt/mu300/bin/mu300-atd index be73def2..125ba13e 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-atd +++ b/rootfs/overlay/opt/mu300/bin/mu300-atd @@ -137,11 +137,13 @@ mkdir -p "$URCDIR" # A message the modem delivers directly (mu300-smsd asks for that with AT+CNMI mt=2, openwrt-luci only) is a two-line # frame: "+CMT: ," and the PDU on the next line, on whichever channel the modem's unsolicited output # goes (nr0 here, where +CMTI comes too), or on the command channel between or inside replies. The two lines go to -# mu300-cmt-spool on its stdin, which puts the PDU on the disk for mu300-smsd to pool and acknowledge; the log gets -# the header only (it is world readable, and emptied when it grows), and a reply never carries the frame (a client -# parsing a listing would take the PDU line for one of its own). Never an argument: argv is public. +# mu300-cmt-spool on its stdin, which puts the PDU on the disk for mu300-smsd to pool and acknowledge. Neither line +# is logged (the log is world readable, and emptied when it grows): in text mode - which nothing here asks for, but +# another tool might - the header carries the sender and the second line the text; the log gets one fixed word +# either way, and the spooler takes nothing but hexadecimal. A reply never carries the frame either (a client parsing +# a listing would take the PDU line for one of its own). Never an argument: argv is public. CMT_SPOOL=${MU300_CMT_SPOOL_BIN:-/opt/mu300/bin/mu300-cmt-spool} -cmt_frame() { # cmt_frame HEADER FD LOG: the PDU line after a +CMT header, from FD, with the header to the spooler +cmt_frame() { # cmt_frame HEADER FD LOG: the line after a +CMT header, from FD, with the header to the spooler _pdu= # the PDU is the next line, written with the header; an empty line (the frame's own CRLF) is skipped once for _try in 1 2; do @@ -150,10 +152,10 @@ cmt_frame() { # cmt_frame HEADER FD LOG: the PDU line after a +CMT header, from [ -n "$_pdu" ] && break done if [ -x "$CMT_SPOOL" ] && printf '%s\n%s\n' "$1" "$_pdu" | "$CMT_SPOOL"; then - printf '%s\n' "$1 (PDU spooled)" >> "$3" 2>/dev/null + printf '%s\n' "+CMT (PDU spooled)" >> "$3" 2>/dev/null else - echo "mu300-atd: a message delivered directly ($1) could not be spooled; it stays unacknowledged" >&2 - printf '%s\n' "$1 (PDU not spooled)" >> "$3" 2>/dev/null + echo "mu300-atd: a message delivered directly (+CMT) could not be spooled; it stays unacknowledged" >&2 + printf '%s\n' "+CMT (not spooled)" >> "$3" 2>/dev/null fi } # '-' and not ':-': a value that is set but empty means this daemon opens no URC channel at all. With ':-' a diff --git a/rootfs/overlay/opt/mu300/bin/mu300-cmt-spool b/rootfs/overlay/opt/mu300/bin/mu300-cmt-spool index 48dea5d3..7194e375 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-cmt-spool +++ b/rootfs/overlay/opt/mu300/bin/mu300-cmt-spool @@ -12,7 +12,9 @@ # # Anything that is not a +CMT header followed by an even number of hexadecimal digits (at most 1024: a PDU with its # SMSC is under 180 octets) is refused with exit 1 and nothing written. The pool is the one mu300-sms uses -# (MU300_SMS_POOL, /etc/mu300/sms-pool), root only. +# (MU300_SMS_POOL, /etc/mu300/sms-pool), root only: this runs as root, so the pool and the spool must be directories +# of ours and no symbolic links (own_dir), the file is made by mktemp inside the spool and renamed into place, and +# nothing here follows a name somebody else may have planted. set -u umask 077 export PATH="$PATH:/opt/mu300/bin:/opt/mu300/busybox-bin:/usr/sbin:/sbin" @@ -20,6 +22,17 @@ POOL=${MU300_SMS_POOL:-/etc/mu300/sms-pool} SPOOL=$POOL/incoming CR=$(printf '\r') +# own_dir DIR: DIR is a directory of ours, mode 700, and no symbolic link (made so when it is missing); else false. +# The same rule as mu300-update's lock directory. +own_dir() { + [ ! -L "$1" ] || return 1 + [ -e "$1" ] || mkdir "$1" 2>/dev/null || [ -d "$1" ] || return 1 + [ ! -L "$1" ] && [ -d "$1" ] || return 1 + _ls=$(ls -ldn "$1" 2>/dev/null) + [ "$(printf '%s\n' "$_ls" | awk '{print $3}')" = "$(id -u)" ] || return 1 + case $_ls in drwx------*) ;; *) chmod 700 "$1" 2>/dev/null || return 1 ;; esac +} + # durable FILE...: FILE on the disk (its data, or a directory's entries) before going on - the same as mu300-sms's durable() { if command -v fsync >/dev/null 2>&1; then @@ -38,10 +51,12 @@ case $header in '+CMT:'*) ;; *) echo "mu300-cmt-spool: not a +CMT frame" >&2; ex case $pdu in ''|*[!0-9A-Fa-f]*) echo "mu300-cmt-spool: the PDU is not hexadecimal" >&2; exit 1 ;; esac [ $(( ${#pdu} % 2 )) -eq 0 ] && [ ${#pdu} -le 1024 ] || { echo "mu300-cmt-spool: the PDU has an odd length, or is too long" >&2; exit 1; } -mkdir -p "$SPOOL" 2>/dev/null || { echo "mu300-cmt-spool: cannot create $SPOOL" >&2; exit 1; } +own_dir "$POOL" || { echo "mu300-cmt-spool: $POOL is not a directory of ours (or a symbolic link)" >&2; exit 1; } +own_dir "$SPOOL" || { echo "mu300-cmt-spool: $SPOOL is not a directory of ours (or a symbolic link)" >&2; exit 1; } # the name sorts by arrival (mu300-smsd takes the oldest first); the random part keeps two frames in one second apart part=$(mktemp "$SPOOL/cmt-$(date +%s).XXXXXX") || { echo "mu300-cmt-spool: cannot write into $SPOOL" >&2; exit 1; } -if printf '%s\n' "$pdu" > "$part" && durable "$part" && mv "$part" "$part.ready" && durable "$SPOOL"; then +# the final name is the random one with .ready: nobody can have planted it, and a link there is refused all the same +if printf '%s\n' "$pdu" > "$part" && durable "$part" && [ ! -L "$part.ready" ] && mv "$part" "$part.ready" && durable "$SPOOL"; then exit 0 fi rm -f "$part" "$part.ready" diff --git a/rootfs/overlay/opt/mu300/bin/sms b/rootfs/overlay/opt/mu300/bin/sms index 69dcd81f..3574ad36 100755 --- a/rootfs/overlay/opt/mu300/bin/sms +++ b/rootfs/overlay/opt/mu300/bin/sms @@ -266,6 +266,9 @@ watch) # Asking the modem how many messages it holds is more dependable here than waiting for a +CMTI URC: the # daemon owns the channel and throws away unsolicited lines between commands, and a message that arrives # while the modem is busy would be missed entirely. Polling cannot miss one. + # Nor does this ask for direct delivery (AT+CNMI mt=2, which openwrt-luci's mu300-smsd uses, FINDINGS 48): a + # message routed to us must be acknowledged by something that is running, or the modem drops the routing and + # stores the next ones unannounced; this tool has no daemon, so the modem keeps storing and this keeps polling. # A long message whose other parts have not come yet waits, up to MU300_SMS_PARTIAL_WAIT seconds (an hour) from # when this first saw it ($STATE/partial, "key epoch"): a part the network lost would otherwise hold it back # for ever. Then the parts that did arrive are reported, "[missing part N]" in place of the others; the whole diff --git a/tests/test_device_scripts.py b/tests/test_device_scripts.py index b25d3d57..c16b608d 100644 --- a/tests/test_device_scripts.py +++ b/tests/test_device_scripts.py @@ -472,7 +472,7 @@ class Atd(ShellTest): if '+CEREG: 1' in log.read_text(): break time.sleep(0.05) - self.assertEqual([l for l in log.read_text().splitlines() if l], ['+CSQ: 20,99', '+CMT: ,27 (PDU spooled)', '+CEREG: 1']) + self.assertEqual([l for l in log.read_text().splitlines() if l], ['+CSQ: 20,99', '+CMT (PDU spooled)', '+CEREG: 1']) self.assertNotIn(self.PDU, log.read_text()) self.assertNotIn(self.PDU, self.err.read_text()) @@ -482,7 +482,7 @@ class Atd(ShellTest): self.send(m, 'AT+CSQ', f'\r\n+CMT: ,27\r\n{self.PDU}\r\n\r\n+CSQ: 20,99\r\n\r\nOK\r\n'.encode(), t=3) self.assertEqual(self.answer, '+CSQ: 20,99\nOK\n') self.assertEqual(self.spooled(), [self.PDU]) - self.assertIn('+CMT: ,27 (PDU spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) + self.assertIn('+CMT (PDU spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) self.assertNotIn(self.PDU, (self.dir / 'urc' / 'tty_nr1.log').read_text()) def test_a_cmt_frame_while_idle_is_spooled_before_the_next_command(self): @@ -494,7 +494,8 @@ class Atd(ShellTest): self.send(m, 'AT', b'\r\nOK\r\n', t=3) # the drain before the command takes it self.assertEqual(self.answer, 'OK\n') self.assertEqual(self.spooled(), [self.PDU]) - self.assertIn('+CMT: "ADANA BLD",27 (PDU spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) + self.assertIn('+CMT (PDU spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) + self.assertNotIn('ADANA', (self.dir / 'urc' / 'tty_nr1.log').read_text()) # the header is never logged def test_a_frame_that_is_no_message_is_not_spooled_and_the_daemon_goes_on(self): for shell in self.each_shell(): @@ -505,7 +506,7 @@ class Atd(ShellTest): if '+CSQ: 1,2' in log.read_text(): break time.sleep(0.05) - self.assertEqual([l for l in log.read_text().splitlines() if l], ['+CMT: ,27 (PDU not spooled)', '+CSQ: 1,2']) + self.assertEqual([l for l in log.read_text().splitlines() if l], ['+CMT (not spooled)', '+CSQ: 1,2']) self.assertEqual(self.spooled(timeout=0.2), []) self.assertIn('could not be spooled', self.err.read_text()) # a header with no PDU after it (two seconds): the same, and the lines after it are logged as usual @@ -516,7 +517,28 @@ class Atd(ShellTest): if '+CSQ: 3,4' in log.read_text(): break time.sleep(0.05) - self.assertEqual([l for l in log.read_text().splitlines() if l][-2:], ['+CMT: ,27 (PDU not spooled)', '+CSQ: 3,4']) + self.assertEqual([l for l in log.read_text().splitlines() if l][-2:], ['+CMT (not spooled)', '+CSQ: 3,4']) + self.assertEqual(self.spooled(timeout=0.2), []) + + def test_a_text_mode_frame_is_written_nowhere(self): + """Nothing here asks for text mode, but another tool might: then the header carries the sender and the next + line the message. Neither reaches the log, a reply or stderr; the spooler takes hexadecimal only.""" + header, text = '+CMT: "+905551112233",,"26/10/12,15:00:00+12"', 'the secret text of a message' + for shell in self.each_shell(): + m, nr0 = self.start_spooling(shell) + os.write(nr0, f'\r\n{header}\r\n{text}\r\n+CSQ: 1,2\r\n'.encode()) + log0 = self.dir / 'urc' / 'tty_nr0.log' + for _ in range(100): + if '+CSQ: 1,2' in log0.read_text(): + break + time.sleep(0.05) + self.assertEqual([l for l in log0.read_text().splitlines() if l], ['+CMT (not spooled)', '+CSQ: 1,2']) + # and inside a reply on the command channel + self.send(m, 'AT+CSQ', f'\r\n{header}\r\n{text}\r\n\r\n+CSQ: 20,99\r\n\r\nOK\r\n'.encode(), t=3) + self.assertEqual(self.answer, '+CSQ: 20,99\nOK\n') + everything = log0.read_text() + (self.dir / 'urc' / 'tty_nr1.log').read_text() + self.err.read_text() + for secret in ('905551112233', 'secret', '26/10/12'): + self.assertNotIn(secret, everything) self.assertEqual(self.spooled(timeout=0.2), []) def test_without_the_spooler_nothing_is_spooled_and_nothing_breaks(self): @@ -526,7 +548,7 @@ class Atd(ShellTest): self.send(m, 'AT+CSQ', f'\r\n+CMT: ,27\r\n{self.PDU}\r\n\r\n+CSQ: 20,99\r\n\r\nOK\r\n'.encode(), t=3) self.assertEqual(self.answer, '+CSQ: 20,99\nOK\n') self.assertFalse((self.pool / 'incoming').exists()) - self.assertIn('+CMT: ,27 (PDU not spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) + self.assertIn('+CMT (not spooled)\n', (self.dir / 'urc' / 'tty_nr1.log').read_text()) def test_empty_urc_channels_open_none(self): for shell in self.each_shell(): diff --git a/tests/test_sms_pool.py b/tests/test_sms_pool.py index a3c98b10..180da915 100644 --- a/tests/test_sms_pool.py +++ b/tests/test_sms_pool.py @@ -986,11 +986,57 @@ class Pool(ShellTest): log = self.at_log() self.assertFalse([l for l in log if l.startswith('GLOBAL SYNC')], log) parts, msg = self.pool / 'parts', self.pool / 'msg' - self.assertRegex(log[0], rf'^FSYNC {re.escape(str(parts))}/p[0-9]+-[0-9]+\.part$') + self.assertRegex(log[0], rf'^FSYNC {re.escape(str(parts))}/\.part\.[A-Za-z0-9]+$') # mktemp's name, then the final one self.assertEqual(log[1], f'FSYNC {parts}') self.assertEqual(log[2], f'FSYNC {msg}/000001.part') self.assertEqual(log[3], f'FSYNC {msg} {self.pool}/next_id {self.pool}') + def test_import_pdu_follows_no_link(self): + """import-pdu runs as root: a spool entry that is a link, a parts directory or a pool that is one, are refused + and nothing is written through them.""" + for shell in self.each_shell(): + # the spool entry + self.fresh() + target = self.tmp / 'elsewhere.pdu' + target.write_text(PDU_GSM + '\n') + (self.pool / 'incoming').mkdir(parents=True) + link = self.pool / 'incoming' / 'cmt-1.link.ready' + link.symlink_to(target) + r = self.run_sms(shell, 'import-pdu', link) + self.assertEqual(r.returncode, 1, r.stdout) + self.assertIn('symbolic link', r.stderr) + self.assertFalse((self.pool / 'msg').exists()) + # the parts directory, pointing somewhere else + self.fresh() + elsewhere = self.tmp / 'elsewhere' + elsewhere.mkdir(exist_ok=True) + self.pool.mkdir() + (self.pool / 'parts').symlink_to(elsewhere) + r = self.run_sms(shell, 'import-pdu', self.spooled(PDU_GSM)) + self.assertEqual(r.returncode, 1, r.stdout) + self.assertIn('symbolic link', r.stderr) + self.assertEqual(sorted(x.name for x in elsewhere.iterdir()), []) + self.assertFalse((self.pool / 'msg').exists()) + # the pool itself + self.fresh() + shutil.rmtree(elsewhere, ignore_errors=True); elsewhere.mkdir() + self.pool.symlink_to(elsewhere) + f = self.tmp / 'spooled.ready' + f.write_text(PDU_GSM + '\n') + r = self.run_sms(shell, 'import-pdu', f) + self.assertEqual(r.returncode, 1, r.stdout) + self.assertEqual(sorted(x.name for x in elsewhere.iterdir()), []) + self.pool.unlink() + # a link among the parked parts is never read; a parts directory that is not mode 700 is made so + self.fresh() + (self.pool / 'parts').mkdir(parents=True, mode=0o755) + (self.pool / 'parts' / 'plink').symlink_to(target) + r = self.run_sms(shell, 'import-pdu', self.spooled(PDU_UCS2)) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual([h['from'] for h, _ in self.msgs().values()], ['ADANA BLD']) + self.assertEqual((self.pool / 'parts').stat().st_mode & 0o777, 0o700) + self.assertTrue((self.pool / 'parts' / 'plink').is_symlink()) + class Spool(ShellTest): """mu300-cmt-spool (every system): the two lines of a +CMT frame on stdin, the PDU into the pool's spool.""" @@ -1038,6 +1084,30 @@ class Spool(ShellTest): self.assertEqual(self.files(), []) self.assertFalse((self.tmp / 'fs.log').exists()) + def test_a_spool_or_pool_that_is_a_link_is_refused(self): + for shell in self.each_shell(): + elsewhere = self.tmp / 'elsewhere' + shutil.rmtree(elsewhere, ignore_errors=True); elsewhere.mkdir() + shutil.rmtree(self.pool, ignore_errors=True) + self.pool.mkdir(mode=0o700) + (self.pool / 'incoming').symlink_to(elsewhere) + r = self.script(shell, self.SPOOLER, stdin=f'+CMT: ,40\n{PDU_GSM}\n', MU300_SMS_POOL=self.pool) + self.assertEqual(r.returncode, 1) + self.assertIn('symbolic link', r.stderr) + self.assertEqual(sorted(x.name for x in elsewhere.iterdir()), []) + shutil.rmtree(self.pool) + self.pool.symlink_to(elsewhere) + r = self.script(shell, self.SPOOLER, stdin=f'+CMT: ,40\n{PDU_GSM}\n', MU300_SMS_POOL=self.pool) + self.assertEqual(r.returncode, 1) + self.assertEqual(sorted(x.name for x in elsewhere.iterdir()), []) + self.pool.unlink() + # a pool somebody left mode 755 is made 700, and the spool is written + self.pool.mkdir(mode=0o755) + r = self.script(shell, self.SPOOLER, stdin=f'+CMT: ,40\n{PDU_GSM}\n', MU300_SMS_POOL=self.pool) + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual(self.pool.stat().st_mode & 0o777, 0o700) + self.assertEqual(len(self.files()), 1) + def test_two_frames_in_one_second_are_two_files(self): for shell in self.each_shell(): self.spool(shell, f'+CMT: ,40\n{PDU_GSM}\n') @@ -1396,6 +1466,24 @@ class Daemon(ShellTest): self.assertIn('could not pool', err) self.assertIn('three rounds of pooling or acknowledging failed', err) + def test_a_link_in_the_spool_is_set_aside_and_never_imported(self): + for shell in self.each_shell(): + self.fresh() + d = self.start(shell) + try: + self.wait_for(lambda: self.flag.exists(), 'direct delivery was not asked for') + self.spool.mkdir(parents=True, exist_ok=True) + target = self.tmp / 'target.pdu' + target.write_text(PDU_GSM + '\n') + (self.spool / 'cmt-1.link.ready').symlink_to(target) + self.wait_for(lambda: (self.spool / 'cmt-1.link.failed').is_symlink(), 'the link was not set aside') + self.assertFalse([l for l in self.at_log() if l.startswith('SMS import-pdu')]) + self.assertNotIn('AT+CNMA=1', self.commands()) + self.assertTrue(self.flag.exists()) # one link is no failure of direct delivery + finally: + out, err = self.stop(d) + self.assertIn('not a regular file; set aside', err) + def test_an_acknowledgement_the_modem_refuses_keeps_the_file(self): for shell in self.each_shell(): self.fresh()