Skip to contentSkip to conversation

xsk: Fix unaligned descriptor validation

PATCH Applied Kal Conley 2 participants 5 messages Apr 5, 2023 – Apr 6, 2023

Subsystems (6)
lore
Share & tools
lore

Kal Conley

author
#0 lore raw

v2 cover xsk: Fix unaligned descriptor validation

This patchset includes the test with the bugfix as requested here:
https://lore.kernel.org/all/[email protected]/

Patch #1 (the bugfix) is identical to the previous submission except that I improved the commit message slightly.

Magnus: I improved the test code a little different than you asked since I thought this was a little simpler than having a separate function for now. Hopefully, you can live with this :-).

Kal Conley (2):
xsk: Fix unaligned descriptor validation
selftests: xsk: Add test UNALIGNED_INV_DESC_4K1_FRAME_SIZE

 include/net/xsk_buff_pool.h              |  9 ++-------
 net/xdp/xsk_queue.h                      |  1 +
 tools/testing/selftests/bpf/xskxceiver.c | 24 ++++++++++++++++++++++++
 tools/testing/selftests/bpf/xskxceiver.h |  1 +
 4 files changed, 28 insertions(+), 7 deletions(-)
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v2 1/2 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. For example, descriptors in zero-copy mode that overrun the end of the UMEM but not a page boundary are (incorrectly) considered valid. The UMEM boundary 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
author
#2 lore raw

PATCH v2 2/2 selftests: xsk: Add test UNALIGNED_INV_DESC_4K1_FRAME_SIZE

In reply to Kal Conley

Add unaligned descriptor test for frame size of 4001. Using an odd frame size ensures that the end of the UMEM is not near a page boundary. This allows testing descriptors that staddle the end of the UMEM but not a page.

This test used to fail without the previous commit ("xsk: Add check for unaligned descriptors that overrun UMEM").

---
 tools/testing/selftests/bpf/xskxceiver.c | 24 ++++++++++++++++++++++++
 tools/testing/selftests/bpf/xskxceiver.h |  1 +
 2 files changed, 25 insertions(+)
tools/testing/selftests/bpf/xskxceiver.c +24 -0
index 1a4bdd5aa78c..5a9691e942de 100644
@@ -69,6 +69,7 @@
  */
 
 #define _GNU_SOURCE
+#include <assert.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <getopt.h>
@@ -1876,6 +1877,29 @@ static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_
 		test->ifobj_rx->umem->unaligned_mode = true;
 		testapp_invalid_desc(test);
 		break;
+	case TEST_TYPE_UNALIGNED_INV_DESC_4K1_FRAME: {
+		u64 page_size, umem_size;
+
+		if (!hugepages_present(test->ifobj_tx)) {
+			ksft_test_result_skip("No 2M huge pages present.\n");
+			return;
+		}
+		test_spec_set_name(test, "UNALIGNED_INV_DESC_4K1_FRAME_SIZE");
+		/* Odd frame size so the UMEM doesn't end near a page boundary. */
+		test->ifobj_tx->umem->frame_size = 4001;
+		test->ifobj_rx->umem->frame_size = 4001;
+		test->ifobj_tx->umem->unaligned_mode = true;
+		test->ifobj_rx->umem->unaligned_mode = true;
+		/* This test exists to test descriptors that staddle the end of
+		 * the UMEM but not a page.
+		 */
+		page_size = sysconf(_SC_PAGESIZE);
+		umem_size = test->ifobj_tx->umem->num_frames * test->ifobj_tx->umem->frame_size;
+		assert(umem_size % page_size > PKT_SIZE);
+		assert(umem_size % page_size < page_size - PKT_SIZE);
+		testapp_invalid_desc(test);
+		break;
+	}
 	case TEST_TYPE_UNALIGNED:
 		if (!testapp_unaligned(test))
 			return;
tools/testing/selftests/bpf/xskxceiver.h +1 -0
index cc24ab72f3ff..919327807a4e 100644
@@ -78,6 +78,7 @@ enum test_type {
 	TEST_TYPE_ALIGNED_INV_DESC,
 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
 	TEST_TYPE_UNALIGNED_INV_DESC,
+	TEST_TYPE_UNALIGNED_INV_DESC_4K1_FRAME,
 	TEST_TYPE_HEADROOM,
 	TEST_TYPE_TEARDOWN,
 	TEST_TYPE_BIDI,
Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH bpf-next v2 2/2] selftests: xsk: Add test UNALIGNED_INV_DESC_4K1_FRAME_SIZE

Quoted 7 lines

Add unaligned descriptor test for frame size of 4001. Using an odd frame size ensures that the end of the UMEM is not near a page boundary. This allows testing descriptors that staddle the end of the UMEM but not a page.

This test used to fail without the previous commit ("xsk: Add check for unaligned descriptors that overrun UMEM").

Sorry I forgot to update the commit message with the new commit label. If accepted, please change the second paragraph to :

This test used to fail without the previous commit ("xsk: Fix unaligned descriptor validation").

#4 lore raw
In reply to Kal Conley
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 11 lines · On Thu, 6 Apr 2023 01:59:17 +0200 you wrote:

This patchset includes the test with the bugfix as requested here:
https://lore.kernel.org/all/[email protected]/

Patch #1 (the bugfix) is identical to the previous submission except that I improved the commit message slightly.

Magnus: I improved the test code a little different than you asked since I thought this was a little simpler than having a separate function for now. Hopefully, you can live with this :-).

[...]

Here is the summary with links:
- [bpf-next,v2,1/2] xsk: Fix unaligned descriptor validation

    https://git.kernel.org/bpf/bpf-next/c/d769ccaf957f
  - [bpf-next,v2,2/2] selftests: xsk: Add test UNALIGNED_INV_DESC_4K1_FRAME_SIZE
    https://git.kernel.org/bpf/bpf-next/c/c0801598e543

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html