Skip to contentSkip to conversation

btrfs: send: Ensure send_fd is writable

PATCH In review Jann Horn 2 participants 2 messages Nov 24, 2023 Btrfs file system

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 btrfs: send: Ensure send_fd is writable

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 [email protected]
Fixes 31db9f7c23fb ("Btrfs: introduce BTRFS_IOC_SEND for btrfs send/receive")
---
 fs/btrfs/send.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/send.c +1 -1
index 3b929f0e8f04..4e36550618e5 100644
@@ -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;
 	}

base-commit: 98b1cc82c4affc16f5598d4fa14b1858671b2263

Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
#1 lore raw
Quoted 12 lines · Jann Horn

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")

Quoted 5 lines
Reported-and-tested-by [email protected]
Fixes 31db9f7c23fb ("Btrfs: introduce BTRFS_IOC_SEND for btrfs send/receive")

Added to misc-next, thanks.