IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr()
Subsystems (2)
PATCH 1/1 IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr()
The pointer gid is checked at the beginning of set_roce_addr(). However, if it is NULL, the function continues execution and may dereference gid when calling mlx5_core_roce_gid_set():
return mlx5_core_roce_gid_set(..., gid->raw, ...)
This can lead to a null-pointer dereference. To prevent this, add an else branch that return -EINVAL when gid is NULL, and remove the redundant gid check in the IB_GID_TYPE_ROCE_UDP_ENCAP case.
--- drivers/infiniband/hw/mlx5/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/hw/mlx5/main.c +3 -1
@@ -645,6 +645,8 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, ret =rdma_read_gid_l2_fields (attr, &vlan_id, &mac[0 ]);if (ret)return ret; + }else { +return -EINVAL ; }switch (gid_type) {
@@ -653,7 +655,7 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,break ;case IB_GID_TYPE_ROCE_UDP_ENCAP : roce_version =MLX5_ROCE_VERSION_2 ; -if (gid &&ipv6_addr_v4mapped ((void *)gid)) +if (ipv6_addr_v4mapped ((void *)gid)) roce_l3_type =MLX5_ROCE_L3_TYPE_IPV4 ;else roce_l3_type =MLX5_ROCE_L3_TYPE_IPV6 ;
Signature · 1 line
2.43.0
Quoted 9 lines · Tuo Li
The pointer gid is checked at the beginning of
set_roce_addr(). However, if it is NULL, the function continues execution and may dereference gid when callingmlx5_core_roce_gid_set():return mlx5_core_roce_gid_set(..., gid->raw, ...)This can lead to a null-pointer dereference. To prevent this, add an else branch that return
-EINVALwhen gid is NULL, and remove the redundant gid check in theIB_GID_TYPE_ROCE_UDP_ENCAPcase.
Can you reproduce this?
Theoretically, gid->raw is translated to NULL+0 which is undefined behavior and static analyzers can complain, but it seems compilers just translate to NULL which leads us to the expected behavior.
Quoted 10 lines
Signed-off-by Tuo Li <[email protected]>--- drivers/infiniband/hw/mlx5/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/infiniband/hw/mlx5/main.c +0
-0
drivers/infiniband/hw/mlx5/main.c int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, · line 650
Quoted 6 lines
drivers/infiniband/hw/mlx5/main.c +2
-0@@ -645,6 +645,8 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, ret =rdma_read_gid_l2_fields (attr, &vlan_id, &mac[0 ]);if (ret)return ret; + }else { +return -EINVAL ; }
This breaks the gid deletion, we should still call mlx5_core with NULL gid for it to update the table.
Quoted 11 lines
drivers/infiniband/hw/mlx5/main.c +1
-1switch (gid_type) {@@ -653,7 +655,7 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,break ;case IB_GID_TYPE_ROCE_UDP_ENCAP : roce_version =MLX5_ROCE_VERSION_2 ; -if (gid &&ipv6_addr_v4mapped ((void *)gid)) +if (ipv6_addr_v4mapped ((void *)gid)) roce_l3_type =MLX5_ROCE_L3_TYPE_IPV4 ;else roce_l3_type =MLX5_ROCE_L3_TYPE_IPV6 ;
Hi Michael,
Quoted 40 lines · Michael Gur
Quoted 9 lines · Tuo Li
The pointer gid is checked at the beginning of
set_roce_addr(). However, if it is NULL, the function continues execution and may dereference gid when callingmlx5_core_roce_gid_set():return mlx5_core_roce_gid_set(..., gid->raw, ...)This can lead to a null-pointer dereference. To prevent this, add an else branch that return
-EINVALwhen gid is NULL, and remove the redundant gid check in theIB_GID_TYPE_ROCE_UDP_ENCAPcase.Can you reproduce this?
Theoretically,
gid->rawis translated to NULL+0 which is undefined behavior and static analyzers can complain, but it seems compilers just translate to NULL which leads us to the expected behavior.Quoted 10 lines
Signed-off-by Tuo Li <[email protected]>--- drivers/infiniband/hw/mlx5/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/infiniband/hw/mlx5/main.c +0
-0drivers/infiniband/hw/mlx5/main.c int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, · line 650
Quoted 6 lines
drivers/infiniband/hw/mlx5/main.c +2
-0@@ -645,6 +645,8 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, ret =rdma_read_gid_l2_fields (attr, &vlan_id, &mac[0 ]);if (ret)return ret; + }else { +return -EINVAL ; }This breaks the gid deletion, we should still call
mlx5_corewith NULL gid for it to update the table.Quoted 1 line
Quoted 10 lines
drivers/infiniband/hw/mlx5/main.c int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, · line 661
Quoted 10 lines
switch (gid_type) {drivers/infiniband/hw/mlx5/main.c +1
-1@@ -653,7 +655,7 @@ int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,break ;case IB_GID_TYPE_ROCE_UDP_ENCAP : roce_version =MLX5_ROCE_VERSION_2 ; -if (gid &&ipv6_addr_v4mapped ((void *)gid)) +if (ipv6_addr_v4mapped ((void *)gid)) roce_l3_type =MLX5_ROCE_L3_TYPE_IPV4 ;else roce_l3_type =MLX5_ROCE_L3_TYPE_IPV6 ;
I have rechecked the code. In this case, accessing the first member of a NULL pointer is safe. Moreover, mlx5_core_roce_gid_set() also checks gid->raw before using it, so an early return is not necessary.
Thanks for your feedback, and sorry for any inconvenience caused.