Skip to content

Commit c7df481

Browse files
kalcutterborkmann
authored andcommitted
xsk: Add missing overflow check in xdp_umem_reg
The number of chunks can overflow u32. Make sure to return -EINVAL on overflow. Also remove a redundant u32 cast assigning umem->npgs. Fixes: bbff2f3 ("xsk: new descriptor addressing scheme") Signed-off-by: Kal Conley <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Magnus Karlsson <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a33a6ea commit c7df481

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

net/xdp/xdp_umem.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
150150

151151
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
152152
{
153-
u32 npgs_rem, chunk_size = mr->chunk_size, headroom = mr->headroom;
154153
bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
155-
u64 npgs, addr = mr->addr, size = mr->len;
156-
unsigned int chunks, chunks_rem;
154+
u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
155+
u64 addr = mr->addr, size = mr->len;
156+
u32 chunks_rem, npgs_rem;
157+
u64 chunks, npgs;
157158
int err;
158159

159160
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
@@ -188,8 +189,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
188189
if (npgs > U32_MAX)
189190
return -EINVAL;
190191

191-
chunks = (unsigned int)div_u64_rem(size, chunk_size, &chunks_rem);
192-
if (chunks == 0)
192+
chunks = div_u64_rem(size, chunk_size, &chunks_rem);
193+
if (!chunks || chunks > U32_MAX)
193194
return -EINVAL;
194195

195196
if (!unaligned_chunks && chunks_rem)
@@ -202,7 +203,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
202203
umem->headroom = headroom;
203204
umem->chunk_size = chunk_size;
204205
umem->chunks = chunks;
205-
umem->npgs = (u32)npgs;
206+
umem->npgs = npgs;
206207
umem->pgs = NULL;
207208
umem->user = NULL;
208209
umem->flags = mr->flags;

0 commit comments

Comments
 (0)