Linux io-uring development
 help / color / mirror / Atom feed
From: "Sieun Yang" <didtldms2525@naver.com>
To: <dsterba@suse.com>
Cc: <mason@kernel.org>, <linux-btrfs@vger.kernel.org>,
	<axboe@kernel.dk>, <io-uring@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH] btrfs: reject encoded reads from write-only file descriptors
Date: Tue, 17 Nov 2026 13:41:23 +0900	[thread overview]
Message-ID: <d22b71efbe22b3dab362e4c94062e073@cweb005.nm> (raw)
In-Reply-To: <652bf9fd6e20ef231652b95efa65355@cweb018.nm>

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.

Fixes: 1881fba89bd5 ("btrfs: add BTRFS_IOC_ENCODED_READ ioctl")
Fixes: 34310c442e17 ("btrfs: add io_uring command for encoded reads (ENCODED_READ ioctl)")
Cc: stable@vger.kernel.org
Signed-off-by: Sieun Yang <didtldms2525@jnu.ac.kr>
---
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(+)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 6ba883db1..f1e46b4e6 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -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
-- 
2.39.5




      reply	other threads:[~2026-11-17  4:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  4:29 [PATCH] btrfs: reject encoded reads from write-only file descriptors Sieun Yang
2026-11-17  4:41 ` Sieun Yang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d22b71efbe22b3dab362e4c94062e073@cweb005.nm \
    --to=didtldms2525@naver.com \
    --cc=axboe@kernel.dk \
    --cc=dsterba@suse.com \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox