Skip to contentSkip to conversation

RDMA/mlx4: Do not allow the user to write to the clock page

PATCH Applied Jiale Yao 3 participants 4 messages Sep 26 – Sep 29

Subsystems (2)
lore
Share & tools
lore

Jiale Yao

author
#0 lore raw

PATCH 1/1 RDMA/mlx4: Do not allow the user to write to the clock page

mlx4_ib_mmap() maps the internal clock page to userspace through rdma_user_mmap_io(), which does not restrict write access. Userspace can request PROT_WRITE directly, or upgrade a read-only mapping later with mprotect() because VM_MAYWRITE remains set.

Reject VM_WRITE and clear VM_MAYWRITE, applying the same read-only policy as commit c660133c339f ("RDMA/mlx5: Do not allow the user to write to the clock page"). The rdma-core mlx4 provider requests this mapping with PROT_READ, so its existing behavior is unchanged.

Fixes 52033cfb5aab ("IB/mlx4: Add mmap call to map the hardware clock")
---
 drivers/infiniband/hw/mlx4/main.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/hw/mlx4/main.c +4 -0
index 7266a6141944..6646a2633511 100644
@@ -1169,6 +1169,10 @@ static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
 		struct mlx4_clock_params params;
 		int ret;
 
+		if (vma->vm_flags & VM_WRITE)
+			return -EPERM;
+		vm_flags_clear(vma, VM_MAYWRITE);
+
 		ret = mlx4_get_internal_clock_params(dev->dev, &params);
 		if (ret)
 			return ret;
Signature · 1 line
2.34.1
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

mlx4_ib_mmap() maps the internal clock page to userspace through rdma_user_mmap_io(), which does not restrict write access. Userspace can request PROT_WRITE directly, or upgrade a read-only mapping later with mprotect() because VM_MAYWRITE remains set.

Quoted 7 lines

Reject VM_WRITE and clear VM_MAYWRITE, applying the same read-only policy as commit c660133c339f ("RDMA/mlx5: Do not allow the user to write to the clock page"). The rdma-core mlx4 provider requests this mapping with PROT_READ, so its existing behavior is unchanged.

Fixes 52033cfb5aab ("IB/mlx4: Add mmap call to map the hardware clock")

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
#2 lore raw
In reply to Jiale Yao
Quoted 21 lines · Jiale Yao

mlx4_ib_mmap() maps the internal clock page to userspace through rdma_user_mmap_io(), which does not restrict write access. Userspace can request PROT_WRITE directly, or upgrade a read-only mapping later with mprotect() because VM_MAYWRITE remains set.

Reject VM_WRITE and clear VM_MAYWRITE, applying the same read-only policy as commit c660133c339f ("RDMA/mlx5: Do not allow the user to write to the clock page"). The rdma-core mlx4 provider requests this mapping with PROT_READ, so its existing behavior is unchanged.

Fixes 52033cfb5aab ("IB/mlx4: Add mmap call to map the hardware clock")
---
 drivers/infiniband/hw/mlx4/main.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/hw/mlx4/main.c +0 -0
index 7266a6141944..6646a2633511 100644

drivers/infiniband/hw/mlx4/main.c static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) · line 1172

Quoted 4 lines
drivers/infiniband/hw/mlx4/main.c +1 -0
@@ -1169,6 +1169,10 @@ static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
 		struct mlx4_clock_params params;
 		int ret;
 
+		if (vma->vm_flags & VM_WRITE)

It needs to be:

               if (vma->vm_flags & (VM_WRITE | VM_EXEC))

I fixed it locally.

Thanks

Quoted 9 lines
drivers/infiniband/hw/mlx4/main.c +3 -0
+			return -EPERM;
+		vm_flags_clear(vma, VM_MAYWRITE);
+
 		ret = mlx4_get_internal_clock_params(dev->dev, &params);
 		if (ret)
 			return ret;
Signature · 1 line
2.34.1
#3 lore raw
In reply to Jiale Yao
Quoted 11 lines · Jiale Yao

mlx4_ib_mmap() maps the internal clock page to userspace through rdma_user_mmap_io(), which does not restrict write access. Userspace can request PROT_WRITE directly, or upgrade a read-only mapping later with mprotect() because VM_MAYWRITE remains set.

Reject VM_WRITE and clear VM_MAYWRITE, applying the same read-only policy as commit c660133c339f ("RDMA/mlx5: Do not allow the user to write to the clock page"). The rdma-core mlx4 provider requests this mapping with PROT_READ, so its existing behavior is unchanged.

[...]

Applied, thanks!

[1/1] RDMA/mlx4: Do not allow the user to write to the clock page

      https://git.kernel.org/rdma/rdma/c/33db1d9683b8fb

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>