Skip to content

Commit 32654bb

Browse files
Stanislav FomichevMartin KaFai Lau
authored andcommitted
xsk: Try to make xdp_umem_reg extension a bit more future-proof
We recently found out that extending xsk_umem_reg might be a bit complicated due to not enforcing padding to be zero [0]. Add a couple of things to make it less error-prone: 1. Remove xdp_umem_reg_v2 since its sizeof is the same as xdp_umem_reg 2. Add BUILD_BUG_ON that checks that the size of xdp_umem_reg_v1 is less than xdp_umem_reg; presumably, when we get to v2, there is gonna be a similar line to enforce that sizeof(v2) > sizeof(v1) 3. Add BUILD_BUG_ON to make sure the last field plus its size matches the overall struct size. The intent is to demonstrate that we don't have any lingering padding. 0: https://lore.kernel.org/bpf/ZqI29QE+5JnkdPmE@boxer/T/#me03113f7c2458fd08f3c4114a7a9472ac3646c98 Reported-by: Julian Schindel <[email protected]> Cc: Magnus Karlsson <[email protected]> Reviewed-by: Maciej Fijalkowski <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 1cbe814 commit 32654bb

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

net/xdp/xsk.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,14 +1320,6 @@ struct xdp_umem_reg_v1 {
13201320
__u32 headroom;
13211321
};
13221322

1323-
struct xdp_umem_reg_v2 {
1324-
__u64 addr; /* Start of packet data area */
1325-
__u64 len; /* Length of packet data area */
1326-
__u32 chunk_size;
1327-
__u32 headroom;
1328-
__u32 flags;
1329-
};
1330-
13311323
static int xsk_setsockopt(struct socket *sock, int level, int optname,
13321324
sockptr_t optval, unsigned int optlen)
13331325
{
@@ -1371,10 +1363,19 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
13711363

13721364
if (optlen < sizeof(struct xdp_umem_reg_v1))
13731365
return -EINVAL;
1374-
else if (optlen < sizeof(struct xdp_umem_reg_v2))
1375-
mr_size = sizeof(struct xdp_umem_reg_v1);
13761366
else if (optlen < sizeof(mr))
1377-
mr_size = sizeof(struct xdp_umem_reg_v2);
1367+
mr_size = sizeof(struct xdp_umem_reg_v1);
1368+
1369+
BUILD_BUG_ON(sizeof(struct xdp_umem_reg_v1) >= sizeof(struct xdp_umem_reg));
1370+
1371+
/* Make sure the last field of the struct doesn't have
1372+
* uninitialized padding. All padding has to be explicit
1373+
* and has to be set to zero by the userspace to make
1374+
* struct xdp_umem_reg extensible in the future.
1375+
*/
1376+
BUILD_BUG_ON(offsetof(struct xdp_umem_reg, tx_metadata_len) +
1377+
sizeof_field(struct xdp_umem_reg, tx_metadata_len) !=
1378+
sizeof(struct xdp_umem_reg));
13781379

13791380
if (copy_from_sockptr(&mr, optval, mr_size))
13801381
return -EFAULT;

0 commit comments

Comments
 (0)