public inbox for linux-rdma@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr()
@ 2025-12-09  7:23 Tuo Li
  2025-12-15  9:42 ` Michael Gur
  0 siblings, 1 reply; 3+ messages in thread
From: Tuo Li @ 2025-12-09  7:23 UTC (permalink / raw)
  To: leon, jgg; +Cc: linux-rdma, linux-kernel, 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 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.

Signed-off-by: Tuo Li <islituo@gmail.com>
---
 drivers/infiniband/hw/mlx5/main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
index 40284bbb45d6..d68a58d249d4 100644
--- a/drivers/infiniband/hw/mlx5/main.c
+++ b/drivers/infiniband/hw/mlx5/main.c
@@ -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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr()
  2025-12-09  7:23 [PATCH] IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr() Tuo Li
@ 2025-12-15  9:42 ` Michael Gur
  2025-12-15 10:07   ` Tuo Li
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Gur @ 2025-12-15  9:42 UTC (permalink / raw)
  To: Tuo Li, leon, jgg; +Cc: linux-rdma, linux-kernel


On 12/9/2025 9:23 AM, Tuo Li wrote:
> 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.

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.

> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
>   drivers/infiniband/hw/mlx5/main.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
> index 40284bbb45d6..d68a58d249d4 100644
> --- a/drivers/infiniband/hw/mlx5/main.c
> +++ b/drivers/infiniband/hw/mlx5/main.c
> @@ -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.

>   
>   	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;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr()
  2025-12-15  9:42 ` Michael Gur
@ 2025-12-15 10:07   ` Tuo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Tuo Li @ 2025-12-15 10:07 UTC (permalink / raw)
  To: Michael Gur; +Cc: leon, jgg, linux-rdma, linux-kernel

Hi Michael,

On Mon, Dec 15, 2025 at 5:42 PM Michael Gur <michaelgur@nvidia.com> wrote:
>
>
> On 12/9/2025 9:23 AM, Tuo Li wrote:
> > 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.
>
> 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.
>
> > Signed-off-by: Tuo Li <islituo@gmail.com>
> > ---
> >   drivers/infiniband/hw/mlx5/main.c | 4 +++-
> >   1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
> > index 40284bbb45d6..d68a58d249d4 100644
> > --- a/drivers/infiniband/hw/mlx5/main.c
> > +++ b/drivers/infiniband/hw/mlx5/main.c
> > @@ -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.
>
> >
> >       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;

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-12-15 10:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-09  7:23 [PATCH] IB/mlx5: Fix a possible null-pointer dereference in set_roce_addr() Tuo Li
2025-12-15  9:42 ` Michael Gur
2025-12-15 10:07   ` Tuo Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox