All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs: Fix lock leak in replace_fd()
@ 2026-05-21  7:49 Hongling Zeng
  2026-05-21 14:45 ` Mateusz Guzik
  0 siblings, 1 reply; 4+ messages in thread
From: Hongling Zeng @ 2026-05-21  7:49 UTC (permalink / raw)
  To: viro, brauner, jack, thomas.weissschuh
  Cc: linux-fsdevel, linux-kernel, zhongling0719, Hongling Zeng, stable

In replace_fd(), the function acquires files->file_lock but then has
two return paths that don't release the lock:
- When do_dup2() fails (returns negative error)
- When do_dup2() succeeds (returns 0)

Both of these paths return directly without unlocking files->file_lock,
causing a lock leak and potential deadlock.

Fix this by making both error and success paths go through the
out_unlock label to ensure the lock is always released.

Fixes: 708c04a5c2b7 ("fs: always return zero on success from replace_fd()")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
 fs/file.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index 2c81c0b162d0..d0f019fb0568 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -1361,8 +1361,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
 		goto out_unlock;
 	err = do_dup2(files, file, fd, flags);
 	if (err < 0)
-		return err;
-	return 0;
+		goto out_unlock;
 
 out_unlock:
 	spin_unlock(&files->file_lock);
-- 
2.25.1


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

* Re: [PATCH] fs: Fix lock leak in replace_fd()
  2026-05-21  7:49 [PATCH] fs: Fix lock leak in replace_fd() Hongling Zeng
@ 2026-05-21 14:45 ` Mateusz Guzik
  2026-05-22  9:23   ` Hongling Zeng
  2026-05-22 10:10   ` Christian Brauner
  0 siblings, 2 replies; 4+ messages in thread
From: Mateusz Guzik @ 2026-05-21 14:45 UTC (permalink / raw)
  To: Hongling Zeng
  Cc: viro, brauner, jack, thomas.weissschuh, linux-fsdevel,
	linux-kernel, zhongling0719, stable

On Thu, May 21, 2026 at 03:49:34PM +0800, Hongling Zeng wrote:
> In replace_fd(), the function acquires files->file_lock but then has
> two return paths that don't release the lock:
> - When do_dup2() fails (returns negative error)
> - When do_dup2() succeeds (returns 0)
> 
> Both of these paths return directly without unlocking files->file_lock,
> causing a lock leak and potential deadlock.
> 
> Fix this by making both error and success paths go through the
> out_unlock label to ensure the lock is always released.

do_dup2 always releases the lock regardless of return value, so this
patch cannot be correct.

that aside, there is another consumer which would also need patching if
the issue was real

> 
> Fixes: 708c04a5c2b7 ("fs: always return zero on success from replace_fd()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---
>  fs/file.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/fs/file.c b/fs/file.c
> index 2c81c0b162d0..d0f019fb0568 100644
> --- a/fs/file.c
> +++ b/fs/file.c
> @@ -1361,8 +1361,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
>  		goto out_unlock;
>  	err = do_dup2(files, file, fd, flags);
>  	if (err < 0)
> -		return err;
> -	return 0;
> +		goto out_unlock;
>  
>  out_unlock:
>  	spin_unlock(&files->file_lock);
> -- 
> 2.25.1
> 

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

* Re: [PATCH] fs: Fix lock leak in replace_fd()
  2026-05-21 14:45 ` Mateusz Guzik
@ 2026-05-22  9:23   ` Hongling Zeng
  2026-05-22 10:10   ` Christian Brauner
  1 sibling, 0 replies; 4+ messages in thread
From: Hongling Zeng @ 2026-05-22  9:23 UTC (permalink / raw)
  To: Mateusz Guzik, Hongling Zeng
  Cc: viro, brauner, jack, thomas.weissschuh, linux-fsdevel,
	linux-kernel, stable

   You're right - I missed the __releases(&files->file_lock) annotation
   on do_dup2(). My patch would cause a double-unlock bug.

   Thanks for the correction. I'll verify warnings more carefully next
time.

   Sorry for the noise.

   Hongling
在 2026年05月21日 22:45, Mateusz Guzik 写道:
> On Thu, May 21, 2026 at 03:49:34PM +0800, Hongling Zeng wrote:
>> In replace_fd(), the function acquires files->file_lock but then has
>> two return paths that don't release the lock:
>> - When do_dup2() fails (returns negative error)
>> - When do_dup2() succeeds (returns 0)
>>
>> Both of these paths return directly without unlocking files->file_lock,
>> causing a lock leak and potential deadlock.
>>
>> Fix this by making both error and success paths go through the
>> out_unlock label to ensure the lock is always released.
> do_dup2 always releases the lock regardless of return value, so this
> patch cannot be correct.
>
> that aside, there is another consumer which would also need patching if
> the issue was real
>
>> Fixes: 708c04a5c2b7 ("fs: always return zero on success from replace_fd()")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
>> ---
>>   fs/file.c | 3 +--
>>   1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/fs/file.c b/fs/file.c
>> index 2c81c0b162d0..d0f019fb0568 100644
>> --- a/fs/file.c
>> +++ b/fs/file.c
>> @@ -1361,8 +1361,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
>>   		goto out_unlock;
>>   	err = do_dup2(files, file, fd, flags);
>>   	if (err < 0)
>> -		return err;
>> -	return 0;
>> +		goto out_unlock;
>>   
>>   out_unlock:
>>   	spin_unlock(&files->file_lock);
>> -- 
>> 2.25.1
>>


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

* Re: [PATCH] fs: Fix lock leak in replace_fd()
  2026-05-21 14:45 ` Mateusz Guzik
  2026-05-22  9:23   ` Hongling Zeng
@ 2026-05-22 10:10   ` Christian Brauner
  1 sibling, 0 replies; 4+ messages in thread
From: Christian Brauner @ 2026-05-22 10:10 UTC (permalink / raw)
  To: Mateusz Guzik
  Cc: Hongling Zeng, viro, jack, thomas.weissschuh, linux-fsdevel,
	linux-kernel, zhongling0719, stable

On Thu, May 21, 2026 at 04:45:28PM +0200, Mateusz Guzik wrote:
> On Thu, May 21, 2026 at 03:49:34PM +0800, Hongling Zeng wrote:
> > In replace_fd(), the function acquires files->file_lock but then has
> > two return paths that don't release the lock:
> > - When do_dup2() fails (returns negative error)
> > - When do_dup2() succeeds (returns 0)
> > 
> > Both of these paths return directly without unlocking files->file_lock,
> > causing a lock leak and potential deadlock.
> > 
> > Fix this by making both error and success paths go through the
> > out_unlock label to ensure the lock is always released.
> 
> do_dup2 always releases the lock regardless of return value, so this
> patch cannot be correct.

I mean, also:

static int do_dup2(struct files_struct *files,
        struct file *file, unsigned fd, unsigned flags)
__releases(&files->file_lock)

it's literally in the annotation... and if that had been a bug it would
be very very noticable very very quickly...

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

end of thread, other threads:[~2026-05-22 10:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-21  7:49 [PATCH] fs: Fix lock leak in replace_fd() Hongling Zeng
2026-05-21 14:45 ` Mateusz Guzik
2026-05-22  9:23   ` Hongling Zeng
2026-05-22 10:10   ` Christian Brauner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.