btrfs: reject encoded reads from write-only file descriptors
Subsystems (2)
PATCH 1/1 btrfs: reject encoded reads from write-only file descriptors
BTRFS_IOC_ENCODED_READ and the matching io_uring command require CAP_SYS_ADMIN, but neither checks FMODE_READ. A caller can therefore read file contents through an O_WRONLY descriptor, whereas read() and IORING_OP_READ on that descriptor return EBADF.
This is observable under three read restrictions while CAP_SYS_ADMIN is retained:
- Landlock denies READ_FILE. A new O_RDONLY open fails with EACCES, but an O_WRONLY open succeeds after the restriction is installed.
- DAC mode 0200 denies O_RDONLY after CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH are dropped, but allows O_WRONLY.
- With CONFIG_BTRFS_FS_POSIX_ACL=y, a POSIX ACL grants only write access to a named user. With fsuid set to that user and the DAC bypass capabilities dropped, O_RDONLY fails and O_WRONLY succeeds.
On Linux 7.4.0-rc3-g666539eecdf4, both encoded-read paths returned the same 8192 bytes from an uncompressed Btrfs extent in each case. Normal read() on the O_WRONLY descriptor returned EBADF.
Check FMODE_READ in both entry points, matching the FMODE_WRITE checks already present in the encoded-write paths. Both encoded reads then return EBADF for O_WRONLY; O_RDONLY controls with read permission still succeed. The Landlock and DAC cases were also verified on Linux 7.3.4.
---
fs/btrfs/ioctl.c | 10 ++++++++++
1 file changed, 10 insertions(+)
fs/btrfs/ioctl.c +0 -0
@@ -4392,6 +4392,11 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
goto out_acct;
}
+ if (!(file->f_mode & FMODE_READ)) {
+ ret = -EBADF;
+ goto out_acct;
+ }
+
if (compat) {
#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
struct btrfs_ioctl_encoded_io_args_32 args32;
@@ -4779,6 +4784,11 @@ static int btrfs_uring_encoded_read(struct io_uring_cmd *cmd, unsigned int issue }
sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
+ if (!(file->f_mode & FMODE_READ)) {
+ ret = -EBADF;
+ goto out_acct;
+ }
+
if (issue_flags & IO_URING_F_COMPAT) {
#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags);
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
Signature · 1 line
2.39.5
PATCH 1/1 btrfs: reject encoded reads from write-only file descriptors
BTRFS_IOC_ENCODED_READ and the matching io_uring command require CAP_SYS_ADMIN, but neither checks FMODE_READ. A caller can therefore read file contents through an O_WRONLY descriptor, whereas read() and IORING_OP_READ on that descriptor return EBADF.
This is observable under three read restrictions while CAP_SYS_ADMIN is retained:
- Landlock denies READ_FILE. A new O_RDONLY open fails with EACCES, but an O_WRONLY open succeeds after the restriction is installed.
- DAC mode 0200 denies O_RDONLY after CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH are dropped, but allows O_WRONLY.
- With CONFIG_BTRFS_FS_POSIX_ACL=y, a POSIX ACL grants only write access to a named user. With fsuid set to that user and the DAC bypass capabilities dropped, O_RDONLY fails and O_WRONLY succeeds.
On Linux 7.4.0-rc3-g666539eecdf4, both encoded-read paths returned the same 8192 bytes from an uncompressed Btrfs extent in each case. Normal read() on the O_WRONLY descriptor returned EBADF.
Check FMODE_READ in both entry points, matching the FMODE_WRITE checks already present in the encoded-write paths. Both encoded reads then return EBADF for O_WRONLY; O_RDONLY controls with read permission still succeed. The Landlock and DAC cases were also verified on Linux 7.3.4.
---
v2: Resend with ASCII spaces in the diff context. The v1 email had
U+00A0 at the start of context lines. No code changes.
fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/btrfs/ioctl.c +10 -0
@@ -4392,6 +4392,11 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, goto out_acct; } +if (!(file->f_mode &FMODE_READ )) { + ret = -EBADF ; + goto out_acct; + } +if (compat) {#if defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT )struct btrfs_ioctl_encoded_io_args_32 args32;
@@ -4779,6 +4784,11 @@ static int btrfs_uring_encoded_read(struct io_uring_cmd *cmd, unsigned int issue } sqe_addr =u64_to_user_ptr (READ_ONCE (cmd->sqe->addr)); +if (!(file->f_mode &FMODE_READ )) { + ret = -EBADF ; + goto out_acct; + } +if (issue_flags &IO_URING_F_COMPAT ) {#if defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT ) copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags);
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
Signature · 1 line
2.39.5