This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new db2cf5b2f5d net/procfs: Do not write past the buffer of a read of
/proc/net/tcp or udp.
db2cf5b2f5d is described below
commit db2cf5b2f5d25d2846d3744425ec3d88cb5d2604
Author: Ricard Rosson <[email protected]>
AuthorDate: Thu Nov 26 22:23:12 2026 +0000
net/procfs: Do not write past the buffer of a read of /proc/net/tcp or udp.
netprocfs_read_tcpstats() and netprocfs_read_udpstats() wrote the
header of the table in the buffer of the caller with snprintf() and
went on with the length that it returns: the length of the whole
header, also when the buffer is shorter. The size left for the lines
of the connections, buflen - len, is unsigned and wrapped, so all the
lines were written past the end of the buffer, and the read returned
more than the caller asked for.
A read with a buffer shorter than the header (about 90 bytes) overran
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() and fgets() of /proc/net/tcp corrupted the heap. A buffer
that holds the header but not a whole line (less than 180 bytes with
IPv6, 120 without) got the header only, and then the end of the file.
Write each line in the line buffer of the open file, as the other
files of /proc/net do, and transfer as much of it as fits; the rest of
the line stays buffered for the next read. The line buffer grows to
the length of a line of these tables.
Assisted-by: Claude Code:claude-fable-5-2
Signed-off-by: Ricard Rosson <[email protected]>
---
net/procfs/net_procfs.c | 35 ++++++++++++++++++++++++
net/procfs/net_tcp.c | 71 +++++++++++++++++++++++++++++++------------------
net/procfs/net_udp.c | 71 +++++++++++++++++++++++++++++++------------------
net/procfs/procfs.h | 30 +++++++++++++++++++--
4 files changed, 153 insertions(+), 54 deletions(-)
diff --git a/net/procfs/net_procfs.c b/net/procfs/net_procfs.c
index a12c97192a7..a88c5801fb8 100644
--- a/net/procfs/net_procfs.c
+++ b/net/procfs/net_procfs.c
@@ -727,6 +727,41 @@ static int netprocfs_stat(FAR const char *relpath, FAR
struct stat *buf)
* Public Functions
****************************************************************************/
+/****************************************************************************
+ * Name: netprocfs_read_line
+ *
+ * Description:
+ * Transfer the rest of the line buffered in priv->line[] to the user
+ * buffer, as much as fits. What does not fit stays buffered for the
+ * next read.
+ *
+ * Input Parameters:
+ * priv - A reference to the network procfs file structure
+ * buffer - The user-provided buffer
+ * buflen - The size in bytes of the user provided buffer.
+ *
+ * Returned Value:
+ * The number of bytes transferred.
+ *
+ ****************************************************************************/
+
+size_t netprocfs_read_line(FAR struct netprocfs_file_s *priv,
+ FAR char *buffer, size_t buflen)
+{
+ size_t xfrsize = priv->linesize;
+
+ if (xfrsize > buflen)
+ {
+ xfrsize = buflen;
+ }
+
+ memcpy(buffer, &priv->line[priv->offset], xfrsize);
+
+ priv->linesize -= xfrsize;
+ priv->offset += xfrsize;
+ return xfrsize;
+}
+
/****************************************************************************
* Name: netprocfs_read_linegen
*
diff --git a/net/procfs/net_tcp.c b/net/procfs/net_tcp.c
index 0b930435cc7..152c072b2b2 100644
--- a/net/procfs/net_tcp.c
+++ b/net/procfs/net_tcp.c
@@ -46,20 +46,15 @@
#ifdef NET_TCP_HAVE_STACK
-#ifdef CONFIG_NET_IPv6
-# define TCP_LINELEN 180
-#else
-# define TCP_LINELEN 120
-#endif
-
/****************************************************************************
* Private Functions
****************************************************************************/
-static int netprocfs_tcpline(FAR struct netprocfs_file_s *priv,
- FAR char *buffer, size_t buflen,
- FAR struct tcp_conn_s *conn)
+static void netprocfs_tcpline(FAR struct netprocfs_file_s *priv,
+ FAR struct tcp_conn_s *conn)
{
+ FAR char *buffer = priv->line;
+ size_t buflen = NET_LINELEN;
uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6);
int addrlen = (domain == PF_INET) ?
INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
@@ -67,7 +62,7 @@ static int netprocfs_tcpline(FAR struct netprocfs_file_s
*priv,
char local[INET6_ADDRSTRLEN];
FAR void *laddr;
FAR void *raddr;
- int len;
+ size_t len;
laddr = net_ip_binding_laddr(&conn->u, domain);
raddr = net_ip_binding_raddr(&conn->u, domain);
@@ -95,16 +90,24 @@ static int netprocfs_tcpline(FAR struct netprocfs_file_s
*priv,
#endif
(conn->readahead) ? conn->readahead->io_pktlen : 0);
- len += snprintf(buffer + len, buflen - len,
- " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
- (domain == PF_INET6) ? addrlen / 2 : addrlen,
- inet_ntop(domain, laddr, local, addrlen),
- ntohs(conn->lport),
- (domain == PF_INET6) ? addrlen / 2 : addrlen,
- inet_ntop(domain, raddr, remote, addrlen),
- ntohs(conn->rport));
+ if (len < buflen)
+ {
+ len += snprintf(buffer + len, buflen - len,
+ " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
+ (domain == PF_INET6) ? addrlen / 2 : addrlen,
+ inet_ntop(domain, laddr, local, addrlen),
+ ntohs(conn->lport),
+ (domain == PF_INET6) ? addrlen / 2 : addrlen,
+ inet_ntop(domain, raddr, remote, addrlen),
+ ntohs(conn->rport));
+ }
- return len;
+ /* snprintf() returns the length of the whole line, also when it does
+ * not fit.
+ */
+
+ priv->linesize = len < buflen ? len : buflen - 1;
+ priv->offset = 0;
}
/****************************************************************************
@@ -164,10 +167,15 @@ static ssize_t netprocfs_tcpstats(FAR struct
netprocfs_file_s *priv,
FAR struct net_driver_s *held = NULL;
FAR struct net_driver_s *dev;
FAR struct tcp_conn_s *conn;
- int len = 0;
+ size_t len = 0;
+
+ /* A line is written in priv->line[] and as much of it as fits is
+ * transferred. If a part of it is left, the buffer is full: the next
+ * read takes that part first.
+ */
conn = netprocfs_tcpnext(priv, NULL);
- while (conn != NULL && buflen - len >= TCP_LINELEN)
+ while (conn != NULL && len < buflen)
{
/* The input path changes the read-ahead and the write queues of a
* connection with only its device locked, and it locks the
@@ -203,7 +211,8 @@ static ssize_t netprocfs_tcpstats(FAR struct
netprocfs_file_s *priv,
held = dev;
}
- len += netprocfs_tcpline(priv, buffer + len, buflen - len, conn);
+ netprocfs_tcpline(priv, conn);
+ len += netprocfs_read_line(priv, buffer + len, buflen - len);
priv->last = conn->listid;
priv->lines++;
conn = netprocfs_tcpnext(priv, conn);
@@ -243,7 +252,13 @@ ssize_t netprocfs_read_tcpstats(FAR struct
netprocfs_file_s *priv,
FAR char *buffer, size_t buflen)
{
FAR struct tcp_conn_s *conn;
- int len = 0;
+ size_t len;
+
+ /* First the rest of a line that did not fit in the buffer of the last
+ * read.
+ */
+
+ len = netprocfs_read_line(priv, buffer, buflen);
tcp_conn_list_lock();
@@ -257,7 +272,7 @@ ssize_t netprocfs_read_tcpstats(FAR struct netprocfs_file_s
*priv,
priv->last = conn->listid - 1;
priv->limit = tcp_conn_lastid();
- len = snprintf(buffer, buflen, "TCP sl "
+ len = snprintf(priv->line, NET_LINELEN, "TCP sl "
"st flg ref tmr uack nrt "
#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
"txsz "
@@ -271,10 +286,14 @@ ssize_t netprocfs_read_tcpstats(FAR struct
netprocfs_file_s *priv,
INET6_ADDRSTRLEN / 2,
"remote_address"
);
- priv->lines = 1;
+ priv->linesize = len < NET_LINELEN ? len : NET_LINELEN - 1;
+ priv->offset = 0;
+ priv->lines = 1;
+
+ len = netprocfs_read_line(priv, buffer, buflen);
}
- if (priv->lines > 0)
+ if (priv->lines > 0 && priv->linesize == 0)
{
len += netprocfs_tcpstats(priv, buffer + len, buflen - len);
}
diff --git a/net/procfs/net_udp.c b/net/procfs/net_udp.c
index e32d898bc52..15f43337042 100644
--- a/net/procfs/net_udp.c
+++ b/net/procfs/net_udp.c
@@ -45,20 +45,15 @@
#ifdef NET_UDP_HAVE_STACK
-#ifdef CONFIG_NET_IPv6
-# define UDP_LINELEN 180
-#else
-# define UDP_LINELEN 120
-#endif
-
/****************************************************************************
* Private Functions
****************************************************************************/
-static int netprocfs_udpline(FAR struct netprocfs_file_s *priv,
- FAR char *buffer, size_t buflen,
- FAR struct udp_conn_s *conn)
+static void netprocfs_udpline(FAR struct netprocfs_file_s *priv,
+ FAR struct udp_conn_s *conn)
{
+ FAR char *buffer = priv->line;
+ size_t buflen = NET_LINELEN;
uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6);
int addrlen = (domain == PF_INET) ?
INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
@@ -66,7 +61,7 @@ static int netprocfs_udpline(FAR struct netprocfs_file_s
*priv,
char local[INET6_ADDRSTRLEN];
FAR void *laddr;
FAR void *raddr;
- int len;
+ size_t len;
laddr = net_ip_binding_laddr(&conn->u, domain);
raddr = net_ip_binding_raddr(&conn->u, domain);
@@ -85,16 +80,24 @@ static int netprocfs_udpline(FAR struct netprocfs_file_s
*priv,
#endif
(conn->readahead) ? conn->readahead->io_pktlen : 0);
- len += snprintf(buffer + len, buflen - len,
- " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
- (domain == PF_INET6) ? addrlen / 2 : addrlen,
- inet_ntop(domain, laddr, local, addrlen),
- ntohs(conn->lport),
- (domain == PF_INET6) ? addrlen / 2 : addrlen,
- inet_ntop(domain, raddr, remote, addrlen),
- ntohs(conn->rport));
+ if (len < buflen)
+ {
+ len += snprintf(buffer + len, buflen - len,
+ " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
+ (domain == PF_INET6) ? addrlen / 2 : addrlen,
+ inet_ntop(domain, laddr, local, addrlen),
+ ntohs(conn->lport),
+ (domain == PF_INET6) ? addrlen / 2 : addrlen,
+ inet_ntop(domain, raddr, remote, addrlen),
+ ntohs(conn->rport));
+ }
- return len;
+ /* snprintf() returns the length of the whole line, also when it does
+ * not fit.
+ */
+
+ priv->linesize = len < buflen ? len : buflen - 1;
+ priv->offset = 0;
}
/****************************************************************************
@@ -116,7 +119,12 @@ static ssize_t netprocfs_udpstats(FAR struct
netprocfs_file_s *priv,
FAR char *buffer, size_t buflen)
{
FAR struct udp_conn_s *conn = NULL;
- int len = 0;
+ size_t len = 0;
+
+ /* A line is written in priv->line[] and as much of it as fits is
+ * transferred. If a part of it is left, the buffer is full: the next
+ * read takes that part first.
+ */
while ((conn = udp_nextconn(conn)) != NULL)
{
@@ -126,12 +134,13 @@ static ssize_t netprocfs_udpstats(FAR struct
netprocfs_file_s *priv,
continue;
}
- if (buflen - len < UDP_LINELEN)
+ if (len >= buflen)
{
break;
}
- len += netprocfs_udpline(priv, buffer + len, buflen - len, conn);
+ netprocfs_udpline(priv, conn);
+ len += netprocfs_read_line(priv, buffer + len, buflen - len);
priv->last = conn->listid;
priv->lines++;
}
@@ -165,7 +174,13 @@ ssize_t netprocfs_read_udpstats(FAR struct
netprocfs_file_s *priv,
FAR char *buffer, size_t buflen)
{
FAR struct udp_conn_s *conn;
- int len = 0;
+ size_t len;
+
+ /* First the rest of a line that did not fit in the buffer of the last
+ * read.
+ */
+
+ len = netprocfs_read_line(priv, buffer, buflen);
udp_conn_list_lock();
conn = udp_nextconn(NULL);
@@ -178,7 +193,7 @@ ssize_t netprocfs_read_udpstats(FAR struct netprocfs_file_s
*priv,
priv->last = conn->listid - 1;
priv->limit = udp_conn_lastid();
- len = snprintf(buffer, buflen, "UDP sl "
+ len = snprintf(priv->line, NET_LINELEN, "UDP sl "
" flg "
#if CONFIG_NET_SEND_BUFSIZE > 0
"txsz "
@@ -192,10 +207,14 @@ ssize_t netprocfs_read_udpstats(FAR struct
netprocfs_file_s *priv,
INET6_ADDRSTRLEN / 2,
"remote_address"
);
- priv->lines = 1;
+ priv->linesize = len < NET_LINELEN ? len : NET_LINELEN - 1;
+ priv->offset = 0;
+ priv->lines = 1;
+
+ len = netprocfs_read_line(priv, buffer, buflen);
}
- if (priv->lines > 0)
+ if (priv->lines > 0 && priv->linesize == 0)
{
len += netprocfs_udpstats(priv, buffer + len, buflen - len);
}
diff --git a/net/procfs/procfs.h b/net/procfs/procfs.h
index c8931afed12..c88d1160638 100644
--- a/net/procfs/procfs.h
+++ b/net/procfs/procfs.h
@@ -43,10 +43,15 @@
#endif
/* Determines the size of an intermediate buffer that must be large enough
- * to handle the longest line generated by this logic.
+ * to handle the longest line generated by this logic: a line of the TCP or
+ * of the UDP connection table, with two addresses.
*/
-#define NET_LINELEN 80
+#ifdef CONFIG_NET_IPv6
+# define NET_LINELEN 180
+#else
+# define NET_LINELEN 120
+#endif
/****************************************************************************
* Public Type Definitions
@@ -99,6 +104,27 @@ extern "C"
* Public Function Prototypes
****************************************************************************/
+/****************************************************************************
+ * Name: netprocfs_read_line
+ *
+ * Description:
+ * Transfer the rest of the line buffered in priv->line[] to the user
+ * buffer, as much as fits. What does not fit stays buffered for the
+ * next read.
+ *
+ * Input Parameters:
+ * priv - A reference to the network procfs file structure
+ * buffer - The user-provided buffer
+ * buflen - The size in bytes of the user provided buffer.
+ *
+ * Returned Value:
+ * The number of bytes transferred.
+ *
+ ****************************************************************************/
+
+size_t netprocfs_read_line(FAR struct netprocfs_file_s *priv,
+ FAR char *buffer, size_t buflen);
+
/****************************************************************************
* Name: netprocfs_read_linegen
*