ricardgb opened a new pull request, #3658:
URL: https://github.com/apache/nuttx-apps/pull/3658

   ## Summary
   
   Two robustness fixes for the telnet daemon 
(`netutils/telnetd/telnetd_daemon.c`). As it stands, the standard NuttX telnet 
console can be disabled by a single aborted connection, and on some 
configurations it silently serves nothing from boot.
   
   **1. Keep the listen socket out of the standard-stream range.**
   When the daemon starts without open standard streams — exactly what happens 
when `nsh_telnetstart()` spawns `telnetd &` before the console device exists 
(e.g. `CONFIG_NSH_USBCONSOLE` boards, where `nsh_initialize()` runs before the 
USB console is connected) — `socket()` returns a descriptor in 0..2. The accept 
loop's own "go silent" `close(0)..close(2)` then destroys the listen socket on 
the first iteration; every subsequent `accept4()` fails and the daemon serves 
nothing, while appearing alive in the task list. Fix: `F_DUPFD_CLOEXEC` the 
descriptor above 2 before use.
   
   **2. Survive transient connection errors instead of exiting.**
   Previously any single failed connection killed the whole daemon:
   - a peer resetting before the accept completes (a port scanner, or a plain 
`nc -z` probe) surfaces as an `accept4()` error such as `ECONNABORTED` and took 
the errout path;
   - the per-connection error paths after a successful accept (setsockopt, 
`/dev/telnet` open, `SIOCTELNET`, session-device open, session spawn) likewise 
exited the daemon.
   
   One bad or aborted connection and the telnet console is dead until reboot — 
a trivial remote way to take out the console of any reachable NuttX device. 
Fix: treat these as per-connection failures (drop the connection, keep 
accepting). Genuinely unrecoverable `accept4()` errors 
(`EBADF`/`ENOTSOCK`/`EINVAL`/`EOPNOTSUPP`) still exit loudly, and a short pause 
on repeated transient failures avoids busy-spinning while an interface is down. 
Daemon setup errors (socket/bind/listen) exit as before.
   
   ## Impact
   
   Any board using `netutils/telnetd` (directly or via `CONFIG_NSH_TELNET`). 
Bug 1 affects configurations where the daemon can start before its standard 
streams exist (USB-console boards being the common case). Bug 2 affects 
everyone: a subnet `nmap` sweep kills every reachable NuttX telnet console.
   
   ## Testing
   
   Reproduced and verified on hardware: Raspberry Pi Pico 2 W (RP2350), 
composite USB CDC-ACM console + CDC-NCM network, `CONFIG_NSH_USBCONSOLE`, 
telnetd auto-started via `nsh_telnetstart()`.
   
   - Before (bug 1): daemon task alive but port 23 refused all connections from 
boot; the RTOS-aware debugger thread list showed it looping on the failed 
accept with the listen socket closed by its own `close(0)`.
   - Before (bug 2): a single `nc -zv <addr> 23` probe permanently killed a 
manually started, correctly listening daemon.
   - After: daemon listens from boot (within 5 s of enumeration), survives 
repeated `nc -z` probe barrages, and interactive nsh telnet sessions work.
   
   `tools/nxstyle.c` passes on the modified file.
   
   ## Note
   
   These changes were developed with the assistance of an AI agent (Claude) and 
have been human-reviewed and tested on real hardware as described above.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to