mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] RDMA/rxe: reject MRs whose iova range wraps, keep ODP lengths unsigned
@ 2026-11-25 20:40 Cygnus Qiu
  0 siblings, 0 replies; only message in thread
From: Cygnus Qiu @ 2026-11-25 20:40 UTC (permalink / raw)
  To: zyjzyj2000, jgg, leon
  Cc: linux-rdma, linux-kernel, yanjun.zhu, Cygnus Qiu, stable

rxe_check_pagefault() bounds the ODP page index against the umem inside
a loop over the pages of the access. When the access ends at 2^64,
iova + length is 0 and the loop does not run, so neither that bound nor
the HMM_PFN_VALID and HMM_PFN_WRITE checks apply. mr_check_range()
admits such an access when the MR's own iova range ends at 2^64, which
rxe_reg_user_mr() accepts. __rxe_odp_mr_copy() and the atomic write and
flush helpers then index map.pfn_list[] with (iova - ib_umem_start()) >>
page_shift, which the registrant controls, and the page derived from the
entry past pfn_list[] is copied to or from the peer.

rxe_check_pagefault() also keeps the index in an int, so an index of
2^32 or more passes the bound truncated, while the atomic write and
flush helpers use all of it.

Reject an MR whose iova + length overflows u64 in rxe_reg_user_mr();
mr_check_range() then keeps every access below 2^64. Carry the length
as unsigned int through mr_check_range() and the ODP helpers, the page
index as unsigned long, and the fault byte count as u64, the type
ib_umem_odp_map_dma_and_lock() takes and rxe_odp_init_pages() needs for
the whole umem.

Found with afb-afl, an LLM-driven kernel audit/repro pipeline; the fix
and this changelog were also drafted with LLM assistance. Reproduced on
an unmodified KASAN build, with the ODP MR registered by an unprivileged
user and the requests sent by a RoCEv2 peer. An RDMA READ of 0x100
bytes at the end of a two-page MR whose iova range ends at 2^64:

  BUG: KASAN: slab-out-of-bounds in rxe_odp_mr_copy+0x357/0x400
  Read of size 8 at addr ffff888103e11390 by task kworker/u16:0/13
  Workqueue: rxe_wq do_work
  Call Trace:
   rxe_odp_mr_copy+0x357/0x400
   rxe_receiver+0x16cc/0x9830
   do_work+0x15d/0x490
   process_one_work+0x6ef/0xf20
   worker_thread+0x501/0xd60
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30
  Allocated by task 180:
   __kvmalloc_node_noprof+0x210/0x770
   ib_init_umem_odp+0x21e/0x3a0
   ib_umem_odp_get+0x229/0x350
   rxe_odp_mr_init_user+0x5b/0x330
   rxe_reg_user_mr+0x1d5/0x2e0
   ib_uverbs_reg_mr+0x3ce/0x920
   ib_uverbs_handler_UVERBS_METHOD_INVOKE_WRITE+0x2b5/0x470
   ib_uverbs_cmd_verbs+0x2291/0x3410
   ib_uverbs_ioctl+0x175/0x280
   __x64_sys_ioctl+0x13c/0x1d0
   do_syscall_64+0x95/0x480
   entry_SYSCALL_64_after_hwframe+0x76/0x7e
  The buggy address belongs to the object at ffff888103e11380
   which belongs to the cache kmalloc-16 of size 16
  The buggy address is located 0 bytes to the right of
   allocated 16-byte region [ffff888103e11380, ffff888103e11390)
  Oops: general protection fault, maybe for address 0xffff888100d586ca: 0000 [#1] SMP KASAN NOPTI
  Workqueue: rxe_wq do_work
  RIP: 0010:memcpy+0xc/0x30
  Call Trace:
   rxe_odp_mr_copy+0x28f/0x400
   rxe_receiver+0x16cc/0x9830
   do_work+0x15d/0x490
   process_one_work+0x6ef/0xf20
   worker_thread+0x501/0xd60
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30

An ATOMIC WRITE to an ODP MR whose iova is 2^44 above the user address:

  BUG: unable to handle page fault for address: ffffed112051c110
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP KASAN NOPTI
  Workqueue: rxe_wq do_work
  RIP: 0010:rxe_odp_do_atomic_write+0x131/0x2f0
  Call Trace:
   rxe_receiver+0x1cb3/0x9830
   do_work+0x15d/0x490
   process_one_work+0x6ef/0xf20
   worker_thread+0x501/0xd60
   kthread+0x34d/0x460
   ret_from_fork+0x339/0x620
   ret_from_fork_asm+0x1a/0x30

With this patch the first MR is not registered (EINVAL), the ATOMIC
WRITE is NAKed with no report, and in-range READ and ATOMIC WRITE
results are unchanged. The int lengths also made ODP MRs of 2 GiB to
4 GiB fail to register and NAKed a 2 GiB FLUSH; both now succeed. The
rdma-core v65.0 tests, ODP included, pass with and without this patch.

Fixes: 2fae67ab63db ("RDMA/rxe: Add support for Send/Recv/Write/Read with ODP")
Cc: stable@vger.kernel.org # 6.15.x: 7797bbb0e86f: RDMA/rxe: bound the ODP page index against the umem in rxe_check_pagefault()
Signed-off-by: Cygnus Qiu <eritque-arcus@ikuyo.dev>
---
Changes in v3:
- Reject the wrap when the MR is registered instead of on each access
  in rxe_odp_map_range_and_lock(), as Leon asked. An MR without ODP
  whose range ends at 2^64 is now rejected too. Zhu's Reviewed-by is
  dropped since the fix changed.
- Carry the access length as unsigned int through mr_check_range() and
  the ODP helpers, as Leon asked. It was unsigned int in rxe_mr_copy()
  and rxe_flush_pmem_iova(), size_t in mr_check_range(), int in
  rxe_odp_mr_copy() and the helpers below it, and u64 in
  ib_umem_odp_map_dma_and_lock().
- Full call traces in the changelog.
- v2: https://lore.kernel.org/r/20261121215426.50988-1-eritque-arcus@ikuyo.dev

Tested in QEMU on rdma.git wip/leon-for-next 94e81a1f3ffa (v7.4-rc2+),
where the traces above come from. Applies to for-next and for-rc. The
wrap was run only through READ and the 2^44 index only through ATOMIC
WRITE; neither was tried on the flush path.

This completes commit 7797bbb0e86f, whose bound sits in the loop that
a wrapping range skips; stable trees need that commit first.

process_flush() has the same mix: for IB_FLUSH_MR it passes the u64 MR
length to rxe_flush_pmem_iova(), which takes an unsigned int, so only
length mod 4 GiB of a larger MR is written back. That was found by
reading, not run; I can send a fix as a separate patch if you want.

Without KASAN, on mainline 4405c18c95d2, an RDMA READ or WRITE at the
end of the range reads or overwrites a physical page the registrant
chooses. On that lab kernel, with CONFIG_RANDOMIZE_BASE=n, this was
taken from an unprivileged user to uid 0; with KASLR the chain also
needs an address leak. The rxe link has to be created by an
administrator; the trigger needs only the uverbs device. A reproducer
and the test logs are available on request.

 drivers/infiniband/sw/rxe/rxe_loc.h   |  8 ++++----
 drivers/infiniband/sw/rxe/rxe_mr.c    |  2 +-
 drivers/infiniband/sw/rxe/rxe_odp.c   | 22 ++++++++++++----------
 drivers/infiniband/sw/rxe/rxe_verbs.c |  4 ++++
 4 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/drivers/infiniband/sw/rxe/rxe_loc.h b/drivers/infiniband/sw/rxe/rxe_loc.h
index 9a8e96dfb7..aa09279f24 100644
--- a/drivers/infiniband/sw/rxe/rxe_loc.h
+++ b/drivers/infiniband/sw/rxe/rxe_loc.h
@@ -72,7 +72,7 @@ enum resp_states rxe_mr_do_atomic_op(struct rxe_mr *mr, u64 iova, int opcode,
 enum resp_states rxe_mr_do_atomic_write(struct rxe_mr *mr, u64 iova, u64 value);
 struct rxe_mr *lookup_mr(struct rxe_pd *pd, int access, u32 key,
 			 enum rxe_mr_lookup_type type);
-int mr_check_range(struct rxe_mr *mr, u64 iova, size_t length);
+int mr_check_range(struct rxe_mr *mr, u64 iova, unsigned int length);
 int advance_dma_data(struct rxe_dma_info *dma, unsigned int length);
 int rxe_invalidate_mr(struct rxe_qp *qp, u32 key);
 int rxe_reg_fast_mr(struct rxe_qp *qp, struct rxe_send_wqe *wqe);
@@ -197,8 +197,8 @@ extern const struct mmu_interval_notifier_ops rxe_mn_ops;
 #if defined CONFIG_INFINIBAND_ON_DEMAND_PAGING
 int rxe_odp_mr_init_user(struct rxe_dev *rxe, u64 start, u64 length,
 			 u64 iova, int access_flags, struct rxe_mr *mr);
-int rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr, int length,
-		    enum rxe_mr_copy_dir dir);
+int rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr,
+		    unsigned int length, enum rxe_mr_copy_dir dir);
 enum resp_states rxe_odp_atomic_op(struct rxe_mr *mr, u64 iova, int opcode,
 				   u64 compare, u64 swap_add, u64 *orig_val);
 int rxe_odp_flush_pmem_iova(struct rxe_mr *mr, u64 iova,
@@ -215,7 +215,7 @@ rxe_odp_mr_init_user(struct rxe_dev *rxe, u64 start, u64 length, u64 iova,
 	return -EOPNOTSUPP;
 }
 static inline int rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr,
-				  int length, enum rxe_mr_copy_dir dir)
+				  unsigned int length, enum rxe_mr_copy_dir dir)
 {
 	return -EOPNOTSUPP;
 }
diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c
index 13cb28d391..152438bf2d 100644
--- a/drivers/infiniband/sw/rxe/rxe_mr.c
+++ b/drivers/infiniband/sw/rxe/rxe_mr.c
@@ -24,7 +24,7 @@ u8 rxe_get_next_key(u32 last_key)
 	return key;
 }
 
-int mr_check_range(struct rxe_mr *mr, u64 iova, size_t length)
+int mr_check_range(struct rxe_mr *mr, u64 iova, unsigned int length)
 {
 	switch (mr->ibmr.type) {
 	case IB_MR_TYPE_DMA:
diff --git a/drivers/infiniband/sw/rxe/rxe_odp.c b/drivers/infiniband/sw/rxe/rxe_odp.c
index 73ed4c45fe..522013ce22 100644
--- a/drivers/infiniband/sw/rxe/rxe_odp.c
+++ b/drivers/infiniband/sw/rxe/rxe_odp.c
@@ -41,7 +41,7 @@ const struct mmu_interval_notifier_ops rxe_mn_ops = {
 #define RXE_PAGEFAULT_DEFAULT 0
 #define RXE_PAGEFAULT_RDONLY BIT(0)
 #define RXE_PAGEFAULT_SNAPSHOT BIT(1)
-static int rxe_odp_do_pagefault_and_lock(struct rxe_mr *mr, u64 user_va, int bcnt, u32 flags)
+static int rxe_odp_do_pagefault_and_lock(struct rxe_mr *mr, u64 user_va, u64 bcnt, u32 flags)
 {
 	struct ib_umem_odp *umem_odp = to_ib_umem_odp(mr->umem);
 	bool fault = !(flags & RXE_PAGEFAULT_SNAPSHOT);
@@ -120,12 +120,12 @@ int rxe_odp_mr_init_user(struct rxe_dev *rxe, u64 start, u64 length,
 }
 
 static inline bool rxe_check_pagefault(struct ib_umem_odp *umem_odp, u64 iova,
-				       int length, bool write)
+				       unsigned int length, bool write)
 {
 	bool need_fault = false;
 	u64 access = HMM_PFN_VALID;
 	u64 addr;
-	int idx;
+	unsigned long idx;
 
 	if (write)
 		access |= HMM_PFN_WRITE;
@@ -161,14 +161,15 @@ static unsigned long rxe_odp_iova_to_page_offset(struct ib_umem_odp *umem_odp, u
 	return iova & (BIT(umem_odp->page_shift) - 1);
 }
 
-static int rxe_odp_map_range_and_lock(struct rxe_mr *mr, u64 iova, int length, u32 flags)
+static int rxe_odp_map_range_and_lock(struct rxe_mr *mr, u64 iova,
+				      unsigned int length, u32 flags)
 {
 	struct ib_umem_odp *umem_odp = to_ib_umem_odp(mr->umem);
 	bool write = !(flags & RXE_PAGEFAULT_RDONLY);
 	bool need_fault;
 	int err;
 
-	if (unlikely(length < 1))
+	if (unlikely(!length))
 		return -EINVAL;
 
 	mutex_lock(&umem_odp->umem_mutex);
@@ -194,11 +195,12 @@ static int rxe_odp_map_range_and_lock(struct rxe_mr *mr, u64 iova, int length, u
 }
 
 static int __rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr,
-			     int length, enum rxe_mr_copy_dir dir)
+			     unsigned int length, enum rxe_mr_copy_dir dir)
 {
 	struct ib_umem_odp *umem_odp = to_ib_umem_odp(mr->umem);
+	unsigned int bytes;
+	unsigned long idx;
 	struct page *page;
-	int idx, bytes;
 	size_t offset;
 	u8 *user_va;
 
@@ -230,8 +232,8 @@ static int __rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr,
 	return 0;
 }
 
-int rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr, int length,
-		    enum rxe_mr_copy_dir dir)
+int rxe_odp_mr_copy(struct rxe_mr *mr, u64 iova, void *addr,
+		    unsigned int length, enum rxe_mr_copy_dir dir)
 {
 	struct ib_umem_odp *umem_odp = to_ib_umem_odp(mr->umem);
 	u32 flags = RXE_PAGEFAULT_DEFAULT;
@@ -273,7 +275,7 @@ static enum resp_states rxe_odp_do_atomic_op(struct rxe_mr *mr, u64 iova,
 	struct ib_umem_odp *umem_odp = to_ib_umem_odp(mr->umem);
 	unsigned int page_offset;
 	struct page *page;
-	unsigned int idx;
+	unsigned long idx;
 	u64 value;
 	u64 *va;
 	int err;
diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c
index 46d0810ea2..8b591b75e6 100644
--- a/drivers/infiniband/sw/rxe/rxe_verbs.c
+++ b/drivers/infiniband/sw/rxe/rxe_verbs.c
@@ -1263,10 +1263,14 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, u64 start,
 	struct rxe_pd *pd = to_rpd(ibpd);
 	struct rxe_mr *mr;
 	int err, cleanup_err;
+	u64 end;
 
 	if (dmah)
 		return ERR_PTR(-EOPNOTSUPP);
 
+	if (check_add_overflow(iova, length, &end))
+		return ERR_PTR(-EINVAL);
+
 	if (access & ~RXE_ACCESS_SUPPORTED_MR) {
 		rxe_err_pd(pd, "access = %#x not supported (%#x)\n", access,
 				RXE_ACCESS_SUPPORTED_MR);
-- 
2.56.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-25 20:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 20:40 [PATCH v3] RDMA/rxe: reject MRs whose iova range wraps, keep ODP lengths unsigned Cygnus Qiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®