diff mbox series

[net-next] net: Prevent use after free in netif_napi_set_irq_locked()

Message ID 5a9c53a4-5487-4b8c-9ffa-d8e5343aaaaf@stanley.mountain (mailing list archive)
State New
Delegated to: Netdev Maintainers
Headers show
Series [net-next] net: Prevent use after free in netif_napi_set_irq_locked() | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
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 success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 5 this patch: 5
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 79 this patch: 79
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2025-03-03--18-00 (tests: 893)

Commit Message

Dan Carpenter March 3, 2025, 12:02 p.m. UTC
The cpu_rmap_put() will call kfree() when the last reference is dropped
so it could result in a use after free when we dereference the same
pointer the next line.  Move the cpu_rmap_put() after the dereference.

Fixes: bd7c00605ee0 ("net: move aRFS rmap management and CPU affinity to core")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Ahmed Zaki March 3, 2025, 3:58 p.m. UTC | #1
On 2025-03-03 5:02 a.m., Dan Carpenter wrote:
> The cpu_rmap_put() will call kfree() when the last reference is dropped
> so it could result in a use after free when we dereference the same
> pointer the next line.  Move the cpu_rmap_put() after the dereference.

The last call to cpu_rmap_put() that frees the rmap will always be in 
netif_del_cpu_rmap(). This matches the ref counter  initially set in 
netif_enable_cpu_rmap().



> 
> Fixes: bd7c00605ee0 ("net: move aRFS rmap management and CPU affinity to core")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
>   net/core/dev.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 9189c4a048d7..c102349e04ee 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -7072,8 +7072,8 @@ void netif_napi_set_irq_locked(struct napi_struct *napi, int irq)
>   put_rmap:
>   #ifdef CONFIG_RFS_ACCEL
>   	if (napi->dev->rx_cpu_rmap_auto) {
> -		cpu_rmap_put(napi->dev->rx_cpu_rmap);
>   		napi->dev->rx_cpu_rmap->obj[napi->napi_rmap_idx] = NULL;
> +		cpu_rmap_put(napi->dev->rx_cpu_rmap);
>   		napi->napi_rmap_idx = -1;
>   	}
>   #endif
diff mbox series

Patch

diff --git a/net/core/dev.c b/net/core/dev.c
index 9189c4a048d7..c102349e04ee 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -7072,8 +7072,8 @@  void netif_napi_set_irq_locked(struct napi_struct *napi, int irq)
 put_rmap:
 #ifdef CONFIG_RFS_ACCEL
 	if (napi->dev->rx_cpu_rmap_auto) {
-		cpu_rmap_put(napi->dev->rx_cpu_rmap);
 		napi->dev->rx_cpu_rmap->obj[napi->napi_rmap_idx] = NULL;
+		cpu_rmap_put(napi->dev->rx_cpu_rmap);
 		napi->napi_rmap_idx = -1;
 	}
 #endif