[PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof

Dheeraj Reddy Jonnalagadda posted 1 patch 9 hours ago
include/linux/ptr_ring.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
[PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof
Posted by Dheeraj Reddy Jonnalagadda 9 hours ago
The ptr_ring_resize_multiple_bh_noprof function may have a race condition
where queues[i] is freed after releasing the locks. Since this function
can be called from multiple threads, another thread could potentially modify
queues[i] between the unlock and kvfree operations.

Move the kvfree inside the critical section to ensure atomicity of the
queue swap and cleanup operations.

Fixes: 59e6ae53248a("ptr_ring: support resizing multiple queues")
Closes: https://scan7.scan.coverity.com/#/project-view/52337/11354?selectedIssue=1602644
Signed-off-by: Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
---
 include/linux/ptr_ring.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 551329220e4f..cb06c74fc791 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -641,13 +641,11 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
 		spin_lock(&(rings[i])->producer_lock);
 		queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
 						  size, gfp, destroy);
+		kvfree(queues[i]);
 		spin_unlock(&(rings[i])->producer_lock);
 		spin_unlock_bh(&(rings[i])->consumer_lock);
 	}
 
-	for (i = 0; i < nrings; ++i)
-		kvfree(queues[i]);
-
 	kfree(queues);
 
 	return 0;
-- 
2.34.1
Re: [PATCH include] ptr_ring: fix potential race in ptr_ring_resize_multiple_bh_noprof
Posted by Eric Dumazet 7 hours ago
On Sat, Dec 21, 2024 at 7:16 AM Dheeraj Reddy Jonnalagadda
<dheeraj.linuxdev@gmail.com> wrote:
>
> The ptr_ring_resize_multiple_bh_noprof function may have a race condition
> where queues[i] is freed after releasing the locks. Since this function
> can be called from multiple threads, another thread could potentially modify
> queues[i] between the unlock and kvfree operations.
>
> Move the kvfree inside the critical section to ensure atomicity of the
> queue swap and cleanup operations.
>
> Fixes: 59e6ae53248a("ptr_ring: support resizing multiple queues")
> Closes: https://scan7.scan.coverity.com/#/project-view/52337/11354?selectedIssue=1602644
> Signed-off-by: Dheeraj Reddy Jonnalagadda <dheeraj.linuxdev@gmail.com>
> ---
>  include/linux/ptr_ring.h | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
> index 551329220e4f..cb06c74fc791 100644
> --- a/include/linux/ptr_ring.h
> +++ b/include/linux/ptr_ring.h
> @@ -641,13 +641,11 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
>                 spin_lock(&(rings[i])->producer_lock);
>                 queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
>                                                   size, gfp, destroy);
> +               kvfree(queues[i]);
>                 spin_unlock(&(rings[i])->producer_lock);
>                 spin_unlock_bh(&(rings[i])->consumer_lock);
>         }
>
> -       for (i = 0; i < nrings; ++i)
> -               kvfree(queues[i]);
> -
>         kfree(queues);

I do not think this patch makes sense.

queues[] is private to this thread, there is no possible race.