diff mbox series

[net] net/mlx5e: fix double free of encap_header

Message ID 20231025032712.79026-1-dust.li@linux.alibaba.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [net] net/mlx5e: fix double free of encap_header | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1362 this patch: 1362
netdev/cc_maintainers fail 2 blamed authors not CCed: roid@nvidia.com vladbu@mellanox.com; 4 maintainers not CCed: roid@nvidia.com paulb@nvidia.com vladbu@mellanox.com maord@nvidia.com
netdev/build_clang success Errors and warnings before: 1386 this patch: 1386
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes fail Problems with Fixes tag: 1
netdev/build_allmodconfig_warn success Errors and warnings before: 1386 this patch: 1386
netdev/checkpatch warning WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes: 7f82abc4cd80 ("net/mlx5e: fix double free of encap_header")'
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Dust Li Oct. 25, 2023, 3:27 a.m. UTC
When mlx5_packet_reformat_alloc() fails, the encap_header allocated in
mlx5e_tc_tun_create_header_ipv4{6} will be released within it. However,
e->encap_header is already set to the previously freed encap_header
before mlx5_packet_reformat_alloc(). As a result, the later
mlx5e_encap_put() will free e->encap_header again, causing a double free
issue.

mlx5e_encap_put()
    --> mlx5e_encap_dealloc()
        --> kfree(e->encap_header)

This happens when cmd: MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT fail.

This patch fix it by not setting e->encap_header until
mlx5_packet_reformat_alloc() success.

Fixes: d589e785baf5e("net/mlx5e: Allow concurrent creation of encap entries")
Reported-by: Cruz Zhao <cruzzhao@linux.alibaba.com>
Reported-by: Tianchen Ding <dtcccc@linux.alibaba.com>
Signed-off-by: Dust Li <dust.li@linux.alibaba.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Comments

Wojciech Drewek Oct. 25, 2023, 9:06 a.m. UTC | #1
On 25.10.2023 05:27, Dust Li wrote:
> When mlx5_packet_reformat_alloc() fails, the encap_header allocated in
> mlx5e_tc_tun_create_header_ipv4{6} will be released within it. However,
> e->encap_header is already set to the previously freed encap_header
> before mlx5_packet_reformat_alloc(). As a result, the later
> mlx5e_encap_put() will free e->encap_header again, causing a double free
> issue.
> 
> mlx5e_encap_put()
>     --> mlx5e_encap_dealloc()
>         --> kfree(e->encap_header)

nit: I think it should mlx5e_encap_put_locked not mlx5e_encap_put to be precise.

> 
> This happens when cmd: MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT fail.
> 
> This patch fix it by not setting e->encap_header until
> mlx5_packet_reformat_alloc() success.
> 
> Fixes: d589e785baf5e("net/mlx5e: Allow concurrent creation of encap entries")
> Reported-by: Cruz Zhao <cruzzhao@linux.alibaba.com>
> Reported-by: Tianchen Ding <dtcccc@linux.alibaba.com>
> Signed-off-by: Dust Li <dust.li@linux.alibaba.com>
> ---

Reviewed-by: Wojciech Drewek <wojciech.drewek@intel.com>

