Commit 03cfd4e0 authored by Björn Töpel's avatar Björn Töpel Committed by Greg Kroah-Hartman

xsk: Add overflow check for u64 division, stored into u32

commit b16a87d0 upstream.

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: c0c77d8f ("xsk: add user memory registration support sockopt")
Reported-by: default avatar"Minh Bùi Quang" <minhquangbui99@gmail.com>
Signed-off-by: default avatarBjörn Töpel <bjorn.topel@intel.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarJonathan Lemon <jonathan.lemon@gmail.com>
Link: https://lore.kernel.org/bpf/CACtPs=GGvV-_Yj6rbpzTVnopgi5nhMoCcTkSkYrJHGQHJWFZMQ@mail.gmail.com/
Link: https://lore.kernel.org/bpf/20200525080400.13195-1-bjorn.topel@gmail.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent a4c9756a
...@@ -258,8 +258,8 @@ static int xdp_umem_account_pages(struct xdp_umem *umem) ...@@ -258,8 +258,8 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
{ {
u32 chunk_size = mr->chunk_size, headroom = mr->headroom; u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
u64 npgs, addr = mr->addr, size = mr->len;
unsigned int chunks, chunks_per_page; unsigned int chunks, chunks_per_page;
u64 addr = mr->addr, size = mr->len;
int err, i; int err, i;
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
...@@ -285,6 +285,10 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) ...@@ -285,6 +285,10 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
if ((addr + size) < addr) if ((addr + size) < addr)
return -EINVAL; return -EINVAL;
npgs = div_u64(size, PAGE_SIZE);
if (npgs > U32_MAX)
return -EINVAL;
chunks = (unsigned int)div_u64(size, chunk_size); chunks = (unsigned int)div_u64(size, chunk_size);
if (chunks == 0) if (chunks == 0)
return -EINVAL; return -EINVAL;
...@@ -303,7 +307,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) ...@@ -303,7 +307,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
umem->props.size = size; umem->props.size = size;
umem->headroom = headroom; umem->headroom = headroom;
umem->chunk_size_nohr = chunk_size - headroom; umem->chunk_size_nohr = chunk_size - headroom;
umem->npgs = size / PAGE_SIZE; umem->npgs = (u32)npgs;
umem->pgs = NULL; umem->pgs = NULL;
umem->user = NULL; umem->user = NULL;
INIT_LIST_HEAD(&umem->xsk_list); INIT_LIST_HEAD(&umem->xsk_list);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment