]> git.dujemihanovic.xyz Git - linux.git/commitdiff
btrfs: send: ensure send_fd is writable
authorJann Horn <jannh@google.com>
Fri, 24 Nov 2023 16:48:31 +0000 (17:48 +0100)
committerDavid Sterba <dsterba@suse.com>
Fri, 24 Nov 2023 17:50:53 +0000 (18:50 +0100)
kernel_write() requires the caller to ensure that the file is writable.
Let's do that directly after looking up the ->send_fd.

We don't need a separate bailout path because the "out" path already
does fput() if ->send_filp is non-NULL.

This has no security impact for two reasons:

 - the ioctl requires CAP_SYS_ADMIN
 - __kernel_write() bails out on read-only files - but only since 5.8,
   see commit a01ac27be472 ("fs: check FMODE_WRITE in __kernel_write")

Reported-and-tested-by: syzbot+12e098239d20385264d3@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=12e098239d20385264d3
Fixes: 31db9f7c23fb ("Btrfs: introduce BTRFS_IOC_SEND for btrfs send/receive")
CC: stable@vger.kernel.org # 4.14+
Signed-off-by: Jann Horn <jannh@google.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/send.c

index 3b929f0e8f04f44ccb718d5c17d5910e7a8c7c85..4e36550618e580044fb0b0d573ddfee196cdca5d 100644 (file)
@@ -8158,7 +8158,7 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)
        }
 
        sctx->send_filp = fget(arg->send_fd);
-       if (!sctx->send_filp) {
+       if (!sctx->send_filp || !(sctx->send_filp->f_mode & FMODE_WRITE)) {
                ret = -EBADF;
                goto out;
        }