Skip to contentSkip to conversation

xsk: Fix unaligned descriptor validation

PATCH Superseded Kal Conley 3 participants 6 messages Apr 3, 2023 – Apr 6, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kal Conley

author
#0 lore raw

PATCH 1/1 xsk: Fix unaligned descriptor validation

Make sure unaligned descriptors that straddle the end of the UMEM are considered invalid. Currently, descriptor validation is broken for zero-copy mode which only checks descriptors at page granularity. Descriptors that cross the end of the UMEM but not a page boundary may be therefore incorrectly considered valid. The check needs to happen before the page boundary and contiguity checks in xp_desc_crosses_non_contig_pg. Do this check in xp_unaligned_validate_desc instead like xp_check_unaligned already does.

Fixes 2b43470add8c ("xsk: Introduce AF_XDP buffer allocation API")
---
 include/net/xsk_buff_pool.h | 9 ++-------
 net/xdp/xsk_queue.h         | 1 +
 2 files changed, 3 insertions(+), 7 deletions(-)
include/net/xsk_buff_pool.h +2 -7
index 3e952e569418..d318c769b445 100644
@@ -180,13 +180,8 @@ static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
 	if (likely(!cross_pg))
 		return false;
 
-	if (pool->dma_pages_cnt) {
-		return !(pool->dma_pages[addr >> PAGE_SHIFT] &
-			 XSK_NEXT_PG_CONTIG_MASK);
-	}
-
-	/* skb path */
-	return addr + len > pool->addrs_cnt;
+	return pool->dma_pages_cnt &&
+	       !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
 }
 
 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
net/xdp/xsk_queue.h +1 -0
index bfb2a7e50c26..66c6f57c9c44 100644
@@ -162,6 +162,7 @@ static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
 		return false;
 
 	if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
+	    addr + desc->len > pool->addrs_cnt ||
 	    xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
 		return false;
 
Signature · 1 line
2.39.2
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kal Conley

Make sure unaligned descriptors that straddle the end of the UMEM are considered invalid. Currently, descriptor validation is broken for zero-copy mode which only checks descriptors at page granularity. Descriptors that cross the end of the UMEM but not a page boundary may be therefore incorrectly considered valid. The check needs to happen before the page boundary and contiguity checks in xp_desc_crosses_non_contig_pg. Do this check in xp_unaligned_validate_desc instead like xp_check_unaligned already does.

Thanks for catching this Kal.

Quoted 42 lines
Fixes 2b43470add8c ("xsk: Introduce AF_XDP buffer allocation API")
---
 include/net/xsk_buff_pool.h | 9 ++-------
 net/xdp/xsk_queue.h         | 1 +
 2 files changed, 3 insertions(+), 7 deletions(-)
include/net/xsk_buff_pool.h +2 -7
index 3e952e569418..d318c769b445 100644
@@ -180,13 +180,8 @@ static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
        if (likely(!cross_pg))
                return false;
 
-       if (pool->dma_pages_cnt) {
-               return !(pool->dma_pages[addr >> PAGE_SHIFT] &
-                        XSK_NEXT_PG_CONTIG_MASK);
-       }
-
-       /* skb path */
-       return addr + len > pool->addrs_cnt;
+       return pool->dma_pages_cnt &&
+              !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
 }
 
 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
net/xdp/xsk_queue.h +1 -1
index bfb2a7e50c26..66c6f57c9c44 100644
@@ -162,6 +162,7 @@ static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
                return false;
 
        if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
+           addr + desc->len > pool->addrs_cnt ||
            xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
                return false;
 
--

2.39.2

#2 lore raw
Quoted 11 lines · Magnus Karlsson
Quoted 9 lines · Kal Conley

Make sure unaligned descriptors that straddle the end of the UMEM are considered invalid. Currently, descriptor validation is broken for zero-copy mode which only checks descriptors at page granularity. Descriptors that cross the end of the UMEM but not a page boundary may be therefore incorrectly considered valid. The check needs to happen before the page boundary and contiguity checks in xp_desc_crosses_non_contig_pg. Do this check in xp_unaligned_validate_desc instead like xp_check_unaligned already does.

Quoted 3 lines

Thanks for catching this Kal.

Is this case covered by an existing test?

#3 lore raw
Quoted 2 lines

Is this case covered by an existing test?

No. I submitted a test but I was asked to make minor changes to it. I plan to submit the test once this gets picked up on bpf-next.

#4 lore raw
Quoted 5 lines · Kal Cutter Conley
Quoted 2 lines

Is this case covered by an existing test?

No. I submitted a test but I was asked to make minor changes to it. I plan to submit the test once this gets picked up on bpf-next.

Since you already have a test case, it is better to submit them together such that this case can be covered earlier than later.

Other xskxceiver fixes have already landed to bpf-next. imo, I think for this particular case, bpf-next for both the fix and the test is fine.

#5 lore raw
Quoted 5 lines

Since you already have a test case, it is better to submit them together such that this case can be covered earlier than later.

Other xskxceiver fixes have already landed to bpf-next. imo, I think for this particular case, bpf-next for both the fix and the test is fine.

Done. See: https://lore.kernel.org/all/[email protected]/