Skip to contentSkip to conversation

IB/mlx4: Test port number before querying type.

PATCH In review Tarick Bedeir 2 participants 4 messages Jul 2, 2018 – Jul 4, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 IB/mlx4: Test port number before querying type.

rdma_ah_find_type() can reach into ib_device->port_immutable with a potentially out-of-bounds port number, so check that the port number is valid first.

Fixes 44c58487d51a ("IB/core: Define 'ib' and 'roce' rdma_ah_attr types")
---
 drivers/infiniband/hw/mlx4/qp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx4/qp.c +1 -1
index f045491f2c14..4f1dabecf9a5 100644
@@ -4034,9 +4034,9 @@ static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
 	u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
 
 	memset(ah_attr, 0, sizeof(*ah_attr));
-	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
 	if (port_num == 0 || port_num > dev->caps.num_ports)
 		return;
+	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
 
 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
 		rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
Signature · 1 line
2.18.0.399.gad0ab374a1-goog
#1 lore raw
Quoted 15 lines · Tarick Bedeir

rdma_ah_find_type() can reach into ib_device->port_immutable with a potentially out-of-bounds port number, so check that the port number is valid first.

Fixes 44c58487d51a ("IB/core: Define 'ib' and 'roce' rdma_ah_attr types")
---
 drivers/infiniband/hw/mlx4/qp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx4/qp.c +0 -0
index f045491f2c14..4f1dabecf9a5 100644

drivers/infiniband/hw/mlx4/qp.c static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev, · line 4037

Quoted 5 lines
drivers/infiniband/hw/mlx4/qp.c +0 -1
@@ -4034,9 +4034,9 @@ static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
 	u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
 
 	memset(ah_attr, 0, sizeof(*ah_attr));
-	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
 	if (port_num == 0 || port_num > dev->caps.num_ports)

Did you get any warning from any checker about out-of-bounds access?

According to the line above, port_num can be 2 or 1 which are valid ports for mlx4 and the check above doesn't do much.

Quoted 8 lines
drivers/infiniband/hw/mlx4/qp.c +1 -1
 		return;
+	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
 
 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
 		rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
--

2.18.0.399.gad0ab374a1-goog

#2 lore raw
Quoted 19 lines · Leon Romanovsky
Quoted 17 lines · Tarick Bedeir

rdma_ah_find_type() can reach into ib_device->port_immutable with a potentially out-of-bounds port number, so check that the port number is valid first.

Fixes 44c58487d51a ("IB/core: Define 'ib' and 'roce' rdma_ah_attr types")
---
 drivers/infiniband/hw/mlx4/qp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx4/qp.c +0 -0
index f045491f2c14..4f1dabecf9a5 100644
@@ -4034,9 +4034,9 @@ static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
      u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
 
Quoted 5 lines

drivers/infiniband/hw/mlx4/qp.c static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev, · line 4037

Quoted 3 lines
drivers/infiniband/hw/mlx4/qp.c +0 -1
      memset(ah_attr, 0, sizeof(*ah_attr));
-     ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
      if (port_num == 0 || port_num > dev->caps.num_ports)

Did you get any warning from any checker about out-of-bounds access?

Indeed, I observed a KASAN warning for an out-of-bounds read in to_rdma_ah_attr().

Quoted 2 lines

According to the line above, port_num can be 2 or 1 which are valid ports for mlx4 and the check above doesn't do much.

There are mlx4 devices with only one port.

Quoted 9 lines
Quoted 9 lines
drivers/infiniband/hw/mlx4/qp.c +1 -1
              return;
+     ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
 
      if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
              rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
--

2.18.0.399.gad0ab374a1-goog

Reviewed-by
#3 lore raw
Quoted 26 lines · Tarick Bedeir
Quoted 19 lines · Leon Romanovsky
Quoted 17 lines · Tarick Bedeir

rdma_ah_find_type() can reach into ib_device->port_immutable with a potentially out-of-bounds port number, so check that the port number is valid first.

Fixes 44c58487d51a ("IB/core: Define 'ib' and 'roce' rdma_ah_attr types")
---
 drivers/infiniband/hw/mlx4/qp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mlx4/qp.c +0 -0
index f045491f2c14..4f1dabecf9a5 100644
@@ -4034,9 +4034,9 @@ static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
      u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
 
Quoted 5 lines

drivers/infiniband/hw/mlx4/qp.c static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev, · line 4037

Quoted 3 lines
drivers/infiniband/hw/mlx4/qp.c +0 -1
      memset(ah_attr, 0, sizeof(*ah_attr));
-     ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
      if (port_num == 0 || port_num > dev->caps.num_ports)

Did you get any warning from any checker about out-of-bounds access?

Quoted 7 lines

Indeed, I observed a KASAN warning for an out-of-bounds read in to_rdma_ah_attr().

Quoted 2 lines

According to the line above, port_num can be 2 or 1 which are valid ports for mlx4 and the check above doesn't do much.

There are mlx4 devices with only one port.

Thanks, I never saw such device, but found one on ebay :).