ricardgb opened a new issue, #22451:
URL: https://github.com/apache/nuttx/issues/22451

   **Description**
   
   `netprocfs_read_tcpstats()` (`net/procfs/net_tcp.c`) writes the header of 
the table in the
   buffer of the caller:
   
   ```c
   len = snprintf(buffer, buflen, "TCP sl  " ...);
   ...
   len += netprocfs_tcpstats(priv, buffer + len, buflen - len);
   ```
   
   `snprintf()` returns the length of the whole header (87 bytes in the sim 
config below), also when
   `buflen` is less. Then `buflen - len` (`size_t` minus `int`) wraps to a huge 
value, the loop
   `while (conn != NULL && buflen - len >= TCP_LINELEN)` runs, and every line 
of the table is
   written past the end of the buffer. The read returns more than was asked for.
   `netprocfs_read_udpstats()` (`net/procfs/net_udp.c`) has the same code.
   
   So a read with a buffer shorter than the header overruns it by the length of 
the whole table.
   That is every read through stdio, whose buffer is `CONFIG_STDIO_BUFFER_SIZE` 
(64 by default):
   `fopen()` + `fgets()` of `/proc/net/tcp` corrupts the heap (seen in the sim 
with an `fgets()`
   reader: SIGSEGV in `mm_forcefree()` from the `fclose()`; the program below 
shows the overrun
   with a guard area instead and does not crash). `cat` is not hit, it reads 
with a large buffer.
   
   A second, smaller defect of the same code: a buffer that holds the header 
but not a whole line
   (`TCP_LINELEN`, 180 bytes with IPv6) gets the header and then the end of the 
file.
   
   The pattern is not new: the same `snprintf(buffer, buflen, ...)` of the 
header is in the
   12.13.0 source (read, not run).
   
   **Reproduce (sim, unmodified master)**
   
   Replace `apps/examples/hello/hello_main.c` with 
`nuttx-procfs-net-tcp-overrun-repro.c` below
   (one TCP and one UDP connection, one `read()` of 64 bytes into a buffer with 
a guard area):
   
   ```sh
   ./tools/configure.sh sim:tcploop
   kconfig-tweak --enable EXAMPLES_HELLO --enable NET_STATISTICS --enable 
NET_UDP
   make olddefconfig && make
   ./nuttx
   nsh> hello
   ```
   
   Result on master `a8bb89bf680`:
   
   ```
   /proc/net/tcp: asked for 64 bytes, read() returned 289, 226 bytes written 
past the buffer
   /proc/net/udp: asked for 64 bytes, read() returned 148, 85 bytes written 
past the buffer
   FAIL
   ```
   
   With six TCP connections a `read()` of 1 byte returns 693.
   
   **Proposed fix**
   
   PR to follow: write each line in the line buffer of the open file 
(`priv->line`), as the other
   files of `/proc/net` do, and transfer as much as fits; the rest stays for 
the next read.
   
   <details>
   <summary>nuttx-procfs-net-tcp-overrun-repro.c (replaces 
apps/examples/hello/hello_main.c)</summary>
   
   ```c
   /* Reproducer: read() of /proc/net/tcp (or udp) with a small buffer writes
    * past the end of the buffer and returns more than was asked for.
    *
    * Drop-in replacement for apps/examples/hello/hello_main.c, run on
    * sim:tcploop with CONFIG_EXAMPLES_HELLO=y and CONFIG_NET_STATISTICS=y.
    */
   
   #include <nuttx/config.h>
   
   #include <stdio.h>
   #include <string.h>
   #include <fcntl.h>
   #include <unistd.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   
   #define ASKED 64    /* CONFIG_STDIO_BUFFER_SIZE: what fopen()/fgets() asks */
   #define GUARD 448
   
   static int check(const char *path)
   {
     static unsigned char buf[ASKED + GUARD];
     ssize_t n;
     int over;
     int fd;
   
     memset(buf, 0xa5, sizeof(buf));
     fd = open(path, O_RDONLY);
     if (fd < 0)
       {
         printf("%s: cannot open\n", path);
         return 0;
       }
   
     n = read(fd, buf, ASKED);
     close(fd);
   
     for (over = sizeof(buf); over > ASKED && buf[over - 1] == 0xa5; over--)
       {
       }
   
     printf("%s: asked for %d bytes, read() returned %zd, "
            "%d bytes written past the buffer\n",
            path, ASKED, n, over - ASKED);
     return n > ASKED || over > ASKED;
   }
   
   int main(int argc, char *argv[])
   {
     struct sockaddr_in6 sa;
     int bad = 0;
     int ts;
     int cs;
     int us;
   
     /* One TCP and one UDP connection, so that the tables are not empty */
   
     memset(&sa, 0, sizeof(sa));
     sa.sin6_family = AF_INET6;
     sa.sin6_port   = htons(8080);
     sa.sin6_addr   = in6addr_loopback;
   
     ts = socket(AF_INET6, SOCK_STREAM, 0);
     bind(ts, (struct sockaddr *)&sa, sizeof(sa));
     listen(ts, 1);
   
     cs = socket(AF_INET6, SOCK_STREAM, 0);
     connect(cs, (struct sockaddr *)&sa, sizeof(sa));
   
     us = socket(AF_INET6, SOCK_DGRAM, 0);
     bind(us, (struct sockaddr *)&sa, sizeof(sa));
   
     bad += check("/proc/net/tcp");
     bad += check("/proc/net/udp");
   
     close(cs);
     close(ts);
     close(us);
     printf("%s\n", bad ? "FAIL" : "PASS");
     return 0;
   }
   ```
   
   </details>
   
   *Disclosure: this report and the patch were prepared with the help of an AI 
agent (Claude Code,
   Anthropic); the failure and the fix were reproduced in the simulator and 
human-reviewed.*
   


-- 
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