Skip to content

Commit b16a87d

Browse files
Björn Töpelborkmann
authored andcommitted
xsk: Add overflow check for u64 division, stored into u32
The npgs member of struct xdp_umem is an u32 entity, and stores the number of pages the UMEM consumes. The calculation of npgs npgs = size / PAGE_SIZE can overflow. To avoid overflow scenarios, the division is now first stored in a u64, and the result is verified to fit into 32b. An alternative would be storing the npgs as a u64, however, this wastes memory and is an unrealisticly large packet area. Fixes: c0c77d8 ("xsk: add user memory registration support sockopt") Reported-by: "Minh Bùi Quang" <[email protected]> Signed-off-by: Björn Töpel <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Jonathan Lemon <[email protected]> Link: https://lore.kernel.org/bpf/CACtPs=GGvV-_Yj6rbpzTVnopgi5nhMoCcTkSkYrJHGQHJWFZMQ@mail.gmail.com/ Link: https://lore.kernel.org/bpf/[email protected]
1 parent d04322a commit b16a87d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

net/xdp/xdp_umem.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
341341
{
342342
bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
343343
u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
344+
u64 npgs, addr = mr->addr, size = mr->len;
344345
unsigned int chunks, chunks_per_page;
345-
u64 addr = mr->addr, size = mr->len;
346346
int err;
347347

348348
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
@@ -372,6 +372,10 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
372372
if ((addr + size) < addr)
373373
return -EINVAL;
374374

375+
npgs = div_u64(size, PAGE_SIZE);
376+
if (npgs > U32_MAX)
377+
return -EINVAL;
378+
375379
chunks = (unsigned int)div_u64(size, chunk_size);
376380
if (chunks == 0)
377381
return -EINVAL;
@@ -391,7 +395,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
391395
umem->size = size;
392396
umem->headroom = headroom;
393397
umem->chunk_size_nohr = chunk_size - headroom;
394-
umem->npgs = size / PAGE_SIZE;
398+
umem->npgs = (u32)npgs;
395399
umem->pgs = NULL;
396400
umem->user = NULL;
397401
umem->flags = mr->flags;

0 commit comments

Comments
 (0)