]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
RDMA/irdma: Fix out-of-bounds write in irdma_copy_user_pgaddrs
authorJacob Moroni <jmoroni@google.com>
Tue, 12 May 2026 18:38:52 +0000 (18:38 +0000)
committerJason Gunthorpe <jgg@nvidia.com>
Mon, 25 May 2026 13:50:42 +0000 (10:50 -0300)
The irdma_copy_user_pgaddrs function loops through all of the umem DMA
blocks to populate the PBLEs and will stop when either the last DMA
block is reached or palloc->total_cnt is reached. The issue is that
the logic for checking palloc->total_cnt would only work for non-zero
values.

When irdma_setup_pbles is called with lvl==0, it
calls irdma_copy_user_pgaddrs with palloc->total_cnt==0, which means
the only way to break out of the loop is to reach the last umem DMA
block, which means it could end up going beyond the fixed size of 4
iwmr->pgaddrmem array that is used in the lvl==0 case.

In the case of QP/CQ/SRQ rings, the value of lvl is determined by a
separate input (for example, req.cq_pages in the case of a CQ). So,
we must perform explicit checking to ensure we don't overflow the
pgaddrmem array if the user provides a umem that consists of more
blocks than their provided req.cq_pages.

Fixes: b48c24c2d710 ("RDMA/irdma: Implement device supported verb APIs")
Link: https://patch.msgid.link/r/20260512183852.614045-1-jmoroni@google.com
Signed-off-by: Jacob Moroni <jmoroni@google.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/infiniband/hw/irdma/verbs.c

index 79e72a457e798389d7041af3ad2bf0eede4346f4..3f4811bb5514c6dc154f4464c748910094fb6563 100644 (file)
@@ -2765,10 +2765,11 @@ static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
  * irdma_copy_user_pgaddrs - copy user page address to pble's os locally
  * @iwmr: iwmr for IB's user page addresses
  * @pbl: ple pointer to save 1 level or 0 level pble
+ * @pbl_len: Max number of PBL entries to populate
  * @level: indicated level 0, 1 or 2
  */
 static void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
-                                   enum irdma_pble_level level)
+                                   u32 pbl_len, enum irdma_pble_level level)
 {
        struct ib_umem *region = iwmr->region;
        struct irdma_pbl *iwpbl = &iwmr->iwpbl;
@@ -2776,7 +2777,9 @@ static void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
        struct irdma_pble_info *pinfo;
        struct ib_block_iter biter;
        u32 idx = 0;
-       u32 pbl_cnt = 0;
+
+       if (!pbl_len)
+               return;
 
        pinfo = (level == PBLE_LEVEL_1) ? NULL : palloc->level2.leaf;
 
@@ -2785,7 +2788,7 @@ static void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
 
        rdma_umem_for_each_dma_block(region, &biter, iwmr->page_size) {
                *pbl = rdma_block_iter_dma_address(&biter);
-               if (++pbl_cnt == palloc->total_cnt)
+               if (!--pbl_len)
                        break;
                pbl = irdma_next_pbl_addr(pbl, &pinfo, &idx);
        }
@@ -2861,6 +2864,7 @@ static int irdma_setup_pbles(struct irdma_pci_f *rf, struct irdma_mr *iwmr,
        u64 *pbl;
        int status;
        enum irdma_pble_level level = PBLE_LEVEL_1;
+       u32 pbl_len;
 
        if (lvl) {
                status = irdma_get_pble(rf->pble_rsrc, palloc, iwmr->page_cnt,
@@ -2868,16 +2872,18 @@ static int irdma_setup_pbles(struct irdma_pci_f *rf, struct irdma_mr *iwmr,
                if (status)
                        return status;
 
+               pbl_len = palloc->total_cnt;
                iwpbl->pbl_allocated = true;
                level = palloc->level;
                pinfo = (level == PBLE_LEVEL_1) ? &palloc->level1 :
                                                  palloc->level2.leaf;
                pbl = pinfo->addr;
        } else {
+               pbl_len = IRDMA_MAX_SAVED_PHY_PGADDR;
                pbl = iwmr->pgaddrmem;
        }
 
-       irdma_copy_user_pgaddrs(iwmr, pbl, level);
+       irdma_copy_user_pgaddrs(iwmr, pbl, pbl_len, level);
 
        if (lvl)
                iwmr->pgaddrmem[0] = *pbl;