]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
file: ensure cleanup
authorChristian Brauner <brauner@kernel.org>
Sat, 13 Dec 2025 07:45:23 +0000 (08:45 +0100)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 13 Dec 2025 08:04:32 +0000 (20:04 +1200)
Brown paper bag time. This is a silly oversight where I missed to drop
the error condition checking to ensure we clean up on early error
returns. I have an internal unit testset coming up for this which will
catch all such issues going forward.

Reported-by: Chris Mason <clm@fb.com>
Reported-by: Jeff Layton <jlayton@kernel.org>
Fixes: 011703a9acd7 ("file: add FD_{ADD,PREPARE}()")
Signed-off-by: Christian Brauner <brauner@kernel.org>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/file.h

index cf389fde9bc28c4b4ee3c3f65abced2388484860..27484b444d3155685cdbb89f546f26ef66e3e1b4 100644 (file)
@@ -161,12 +161,10 @@ typedef struct fd_prepare class_fd_prepare_t;
 /* Do not use directly. */
 static inline void class_fd_prepare_destructor(const struct fd_prepare *fdf)
 {
-       if (unlikely(fdf->err)) {
-               if (likely(fdf->__fd >= 0))
-                       put_unused_fd(fdf->__fd);
-               if (unlikely(!IS_ERR_OR_NULL(fdf->__file)))
-                       fput(fdf->__file);
-       }
+       if (unlikely(fdf->__fd >= 0))
+               put_unused_fd(fdf->__fd);
+       if (unlikely(!IS_ERR_OR_NULL(fdf->__file)))
+               fput(fdf->__file);
 }
 
 /* Do not use directly. */
@@ -230,7 +228,8 @@ static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf)
                VFS_WARN_ON_ONCE(fdp->__fd < 0);               \
                VFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp->__file)); \
                fd_install(fdp->__fd, fdp->__file);            \
-               fdp->__fd;                                     \
+               retain_and_null_ptr(fdp->__file);              \
+               take_fd(fdp->__fd);                            \
        })
 
 /* Do not use directly. */