Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v3 0/2] fs: avoid fdput() after failed fdget()
@ 2020-05-13 23:33 Shuah Khan
  2020-05-13 23:33 ` [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range() Shuah Khan
  2020-05-13 23:33 ` [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd() Shuah Khan
  0 siblings, 2 replies; 5+ messages in thread
From: Shuah Khan @ 2020-05-13 23:33 UTC (permalink / raw)
  To: viro, axboe, zohar, mcgrof, keescook
  Cc: Shuah Khan, linux-fsdevel, linux-kernel

While debugging an unrelated problem, I noticed these two cases fdput()
is called after failed fdget() while reviewing at all the fdget() and
fdput() paths in the kernel.

Changes since v2:
Patches 1&2 changed to get rid of goto.

Changes since v1:
Patch 1:
  Changed to address review comments to refine the code for improved
  readability in addition to the change to avoid fdput() on failed
  fdget()
Patch 2:
  No change to v1. Including it in the series to keep the patches
  together.

Shuah Khan (2):
  fs: avoid fdput() after failed fdget() in ksys_sync_file_range()
  fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd()

 fs/exec.c |  6 +++---
 fs/sync.c | 10 +++++-----
 2 files changed, 8 insertions(+), 8 deletions(-)

-- 
2.25.1


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

* [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range()
  2020-05-13 23:33 [PATCH v3 0/2] fs: avoid fdput() after failed fdget() Shuah Khan
@ 2020-05-13 23:33 ` Shuah Khan
  2020-05-15 16:12   ` Luis Chamberlain
  2020-05-13 23:33 ` [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd() Shuah Khan
  1 sibling, 1 reply; 5+ messages in thread
From: Shuah Khan @ 2020-05-13 23:33 UTC (permalink / raw)
  To: viro, axboe, zohar, mcgrof, keescook
  Cc: Shuah Khan, linux-fsdevel, linux-kernel

Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
in fd.flags.

Change it anyway since failed fdget() doesn't require a fdput(). Refine
the code path a bit for it to read more clearly.
Reference: 22f96b3808c1 ("fs: add sync_file_range() helper")

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 fs/sync.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/sync.c b/fs/sync.c
index 4d1ff010bc5a..3ec312bf62eb 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -365,14 +365,14 @@ int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
 			 unsigned int flags)
 {
 	int ret;
-	struct fd f;
+	struct fd f = fdget(fd);
 
-	ret = -EBADF;
-	f = fdget(fd);
-	if (f.file)
-		ret = sync_file_range(f.file, offset, nbytes, flags);
+	if (!f.file)
+		return -EBADF;
 
+	ret = sync_file_range(f.file, offset, nbytes, flags);
 	fdput(f);
+
 	return ret;
 }
 
-- 
2.25.1


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

* [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd()
  2020-05-13 23:33 [PATCH v3 0/2] fs: avoid fdput() after failed fdget() Shuah Khan
  2020-05-13 23:33 ` [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range() Shuah Khan
@ 2020-05-13 23:33 ` Shuah Khan
  2020-05-15 19:22   ` Mimi Zohar
  1 sibling, 1 reply; 5+ messages in thread
From: Shuah Khan @ 2020-05-13 23:33 UTC (permalink / raw)
  To: viro, axboe, zohar, mcgrof, keescook
  Cc: Shuah Khan, linux-fsdevel, linux-kernel

Fix kernel_read_file_from_fd() to avoid fdput() after a failed fdget().
fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
in fd.flags. Fix it anyway since failed fdget() doesn't require
a fdput().

This was introduced in a commit that added kernel_read_file_from_fd() as
a wrapper for the VFS common kernel_read_file().

Fixes: b844f0ecbc56 ("vfs: define kernel_copy_file_from_fd()")
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 fs/exec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 06b4c550af5d..16a3d3192d6a 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1015,14 +1015,14 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
 			     enum kernel_read_file_id id)
 {
 	struct fd f = fdget(fd);
-	int ret = -EBADF;
+	int ret;
 
 	if (!f.file)
-		goto out;
+		return -EBADF;
 
 	ret = kernel_read_file(f.file, buf, size, max_size, id);
-out:
 	fdput(f);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);
-- 
2.25.1


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

* Re: [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range()
  2020-05-13 23:33 ` [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range() Shuah Khan
@ 2020-05-15 16:12   ` Luis Chamberlain
  0 siblings, 0 replies; 5+ messages in thread
From: Luis Chamberlain @ 2020-05-15 16:12 UTC (permalink / raw)
  To: Shuah Khan; +Cc: viro, axboe, zohar, keescook, linux-fsdevel, linux-kernel

On Wed, May 13, 2020 at 05:33:20PM -0600, Shuah Khan wrote:
> Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
> fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
> in fd.flags.
> 
> Change it anyway since failed fdget() doesn't require a fdput(). Refine
> the code path a bit for it to read more clearly.
> Reference: 22f96b3808c1 ("fs: add sync_file_range() helper")
> 
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>

  Luis

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

* Re: [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd()
  2020-05-13 23:33 ` [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd() Shuah Khan
@ 2020-05-15 19:22   ` Mimi Zohar
  0 siblings, 0 replies; 5+ messages in thread
From: Mimi Zohar @ 2020-05-15 19:22 UTC (permalink / raw)
  To: Shuah Khan, viro, axboe, zohar, mcgrof, keescook
  Cc: linux-fsdevel, linux-kernel

On Wed, 2020-05-13 at 17:33 -0600, Shuah Khan wrote:
> Fix kernel_read_file_from_fd() to avoid fdput() after a failed fdget().
> fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
> in fd.flags. Fix it anyway since failed fdget() doesn't require
> a fdput().
> 
> This was introduced in a commit that added kernel_read_file_from_fd() as
> a wrapper for the VFS common kernel_read_file().
> 
> Fixes: b844f0ecbc56 ("vfs: define kernel_copy_file_from_fd()")
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Thanks, Shuah.

Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>

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

end of thread, other threads:[~2020-05-15 19:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 23:33 [PATCH v3 0/2] fs: avoid fdput() after failed fdget() Shuah Khan
2020-05-13 23:33 ` [PATCH v3 1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range() Shuah Khan
2020-05-15 16:12   ` Luis Chamberlain
2020-05-13 23:33 ` [PATCH v3 2/2] fs: avoid fdput() after failed fdget() in kernel_read_file_from_fd() Shuah Khan
2020-05-15 19:22   ` Mimi Zohar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).