diff mbox series

cachefiles: Fix oops in vfs_mkdir from cachefiles_get_directory

Message ID 20250325125905.395372-1-marc.dionne@auristor.com (mailing list archive)
State New
Headers show
Series cachefiles: Fix oops in vfs_mkdir from cachefiles_get_directory | expand

Commit Message

Marc Dionne March 25, 2025, 12:59 p.m. UTC
Commit c54b386969a5 ("VFS: Change vfs_mkdir() to return the dentry.")
changed cachefiles_get_directory, replacing "subdir" with a ERR_PTR
from the result of cachefiles_inject_write_error, which is either 0
or some error code.  This causes an oops when the resulting pointer
is passed to vfs_mkdir.

Use a similar pattern to what is used earlier in the function; replace
subdir with either the return value from vfs_mkdir, or the ERR_PTR
of the cachefiles_inject_write_error() return value, but only if it
is non zero.

Fixes: c54b386969a5 ("VFS: Change vfs_mkdir() to return the dentry.")
cc: netfs@lists.linux.dev
Signed-off-by: Marc Dionne <marc.dionne@auristor.com>
---
 fs/cachefiles/namei.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Christian Brauner March 25, 2025, 1:59 p.m. UTC | #1
On Tue, 25 Mar 2025 09:59:05 -0300, Marc Dionne wrote:
> Commit c54b386969a5 ("VFS: Change vfs_mkdir() to return the dentry.")
> changed cachefiles_get_directory, replacing "subdir" with a ERR_PTR
> from the result of cachefiles_inject_write_error, which is either 0
> or some error code.  This causes an oops when the resulting pointer
> is passed to vfs_mkdir.
> 
> Use a similar pattern to what is used earlier in the function; replace
> subdir with either the return value from vfs_mkdir, or the ERR_PTR
> of the cachefiles_inject_write_error() return value, but only if it
> is non zero.
> 
> [...]

Applied to the vfs.fixes branch of the vfs/vfs.git tree.
Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] cachefiles: Fix oops in vfs_mkdir from cachefiles_get_directory
      https://git.kernel.org/vfs/vfs/c/406fad7698f5
NeilBrown March 27, 2025, 10:41 p.m. UTC | #2
On Tue, 25 Mar 2025, Marc Dionne wrote:
> Commit c54b386969a5 ("VFS: Change vfs_mkdir() to return the dentry.")
> changed cachefiles_get_directory, replacing "subdir" with a ERR_PTR
> from the result of cachefiles_inject_write_error, which is either 0
> or some error code.  This causes an oops when the resulting pointer
> is passed to vfs_mkdir.

Thanks for fixing that - now that I look at my code again it is
obviously wrong :-(

Reviewed-by: NeilBrown <neilb@sue.de>

Thanks,
NeilBrown


> 
> Use a similar pattern to what is used earlier in the function; replace
> subdir with either the return value from vfs_mkdir, or the ERR_PTR
> of the cachefiles_inject_write_error() return value, but only if it
> is non zero.
> 
> Fixes: c54b386969a5 ("VFS: Change vfs_mkdir() to return the dentry.")
> cc: netfs@lists.linux.dev
> Signed-off-by: Marc Dionne <marc.dionne@auristor.com>
> ---
>  fs/cachefiles/namei.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c
> index 83a60126de0f..14d0cc894000 100644
> --- a/fs/cachefiles/namei.c
> +++ b/fs/cachefiles/namei.c
> @@ -128,10 +128,11 @@ struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
>  		ret = security_path_mkdir(&path, subdir, 0700);
>  		if (ret < 0)
>  			goto mkdir_error;
> -		subdir = ERR_PTR(cachefiles_inject_write_error());
> -		if (!IS_ERR(subdir))
> +		ret = cachefiles_inject_write_error();
> +		if (ret == 0)
>  			subdir = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), subdir, 0700);
> -		ret = PTR_ERR(subdir);
> +		else
> +			subdir = ERR_PTR(ret);
>  		if (IS_ERR(subdir)) {
>  			trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
>  						   cachefiles_trace_mkdir_error);
> -- 
> 2.48.1
> 
>
diff mbox series

Patch

diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c
index 83a60126de0f..14d0cc894000 100644
--- a/fs/cachefiles/namei.c
+++ b/fs/cachefiles/namei.c
@@ -128,10 +128,11 @@  struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
 		ret = security_path_mkdir(&path, subdir, 0700);
 		if (ret < 0)
 			goto mkdir_error;
-		subdir = ERR_PTR(cachefiles_inject_write_error());
-		if (!IS_ERR(subdir))
+		ret = cachefiles_inject_write_error();
+		if (ret == 0)
 			subdir = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), subdir, 0700);
-		ret = PTR_ERR(subdir);
+		else
+			subdir = ERR_PTR(ret);
 		if (IS_ERR(subdir)) {
 			trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
 						   cachefiles_trace_mkdir_error);