All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] xsk: add overflow check for u64 division, stored into u32
@ 2020-05-25  8:03 Björn Töpel
  2020-05-25 18:30 ` Jonathan Lemon
  2020-05-25 22:19 ` Daniel Borkmann
  0 siblings, 2 replies; 3+ messages in thread
From: Björn Töpel @ 2020-05-25  8:03 UTC (permalink / raw)
  To: magnus.karlsson, jonathan.lemon, ast, daniel, netdev, bpf
  Cc: Björn Töpel, maximmi, Minh Bùi Quang

From: Björn Töpel <bjorn.topel@intel.com>

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.

Link: https://lore.kernel.org/bpf/CACtPs=GGvV-_Yj6rbpzTVnopgi5nhMoCcTkSkYrJHGQHJWFZMQ@mail.gmail.com/
Fixes: c0c77d8fb787 ("xsk: add user memory registration support sockopt")
Reported-by: "Minh Bùi Quang" <minhquangbui99@gmail.com>
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
---
 net/xdp/xdp_umem.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c
index ed7a6060f73c..3889bd9aec46 100644
--- a/net/xdp/xdp_umem.c
+++ b/net/xdp/xdp_umem.c
@@ -341,8 +341,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
 {
 	bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
 	u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
+	u64 npgs, addr = mr->addr, size = mr->len;
 	unsigned int chunks, chunks_per_page;
-	u64 addr = mr->addr, size = mr->len;
 	int err;
 
 	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)
 	if ((addr + size) < addr)
 		return -EINVAL;
 
+	npgs = div_u64(size, PAGE_SIZE);
+	if (npgs > U32_MAX)
+		return -EINVAL;
+
 	chunks = (unsigned int)div_u64(size, chunk_size);
 	if (chunks == 0)
 		return -EINVAL;
@@ -391,7 +395,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
 	umem->size = size;
 	umem->headroom = headroom;
 	umem->chunk_size_nohr = chunk_size - headroom;
-	umem->npgs = size / PAGE_SIZE;
+	umem->npgs = (u32)npgs;
 	umem->pgs = NULL;
 	umem->user = NULL;
 	umem->flags = mr->flags;

base-commit: d04322a0da1e86aedaa322ce933cfb8c0191d1eb
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] xsk: add overflow check for u64 division, stored into u32
  2020-05-25  8:03 [PATCH bpf] xsk: add overflow check for u64 division, stored into u32 Björn Töpel
@ 2020-05-25 18:30 ` Jonathan Lemon
  2020-05-25 22:19 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Lemon @ 2020-05-25 18:30 UTC (permalink / raw)
  To: Björn Töpel
  Cc: magnus.karlsson, jonathan.lemon, ast, daniel, netdev, bpf,
	Björn Töpel, maximmi, Minh Bùi Quang

On Mon, May 25, 2020 at 10:03:59AM +0200, Björn Töpel wrote:
> From: Björn Töpel <bjorn.topel@intel.com>
> 
> 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.
> 
> Link: https://lore.kernel.org/bpf/CACtPs=GGvV-_Yj6rbpzTVnopgi5nhMoCcTkSkYrJHGQHJWFZMQ@mail.gmail.com/
> Fixes: c0c77d8fb787 ("xsk: add user memory registration support sockopt")
> Reported-by: "Minh Bùi Quang" <minhquangbui99@gmail.com>
> Signed-off-by: Björn Töpel <bjorn.topel@intel.com>

Acked-by: Jonathan Lemon <jonathan.lemon@gmail.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] xsk: add overflow check for u64 division, stored into u32
  2020-05-25  8:03 [PATCH bpf] xsk: add overflow check for u64 division, stored into u32 Björn Töpel
  2020-05-25 18:30 ` Jonathan Lemon
@ 2020-05-25 22:19 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2020-05-25 22:19 UTC (permalink / raw)
  To: Björn Töpel, magnus.karlsson, jonathan.lemon, ast, netdev, bpf
  Cc: Björn Töpel, maximmi, Minh Bùi Quang

On 5/25/20 10:03 AM, Björn Töpel wrote:
> From: Björn Töpel <bjorn.topel@intel.com>
> 
> 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.
> 
> Link: https://lore.kernel.org/bpf/CACtPs=GGvV-_Yj6rbpzTVnopgi5nhMoCcTkSkYrJHGQHJWFZMQ@mail.gmail.com/
> Fixes: c0c77d8fb787 ("xsk: add user memory registration support sockopt")
> Reported-by: "Minh Bùi Quang" <minhquangbui99@gmail.com>
> Signed-off-by: Björn Töpel <bjorn.topel@intel.com>

Applied, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-05-25 22:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-25  8:03 [PATCH bpf] xsk: add overflow check for u64 division, stored into u32 Björn Töpel
2020-05-25 18:30 ` Jonathan Lemon
2020-05-25 22:19 ` Daniel Borkmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.