[PATCH v7 1/7] pcapng: add length checks to string arguments
Stephen Hemminger
stephen at networkplumber.org
Fri Feb 13 20:18:18 CET 2026
The pcapng file format uses a 16-bit length field in the option
TLV (Type-Length-Value) encoding, limiting strings to UINT16_MAX
bytes.
Add validation for string arguments to prevent silent truncation
or buffer issues when callers pass excessively long strings.
Also update the Doxygen comments for rte_pcapng_add_interface()
and rte_pcapng_write_stats() to document return values.
Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
doc/guides/rel_notes/release_26_03.rst | 9 +++++++
lib/pcapng/rte_pcapng.c | 37 +++++++++++++++++++++++---
lib/pcapng/rte_pcapng.h | 11 ++++++--
3 files changed, 51 insertions(+), 6 deletions(-)
diff --git a/doc/guides/rel_notes/release_26_03.rst b/doc/guides/rel_notes/release_26_03.rst
index afdf1af06c..7a99420453 100644
--- a/doc/guides/rel_notes/release_26_03.rst
+++ b/doc/guides/rel_notes/release_26_03.rst
@@ -140,6 +140,15 @@ API Changes
* cfgfile: name must be less than CFG_NAME_LEN
and value must be less than CFG_VALUE_LEN.
+* **Updated the pcapng library.**
+
+ API functions now do more validation.
+
+ * The length of comment strings is now validated.
+ Maximum allowable length is 2^16-1 because of pcapng file format.
+ * Passing an invalid port id returns uses ENODEV rather than EINVAL
+ for consistency with ethdev API's.
+
ABI Changes
-----------
diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index 2cc9e2040d..4ccf833777 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -34,6 +34,9 @@
/* conversion from DPDK speed to PCAPNG */
#define PCAPNG_MBPS_SPEED 1000000ull
+/* upper bound for strings in pcapng option data */
+#define PCAPNG_STR_MAX UINT16_MAX
+
/* upper bound for section, stats and interface blocks (in uint32_t) */
#define PCAPNG_BLKSIZ (2048 / sizeof(uint32_t))
@@ -218,9 +221,11 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,
char ifname_buf[IF_NAMESIZE];
char ifhw[256];
uint64_t speed = 0;
+ int ret;
- if (rte_eth_dev_info_get(port, &dev_info) < 0)
- return -1;
+ ret = rte_eth_dev_info_get(port, &dev_info);
+ if (ret < 0)
+ return ret;
/* make something like an interface name */
if (ifname == NULL) {
@@ -230,8 +235,14 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,
snprintf(ifname_buf, IF_NAMESIZE, "dpdk:%u", port);
ifname = ifname_buf;
}
+ } else if (strlen(ifname) > PCAPNG_STR_MAX) {
+ return -EINVAL;
}
+ if ((ifdescr && strlen(ifdescr) > PCAPNG_STR_MAX) ||
+ (filter && strlen(filter) > PCAPNG_STR_MAX))
+ return -EINVAL;
+
/* make a useful device hardware string */
dev = dev_info.device;
if (dev)
@@ -269,7 +280,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,
len += sizeof(uint32_t);
if (len > sizeof(buf))
- return -1;
+ return -EINVAL;
hdr = (struct pcapng_interface_block *)buf;
*hdr = (struct pcapng_interface_block) {
@@ -334,7 +345,10 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,
uint32_t optlen, len;
uint32_t buf[PCAPNG_BLKSIZ];
- RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
+ RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+
+ if (comment && strlen(comment) > PCAPNG_STR_MAX)
+ return -EINVAL;
optlen = 0;
@@ -487,7 +501,14 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue,
bool rss_hash;
#ifdef RTE_LIBRTE_ETHDEV_DEBUG
+ /*
+ * Since this function is used in the fast path for packet capture
+ * skip argument validation checks unless debug is enabled.
+ */
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
+
+ if (comment && strlen(comment) > PCAPNG_STR_MAX)
+ return NULL;
#endif
orig_len = rte_pktmbuf_pkt_len(md);
@@ -692,6 +713,14 @@ rte_pcapng_fdopen(int fd,
struct timespec ts;
uint64_t cycles;
+ if ((osname && strlen(osname) > PCAPNG_STR_MAX) ||
+ (hardware && strlen(hardware) > PCAPNG_STR_MAX) ||
+ (appname && strlen(appname) > PCAPNG_STR_MAX) ||
+ (comment && strlen(comment) > PCAPNG_STR_MAX)) {
+ rte_errno = EINVAL;
+ return NULL;
+ }
+
self = malloc(sizeof(*self));
if (!self) {
rte_errno = ENOMEM;
diff --git a/lib/pcapng/rte_pcapng.h b/lib/pcapng/rte_pcapng.h
index de1bf953e9..2dd438842d 100644
--- a/lib/pcapng/rte_pcapng.h
+++ b/lib/pcapng/rte_pcapng.h
@@ -89,6 +89,10 @@ rte_pcapng_close(rte_pcapng_t *self);
* Interfaces must be added to the output file after opening
* and before any packet record. All ports used in packet capture
* must be added.
+ *
+ * @return
+ * - returns number of bytes written on success,
+ * or negative errno on failure.
*/
int
rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,
@@ -128,7 +132,7 @@ enum rte_pcapng_direction {
*
* @return
* - The pointer to the new mbuf formatted for pcapng_write
- * - NULL if allocation fails.
+ * - NULL on error such as invalid port or out of memory.
*/
struct rte_mbuf *
rte_pcapng_copy(uint16_t port_id, uint32_t queue,
@@ -192,7 +196,10 @@ rte_pcapng_write_packets(rte_pcapng_t *self,
* @param comment
* Optional comment to add to statistics.
* @return
- * number of bytes written to file, -1 on failure to write file
+ * On success number of bytes written to file,
+ * -1 on failure to write file (and errno is set)
+ * - (-ENODEV) if *port_id* is invalid.
+ * - (-EINVAL) if bad parameter
*/
ssize_t
rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port,
--
2.51.0
More information about the dev
mailing list