xsk: reject undersized tx_metadata at runtime validation
Subsystems (9)
PATCH v2 1/1 xsk: reject undersized tx_metadata at runtime validation
There is no metadata length check before accessing struct xsk_tx_metadata fields in both xdp_umem_reg() (registration) and xsk_buff_valid_tx_metadata() (runtime). Thus, an invalid small tx_metadata_len (e.g. 8 bytes) passes registration and later causes an out-of-bounds read when xsk_skb_metadata() accesses fields beyond the declared metadata region.
KASAN reports this as:
BUG: KASAN: vmalloc-out-of-bounds in xsk_skb_metadata+0x4b2/0x500 Read of size 8 at addr ffffc90000f11000 by task exploit/148
xsk_skb_metadata (net/xdp/xsk.c:837)
xsk_build_skb (net/xdp/xsk.c)
__xsk_generic_xmit (net/xdp/xsk.c)
xsk_sendmsg (net/xdp/xsk.c)
Fixing at registration time would break backward compatibility with older userspace applications, so apply a runtime length check in xsk_buff_valid_tx_metadata() that validates tx_metadata_len against the fields required by the enabled flags.
---
V2:
- Change to runtime per-flag length check as suggested by Stanislav.
include/net/xdp_sock_drv.h | 22 ++++++++++++++++++---- net/xdp/xsk.c | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-)
include/net/xdp_sock_drv.h +18 -4
@@ -260,9 +260,22 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 0)static inline bool -xsk_buff_valid_tx_metadata (const struct xsk_tx_metadata *meta) +xsk_buff_valid_tx_metadata (const struct xsk_buff_pool *pool, +const struct xsk_tx_metadata *meta) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); +/* covers flags, XDP_TXMD_FLAGS_CHECKSUM & XDP_TXMD_FLAGS_TIMESTAMP */ +if (unlikely (pool->tx_metadata_len <16 )) +return false; + +if (unlikely (meta->flags & ~XDP_TXMD_FLAGS_VALID )) +return false; + +if (meta->flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) +if (unlikely (pool->tx_metadata_len < + offsetofend(struct xsk_tx_metadata, request.launch_time))) +return false; + +return true; } static inline struct xsk_tx_metadata *
@@ -274,7 +287,7 @@ __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data)return NULL ; meta = data - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) +if (unlikely (!xsk_buff_valid_tx_metadata (pool, meta)))return NULL ;/* no way to signal the error to the user */ return meta;
@@ -469,7 +482,8 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)return (struct xdp_desc_ctx ){ }; } -static inline bool xsk_buff_valid_tx_metadata (struct xsk_tx_metadata *meta) +static inline bool xsk_buff_valid_tx_metadata (const struct xsk_buff_pool *pool, +struct xsk_tx_metadata *meta) {return false; }
net/xdp/xsk.c +1 -1
@@ -809,7 +809,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,return -EINVAL ; meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) +if (unlikely (!xsk_buff_valid_tx_metadata (pool, meta)))return -EINVAL ;if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) {
Signature · 1 line
2.53.0
Quoted 22 lines · Cen Zhang (Microsoft)
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.KASAN reports this as:
BUG: KASAN: vmalloc-out-of-bounds in xsk_skb_metadata+0x4b2/0x500 Read of size 8 at addr ffffc90000f11000 by task exploit/148
xsk_skb_metadata(net/xdp/xsk.c:837)
xsk_build_skb(net/xdp/xsk.c)
__xsk_generic_xmit(net/xdp/xsk.c)
xsk_sendmsg(net/xdp/xsk.c)Fixing at registration time would break backward compatibility with older userspace applications, so apply a runtime length check in
xsk_buff_valid_tx_metadata()that validatestx_metadata_lenagainst the fields required by the enabled flags.
Quoted 8 lines
Fixes 341ac980eab9 ("xsk: Support tx_metadata_len")Reported-by [email protected]Suggested-by Stanislav Fomichev <[email protected]>Signed-off-by Cen Zhang (Microsoft) <[email protected]>---
V2:
- Change to runtime per-flag length check as suggested by Stanislav.
Hmm, the AI review points out another set of TOCTOU issues :-/ I wonder whether want to go all in and rewrite the users to have a copy of metadata? Something like the following below, build tested only, any issues with that?
(xsk_tx_metadata_to_compl (!meta) check is still buggy? not sure)
drivers/net/ethernet/intel/igc/igc.h +1 -1
@@ -604,7 +604,7 @@ struct igc_xdp_buff {struct igc_metadata_request {struct igc_tx_buffer *tx_buffer; -struct xsk_tx_metadata *meta; +struct xsk_tx_metadata meta;struct igc_ring *tx_ring;u32 cmd_type;u16 used_desc;
drivers/net/ethernet/intel/igc/igc_main.c +4 -4
@@ -2961,7 +2961,7 @@ static void igc_xsk_request_timestamp(void *_priv)* metadata area. It is the location to store the value of * tx hardware timestamp. */ -xsk_tx_metadata_to_compl (meta_req->meta, &tstamp->xsk_meta); +xsk_tx_metadata_to_compl (&meta_req->meta, &tstamp->xsk_meta);/* Set timestamp bit based on the _TSTAMP(_X) bit. */ tx_flags |= tstamp->flags;
@@ -3059,7 +3059,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring)*/ while (budget >=4 &&xsk_tx_peek_desc (pool, &xdp_desc)) {struct igc_metadata_request meta_req; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};struct igc_tx_buffer *bi;u32 olinfo_status;dma_addr_t dma;
@@ -3071,7 +3071,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring) olinfo_status = xdp_desc.len <<IGC_ADVTXD_PAYLEN_SHIFT ; dma =xsk_buff_raw_get_dma (pool, xdp_desc.addr); - meta =xsk_buff_get_metadata (pool, xdp_desc.addr); +xsk_buff_read_metadata (pool, xdp_desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, dma, xdp_desc.len); bi = &ring->tx_buffer_info[ntu];
@@ -3079,7 +3079,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring) meta_req.tx_buffer = bi; meta_req.meta = meta; meta_req.used_desc =0 ; -xsk_tx_metadata_request (meta, &igc_xsk_tx_metadata_ops, +xsk_tx_metadata_request (&meta, &igc_xsk_tx_metadata_ops, &meta_req);/* xsk_tx_metadata_request() may have updated next_to_use */
drivers/net/ethernet/mellanox/mlx5/core/en/xsk/tx.c +4 -4
@@ -66,7 +66,7 @@ static void mlx5e_xsk_tx_post_err(struct mlx5e_xdpsq *sq,bool mlx5e_xsk_tx (struct mlx5e_xdpsq *sq,unsigned int budget) {struct xsk_buff_pool *pool = sq->xsk_pool; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};union mlx5e_xdp_info xdpi;bool work_done = true;bool flush = false;
@@ -99,13 +99,13 @@ bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget) xdptxd.dma_addr =xsk_buff_raw_get_dma (pool, desc.addr); xdptxd.data =xsk_buff_raw_get_data (pool, desc.addr); xdptxd.len = desc.len; - meta =xsk_buff_get_metadata (pool, desc.addr); +xsk_buff_read_metadata (pool, desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, xdptxd.dma_addr, xdptxd.len); ret =INDIRECT_CALL_2 (sq->xmit_xdp_frame, mlx5e_xmit_xdp_frame_mpwqe, mlx5e_xmit_xdp_frame, sq, &xdptxd, - check_result, meta); + check_result, &meta);if (unlikely (!ret)) {if (sq->mpwqe.wqe)mlx5e_xdp_mpwqe_complete (sq);
@@ -116,7 +116,7 @@ bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget)if (xp_tx_metadata_enabled (sq->xsk_pool)) {struct xsk_tx_metadata_compl compl; -xsk_tx_metadata_to_compl (meta, &compl); +xsk_tx_metadata_to_compl (&meta, &compl);XSK_TX_COMPL_FITS (void *); mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo,
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +4 -4
@@ -2707,7 +2707,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)for (; budget >0 ; budget--) {struct stmmac_metadata_request meta_req; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};dma_addr_t dma_addr;bool set_ic;
@@ -2732,7 +2732,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) tx_desc =stmmac_get_tx_desc (priv, tx_q, entry); dma_addr =xsk_buff_raw_get_dma (pool, xdp_desc.addr); - meta =xsk_buff_get_metadata (pool, xdp_desc.addr); +xsk_buff_read_metadata (pool, xdp_desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, dma_addr, xdp_desc.len);/* To return XDP buffer to XSK pool, we simple call
@@ -2761,7 +2761,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) meta_req.set_ic = &set_ic; meta_req.tbs = tx_q->tbs; meta_req.edesc = &tx_q->dma_entx[entry]; -xsk_tx_metadata_request (meta, &stmmac_xsk_tx_metadata_ops, +xsk_tx_metadata_request (&meta, &stmmac_xsk_tx_metadata_ops, &meta_req);if (set_ic) { tx_q->tx_count_frames =0 ;
@@ -2775,7 +2775,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)stmmac_enable_dma_transmission (priv, priv->ioaddr, queue); -xsk_tx_metadata_to_compl (meta, +xsk_tx_metadata_to_compl (&meta, &tx_q->tx_skbuff_dma[entry].xsk_meta); tx_q->cur_tx =STMMAC_NEXT_ENTRY (tx_q->cur_tx, priv->dma_conf.dma_tx_size);
include/net/libeth/xsk.h +1 -1
@@ -205,7 +205,7 @@ __libeth_xsk_xmit_fill_buf_md(const struct xdp_desc *xdesc,BUILD_BUG_ON (!__builtin_constant_p (tmo == libeth_xsktmo)); tmo = tmo == libeth_xsktmo ? &__libeth_xsktmo : tmo; -xsk_tx_metadata_request (ctx.meta, tmo, &desc); +xsk_tx_metadata_request (&ctx.meta, tmo, &desc);return desc; }
include/net/xdp_sock.h +1 -1
@@ -163,7 +163,7 @@ static inline void xsk_tx_metadata_request(const struct xsk_tx_metadata *meta, const struct xsk_tx_metadata_ops *ops, void *priv) { - if (!meta) + if (!meta || !meta->flags) return;if (ops->tmo_request_launch_time)
include/net/xdp_sock_drv.h +35 -27
87 lines not on this page · raw message
include/net/xsk_buff_pool.h +2 -2
@@ -141,12 +141,12 @@ void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);struct xdp_buff *xp_alloc (struct xsk_buff_pool *pool);u32 xp_alloc_batch (struct xsk_buff_pool *pool,struct xdp_buff **xdp,u32 max);bool xp_can_alloc (struct xsk_buff_pool *pool,u32 count); -void *xp_raw_get_data (struct xsk_buff_pool *pool,u64 addr); +void *xp_raw_get_data (const struct xsk_buff_pool *pool,u64 addr);dma_addr_t xp_raw_get_dma (struct xsk_buff_pool *pool,u64 addr);struct xdp_desc_ctx {dma_addr_t dma; -struct xsk_tx_metadata *meta; +struct xsk_tx_metadata meta; };struct xdp_desc_ctx xp_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr);
net/xdp/xsk.c +11 -13
@@ -802,19 +802,17 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, struct xdp_desc *desc, struct xsk_buff_pool *pool, u32 hr) { - struct xsk_tx_metadata *meta = NULL; + struct xsk_tx_metadata meta = {};u16 csum_start, csum_offset; +int ret; -if (unlikely (pool->tx_metadata_len ==0 )) -return -EINVAL ; - - meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) -return -EINVAL ; + ret =xsk_buff_read_tx_metadata (pool, buffer, &meta); +if (ret <0 ) +return ret; -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) { - csum_start =READ_ONCE (meta->request.csum_start); - csum_offset =READ_ONCE (meta->request.csum_offset); +if (meta.flags &XDP_TXMD_FLAGS_CHECKSUM ) { + csum_start =READ_ONCE (meta.request.csum_start); + csum_offset =READ_ONCE (meta.request.csum_offset);if (unlikely (csum_start + csum_offset +sizeof (__sum16) > desc->len))
@@ -833,9 +831,9 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, } } -if (meta->flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) - skb->skb_mstamp_ns = meta->request.launch_time; -xsk_tx_metadata_to_compl (meta, &skb_shinfo (skb)->xsk_meta); +if (meta.flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) + skb->skb_mstamp_ns = meta.request.launch_time; +xsk_tx_metadata_to_compl (&meta, &skb_shinfo (skb)->xsk_meta);return 0 ; }
net/xdp/xsk_buff_pool.c +2 -2
@@ -735,7 +735,7 @@ static void *__xp_raw_get_data(const struct xsk_buff_pool *pool, u64 addr)return pool->addrs + addr; } -void *xp_raw_get_data (struct xsk_buff_pool *pool,u64 addr) +void *xp_raw_get_data (const struct xsk_buff_pool *pool,u64 addr) {return __xp_raw_get_data (pool,__xp_raw_get_addr (pool, addr)); }
@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr); ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);return ret; }
Maciej Fijalkowski
maintainerQuoted 32 lines · Stanislav Fomichev
Quoted 22 lines · Cen Zhang (Microsoft)
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.KASAN reports this as:
BUG: KASAN: vmalloc-out-of-bounds in xsk_skb_metadata+0x4b2/0x500 Read of size 8 at addr ffffc90000f11000 by task exploit/148
xsk_skb_metadata(net/xdp/xsk.c:837)
xsk_build_skb(net/xdp/xsk.c)
__xsk_generic_xmit(net/xdp/xsk.c)
xsk_sendmsg(net/xdp/xsk.c)Fixing at registration time would break backward compatibility with older userspace applications, so apply a runtime length check in
xsk_buff_valid_tx_metadata()that validatestx_metadata_lenagainst the fields required by the enabled flags.Quoted 8 lines
Fixes 341ac980eab9 ("xsk: Support tx_metadata_len")Reported-by [email protected]Suggested-by Stanislav Fomichev <[email protected]>Signed-off-by Cen Zhang (Microsoft) <[email protected]>---
V2:
- Change to runtime per-flag length check as suggested by Stanislav.
Quoted 4 lines
Hmm, the AI review points out another set of TOCTOU issues :-/ I wonder whether want to go all in and rewrite the users to have a copy of metadata? Something like the following below, build tested only, any issues with that?
Maybe you want to take ownership of this patch and run the proposed resolution against internal AI review?
Quoted 339 lines
(
xsk_tx_metadata_to_compl(!meta) check is still buggy? not sure)drivers/net/ethernet/intel/igc/igc.h +1
-1@@ -604,7 +604,7 @@ struct igc_xdp_buff {struct igc_metadata_request {struct igc_tx_buffer *tx_buffer; -struct xsk_tx_metadata *meta; +struct xsk_tx_metadata meta;struct igc_ring *tx_ring;u32 cmd_type;u16 used_desc;drivers/net/ethernet/intel/igc/igc_main.c +4
-4@@ -2961,7 +2961,7 @@ static void igc_xsk_request_timestamp(void *_priv)* metadata area. It is the location to store the value of * tx hardware timestamp. */ -xsk_tx_metadata_to_compl (meta_req->meta, &tstamp->xsk_meta); +xsk_tx_metadata_to_compl (&meta_req->meta, &tstamp->xsk_meta);/* Set timestamp bit based on the _TSTAMP(_X) bit. */ tx_flags |= tstamp->flags;@@ -3059,7 +3059,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring)*/ while (budget >=4 &&xsk_tx_peek_desc (pool, &xdp_desc)) {struct igc_metadata_request meta_req; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};struct igc_tx_buffer *bi;u32 olinfo_status;dma_addr_t dma;@@ -3071,7 +3071,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring) olinfo_status = xdp_desc.len <<IGC_ADVTXD_PAYLEN_SHIFT ; dma =xsk_buff_raw_get_dma (pool, xdp_desc.addr); - meta =xsk_buff_get_metadata (pool, xdp_desc.addr); +xsk_buff_read_metadata (pool, xdp_desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, dma, xdp_desc.len); bi = &ring->tx_buffer_info[ntu];@@ -3079,7 +3079,7 @@ static void igc_xdp_xmit_zc(struct igc_ring *ring) meta_req.tx_buffer = bi; meta_req.meta = meta; meta_req.used_desc =0 ; -xsk_tx_metadata_request (meta, &igc_xsk_tx_metadata_ops, +xsk_tx_metadata_request (&meta, &igc_xsk_tx_metadata_ops, &meta_req);/* xsk_tx_metadata_request() may have updated next_to_use */ drivers/net/ethernet/mellanox/mlx5/core/en/xsk/tx.c +4
-4@@ -66,7 +66,7 @@ static void mlx5e_xsk_tx_post_err(struct mlx5e_xdpsq *sq,bool mlx5e_xsk_tx (struct mlx5e_xdpsq *sq,unsigned int budget) {struct xsk_buff_pool *pool = sq->xsk_pool; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};union mlx5e_xdp_info xdpi;bool work_done = true;bool flush = false;@@ -99,13 +99,13 @@ bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget) xdptxd.dma_addr =xsk_buff_raw_get_dma (pool, desc.addr); xdptxd.data =xsk_buff_raw_get_data (pool, desc.addr); xdptxd.len = desc.len; - meta =xsk_buff_get_metadata (pool, desc.addr); +xsk_buff_read_metadata (pool, desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, xdptxd.dma_addr, xdptxd.len); ret =INDIRECT_CALL_2 (sq->xmit_xdp_frame, mlx5e_xmit_xdp_frame_mpwqe, mlx5e_xmit_xdp_frame, sq, &xdptxd, - check_result, meta); + check_result, &meta);if (unlikely (!ret)) {if (sq->mpwqe.wqe)mlx5e_xdp_mpwqe_complete (sq);@@ -116,7 +116,7 @@ bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget)if (xp_tx_metadata_enabled (sq->xsk_pool)) {struct xsk_tx_metadata_compl compl; -xsk_tx_metadata_to_compl (meta, &compl); +xsk_tx_metadata_to_compl (&meta, &compl);XSK_TX_COMPL_FITS (void *); mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo,drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +4
-4@@ -2707,7 +2707,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)for (; budget >0 ; budget--) {struct stmmac_metadata_request meta_req; -struct xsk_tx_metadata *meta =NULL ; +struct xsk_tx_metadata meta = {};dma_addr_t dma_addr;bool set_ic;@@ -2732,7 +2732,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) tx_desc =stmmac_get_tx_desc (priv, tx_q, entry); dma_addr =xsk_buff_raw_get_dma (pool, xdp_desc.addr); - meta =xsk_buff_get_metadata (pool, xdp_desc.addr); +xsk_buff_read_metadata (pool, xdp_desc.addr, &meta);xsk_buff_raw_dma_sync_for_device (pool, dma_addr, xdp_desc.len);/* To return XDP buffer to XSK pool, we simple call @@ -2761,7 +2761,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) meta_req.set_ic = &set_ic; meta_req.tbs = tx_q->tbs; meta_req.edesc = &tx_q->dma_entx[entry]; -xsk_tx_metadata_request (meta, &stmmac_xsk_tx_metadata_ops, +xsk_tx_metadata_request (&meta, &stmmac_xsk_tx_metadata_ops, &meta_req);if (set_ic) { tx_q->tx_count_frames =0 ;@@ -2775,7 +2775,7 @@ static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)stmmac_enable_dma_transmission (priv, priv->ioaddr, queue); -xsk_tx_metadata_to_compl (meta, +xsk_tx_metadata_to_compl (&meta, &tx_q->tx_skbuff_dma[entry].xsk_meta); tx_q->cur_tx =STMMAC_NEXT_ENTRY (tx_q->cur_tx, priv->dma_conf.dma_tx_size);include/net/libeth/xsk.h +1
-1@@ -205,7 +205,7 @@ __libeth_xsk_xmit_fill_buf_md(const struct xdp_desc *xdesc,BUILD_BUG_ON (!__builtin_constant_p (tmo == libeth_xsktmo)); tmo = tmo == libeth_xsktmo ? &__libeth_xsktmo : tmo; -xsk_tx_metadata_request (ctx.meta, tmo, &desc); +xsk_tx_metadata_request (&ctx.meta, tmo, &desc);return desc; }include/net/xdp_sock.h +1
-1@@ -163,7 +163,7 @@ static inline void xsk_tx_metadata_request(const struct xsk_tx_metadata *meta, const struct xsk_tx_metadata_ops *ops, void *priv) { - if (!meta) + if (!meta || !meta->flags) return;if (ops->tmo_request_launch_time)include/net/xdp_sock_drv.h +35
-27@@ -259,31 +259,42 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) XDP_TXMD_FLAGS_LAUNCH_TIME | \ 0) -static inline bool -xsk_buff_valid_tx_metadata (const struct xsk_tx_metadata *meta) +static inline int +xsk_buff_read_tx_metadata (const struct xsk_buff_pool *pool, +void *data, +struct xsk_tx_metadata *meta) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); -} - -static inline struct xsk_tx_metadata * -__xsk_buff_get_metadata (const struct xsk_buff_pool *pool,void *data) -{ -struct xsk_tx_metadata *meta; +struct xsk_tx_metadata *p; +u64 flags =0 ;if (!pool->tx_metadata_len) -return NULL ; +return 0 ; + +/* covers flags, XDP_TXMD_FLAGS_CHECKSUM & XDP_TXMD_FLAGS_TIMESTAMP */ +if (unlikely (pool->tx_metadata_len <16 )) +return -EINVAL ; + + p = data - pool->tx_metadata_len; + flags =READ_ONCE (p->flags); + +if (flags & ~XDP_TXMD_FLAGS_VALID ) +return -EINVAL ; - meta = data - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) -return NULL ;/* no way to signal the error to the user */ +if (flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) + if (pool->tx_metadata_len < offsetofend(struct xsk_tx_metadata, request.launch_time)) +return -EINVAL ; -return meta; +memcpy (meta, p,min (pool->tx_metadata_len,sizeof (struct xsk_tx_metadata ))); + meta->flags = flags; +return 0 ; } -static inline struct xsk_tx_metadata * -xsk_buff_get_metadata (struct xsk_buff_pool *pool,u64 addr) +static inline void xsk_buff_read_metadata (const struct xsk_buff_pool *pool, +u64 addr, +struct xsk_tx_metadata *meta) { -return __xsk_buff_get_metadata (pool,xp_raw_get_data (pool, addr)); +if (xsk_buff_read_tx_metadata (pool,xp_raw_get_data (pool, addr), meta) <0 ) + meta->flags =0 ; }static inline void xsk_buff_dma_sync_for_cpu (struct xdp_buff *xdp)@@ -469,21 +480,18 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)return (struct xdp_desc_ctx ){ }; } -static inline bool xsk_buff_valid_tx_metadata (struct xsk_tx_metadata *meta) +static inline int +xsk_buff_read_tx_metadata (const struct xsk_buff_pool *pool, +void *data, +struct xsk_tx_metadata *meta) {return false; } -static inline struct xsk_tx_metadata * -__xsk_buff_get_metadata (const struct xsk_buff_pool *pool,void *data) -{ -return NULL ; -} - -static inline struct xsk_tx_metadata * -xsk_buff_get_metadata (struct xsk_buff_pool *pool,u64 addr) +static inline void xsk_buff_read_metadata (const struct xsk_buff_pool *pool, +u64 addr, +struct xsk_tx_metadata *meta) { -return NULL ; }static inline void xsk_buff_dma_sync_for_cpu (struct xdp_buff *xdp)include/net/xsk_buff_pool.h +2
-2@@ -141,12 +141,12 @@ void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);struct xdp_buff *xp_alloc (struct xsk_buff_pool *pool);u32 xp_alloc_batch (struct xsk_buff_pool *pool,struct xdp_buff **xdp,u32 max);bool xp_can_alloc (struct xsk_buff_pool *pool,u32 count); -void *xp_raw_get_data (struct xsk_buff_pool *pool,u64 addr); +void *xp_raw_get_data (const struct xsk_buff_pool *pool,u64 addr);dma_addr_t xp_raw_get_dma (struct xsk_buff_pool *pool,u64 addr);struct xdp_desc_ctx {dma_addr_t dma; -struct xsk_tx_metadata *meta; +struct xsk_tx_metadata meta; };struct xdp_desc_ctx xp_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr);net/xdp/xsk.c +11
-13@@ -802,19 +802,17 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, struct xdp_desc *desc, struct xsk_buff_pool *pool, u32 hr) { - struct xsk_tx_metadata *meta = NULL; + struct xsk_tx_metadata meta = {};u16 csum_start, csum_offset; +int ret; -if (unlikely (pool->tx_metadata_len ==0 )) -return -EINVAL ; - - meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) -return -EINVAL ; + ret =xsk_buff_read_tx_metadata (pool, buffer, &meta); +if (ret <0 ) +return ret; -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) { - csum_start =READ_ONCE (meta->request.csum_start); - csum_offset =READ_ONCE (meta->request.csum_offset); +if (meta.flags &XDP_TXMD_FLAGS_CHECKSUM ) { + csum_start =READ_ONCE (meta.request.csum_start); + csum_offset =READ_ONCE (meta.request.csum_offset);if (unlikely (csum_start + csum_offset +sizeof (__sum16) > desc->len))@@ -833,9 +831,9 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, } } -if (meta->flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) - skb->skb_mstamp_ns = meta->request.launch_time; -xsk_tx_metadata_to_compl (meta, &skb_shinfo (skb)->xsk_meta); +if (meta.flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) + skb->skb_mstamp_ns = meta.request.launch_time; +xsk_tx_metadata_to_compl (&meta, &skb_shinfo (skb)->xsk_meta);return 0 ; }net/xdp/xsk_buff_pool.c +2
-2@@ -735,7 +735,7 @@ static void *__xp_raw_get_data(const struct xsk_buff_pool *pool, u64 addr)return pool->addrs + addr; } -void *xp_raw_get_data (struct xsk_buff_pool *pool,u64 addr) +void *xp_raw_get_data (const struct xsk_buff_pool *pool,u64 addr) {return __xp_raw_get_data (pool,__xp_raw_get_addr (pool, addr)); }@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr); ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);return ret; }
Quoted 4 lines · Stanislav Fomichev
Hmm, the AI review points out another set of TOCTOU issues :-/ I wonder whether want to go all in and rewrite the users to have a copy of metadata? Something like the following below, build tested only, any issues with that?
I applied your proposed patch and tested it against my local reproducer -- the bug is gone.
Quoted 2 lines
Maybe you want to take ownership of this patch and run the proposed resolution against internal AI review?
Please go ahead and carry this forward. Happy to help test further if needed.
Maciej Fijalkowski
maintainerQuoted 6 lines · Cen Zhang
Quoted 4 lines · Stanislav Fomichev
Hmm, the AI review points out another set of TOCTOU issues :-/ I wonder whether want to go all in and rewrite the users to have a copy of metadata? Something like the following below, build tested only, any issues with that?
Quoted 8 lines
I applied your proposed patch and tested it against my local reproducer -- the bug is gone.
Quoted 2 lines
Maybe you want to take ownership of this patch and run the proposed resolution against internal AI review?
Please go ahead and carry this forward. Happy to help test further if needed.
Let's see whether Stan is ok with that, you can get credit via
Reported-by: tag or you can continue working on this, that was only a suggestion, so I hope you did not find it offensive.
Not at all! Thanks for both your suggestion and Stanislav's rewrite!
Quoted 1 line · Cen Zhang
Not at all! Thanks for both your suggestion and Stanislav's rewrite!
SG, I can take it over..
Alexander Lobakin
maintainerFrom: Stanislav Fomichev <[email protected]>
Date: Wed, 22 Jul 2026 10:55:17 -0700
Quoted 7 lines · Cen Zhang (Microsoft)
Quoted 6 lines
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.
[...]
net/xdp/xsk_buff_pool.c struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 776
Quoted 6 lines
net/xdp/xsk_buff_pool.c +1
-1@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr); ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);
That effectively disables some micro-optimizations that were the reason I created this helper -- __xp_raw_get_data() is inline, while xsk_buff_read_metadata() uses the external version, probably some other stuff (note the underscored __xsk_buff_get_metadata()) =\ This function expanded to have no external calls on clang -O2, with this patch I'm not sure it would.
net/xdp/xsk_buff_pool.c struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 779
Quoted 3 lines
net/xdp/xsk_buff_pool.c +0
-0return ret; }
Thanks,
Olek
Quoted 16 lines · Alexander Lobakin
From: Stanislav Fomichev <[email protected]>
Date: Wed, 22 Jul 2026 10:55:17 -0700Quoted 7 lines · Cen Zhang (Microsoft)
Quoted 6 lines
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.[...]
Quoted 3 lines
net/xdp/xsk_buff_pool.c +0
-0@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr);
Quoted 10 lines
net/xdp/xsk_buff_pool.c struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 776
Quoted 3 lines
net/xdp/xsk_buff_pool.c +1
-1ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);That effectively disables some micro-optimizations that were the reason I created this helper --
__xp_raw_get_data()is inline, whilexsk_buff_read_metadata()uses the external version, probably some other stuff (note the underscored__xsk_buff_get_metadata()) =\ This function expanded to have no external calls on clang -O2, with this patch I'm not sure it would.
Any better ideas on how to solve it? I can convert all non libeth stuff to these new xsk_buff_read_metadata helpers and keep xp_raw_get_ctx to you? Will that work?
Alexander Lobakin
maintainerFrom: Stanislav Fomichev <[email protected]>
Date: Thu, 23 Jul 2026 10:15:15 -0700
Quoted 28 lines · Alexander Lobakin
Quoted 16 lines
From: Stanislav Fomichev <[email protected]>
Date: Wed, 22 Jul 2026 10:55:17 -0700Quoted 7 lines · Cen Zhang (Microsoft)
Quoted 6 lines
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.[...]
Quoted 3 lines
net/xdp/xsk_buff_pool.c +0
-0@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr);Quoted 10 lines
net/xdp/xsk_buff_pool.c struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 776
Quoted 3 lines
net/xdp/xsk_buff_pool.c +1
-1ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);That effectively disables some micro-optimizations that were the reason I created this helper --
__xp_raw_get_data()is inline, whilexsk_buff_read_metadata()uses the external version, probably some other stuff (note the underscored__xsk_buff_get_metadata()) =\ This function expanded to have no external calls on clang -O2, with this patch I'm not sure it would.
Quoted 3 lines
Any better ideas on how to solve it? I can convert all non libeth stuff to these new
xsk_buff_read_metadatahelpers and keepxp_raw_get_ctxto you? Will that work?
Sure, I can take a look.
Do you want me to pick this patch as a base or you'd like to send a new rev?
Thanks,
Olek
Quoted 31 lines · Alexander Lobakin
From: Stanislav Fomichev <[email protected]>
Date: Thu, 23 Jul 2026 10:15:15 -0700Quoted 28 lines · Alexander Lobakin
Quoted 26 lines
From: Stanislav Fomichev <[email protected]>
Date: Wed, 22 Jul 2026 10:55:17 -0700Quoted 7 lines · Cen Zhang (Microsoft)
Quoted 6 lines
There is no metadata length check before accessing struct
xsk_tx_metadatafields in bothxdp_umem_reg()(registration) andxsk_buff_valid_tx_metadata()(runtime). Thus, an invalid smalltx_metadata_len(e.g. 8 bytes) passes registration and later causes an out-of-bounds read whenxsk_skb_metadata()accesses fields beyond the declared metadata region.[...]
net/xdp/xsk_buff_pool.c struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 776
Quoted 6 lines
net/xdp/xsk_buff_pool.c +1
-1@@ -773,7 +773,7 @@ struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) addr =__xp_raw_get_addr (pool, addr); ret.dma =__xp_raw_get_dma (pool, addr); - ret.meta =__xsk_buff_get_metadata (pool,__xp_raw_get_data (pool, addr)); +xsk_buff_read_metadata (pool, addr, &ret.meta);That effectively disables some micro-optimizations that were the reason I created this helper --
__xp_raw_get_data()is inline, whilexsk_buff_read_metadata()uses the external version, probably some other stuff (note the underscored__xsk_buff_get_metadata()) =\ This function expanded to have no external calls on clang -O2, with this patch I'm not sure it would.
Quoted 7 lines
Quoted 3 lines
Any better ideas on how to solve it? I can convert all non libeth stuff to these new
xsk_buff_read_metadatahelpers and keepxp_raw_get_ctxto you? Will that work?Sure, I can take a look.
Do you want me to pick this patch as a base or you'd like to send a new rev?
I have an idea on how to keep the existing pointer (instead of introducing a copy). Let me play with it today, will send something hopefully Monday..
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [High] The patch introduces a backward compatibility regression by unconditionally dropping valid packets with tx_metadata_len < 16.
- [High] A Time-of-Check to Time-of-Use (TOCTOU) vulnerability bypasses the newly introduced metadata length validation, leaving the out-of-bounds read unfixed.
Pre-existing issues:
- [High] UAPI / ABI Incompatibility between 32-bit and 64-bit architectures for struct xsk_tx_metadata.
- [High] Calling skb_checksum_help() in xsk_build_skb_zerocopy() triggers a kernel warning and wedges the TX queue because the SKB has no linear data.
- [High] Unaligned memory access panic when reading meta->flags in unaligned chunk mode.
--
--- Patch [1]: [PATCH net v2] xsk: reject undersized tx_metadata at runtime validation --- commit 16c1a55389347ae87a6cce0af1dfd6639cadc298
Author: Cen Zhang (Microsoft) <[email protected]>
xsk: reject undersized tx_metadata at runtime validation
This commit adds a runtime check in xsk_buff_valid_tx_metadata() to validate that tx_metadata_len is sufficient for the enabled flags. It aims to fix an out-of-bounds read when accessing metadata fields in xsk_skb_metadata().
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0
[ ... ]
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 268
Quoted 12 lines
include/net/xdp_sock_drv.h +5
-2@@ -260,9 +260,22 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 0)static inline bool -xsk_buff_valid_tx_metadata (const struct xsk_tx_metadata *meta) +xsk_buff_valid_tx_metadata (const struct xsk_buff_pool *pool, +const struct xsk_tx_metadata *meta) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); +/* covers flags, XDP_TXMD_FLAGS_CHECKSUM & XDP_TXMD_FLAGS_TIMESTAMP */ +if (unlikely (pool->tx_metadata_len <16 )) +return false;
[Severity: High]
Does this unconditional check break backward compatibility for applications using a length of 8 and requesting no metadata flags? If an application didn't request features requiring more space, won't returning false here cause xsk_skb_metadata() to return -EINVAL, permanently wedging the socket's TX queue because __xsk_generic_xmit() aborts without releasing the descriptor?
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 271
Quoted 3 lines
include/net/xdp_sock_drv.h +3
-0+ +if (unlikely (meta->flags & ~XDP_TXMD_FLAGS_VALID )) +return false;
[Severity: High]
This isn't a bug introduced by this patch, but when the UMEM operates in unaligned chunk mode, won't reading meta->flags directly perform an unaligned 64-bit memory access? Could this cause a strict alignment fault and panic the kernel on architectures like ARM32 or SPARC? Should this be using get_unaligned() instead?
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 273
Quoted 2 lines
include/net/xdp_sock_drv.h +2
-0+ +if (meta->flags &XDP_TXMD_FLAGS_LAUNCH_TIME )
[Severity: High]
Is this validation safe against a Time-of-Check to Time-of-Use race? Since meta->flags is located in shared UMEM, couldn't a malicious userspace application set flags to 0 during this check, then immediately change it to XDP_TXMD_FLAGS_LAUNCH_TIME before xsk_skb_metadata() reads it again? This seems to allow bypassing the length validation and triggering the exact out-of-bounds read this patch aims to fix.
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 276
Quoted 3 lines
include/net/xdp_sock_drv.h +3
-0+if (unlikely (pool->tx_metadata_len < + offsetofend(struct xsk_tx_metadata, request.launch_time))) +return false;
[Severity: High]
This isn't a bug introduced by this patch, but doesn't struct xsk_tx_metadata have an ABI mismatch between 32-bit and 64-bit architectures due to a lack of explicit padding?
include/uapi/linux/if_xdp.h:
struct xsk_tx_metadata {
__u64 flags;
union {
struct {
__u16 csum_start;
__u16 csum_offset;
__u64 launch_time;
} request;
};
};On 64-bit, launch_time requires 8-byte alignment, causing 4 bytes of padding and making the struct 24 bytes long. On 32-bit, launch_time requires 4-byte alignment, making the struct 20 bytes long. Won't this cause a 32-bit application on a 64-bit kernel to suffer data corruption, and unconditionally fail UMEM registration since the length isn't a multiple of 8?
Quoted 3 lines
include/net/xdp_sock_drv.h +2
-0+ +return true; }
[ ... ]
net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 815
Quoted 13 lines
net/xdp/xsk.c +1
-1@@ -809,7 +809,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,return -EINVAL ; meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) +if (unlikely (!xsk_buff_valid_tx_metadata (pool, meta)))return -EINVAL ;if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) {
[Severity: High]
This is a pre-existing issue, but when xsk_skb_metadata() is called from xsk_build_skb_zerocopy(), doesn't it invoke skb_checksum_help() on an SKB with no linear data (skb_headlen is 0)?
net/xdp/xsk.c:xsk_skb_metadata() {
...
if (unlikely(pool->tx_sw_csum)) {
int err;
err = skb_checksum_help(skb);
if (err)
return err;
}
}Would this cause the check inside skb_checksum_help() for offset >= skb_headlen(skb) to evaluate to true, triggering a warning and returning -EINVAL, which then wedges the TX queue?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1