rds: ib: Clear the sg list when mapping an MR fails
Subsystems (5)
PATCH 1/1 rds: ib: Clear the sg list when mapping an MR fails
rds_ib_map_frmr() stores the caller's scatterlist in the MR before DMA mapping and registration can fail. On failure, __rds_rdma_map() unpins the pages and frees the scatterlist, but rds_ib_free_frmr() can still return the MR to the pool with the stale pointer set.
This leaves the pool with a dangling scatterlist and can lead to local privilege escalation. KASAN detects the resulting use-after-free when the MR is later torn down:
BUG: KASAN: slab-use-after-free in __rds_ib_teardown_mr Read of size 8 Call Trace: __rds_ib_teardown_mr rds_ib_unreg_frmr rds_ib_flush_mr_pool rds_ib_flush_mrs rds_free_mr rds_setsockopt
Clear the scatterlist fields before returning an error so the caller retains sole ownership of the scatterlist and its pinned pages. Route a zero-length DMA mapping through the same cleanup path, and skip unmap when no mapping was created.
--- net/rds/ib_frmr.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
net/rds/ib_frmr.c +9 -4
@@ -213,7 +213,8 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev, DMA_BIDIRECTIONAL);if (unlikely (!ibmr->sg_dma_len)) {pr_warn ("RDS/IB: %s failed!\n" , __func__); -return -EBUSY ; + ret = -EBUSY ; + goto out_unmap; } frmr->sg_byte_len =0 ;
@@ -261,9 +262,13 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,return ret; out_unmap: -ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, -DMA_BIDIRECTIONAL ); - ibmr->sg_dma_len =0 ; +if (ibmr->sg_dma_len) { +ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, +DMA_BIDIRECTIONAL ); + ibmr->sg_dma_len =0 ; + } + ibmr->sg =NULL ; + ibmr->sg_len =0 ;return ret; }
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
rds_ib_map_frmr()stores the caller's scatterlist in the MR before DMA mapping and registration can fail. On failure,__rds_rdma_map()unpins the pages and frees the scatterlist, butrds_ib_free_frmr()can still return the MR to the pool with the stale pointer set.This leaves the pool with a dangling scatterlist and can lead to local privilege escalation. KASAN detects the resulting use-after-free when the MR is later torn down:
BUG: KASAN: slab-use-after-free in __rds_ib_teardown_mr Read of size 8 Call Trace: __rds_ib_teardown_mr rds_ib_unreg_frmr rds_ib_flush_mr_pool rds_ib_flush_mrs rds_free_mr rds_setsockopt
Quoted 8 lines
Clear the scatterlist fields before returning an error so the caller retains sole ownership of the scatterlist and its pinned pages. Route a zero-length DMA mapping through the same cleanup path, and skip unmap when no mapping was created.
Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")Signed-off-by Dongliang Qin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Simon Horman
reviewerQuoted 37 lines · Dongliang Qin
rds_ib_map_frmr()stores the caller's scatterlist in the MR before DMA mapping and registration can fail. On failure,__rds_rdma_map()unpins the pages and frees the scatterlist, butrds_ib_free_frmr()can still return the MR to the pool with the stale pointer set.This leaves the pool with a dangling scatterlist and can lead to local privilege escalation. KASAN detects the resulting use-after-free when the MR is later torn down:
BUG: KASAN: slab-use-after-free in __rds_ib_teardown_mr Read of size 8 Call Trace: __rds_ib_teardown_mr rds_ib_unreg_frmr rds_ib_flush_mr_pool rds_ib_flush_mrs rds_free_mr rds_setsockoptClear the scatterlist fields before returning an error so the caller retains sole ownership of the scatterlist and its pinned pages. Route a zero-length DMA mapping through the same cleanup path, and skip unmap when no mapping was created.
Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")Signed-off-by Dongliang Qin <[email protected]>--- net/rds/ib_frmr.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)net/rds/ib_frmr.c +0
-0
net/rds/ib_frmr.c static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev, · line 218
Quoted 7 lines
net/rds/ib_frmr.c +2
-1@@ -213,7 +213,8 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev, DMA_BIDIRECTIONAL);if (unlikely (!ibmr->sg_dma_len)) {pr_warn ("RDS/IB: %s failed!\n" , __func__); -return -EBUSY ; + ret = -EBUSY ; + goto out_unmap; }
Hi Dongliang,
This approach leads to the complexity of a condition in the out_unmap label. I wonder if a cleaner approach would be to, instead, only store the scatterlist in the MR once mapping has succeeded.
With such an approach I think that ibmr->sg and ibmr->sg_len would still need to be cleared in the error path. But neither the condition in the out_unmap label nor the hunk above would not be necessary. And I wonder if that would be a cleaner approach.
Quoted 22 lines
net/rds/ib_frmr.c +7
-3frmr->sg_byte_len =0 ;@@ -261,9 +262,13 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,return ret; out_unmap: -ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, -DMA_BIDIRECTIONAL ); - ibmr->sg_dma_len =0 ; +if (ibmr->sg_dma_len) { +ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, +DMA_BIDIRECTIONAL ); + ibmr->sg_dma_len =0 ; + } + ibmr->sg =NULL ; + ibmr->sg_len =0 ;return ret; }Signature · 1 line
2.43.0
Allison Henderson
maintainerQuoted 48 lines · Simon Horman
Quoted 37 lines · Dongliang Qin
rds_ib_map_frmr()stores the caller's scatterlist in the MR before DMA mapping and registration can fail. On failure,__rds_rdma_map()unpins the pages and frees the scatterlist, butrds_ib_free_frmr()can still return the MR to the pool with the stale pointer set.This leaves the pool with a dangling scatterlist and can lead to local privilege escalation. KASAN detects the resulting use-after-free when the MR is later torn down:
BUG: KASAN: slab-use-after-free in __rds_ib_teardown_mr Read of size 8 Call Trace: __rds_ib_teardown_mr rds_ib_unreg_frmr rds_ib_flush_mr_pool rds_ib_flush_mrs rds_free_mr rds_setsockoptClear the scatterlist fields before returning an error so the caller retains sole ownership of the scatterlist and its pinned pages. Route a zero-length DMA mapping through the same cleanup path, and skip unmap when no mapping was created.
Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")Signed-off-by Dongliang Qin <[email protected]>--- net/rds/ib_frmr.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)net/rds/ib_frmr.c +0
-0net/rds/ib_frmr.c static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev, · line 218
Quoted 7 lines
net/rds/ib_frmr.c +2
-1@@ -213,7 +213,8 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev, DMA_BIDIRECTIONAL);if (unlikely (!ibmr->sg_dma_len)) {pr_warn ("RDS/IB: %s failed!\n" , __func__); -return -EBUSY ; + ret = -EBUSY ; + goto out_unmap; }Hi Dongliang,
Quoted 8 lines
This approach leads to the complexity of a condition in the
out_unmaplabel. I wonder if a cleaner approach would be to, instead, only store the scatterlist in the MR once mapping has succeeded.With such an approach I think that
ibmr->sgandibmr->sg_lenwould still need to be cleared in the error path. But neither the condition in theout_unmaplabel nor the hunk above would not be necessary. And I wonder if that would be a cleaner approach.
That sounds reasonable, the rds_ib_teardown_mr() at the top already leaves ibmr->sg and ibmr->sg_len cleared. So if we move the ib_dma_map_sg() below it and do the check and goto there, then we dont need the unmap in the out path. Just clear ibmr->sg and ibmr->sg_len. I think that would be a lot simpler.
Thanks for working on this!
Allison
Quoted 23 lines
Quoted 23 lines
net/rds/ib_frmr.c +7
-3frmr->sg_byte_len =0 ;@@ -261,9 +262,13 @@ static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,return ret; out_unmap: -ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, -DMA_BIDIRECTIONAL ); - ibmr->sg_dma_len =0 ; +if (ibmr->sg_dma_len) { +ib_dma_unmap_sg (rds_ibdev->dev, ibmr->sg, ibmr->sg_len, +DMA_BIDIRECTIONAL ); + ibmr->sg_dma_len =0 ; + } + ibmr->sg =NULL ; + ibmr->sg_len =0 ;return ret; }Signature · 1 line
2.43.0