From: Cosmin Ratiu mlx5e_xfrm_add_state() reads priv->ipsec and validates mode-dependent capabilities before blocking eswitch mode changes. A concurrent profile change can free the saved IPsec context and cause use-after-free. Move the mode block before the first context access and release it on all error paths. Keep the atomic acquire-placeholder path exempt, since it creates no hardware state and cannot take sleeping locks. Fixes: 22239eb258bc ("net/mlx5e: Prevent tunnel reformat when tunnel mode not allowed") Signed-off-by: Cosmin Ratiu Reviewed-by: Dragos Tatulea Signed-off-by: Tariq Toukan --- .../mellanox/mlx5/core/en_accel/ipsec.c | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c index 841ecdc2c4d9..1488faece80e 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c @@ -771,6 +771,7 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, struct xfrm_state *x, struct netlink_ext_ack *extack) { + bool is_acq = x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ; struct mlx5e_ipsec_sa_entry *sa_entry = NULL; bool allow_tunnel_mode = false; struct mlx5e_ipsec *ipsec; @@ -779,20 +780,30 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, int err; priv = netdev_priv(dev); - if (!priv->ipsec) - return -EOPNOTSUPP; + if (!is_acq) { + err = mlx5_eswitch_block_mode(priv->mdev, true); + if (err) + return err; + } ipsec = priv->ipsec; - gfp = (x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ) ? GFP_ATOMIC : GFP_KERNEL; + if (!ipsec) { + err = -EOPNOTSUPP; + goto unblock_mode; + } + + gfp = is_acq ? GFP_ATOMIC : GFP_KERNEL; sa_entry = kzalloc_obj(*sa_entry, gfp); - if (!sa_entry) - return -ENOMEM; + if (!sa_entry) { + err = -ENOMEM; + goto unblock_mode; + } sa_entry->x = x; sa_entry->dev = dev; sa_entry->ipsec = ipsec; /* Check if this SA is originated from acquire flow temporary SA */ - if (x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ) { + if (is_acq) { x->xso.offload_handle = (unsigned long)sa_entry; return 0; } @@ -806,10 +817,6 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, goto err_xfrm; } - err = mlx5_eswitch_block_mode(priv->mdev, true); - if (err) - goto unblock_ipsec; - if (x->props.mode == XFRM_MODE_TUNNEL && x->xso.type == XFRM_DEV_OFFLOAD_PACKET) { allow_tunnel_mode = mlx5e_ipsec_fs_tunnel_allowed(sa_entry); @@ -817,7 +824,7 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, NL_SET_ERR_MSG_MOD(extack, "Packet offload tunnel mode is disabled due to encap settings"); err = -EINVAL; - goto unblock_mode; + goto unblock_ipsec; } } @@ -893,12 +900,13 @@ static int mlx5e_xfrm_add_state(struct net_device *dev, unblock_encap: if (allow_tunnel_mode) mlx5_eswitch_unblock_encap(priv->mdev); -unblock_mode: - mlx5_eswitch_unblock_mode(priv->mdev); unblock_ipsec: mlx5_eswitch_unblock_ipsec(priv->mdev); err_xfrm: kfree(sa_entry); +unblock_mode: + if (!is_acq) + mlx5_eswitch_unblock_mode(priv->mdev); NL_SET_ERR_MSG_WEAK_MOD(extack, "Device failed to offload this state"); return err; } -- 2.44.0