xsk: harden TX metadata validation against races
Subsystems (11)
cover xsk: harden TX metadata validation against races
Cen Zhang reported a KASAN out-of-bounds read when AF_XDP is configured with a TX metadata area smaller than struct xsk_tx_metadata. The metadata is also shared with user space, so reading its flags more than once can produce inconsistent validation and processing decisions.
Require enough space for the flags and one request field, validate the launch-time field against the configured metadata length, and use one snapshot of the flags while processing each request. Carry the validated decision through completion handling so later user-space changes cannot enable an unrequested completion timestamp.
Stanislav Fomichev (6):
xsk: require at least 16 bytes of TX metadata
xsk: pass TX metadata pointer by reference
xsk: clear metadata pointer when no timestamp is requested
xsk: validate launch-time metadata size
xsk: move xsk_tx_metadata_request() to xdp_sock_drv.h
xsk: validate metadata when processing requests
drivers/net/ethernet/intel/igc/igc_main.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en.h | 2 +- .../net/ethernet/mellanox/mlx5/core/en/xdp.c | 15 ++-- .../net/ethernet/mellanox/mlx5/core/en/xdp.h | 4 +- .../ethernet/mellanox/mlx5/core/en/xsk/tx.c | 2 +- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 4 +- include/net/libeth/xsk.h | 2 +- include/net/xdp_sock.h | 45 ++--------- include/net/xdp_sock_drv.h | 77 ++++++++++++++++--- net/xdp/xdp_umem.c | 2 + net/xdp/xsk.c | 11 ++- net/xdp/xsk_buff_pool.c | 6 +- 12 files changed, 101 insertions(+), 71 deletions(-)
Signature · 1 line
2.53.0-Meta
PATCH 1/6 xsk: require at least 16 bytes of TX metadata
AF_XDP accepts a TX metadata length as small as eight bytes, but every supported request needs the flags plus at least one eight-byte request field. Such short metadata also lets the kernel read beyond the registered area.
Require 16 bytes rather than sizeof(struct xsk_tx_metadata) to preserve compatibility with applications that do not use launch-time metadata.
--- net/xdp/xdp_umem.c | 2 ++ 1 file changed, 2 insertions(+)
net/xdp/xdp_umem.c +2 -0
@@ -210,6 +210,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)if (mr->flags &XDP_UMEM_TX_METADATA_LEN ) {if (mr->tx_metadata_len >=256 || mr->tx_metadata_len %8 )return -EINVAL ; +if (mr->tx_metadata_len <16 ) +return -EINVAL ; umem->tx_metadata_len = mr->tx_metadata_len; }
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH net 1/6] xsk: require at least 16 bytes of TX metadata
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Incomplete Fix / Out-of-Bounds Memory Read in TX Metadata
- [High] UAPI ABI Incompatibility between 32-bit and 64-bit architectures --
--- Patch [1]: [PATCH net 1/6] xsk: require at least 16 bytes of TX metadata --- commit 8f587b21f2b30dd32c9e63e0817476297538dfb0
Author: Stanislav Fomichev <[email protected]>
xsk: require at least 16 bytes of TX metadata
AF_XDP accepts a TX metadata length as small as eight bytes. This commit requires a minimum of 16 bytes of TX metadata to preserve compatibility with applications that do not use launch-time metadata while addressing out-of-bounds reads.
net/xdp/xdp_umem.c static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) · line 214
Quoted 10 lines
net/xdp/xdp_umem.c +2
-0@@ -210,6 +210,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)if (mr->flags &XDP_UMEM_TX_METADATA_LEN ) {if (mr->tx_metadata_len >=256 || mr->tx_metadata_len %8 )return -EINVAL ; +if (mr->tx_metadata_len <16 ) +return -EINVAL ;
[Severity: Critical]
This is a pre-existing issue, but is a 16-byte minimum sufficient when the launch_time flag is requested?
If a userspace application sets XDP_TXMD_FLAGS_LAUNCH_TIME in the metadata flags and provides a tx_metadata_len of 16, it appears we can still read out of bounds. In include/net/xdp_sock.h:xsk_tx_metadata_request(), the code reads the 8-byte launch_time field at offset 16 when the flag is set:
include/net/xdp_sock.h:xsk_tx_metadata_request() {
...
ops->tmo_request_launch_time(meta->request.launch_time, priv);
...
}If the descriptor addr is placed at the very end of the registered umem (for example, desc->addr = umem_size - 1 with len = 1), reading 8 bytes at offset 16 extends beyond the umem bounds. Since xp_aligned_validate_desc() validates based on the chunk, does this allow reading into potentially unmapped memory if the launch_time flag is set?
[Severity: High]
This isn't a bug introduced by this patch, but does this tx_metadata_len validation correctly handle 32-bit applications running on a 64-bit kernel?
Looking at struct xsk_tx_metadata in include/uapi/linux/if_xdp.h, there is no explicit padding between csum_offset and launch_time:
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 32-bit x86, __u64 has a 4-byte alignment, which places launch_time at offset 12 and makes the total structure size 20 bytes. On a 64-bit kernel, __u64 is 8-byte aligned, placing launch_time at offset 16 with a 24-byte structure size.
If a 32-bit application registers a 20-byte length, it would be rejected by the tx_metadata_len % 8 check above. If it registers 24 bytes but populates launch_time at its native offset 12, would the 64-bit kernel read unexpected data from offset 16?
Quoted 2 lines
net/xdp/xdp_umem.c +0
-0umem->tx_metadata_len = mr->tx_metadata_len; }
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH net 1/6] xsk: require at least 16 bytes of TX metadata
Quoted 45 lines · On 07/28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Incomplete Fix / Out-of-Bounds Memory Read in TX Metadata
- [High] UAPI ABI Incompatibility between 32-bit and 64-bit architectures ----- Patch [1]: [PATCH net 1/6] xsk: require at least 16 bytes of TX metadata --- commit
8f587b21f2b30dd32c9e63e0817476297538dfb0
Author: Stanislav Fomichev <[email protected]>
xsk: require at least 16 bytes of TX metadata
AF_XDPaccepts a TX metadata length as small as eight bytes. This commit requires a minimum of 16 bytes of TX metadata to preserve compatibility with applications that do not use launch-time metadata while addressing out-of-bounds reads.net/xdp/xdp_umem.c static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) · line 214
Quoted 10 lines
net/xdp/xdp_umem.c +2
-0@@ -210,6 +210,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)if (mr->flags &XDP_UMEM_TX_METADATA_LEN ) {if (mr->tx_metadata_len >=256 || mr->tx_metadata_len %8 )return -EINVAL ; +if (mr->tx_metadata_len <16 ) +return -EINVAL ;[Severity: Critical]
This is a pre-existing issue, but is a 16-byte minimum sufficient when thelaunch_timeflag is requested?If a userspace application sets
XDP_TXMD_FLAGS_LAUNCH_TIMEin the metadata flags and provides atx_metadata_lenof 16, it appears we can still read out of bounds. In include/net/xdp_sock.h:xsk_tx_metadata_request(), the code reads the 8-bytelaunch_timefield at offset 16 when the flag is set:include/net/xdp_sock.h:xsk_tx_metadata_request() { ... ops->tmo_request_launch_time(meta->request.launch_time, priv); ... }
Quoted 5 lines
If the descriptor addr is placed at the very end of the registered umem (for example,
desc->addr=umem_size- 1 with len = 1), reading 8 bytes at offset 16 extends beyond the umem bounds. Sincexp_aligned_validate_desc()validates based on the chunk, does this allow reading into potentially unmapped memory if thelaunch_timeflag is set?
Lanuch time is handled separately later in the series:
- [PATCH net 4/6] xsk: validate launch-time metadata size
Quoted 24 lines
[Severity: High]
This isn't a bug introduced by this patch, but does thistx_metadata_lenvalidation correctly handle 32-bit applications running on a 64-bit kernel?Looking at struct
xsk_tx_metadataininclude/uapi/linux/if_xdp.h, there is no explicit padding betweencsum_offsetandlaunch_time: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 32-bit x86,
__u64has a 4-byte alignment, which placeslaunch_timeat offset 12 and makes the total structure size 20 bytes. On a 64-bit kernel,__u64is 8-byte aligned, placinglaunch_timeat offset 16 with a 24-byte structure size.
Quoted 4 lines
If a 32-bit application registers a 20-byte length, it would be rejected by the
tx_metadata_len% 8 check above. If it registers 24 bytes but populateslaunch_timeat its native offset 12, would the 64-bit kernel read unexpected data from offset 16?
Noted, I don't think we test 32 bit arches, the sensible thing is to probably add some padding (potentially breaking existing, if any, 32 bit apps). Will put it on the todo.
PATCH 2/6 xsk: pass TX metadata pointer by reference
In reply to Stanislav FomichevCompletion handling needs to know whether a timestamp was requested when the metadata was processed. Let xsk_tx_metadata_request() update the caller's metadata pointer so that decision can be carried forward without rereading user-controlled flags.
This only changes the interface; behavior remains unchanged.
--- drivers/net/ethernet/intel/igc/igc_main.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en.h | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c | 14 +++++++++----- drivers/net/ethernet/mellanox/mlx5/core/en/xdp.h | 4 ++-- .../net/ethernet/mellanox/mlx5/core/en/xsk/tx.c | 2 +- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +- include/net/libeth/xsk.h | 2 +- include/net/xdp_sock.h | 8 +++++--- 8 files changed, 21 insertions(+), 15 deletions(-)
drivers/net/ethernet/intel/igc/igc_main.c +1 -1
@@ -3082,7 +3082,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.h +1 -1
@@ -483,7 +483,7 @@ typedef int (*mlx5e_fp_xmit_xdp_frame_check)(struct mlx5e_xdpsq *);typedef bool (*mlx5e_fp_xmit_xdp_frame )(struct mlx5e_xdpsq *,struct mlx5e_xmit_data *,int , -struct xsk_tx_metadata *); +struct xsk_tx_metadata **);struct mlx5e_xdpsq {/* data path */
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c +9 -5
@@ -452,11 +452,11 @@ INDIRECT_CALLABLE_SCOPE int mlx5e_xmit_xdp_frame_check_mpwqe(struct mlx5e_xdpsq INDIRECT_CALLABLE_SCOPE boolmlx5e_xmit_xdp_frame (struct mlx5e_xdpsq *sq,struct mlx5e_xmit_data *xdptxd, -int check_result,struct xsk_tx_metadata *meta); +int check_result,struct xsk_tx_metadata **meta); INDIRECT_CALLABLE_SCOPE boolmlx5e_xmit_xdp_frame_mpwqe (struct mlx5e_xdpsq *sq,struct mlx5e_xmit_data *xdptxd, -int check_result,struct xsk_tx_metadata *meta) +int check_result,struct xsk_tx_metadata **meta) {struct mlx5e_tx_mpwqe *session = &sq->mpwqe;struct mlx5e_xdpsq_stats *stats = sq->stats;
@@ -504,7 +504,10 @@ mlx5e_xmit_xdp_frame_mpwqe(struct mlx5e_xdpsq *sq, struct mlx5e_xmit_data *xdptx* and it's safe to complete it at any time. */ mlx5e_xdp_mpwqe_session_start (sq); -xsk_tx_metadata_request (meta, &mlx5e_xsk_tx_metadata_ops, &session->wqe->eth); +if (meta) +xsk_tx_metadata_request (meta, + &mlx5e_xsk_tx_metadata_ops, + &session->wqe->eth); }mlx5e_xdp_mpwqe_add_dseg (sq, p, stats);
@@ -535,7 +538,7 @@ INDIRECT_CALLABLE_SCOPE int mlx5e_xmit_xdp_frame_check(struct mlx5e_xdpsq *sq) INDIRECT_CALLABLE_SCOPE boolmlx5e_xmit_xdp_frame (struct mlx5e_xdpsq *sq,struct mlx5e_xmit_data *xdptxd, -int check_result,struct xsk_tx_metadata *meta) +int check_result,struct xsk_tx_metadata **meta) {struct mlx5e_xmit_data_frags *xdptxdf = container_of(xdptxd, struct mlx5e_xmit_data_frags, xd);
@@ -649,7 +652,8 @@ mlx5e_xmit_xdp_frame(struct mlx5e_xdpsq *sq, struct mlx5e_xmit_data *xdptxd, sq->pc += num_wqebbs; -xsk_tx_metadata_request (meta, &mlx5e_xsk_tx_metadata_ops, eseg); +if (meta) +xsk_tx_metadata_request (meta, &mlx5e_xsk_tx_metadata_ops, eseg); sq->doorbell_cseg = cseg;
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.h +2 -2
@@ -114,11 +114,11 @@ extern const struct xsk_tx_metadata_ops mlx5e_xsk_tx_metadata_ops; INDIRECT_CALLABLE_DECLARE(bool mlx5e_xmit_xdp_frame_mpwqe(struct mlx5e_xdpsq *sq,struct mlx5e_xmit_data *xdptxd,int check_result, -struct xsk_tx_metadata *meta)); +struct xsk_tx_metadata **meta)); INDIRECT_CALLABLE_DECLARE(bool mlx5e_xmit_xdp_frame(struct mlx5e_xdpsq *sq,struct mlx5e_xmit_data *xdptxd,int check_result, -struct xsk_tx_metadata *meta)); +struct xsk_tx_metadata **meta)); INDIRECT_CALLABLE_DECLARE(int mlx5e_xmit_xdp_frame_check_mpwqe(struct mlx5e_xdpsq *sq)); INDIRECT_CALLABLE_DECLARE(int mlx5e_xmit_xdp_frame_check(struct mlx5e_xdpsq *sq));
drivers/net/ethernet/mellanox/mlx5/core/en/xsk/tx.c +1 -1
@@ -105,7 +105,7 @@ bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget) 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);
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -1
@@ -2747,7 +2747,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 ;
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 +5 -3
@@ -153,17 +153,19 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,/** * xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission * and call appropriate xsk_tx_metadata_ops operation. -* @meta: pointer to AF_XDP metadata area +* @pmeta: pointer to pointer to AF_XDP metadata area * @ops: pointer to struct xsk_tx_metadata_ops * @priv: pointer to driver-private aread * * This function should be called by the networking device when * it prepares AF_XDP egress packet. */ -static inline void xsk_tx_metadata_request (const struct xsk_tx_metadata *meta, +static inline void xsk_tx_metadata_request (struct xsk_tx_metadata **pmeta,const struct xsk_tx_metadata_ops *ops,void *priv) { +const struct xsk_tx_metadata *meta = *pmeta; +if (!meta)return ;
@@ -231,7 +233,7 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta, { } -static inline void xsk_tx_metadata_request (struct xsk_tx_metadata *meta, +static inline void xsk_tx_metadata_request (struct xsk_tx_metadata **pmeta,const struct xsk_tx_metadata_ops *ops,void *priv) {
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH net 2/6] xsk: pass TX metadata pointer by reference
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] xsk_tx_metadata_to_compl() returns early when meta == NULL without clearing the compl output structure, leading to arbitrary kernel memory writes during TX completion. --
--- Patch [2]: [PATCH net 2/6] xsk: pass TX metadata pointer by reference --- commit 83fcec7a0c0075ec991849b8d99f6d6cd083c335
Author: Stanislav Fomichev <[email protected]>
xsk: pass TX metadata pointer by reference
Completion handling needs to know whether a timestamp was requested when the metadata was processed. Let xsk_tx_metadata_request() update the caller's metadata pointer so that decision can be carried forward without rereading user-controlled flags.
[ ... ]
include/net/xdp_sock.h static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
Quoted 5 lines
include/net/xdp_sock.h +0
-0@@ -153,17 +153,19 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
[Severity: Critical]
This isn't a bug introduced by this patch, but I noticed that xsk_tx_metadata_to_compl() returns early without explicitly clearing the compl structure when meta is NULL:
static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
struct xsk_tx_metadata_compl *compl)
{
if (!meta)
return;In drivers like mlx5e_xsk_tx(), compl is declared on the stack and passed in uninitialized:
mlx5e_xsk_tx() {
if (xp_tx_metadata_enabled(sq->xsk_pool)) {
struct xsk_tx_metadata_compl compl;
xsk_tx_metadata_to_compl(meta, &compl);
...
mlx5e_xdpi_fifo_push(..., { .xsk_meta = compl });
}
}If an untrusted user sends a packet with invalid flags, causing meta to be NULL, could the uninitialized stack garbage in compl be pushed to the NAPI TX completion FIFO? Would this cause the completion path to dereference a garbage pointer and write a hardware timestamp to arbitrary kernel memory?
Similarly, in stmmac_xdp_xmit_zc():
stmmac_xdp_xmit_zc() {
...
xsk_tx_metadata_to_compl(meta,
&tx_q->tx_skbuff_dma[entry].xsk_meta);
}Could this early return leave a stale pointer from a previous packet in the descriptor ring, leading to an accidental overwrite of userspace UMEM?
Quoted 4 lines
include/net/xdp_sock.h +1
-1/** * xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission -* @meta: pointer to AF_XDP metadata area +* @pmeta: pointer to pointer to AF_XDP metadata area
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH net 2/6] xsk: pass TX metadata pointer by reference
Quoted 56 lines · On 07/28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical]xsk_tx_metadata_to_compl()returns early whenmeta == NULLwithout clearing thecomploutput structure, leading to arbitrary kernel memory writes during TX completion. ----- Patch [2]: [PATCH net 2/6] xsk: pass TX metadata pointer by reference --- commit
83fcec7a0c0075ec991849b8d99f6d6cd083c335
Author: Stanislav Fomichev <[email protected]>xsk: pass TX metadata pointer by reference
Completion handling needs to know whether a timestamp was requested when the metadata was processed. Let
xsk_tx_metadata_request()update the caller's metadata pointer so that decision can be carried forward without rereading user-controlled flags.[ ... ]
include/net/xdp_sock.h static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
Quoted 5 lines
include/net/xdp_sock.h +0
-0@@ -153,17 +153,19 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,[Severity: Critical]
This isn't a bug introduced by this patch, but I noticed thatxsk_tx_metadata_to_compl()returns early without explicitly clearing the compl structure when meta is NULL:static inline void xsk_tx_metadata_to_compl(struct
xsk_tx_metadata*meta,
structxsk_tx_metadata_compl*compl){ if (!meta) return;In drivers like
mlx5e_xsk_tx(), compl is declared on the stack and passed in uninitialized:mlx5e_xsk_tx() { if (xp_tx_metadata_enabled(sq->xsk_pool)) { struct xsk_tx_metadata_compl compl; xsk_tx_metadata_to_compl(meta, &compl); ... mlx5e_xdpi_fifo_push(..., { .xsk_meta = compl }); } }If an untrusted user sends a packet with invalid flags, causing meta to be NULL, could the uninitialized stack garbage in compl be pushed to the NAPI TX completion FIFO? Would this cause the completion path to dereference a garbage pointer and write a hardware timestamp to arbitrary kernel memory?
Similarly, in
stmmac_xdp_xmit_zc():
Quoted 8 lines
stmmac_xdp_xmit_zc() { ... xsk_tx_metadata_to_compl(meta, &tx_q->tx_skbuff_dma[entry].xsk_meta); }Could this early return leave a stale pointer from a previous packet in the descriptor ring, leading to an accidental overwrite of userspace UMEM?
I think this is now addressed by:
- [PATCH net 3/6] xsk: clear metadata pointer when no timestamp is requested
PATCH 3/6 xsk: clear metadata pointer when no timestamp is requested
In reply to Stanislav FomichevUser space can change metadata flags after request processing. Rereading them during completion can therefore make the kernel write a timestamp that was not requested when the packet was submitted.
Clear the metadata pointer during request processing unless timestamp completion is requested. Completion handling can then use the pointer itself instead of rereading the flags.
On the mlx5 multi-packet WQE path metadata is evaluated per batch:xsk_tx_metadata_request() runs only for the descriptor that starts a session, just like the checksum offload that is applied once through the shared WQE. Only that descriptor's pointer is reset, so completion handling can record a timestamp for the other descriptors of the session regardless of their own XDP_TXMD_FLAGS_TIMESTAMP bit. The write stays inside the metadata area; the single-WQE, other zero-copy, and generic paths reset the pointer per descriptor and are unaffected.
--- include/net/xdp_sock.h | 14 ++++++++++---- net/xdp/xsk.c | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-)
include/net/xdp_sock.h +10 -4
@@ -141,13 +141,16 @@ INDIRECT_CALLABLE_DECLARE(void xsk_destruct_skb(struct sk_buff *));static inline void xsk_tx_metadata_to_compl (struct xsk_tx_metadata *meta,struct xsk_tx_metadata_compl *compl) { + compl->tx_timestamp =NULL ; +if (!meta)return ; -if (meta->flags &XDP_TXMD_FLAGS_TIMESTAMP ) - compl->tx_timestamp = &meta->completion.tx_timestamp; -else - compl->tx_timestamp =NULL ; +/* we can only arrive here if the completion timestamp has been +* requested via XDP_TXMD_FLAGS_TIMESTAMP, see xsk_tx_metadata_request +*/ + + compl->tx_timestamp = &meta->completion.tx_timestamp; }/**
@@ -182,6 +185,9 @@ static inline void xsk_tx_metadata_request(struct xsk_tx_metadata **pmeta,if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) ops->tmo_request_checksum (meta->request.csum_start, meta->request.csum_offset, priv); + +if (!(meta->flags &XDP_TXMD_FLAGS_TIMESTAMP )) + *pmeta =NULL ; }/**
net/xdp/xsk.c +2 -0
@@ -998,6 +998,8 @@ 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; +if (!(meta->flags &XDP_TXMD_FLAGS_TIMESTAMP )) + meta =NULL ;xsk_tx_metadata_to_compl (meta, &skb_shinfo (skb)->xsk_meta);return 0 ;
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH net 3/6] xsk: clear metadata pointer when no timestamp is requested
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Integer overflow in xsk_skb_metadata when calculating skb->csum_start, leading to a potential kernel warning and panic (DoS). --
--- Patch [3]: [PATCH net 3/6] xsk: clear metadata pointer when no timestamp is requested --- commit 814027e56069dbb5716ac5b9f7f38aebab332dd6
Author: Stanislav Fomichev <[email protected]>
xsk: clear metadata pointer when no timestamp is requested
User space can change metadata flags after request processing. Rereading them during completion can therefore make the kernel write a timestamp that was not requested when the packet was submitted. Clear the metadata pointer during request processing unless timestamp completion is requested.
Quoted 3 lines
net/xdp/xsk.c +0
-0
[ ... ]
net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1000
Quoted 4 lines
net/xdp/xsk.c +0
-0@@ -998,6 +998,8 @@ 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;
[Severity: High]
This is a pre-existing issue, but can the checksum offset calculation earlier in this function cause an integer overflow?
When handling XDP_TXMD_FLAGS_CHECKSUM, the code does:
skb->csum_start = hr + csum_start;
On architectures with 64KB pages, csum_start can be large enough that adding hr wraps the u16 skb->csum_start. This would result in skb->csum_start being smaller than the headroom.
If pool->tx_sw_csum is true, this then calls skb_checksum_help(). Inside skb_checksum_help(), the offset is calculated by subtracting headroom from skb->csum_start, which yields a negative integer due to the wrap-around.
When this negative offset is compared against skb_headlen() (which is unsigned), it is implicitly cast to a huge unsigned value:
net/core/dev.c:skb_checksum_help() {
...
offset = skb_checksum_start_offset(skb);
...
if (unlikely(offset >= skb_headlen(skb))) {
DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n", ...);
...
}Could this allow an unprivileged user to trigger a kernel warning and potentially panic the system if panic_on_warn is set?
Quoted 5 lines
net/xdp/xsk.c +2
-0+if (!(meta->flags &XDP_TXMD_FLAGS_TIMESTAMP )) + meta =NULL ;xsk_tx_metadata_to_compl (meta, &skb_shinfo (skb)->xsk_meta);return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH net 3/6] xsk: clear metadata pointer when no timestamp is requested
Quoted 57 lines · On 07/28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Integer overflow inxsk_skb_metadatawhen calculatingskb->csum_start, leading to a potential kernel warning and panic (DoS). ----- Patch [3]: [PATCH net 3/6] xsk: clear metadata pointer when no timestamp is requested --- commit
814027e56069dbb5716ac5b9f7f38aebab332dd6
Author: Stanislav Fomichev <[email protected]>xsk: clear metadata pointer when no timestamp is requested
User space can change metadata flags after request processing. Rereading them during completion can therefore make the kernel write a timestamp that was not requested when the packet was submitted. Clear the metadata pointer during request processing unless timestamp completion is requested.
Quoted 3 lines
net/xdp/xsk.c +0
-0[ ... ]
net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1000
Quoted 4 lines
net/xdp/xsk.c +0
-0@@ -998,6 +998,8 @@ 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;[Severity: High]
This is a pre-existing issue, but can the checksum offset calculation earlier in this function cause an integer overflow?When handling
XDP_TXMD_FLAGS_CHECKSUM, the code does:skb->csum_start = hr + csum_start;On architectures with 64KB pages,
csum_startcan be large enough that adding hr wraps the u16skb->csum_start. This would result inskb->csum_startbeing smaller than the headroom.If
pool->tx_sw_csumis true, this then callsskb_checksum_help(). Insideskb_checksum_help(), the offset is calculated by subtracting headroom fromskb->csum_start, which yields a negative integer due to the wrap-around.When this negative offset is compared against
skb_headlen()(which is unsigned), it is implicitly cast to a huge unsigned value:net/core/dev.c:skb_checksum_help() { ... offset = skb_checksum_start_offset(skb); ... if (unlikely(offset >= skb_headlen(skb))) { DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false); WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n", ...); ... }
Quoted 2 lines
Could this allow an unprivileged user to trigger a kernel warning and potentially panic the system if
panic_on_warnis set?
I guess.. We can do a similar size check before calling skb_checksum_help to avoid WARN_ONCE.
PATCH 4/6 xsk: validate launch-time metadata size
In reply to Stanislav FomichevLaunch-time metadata extends beyond the first 16 bytes of struct xsk_tx_metadata. Reject the request when the registered metadata area does not contain the complete field.
Snapshot the validated flags for the generic transmit path and use that snapshot for request and completion processing, avoiding inconsistent decisions if user space changes the flags concurrently.
Note that only xsk_skb_metadata is properly using the flags, __xsk_buff_get_metadata ignores them. Next commits address that.
--- include/net/xdp_sock_drv.h | 17 +++++++++++++---- net/xdp/xsk.c | 11 ++++++----- 2 files changed, 19 insertions(+), 9 deletions(-)
include/net/xdp_sock_drv.h +13 -4
@@ -260,21 +260,28 @@ 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,u64 *flags) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); + *flags =READ_ONCE (meta->flags); +if (*flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) +if (pool->tx_metadata_len < + offsetofend(struct xsk_tx_metadata, request.launch_time)) +return false; +return !(*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; +u64 flags;if (!pool->tx_metadata_len)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, &flags)))return NULL ;/* no way to signal the error to the user */ return meta;
@@ -469,7 +476,9 @@ 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, +const struct xsk_tx_metadata *meta,u64 *flags) {return false; }
net/xdp/xsk.c +6 -5
@@ -967,15 +967,16 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, {struct xsk_tx_metadata *meta =NULL ;u16 csum_start, csum_offset; +u64 flags;if (unlikely (pool->tx_metadata_len ==0 ))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, &flags)))return -EINVAL ; -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) { +if (flags &XDP_TXMD_FLAGS_CHECKSUM ) { csum_start =READ_ONCE (meta->request.csum_start); csum_offset =READ_ONCE (meta->request.csum_offset);
@@ -996,9 +997,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; -if (!(meta->flags &XDP_TXMD_FLAGS_TIMESTAMP )) +if (flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) + skb->skb_mstamp_ns =READ_ONCE (meta->request.launch_time); +if (!(flags &XDP_TXMD_FLAGS_TIMESTAMP )) meta =NULL ;xsk_tx_metadata_to_compl (meta, &skb_shinfo (skb)->xsk_meta);
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH net 4/6] xsk: validate launch-time metadata size
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Medium] Time-of-Check to Time-of-Use (TOCTOU) race condition in zero-copy TX metadata validation.
Pre-existing issues:
- [Critical] struct xsk_tx_metadata has a layout mismatch between 32-bit and 64-bit architectures, breaking 32-bit userspace compatibility.
- [High] Calling skb_checksum_help() on an empty or fragmented SKB in AF_XDP zero-copy path drops packets.
--
--- Patch [4]: [PATCH net 4/6] xsk: validate launch-time metadata size --- commit 812dce9383d33c01b8fe81b1fbbcc8371998ca62
Author: Stanislav Fomichev <[email protected]>
xsk: validate launch-time metadata size
This commit validates the launch-time metadata size by rejecting requests when the registered metadata area does not contain the complete field. It also snapshots the validated flags for the generic transmit path to avoid inconsistent decisions if user space changes the flags concurrently.
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0
[ ... ]
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0@@ -260,21 +260,28 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 0) static inline bool
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 270
Quoted 10 lines
include/net/xdp_sock_drv.h +7
-2-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, u64 *flags) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); + *flags =READ_ONCE (meta->flags); +if (*flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) +if (pool->tx_metadata_len < + offsetofend(struct xsk_tx_metadata, request.launch_time)) +return false;
[Severity: Critical]
This is a pre-existing issue, but the UAPI struct definition in include/uapi/linux/if_xdp.h has two __u16 fields followed by a __u64 field without explicit padding.
64-bit kernels will read launch_time from offset 16, while 32-bit userspace writes it to offset 12.
This newly added validation explicitly expects the 64-bit structure size, which solidifies the layout incompatibility.
Does struct xsk_tx_metadata have a layout mismatch between 32-bit and 64-bit architectures that prevents 32-bit userspace compatibility?
Quoted 8 lines
include/net/xdp_sock_drv.h +2
-0+return !(*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; +u64 flags;
include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 287
Quoted 10 lines
include/net/xdp_sock_drv.h +1
-1if (!pool->tx_metadata_len)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, &flags)))return NULL ;/* no way to signal the error to the user */ return meta;
[Severity: Medium]
The snapshotted flags are validated for size compliance in xsk_buff_valid_tx_metadata() but then discarded, returning the un-snapshotted user memory pointer (meta) to the driver.
Later in the zero-copy driver path, xsk_tx_metadata_request() re-reads meta->flags directly from user memory:
include/net/xdp_sock.h:xsk_tx_metadata_request() {
...
if (meta->flags & XDP_TXMD_FLAGS_LAUNCH_TIME)
ops->tmo_request_launch_time(meta->request.launch_time, priv);
...
}Does this create a Time-of-Check to Time-of-Use (TOCTOU) race condition in the zero-copy TX metadata validation where userspace can concurrently enable launch time after the size validation?
Quoted 1 line
}
[ ... ]
Quoted 12 lines
net/xdp/xsk.c +1
-0@@ -967,15 +967,16 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, {struct xsk_tx_metadata *meta =NULL ;u16 csum_start, csum_offset; +u64 flags;if (unlikely (pool->tx_metadata_len ==0 ))return -EINVAL ;
net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 981
Quoted 10 lines
net/xdp/xsk.c +2
-2meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) +if (unlikely (!xsk_buff_valid_tx_metadata (pool, meta, &flags)))return -EINVAL ; -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) { +if (flags &XDP_TXMD_FLAGS_CHECKSUM ) { csum_start =READ_ONCE (meta->request.csum_start); csum_offset =READ_ONCE (meta->request.csum_offset);
[Severity: High]
This isn't a bug introduced by this patch, but for zero-copy SKB construction (xsk_build_skb_zerocopy), the payload is populated strictly in fragments, leaving skb_headlen(skb) == 0.
When a user requests XDP TX metadata with checksum offload on a device that uses software checksum fallback (pool->tx_sw_csum), skb_checksum_help() fails immediately on such fragmented SKBs by returning -EINVAL, which drops the packet.
Does calling skb_checksum_help() later in this checksum block incorrectly assume the SKB has a linearized payload?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH net 4/6] xsk: validate launch-time metadata size
Quoted 46 lines · On 07/28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Medium] Time-of-Check to Time-of-Use (TOCTOU) race condition in zero-copy TX metadata validation.Pre-existing issues:
- [Critical] structxsk_tx_metadatahas a layout mismatch between 32-bit and 64-bit architectures, breaking 32-bit userspace compatibility.
- [High] Callingskb_checksum_help()on an empty or fragmented SKB inAF_XDPzero-copy path drops packets.
----- Patch [4]: [PATCH net 4/6] xsk: validate launch-time metadata size --- commit
812dce9383d33c01b8fe81b1fbbcc8371998ca62
Author: Stanislav Fomichev <[email protected]>xsk: validate launch-time metadata size
This commit validates the launch-time metadata size by rejecting requests when the registered metadata area does not contain the complete field. It also snapshots the validated flags for the generic transmit path to avoid inconsistent decisions if user space changes the flags concurrently.
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0[ ... ]
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0@@ -260,21 +260,28 @@ xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) 0) static inline boolinclude/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 270
Quoted 10 lines
include/net/xdp_sock_drv.h +7
-2-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, u64 *flags) { -return !(meta->flags & ~XDP_TXMD_FLAGS_VALID ); + *flags =READ_ONCE (meta->flags); +if (*flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) +if (pool->tx_metadata_len < + offsetofend(struct xsk_tx_metadata, request.launch_time)) +return false;[Severity: Critical]
This is a pre-existing issue, but the UAPI struct definition ininclude/uapi/linux/if_xdp.hhas two__u16fields followed by a__u64field without explicit padding.
Quoted 8 lines
64-bit kernels will read
launch_timefrom offset 16, while 32-bit userspace writes it to offset 12.This newly added validation explicitly expects the 64-bit structure size, which solidifies the layout incompatibility.
Does struct
xsk_tx_metadatahave a layout mismatch between 32-bit and 64-bit architectures that prevents 32-bit userspace compatibility?
Same as the other email, yes, adding padding here is probably a good idea, will send separately.
Quoted 34 lines
Quoted 8 lines
include/net/xdp_sock_drv.h +2
-0+return !(*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; +u64 flags;include/net/xdp_sock_drv.h xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr) · line 287
Quoted 10 lines
include/net/xdp_sock_drv.h +1
-1if (!pool->tx_metadata_len)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, &flags)))return NULL ;/* no way to signal the error to the user */ return meta;[Severity: Medium]
The snapshotted flags are validated for size compliance inxsk_buff_valid_tx_metadata()but then discarded, returning the un-snapshotted user memory pointer (meta) to the driver.Later in the zero-copy driver path,
xsk_tx_metadata_request()re-readsmeta->flagsdirectly from user memory:include/net/xdp_sock.h:xsk_tx_metadata_request() { ... if (meta->flags & XDP_TXMD_FLAGS_LAUNCH_TIME) ops->tmo_request_launch_time(meta->request.launch_time, priv); ... }
Quoted 3 lines
Does this create a Time-of-Check to Time-of-Use (TOCTOU) race condition in the zero-copy TX metadata validation where userspace can concurrently enable launch time after the size validation?
It is explained in the commit message:
Note that only xsk_skb_metadata is properly using the flags, __xsk_buff_get_metadata ignores them. Next commits address that.
Quoted 30 lines
Quoted 1 line
}
[ ... ]
Quoted 12 lines
net/xdp/xsk.c +1
-0@@ -967,15 +967,16 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, {struct xsk_tx_metadata *meta =NULL ;u16 csum_start, csum_offset; +u64 flags;if (unlikely (pool->tx_metadata_len ==0 ))return -EINVAL ;net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 981
Quoted 10 lines
net/xdp/xsk.c +2
-2meta = buffer - pool->tx_metadata_len; -if (unlikely (!xsk_buff_valid_tx_metadata (meta))) +if (unlikely (!xsk_buff_valid_tx_metadata (pool, meta, &flags)))return -EINVAL ; -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) { +if (flags &XDP_TXMD_FLAGS_CHECKSUM ) { csum_start =READ_ONCE (meta->request.csum_start); csum_offset =READ_ONCE (meta->request.csum_offset);[Severity: High]
This isn't a bug introduced by this patch, but for zero-copy SKB construction (xsk_build_skb_zerocopy), the payload is populated strictly in fragments, leavingskb_headlen(skb)== 0.
Quoted 7 lines
When a user requests XDP TX metadata with checksum offload on a device that uses software checksum fallback (
pool->tx_sw_csum),skb_checksum_help()fails immediately on such fragmented SKBs by returning-EINVAL, which drops the packet.Does calling
skb_checksum_help()later in this checksum block incorrectly assume the SKB has a linearized payload?
We treat skb path as a mostly test-fallback, so I think it is ok. I'll add a headlen check just to give the users better signals..
PATCH 5/6 xsk: move xsk_tx_metadata_request() to xdp_sock_drv.h
In reply to Stanislav Fomichevxsk_tx_metadata_request() must validate metadata with xsk_buff_valid_tx_metadata(), which is defined in xdp_sock_drv.h. Move the helper there before adding that dependency. All callers already include the destination header, so this has no functional effect.
--- include/net/xdp_sock.h | 43 -------------------------------------- include/net/xdp_sock_drv.h | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 43 deletions(-)
include/net/xdp_sock.h +0 -43
@@ -153,43 +153,6 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta, compl->tx_timestamp = &meta->completion.tx_timestamp; } -/** -* xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission -* and call appropriate xsk_tx_metadata_ops operation. -* @pmeta: pointer to pointer to AF_XDP metadata area -* @ops: pointer to struct xsk_tx_metadata_ops -* @priv: pointer to driver-private aread -* -* This function should be called by the networking device when -* it prepares AF_XDP egress packet. -*/ -static inline void xsk_tx_metadata_request (struct xsk_tx_metadata **pmeta, -const struct xsk_tx_metadata_ops *ops, -void *priv) -{ -const struct xsk_tx_metadata *meta = *pmeta; - -if (!meta) -return ; - -if (ops->tmo_request_launch_time) -if (meta->flags &XDP_TXMD_FLAGS_LAUNCH_TIME ) - ops->tmo_request_launch_time (meta->request.launch_time, - priv); - -if (ops->tmo_request_timestamp) -if (meta->flags &XDP_TXMD_FLAGS_TIMESTAMP ) - ops->tmo_request_timestamp (priv); - -if (ops->tmo_request_checksum) -if (meta->flags &XDP_TXMD_FLAGS_CHECKSUM ) - ops->tmo_request_checksum (meta->request.csum_start, - meta->request.csum_offset, priv); - -if (!(meta->flags &XDP_TXMD_FLAGS_TIMESTAMP )) - *pmeta =NULL ; -} -/** * xsk_tx_metadata_complete - Evaluate AF_XDP TX metadata at completion * and call appropriate xsk_tx_metadata_ops operation.
@@ -239,12 +202,6 @@ static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta, { } -static inline void xsk_tx_metadata_request (struct xsk_tx_metadata **pmeta, -const struct xsk_tx_metadata_ops *ops, -void *priv) -{ -} -static inline void xsk_tx_metadata_complete (struct xsk_tx_metadata_compl *compl,const struct xsk_tx_metadata_ops *ops,void *priv)
include/net/xdp_sock_drv.h +43 -0
55 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH 6/6 xsk: validate metadata when processing requests
In reply to Stanislav FomichevThe zero-copy path validates TX metadata while obtaining the descriptor context, then reads it again later when preparing the hardware request. User space can change the metadata between those operations and bypass the original validation.
Validate the metadata in xsk_tx_metadata_request() and use the resulting flags snapshot for every feature check. Read request fields once so all zero-copy drivers process only values observed after successful validation.
--- drivers/net/ethernet/intel/igc/igc_main.c | 2 +- .../net/ethernet/mellanox/mlx5/core/en/xdp.c | 5 +- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 4 +- include/net/libeth/xsk.h | 2 +- include/net/xdp_sock_drv.h | 51 ++++++++++--------- net/xdp/xsk_buff_pool.c | 6 +-- 6 files changed, 37 insertions(+), 33 deletions(-)
drivers/net/ethernet/intel/igc/igc_main.c +1 -1
@@ -3082,7 +3082,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 (pool, &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/xdp.c +3 -2
@@ -505,7 +505,7 @@ mlx5e_xmit_xdp_frame_mpwqe(struct mlx5e_xdpsq *sq, struct mlx5e_xmit_data *xdptx*/ mlx5e_xdp_mpwqe_session_start (sq);if (meta) -xsk_tx_metadata_request (meta, +xsk_tx_metadata_request (sq->xsk_pool, meta, &mlx5e_xsk_tx_metadata_ops, &session->wqe->eth); }
@@ -653,7 +653,8 @@ mlx5e_xmit_xdp_frame(struct mlx5e_xdpsq *sq, struct mlx5e_xmit_data *xdptxd, sq->pc += num_wqebbs;if (meta) -xsk_tx_metadata_request (meta, &mlx5e_xsk_tx_metadata_ops, eseg); +xsk_tx_metadata_request (sq->xsk_pool, meta, + &mlx5e_xsk_tx_metadata_ops, eseg); sq->doorbell_cseg = cseg;
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -2
@@ -2747,8 +2747,8 @@ 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, - &meta_req); +xsk_tx_metadata_request (pool, &meta, + &stmmac_xsk_tx_metadata_ops, &meta_req);if (set_ic) { tx_q->tx_count_frames =0 ;stmmac_set_tx_ic (priv, tx_desc);
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 (sq->pool, &ctx.meta, tmo, &desc);return desc; }
include/net/xdp_sock_drv.h +27 -24
97 lines not on this page · raw message
net/xdp/xsk_buff_pool.c +3 -3
@@ -765,11 +765,11 @@ EXPORT_SYMBOL(xp_raw_get_dma);* @addr: desc address (from userspace) * * Helper for getting desc's DMA address and metadata pointer, if present. -* Saves one call on hotpath, double calculation of the actual address, -* and inline checks for metadata presence and sanity. +* Saves one call on hotpath and double calculation of the actual address. +* Metadata is validated later by xsk_tx_metadata_request(). * * Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata -* pointer, if it is present and valid (initialized to %NULL otherwise). +* pointer, if it is present (initialized to %NULL otherwise). */ struct xdp_desc_ctx xp_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr) {
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH net 6/6] xsk: validate metadata when processing requests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Zero-copy drivers unconditionally parse XDP TX metadata ignoring the XDP_TX_METADATA option flag.
- [High] The stmmac driver drops AF_XDP TX frames without generating completions when the frame length exceeds max_sdu, causing the AF_XDP completion queue (CQ) to permanently desynchronize. --
--- Patch [6]: [PATCH net 6/6] xsk: validate metadata when processing requests --- commit 8967b4032038ba439c0a13ce2dc4cab303b87c2e
Author: Stanislav Fomichev <[email protected]>
xsk: validate metadata when processing requests
The zero-copy path validates TX metadata while obtaining the descriptor context, then reads it again later when preparing the hardware request. Validate the metadata in xsk_tx_metadata_request() and use the resulting flags snapshot for every feature check.
drivers/net/ethernet/intel/igc/igc_main.c static void igc_xdp_xmit_zc(struct igc_ring *ring) · line 3086
Quoted 11 lines
drivers/net/ethernet/intel/igc/igc_main.c +1
-1@@ -3082,7 +3082,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 (pool, &meta, &igc_xsk_tx_metadata_ops, &meta_req);
[Severity: High]
This is a pre-existing issue, but do the zero-copy drivers unconditionally parse XDP TX metadata while ignoring the XDP_TX_METADATA option flag?
Looking at igc_xdp_xmit_zc() (and similarly in mlx5e_xsk_tx() and stmmac_xdp_xmit_zc()), the zero-copy path reads metadata directly without checking xdp_desc.options:
meta = xsk_buff_get_metadata(pool, xdp_desc.addr);
If an application sends a standard AF_XDP packet without requesting metadata (by clearing the XDP_TX_METADATA flag), the driver ignores desc->options and unconditionally reads the metadata memory. Since the memory might contain stale data, meta->flags could contain valid but bogus bits.
Could this trigger unintended hardware offloads, causing data corruption (bogus checksum offsets) or TX queue stalls (bogus launch times)?
[ ... ]
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) · line 2751
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2
-2@@ -2747,8 +2747,8 @@ 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, - &meta_req); +xsk_tx_metadata_request (pool, &meta, + &stmmac_xsk_tx_metadata_ops, &meta_req);
[Severity: High]
This isn't a bug introduced by this patch, but does stmmac_xdp_xmit_zc() permanently desynchronize the AF_XDP completion queue when dropping oversized frames?
Earlier in stmmac_xdp_xmit_zc(), if the frame length exceeds max_sdu, the descriptor is dropped via continue without issuing an XSK completion or posting a dummy hardware descriptor:
if (priv->est && priv->est->enable &&
priv->est->max_sdu[queue] &&
xdp_desc.len > priv->est->max_sdu[queue]) {
priv->xstats.max_sdu_txq_drop[queue]++;
continue;
}Since xsk_tx_peek_desc() reserves a slot in the AF_XDP completion queue (CQ), dropping the frame skips the hardware descriptor setup, and no completion interrupt is generated for this packet.
When a subsequent packet completes, xsk_tx_completed() will advance the CQ producer. Because CQ entries are positional, would user space receive the address of the dropped packet as a completion instead of the actually completed packet, permanently leaking descriptors?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH net 6/6] xsk: validate metadata when processing requests
Quoted 40 lines · On 07/28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Zero-copy drivers unconditionally parse XDP TX metadata ignoring theXDP_TX_METADATAoption flag.
- [High] The stmmac driver dropsAF_XDPTX frames without generating completions when the frame length exceedsmax_sdu, causing theAF_XDPcompletion queue (CQ) to permanently desynchronize. ----- Patch [6]: [PATCH net 6/6] xsk: validate metadata when processing requests --- commit
8967b4032038ba439c0a13ce2dc4cab303b87c2e
Author: Stanislav Fomichev <[email protected]>xsk: validate metadata when processing requests
The zero-copy path validates TX metadata while obtaining the descriptor context, then reads it again later when preparing the hardware request. Validate the metadata in
xsk_tx_metadata_request()and use the resulting flags snapshot for every feature check.drivers/net/ethernet/intel/igc/igc_main.c static void igc_xdp_xmit_zc(struct igc_ring *ring) · line 3086
Quoted 11 lines
drivers/net/ethernet/intel/igc/igc_main.c +1
-1@@ -3082,7 +3082,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 (pool, &meta, &igc_xsk_tx_metadata_ops, &meta_req);[Severity: High]
This is a pre-existing issue, but do the zero-copy drivers unconditionally parse XDP TX metadata while ignoring theXDP_TX_METADATAoption flag?Looking at
igc_xdp_xmit_zc()(and similarly inmlx5e_xsk_tx()andstmmac_xdp_xmit_zc()), the zero-copy path reads metadata directly without checking xdp_desc.options:meta = xsk_buff_get_metadata(pool, xdp_desc.addr);
Quoted 7 lines
If an application sends a standard
AF_XDPpacket without requesting metadata (by clearing theXDP_TX_METADATAflag), the driver ignoresdesc->optionsand unconditionally reads the metadata memory. Since the memory might contain stale data,meta->flagscould contain valid but bogus bits.Could this trigger unintended hardware offloads, causing data corruption (bogus checksum offsets) or TX queue stalls (bogus launch times)?
That seems like a valid pre-existing issue, will address separately.
Quoted 31 lines
[ ... ]
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) · line 2751
Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2
-2@@ -2747,8 +2747,8 @@ 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, - &meta_req); +xsk_tx_metadata_request (pool, &meta, + &stmmac_xsk_tx_metadata_ops, &meta_req);[Severity: High]
This isn't a bug introduced by this patch, but doesstmmac_xdp_xmit_zc()permanently desynchronize theAF_XDPcompletion queue when dropping oversized frames?Earlier in
stmmac_xdp_xmit_zc(), if the frame length exceedsmax_sdu, the descriptor is dropped via continue without issuing an XSK completion or posting a dummy hardware descriptor:if (priv->est && priv->est->enable && priv->est->max_sdu[queue] && xdp_desc.len > priv->est->max_sdu[queue]) { priv->xstats.max_sdu_txq_drop[queue]++; continue; }
Quoted 8 lines
Since
xsk_tx_peek_desc()reserves a slot in theAF_XDPcompletion queue (CQ), dropping the frame skips the hardware descriptor setup, and no completion interrupt is generated for this packet.When a subsequent packet completes,
xsk_tx_completed()will advance the CQ producer. Because CQ entries are positional, would user space receive the address of the dropped packet as a completion instead of the actually completed packet, permanently leaking descriptors?
Hmm, good point, will send a patch separately to reorder xsk_tx_peek_desc and max_sdu check.
Maciej Fijalkowski
maintainerQuoted 10 lines · Stanislav Fomichev
Cen Zhang reported a KASAN out-of-bounds read when
AF_XDPis configured with a TX metadata area smaller than structxsk_tx_metadata. The metadata is also shared with user space, so reading its flags more than once can produce inconsistent validation and processing decisions.Require enough space for the flags and one request field, validate the launch-time field against the configured metadata length, and use one snapshot of the flags while processing each request. Carry the validated decision through completion handling so later user-space changes cannot enable an unrequested completion timestamp.
Stan,
I'm taking week off and I see one sashiko-reported issue which is not a pre-existing one. Hope you could clarify it and unless no one picks the review of this set i'll get back at it next week.
Quoted 5 lines
static inline struct
xsk_tx_metadata*__xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data){(quoted diff) +1
-0struct xsk_tx_metadata *meta; + u64 flags;
Review
Quoted 10 lines
(quoted diff) +1
-1if (!pool->tx_metadata_len) 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, &flags))) return NULL; /* no way to signal the error to the user */ return meta;
The snapshotted flags are validated for size compliance in xsk_buff_valid_tx_metadata() but then discarded, returning the un-snapshotted user memory pointer (meta) to the driver. Later in the zero-copy driver path, xsk_tx_metadata_request() re-reads meta->flags directly from user memory:
include/net/xdp_sock.h:xsk_tx_metadata_request() {
...
if (meta->flags & XDP_TXMD_FLAGS_LAUNCH_TIME)
ops->tmo_request_launch_time(meta->request.launch_time, priv);
...
}Does this create a Time-of-Check to Time-of-Use (TOCTOU) race condition in the zero-copy TX metadata validation where userspace can concurrently enable launch time after the size validation?
Quoted 30 lines
Reported-by [email protected]Reported-by Cen Zhang (Microsoft) <[email protected]>Stanislav Fomichev (6):
xsk: require at least 16 bytes of TX metadata
xsk: pass TX metadata pointer by reference
xsk: clear metadata pointer when no timestamp is requested
xsk: validate launch-time metadata size
xsk: movexsk_tx_metadata_request()toxdp_sock_drv.h
xsk: validate metadata when processing requestsdrivers/net/ethernet/intel/igc/igc_main.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en.h | 2 +- .../net/ethernet/mellanox/mlx5/core/en/xdp.c | 15 ++-- .../net/ethernet/mellanox/mlx5/core/en/xdp.h | 4 +- .../ethernet/mellanox/mlx5/core/en/xsk/tx.c | 2 +- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 4 +- include/net/libeth/xsk.h | 2 +- include/net/xdp_sock.h | 45 ++--------- include/net/xdp_sock_drv.h | 77 ++++++++++++++++--- net/xdp/xdp_umem.c | 2 + net/xdp/xsk.c | 11 ++- net/xdp/xsk_buff_pool.c | 6 +- 12 files changed, 101 insertions(+), 71 deletions(-)Signature · 1 line
2.53.0-Meta
Quoted 12 lines · Maciej Fijalkowski
Quoted 10 lines · Stanislav Fomichev
Cen Zhang reported a KASAN out-of-bounds read when
AF_XDPis configured with a TX metadata area smaller than structxsk_tx_metadata. The metadata is also shared with user space, so reading its flags more than once can produce inconsistent validation and processing decisions.Require enough space for the flags and one request field, validate the launch-time field against the configured metadata length, and use one snapshot of the flags while processing each request. Carry the validated decision through completion handling so later user-space changes cannot enable an unrequested completion timestamp.
Quoted 5 lines
Stan,
I'm taking week off and I see one sashiko-reported issue which is not a pre-existing one. Hope you could clarify it and unless no one picks the review of this set i'll get back at it next week.
Went through the issues, I don't think it found anything interesting. Everything is pre-existing, but still worth addressing, will send those fixes separately.
Quoted 26 lines
Quoted 5 lines
static inline struct
xsk_tx_metadata*__xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data){(quoted diff) +1
-0struct xsk_tx_metadata *meta; + u64 flags;Review
Quoted 10 lines
(quoted diff) +1
-1if (!pool->tx_metadata_len) 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, &flags))) return NULL; /* no way to signal the error to the user */ return meta;The snapshotted flags are validated for size compliance in
xsk_buff_valid_tx_metadata()but then discarded, returning the un-snapshotted user memory pointer (meta) to the driver. Later in the zero-copy driver path,xsk_tx_metadata_request()re-readsmeta->flagsdirectly from user memory:include/net/xdp_sock.h:xsk_tx_metadata_request() { ... if (meta->flags & XDP_TXMD_FLAGS_LAUNCH_TIME) ops->tmo_request_launch_time(meta->request.launch_time, priv); ... }
Quoted 3 lines
Does this create a Time-of-Check to Time-of-Use (TOCTOU) race condition in the zero-copy TX metadata validation where userspace can concurrently enable launch time after the size validation?
For this, yes, it is explained in the commit message:
Note that only xsk_skb_metadata is properly using the flags, __xsk_buff_get_metadata ignores them. Next commits address that.
And eventually addressed in "[PATCH net 6/6] xsk: validate metadata when processing requests". Couldn't find a less confusing way to split the patches..
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 5 lines · On Mon, 27 Jul 2026 09:19:53 -0700 you wrote:
Cen Zhang reported a KASAN out-of-bounds read when
AF_XDPis configured with a TX metadata area smaller than structxsk_tx_metadata. The metadata is also shared with user space, so reading its flags more than once can produce inconsistent validation and processing decisions.
Quoted 7 lines
Require enough space for the flags and one request field, validate the launch-time field against the configured metadata length, and use one snapshot of the flags while processing each request. Carry the validated decision through completion handling so later user-space changes cannot enable an unrequested completion timestamp.
[...]
Here is the summary with links:
- [net,1/6] xsk: require at least 16 bytes of TX metadata
https://git.kernel.org/netdev/net/c/1bb30b181d9f - [net,2/6] xsk: pass TX metadata pointer by reference https://git.kernel.org/netdev/net/c/19366db6dfcc - [net,3/6] xsk: clear metadata pointer when no timestamp is requested https://git.kernel.org/netdev/net/c/9f60a67df8d3 - [net,4/6] xsk: validate launch-time metadata size https://git.kernel.org/netdev/net/c/439ce2dddf3d - [net,5/6] xsk: move xsk_tx_metadata_request() to xdp_sock_drv.h https://git.kernel.org/netdev/net/c/ddd0d6c5bfe2 - [net,6/6] xsk: validate metadata when processing requests https://git.kernel.org/netdev/net/c/849b1664dbda
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html