>  drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
> index 00a04fdd756f..8bca696b6658 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
> @@ -300,9 +300,6 @@ int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
>  	if (err)
>  		goto destroy_neigh_entry;
>  
> -	e->encap_size = ipv4_encap_size;
> -	e->encap_header = encap_header;
> -
>  	if (!(nud_state & NUD_VALID)) {
>  		neigh_event_send(attr.n, NULL);
>  		/* the encap entry will be made valid on neigh update event
> @@ -322,6 +319,8 @@ int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
>  		goto destroy_neigh_entry;
>  	}
>  
> +	e->encap_size = ipv4_encap_size;
> +	e->encap_header = encap_header;
>  	e->flags |= MLX5_ENCAP_ENTRY_VALID;
>  	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
>  	mlx5e_route_lookup_ipv4_put(&attr);
> @@ -568,9 +567,6 @@ int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
>  	if (err)
>  		goto destroy_neigh_entry;
>  
> -	e->encap_size = ipv6_encap_size;
> -	e->encap_header = encap_header;
> -
>  	if (!(nud_state & NUD_VALID)) {
>  		neigh_event_send(attr.n, NULL);
>  		/* the encap entry will be made valid on neigh update event
> @@ -590,6 +586,8 @@ int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
>  		goto destroy_neigh_entry;
>  	}
>  
> +	e->encap_size = ipv6_encap_size;
> +	e->encap_header = encap_header;
>  	e->flags |= MLX5_ENCAP_ENTRY_VALID;
>  	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
>  	mlx5e_route_lookup_ipv6_put(&attr);
Saeed Mahameed Oct. 26, 2023, 11 p.m. UTC | #2
On 25 Oct 11:27, Dust Li wrote:
>When mlx5_packet_reformat_alloc() fails, the encap_header allocated in
>mlx5e_tc_tun_create_header_ipv4{6} will be released within it. However,
>e->encap_header is already set to the previously freed encap_header
>before mlx5_packet_reformat_alloc(). As a result, the later
>mlx5e_encap_put() will free e->encap_header again, causing a double free
>issue.
>
>mlx5e_encap_put()
>    --> mlx5e_encap_dealloc()
>        --> kfree(e->encap_header)
>
>This happens when cmd: MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT fail.
>
>This patch fix it by not setting e->encap_header until
>mlx5_packet_reformat_alloc() success.
>
>Fixes: d589e785baf5e("net/mlx5e: Allow concurrent creation of encap entries")
>Reported-by: Cruz Zhao <cruzzhao@linux.alibaba.com>
>Reported-by: Tianchen Ding <dtcccc@linux.alibaba.com>
>Signed-off-by: Dust Li <dust.li@linux.alibaba.com>

Applied to net-mlx5
Dust Li Oct. 27, 2023, 6:58 a.m. UTC | #3
On Wed, Oct 25, 2023 at 11:06:25AM +0200, Wojciech Drewek wrote:
>
>
>On 25.10.2023 05:27, Dust Li wrote:
>> When mlx5_packet_reformat_alloc() fails, the encap_header allocated in
>> mlx5e_tc_tun_create_header_ipv4{6} will be released within it. However,
>> e->encap_header is already set to the previously freed encap_header
>> before mlx5_packet_reformat_alloc(). As a result, the later
>> mlx5e_encap_put() will free e->encap_header again, causing a double free
>> issue.
>> 
>> mlx5e_encap_put()
>>     --> mlx5e_encap_dealloc()
>>         --> kfree(e->encap_header)
>
>nit: I think it should mlx5e_encap_put_locked not mlx5e_encap_put to be precise.

You are right.

The original version is mlx5e_encap_put, I mistakenly used the name of
the old version when doing git blame.

Best regards,
Dust


>
>> 
>> This happens when cmd: MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT fail.
>> 
>> This patch fix it by not setting e->encap_header until
>> mlx5_packet_reformat_alloc() success.
>> 
>> Fixes: d589e785baf5e("net/mlx5e: Allow concurrent creation of encap entries")
>> Reported-by: Cruz Zhao <cruzzhao@linux.alibaba.com>
>> Reported-by: Tianchen Ding <dtcccc@linux.alibaba.com>
>> Signed-off-by: Dust Li <dust.li@linux.alibaba.com>
>> ---
>
>Reviewed-by: Wojciech Drewek <wojciech.drewek@intel.com>
>
>>  drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c | 10 ++++------
>>  1 file changed, 4 insertions(+), 6 deletions(-)
>> 
>> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
>> index 00a04fdd756f..8bca696b6658 100644
>> --- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
>> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
>> @@ -300,9 +300,6 @@ int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
>>  	if (err)
>>  		goto destroy_neigh_entry;
>>  
>> -	e->encap_size = ipv4_encap_size;
>> -	e->encap_header = encap_header;
>> -
>>  	if (!(nud_state & NUD_VALID)) {
>>  		neigh_event_send(attr.n, NULL);
>>  		/* the encap entry will be made valid on neigh update event
>> @@ -322,6 +319,8 @@ int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
>>  		goto destroy_neigh_entry;
>>  	}
>>  
>> +	e->encap_size = ipv4_encap_size;
>> +	e->encap_header = encap_header;
>>  	e->flags |= MLX5_ENCAP_ENTRY_VALID;
>>  	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
>>  	mlx5e_route_lookup_ipv4_put(&attr);
>> @@ -568,9 +567,6 @@ int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
>>  	if (err)
>>  		goto destroy_neigh_entry;
>>  
>> -	e->encap_size = ipv6_encap_size;
>> -	e->encap_header = encap_header;
>> -
>>  	if (!(nud_state & NUD_VALID)) {
>>  		neigh_event_send(attr.n, NULL);
>>  		/* the encap entry will be made valid on neigh update event
>> @@ -590,6 +586,8 @@ int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
>>  		goto destroy_neigh_entry;
>>  	}
>>  
>> +	e->encap_size = ipv6_encap_size;
>> +	e->encap_header = encap_header;
>>  	e->flags |= MLX5_ENCAP_ENTRY_VALID;
>>  	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
>>  	mlx5e_route_lookup_ipv6_put(&attr);
diff mbox series

Patch

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
index 00a04fdd756f..8bca696b6658 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun.c
@@ -300,9 +300,6 @@  int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
 	if (err)
 		goto destroy_neigh_entry;
 
-	e->encap_size = ipv4_encap_size;
-	e->encap_header = encap_header;
-
 	if (!(nud_state & NUD_VALID)) {
 		neigh_event_send(attr.n, NULL);
 		/* the encap entry will be made valid on neigh update event
@@ -322,6 +319,8 @@  int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
 		goto destroy_neigh_entry;
 	}
 
+	e->encap_size = ipv4_encap_size;
+	e->encap_header = encap_header;
 	e->flags |= MLX5_ENCAP_ENTRY_VALID;
 	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
 	mlx5e_route_lookup_ipv4_put(&attr);
@@ -568,9 +567,6 @@  int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
 	if (err)
 		goto destroy_neigh_entry;
 
-	e->encap_size = ipv6_encap_size;
-	e->encap_header = encap_header;
-
 	if (!(nud_state & NUD_VALID)) {
 		neigh_event_send(attr.n, NULL);
 		/* the encap entry will be made valid on neigh update event
@@ -590,6 +586,8 @@  int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
 		goto destroy_neigh_entry;
 	}
 
+	e->encap_size = ipv6_encap_size;
+	e->encap_header = encap_header;
 	e->flags |= MLX5_ENCAP_ENTRY_VALID;
 	mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
 	mlx5e_route_lookup_ipv6_put(&attr);