Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped
@ 2026-11-18 15:40 Zakaria Belkadi
  2026-11-19 22:44 ` Caleb Sander Mateos
  2026-11-20 15:28 ` Christoph Hellwig
  0 siblings, 2 replies; 5+ messages in thread
From: Zakaria Belkadi @ 2026-11-18 15:40 UTC (permalink / raw)
  To: Jens Axboe
  Cc: linux-block, linux-kernel, Kanchan Joshi, Anuj Gupta,
	florence.schadle, frederic.tronel, louis.rilling, Zakaria Belkadi,
	stable

For an O_DIRECT write that carries a user protection-information buffer
(io_uring PI attribute), the PI pages are pinned as a data source,
without FOLL_WRITE: io_prep_rw_pi() imports the buffer as ITER_SOURCE,
and bio_integrity_map_user() pins the pages and normally uses them
directly. It copies them to a kernel buffer only if they are misaligned
or in too many segments.

If the integrity profile of the device has BLK_INTEGRITY_REF_TAG,
blk_mq_start_request() -> blk_integrity_prepare() then remaps the
reference tags in place in those pages. So the kernel stores into
whatever page backs the buffer, including a page the caller can only
read.

Write access to one such device is therefore enough to make the kernel
write into any page the user can map for reading, wherever that page
comes from. Two examples, both tested: the page cache page of a file
the user can only read, and the shared zero page.

Pin the pages for writing when the request is a write and the profile
has a reference tag. A read-only buffer is then refused, and a
never-written anonymous buffer gets its own page.

Fixes: 3d8b5a22d404 ("block: add support to pass user meta buffer")
Cc: stable@vger.kernel.org
Signed-off-by: Zakaria Belkadi <zakaria.belkadi@inria.fr>
---
Resend: my first attempt today went out with an HTML part and was
rejected by the lists. Sorry for the noise to those who got it
directly.

Found by a source review assisted by an LLM. I first sent this
privately to Jens and security@kernel.org on 16 November, because I had
missed that Documentation/process/security-bugs.rst asks to treat such
a finding as public. So I resend it here as a normal patch; sorry for
the duplicate. The patch in the private mail was truncated, please use
this one.

I reproduced it myself. A reproducer is available on request.

Affected version: mainline v7.4-rc3 (aebb72744764); both cases below
were observed on it. The path exists since the commit in Fixes, v6.14.

Path (line numbers at v7.4-rc3):
  io_uring/rw.c:220, io_prep_rw_pi(): for a write, the PI buffer is
    imported as ITER_SOURCE.
  block/bio-integrity.c:368, bio_integrity_map_user(): the pages are
    pinned without FOLL_WRITE (lib/iov_iter.c:1784) and normally used
    directly.
  block/blk-mq.c:1377, blk_mq_start_request() -> blk_integrity_prepare()
    -> blk_set_t10_map_ref() (block/t10-pi.c:483) stores into them:
        if (ref == virt && ref != ref_tag)
                put_unaligned_be32(ref_tag, &pi->ref_tag);

Setup: QEMU guest, emulated NVMe namespace with T10-DIF type 1. Root
gives one unprivileged user (uid 1000, no supplementary groups, no
capabilities) ownership of the device node of that namespace, and
nothing else. That user does one io_uring O_DIRECT write to it.

1. PI buffer = read-only mapping of a file. The file is on another disk
   and belongs to root (mode 0444); the user can write neither. The
   write changed the reference tag stored in the file, as seen through
   the mapping, through pread() and by root. After dropping the page
   cache the old content was back: the cached page was changed, not
   marked dirty.

2. PI buffer = never-written anonymous memory (shared zero page). On
   v7.4-rc3 the zero page is read-only, and the kernel oopses with a
   write fault in blk_integrity_remap(). Up to v7.1 the x86-64 zero
   page is writable, so the store lands: on stable 6.18.58 the write
   succeeded, and a new, independent anonymous mapping then read the
   reference tag where there should be zeros.

These two pages are only examples. The device is the one thing the user
was given, and it does not limit where the store lands: that depends
only on what the user maps as the PI buffer.

Conditions: a device whose integrity profile has a reference tag (NVMe
PI type 1/2; SCSI DIF type 1/2 by the source, not tested), opened for
writing with O_DIRECT. What is stored is the 4-byte reference tag of
the device, at the tag positions, not data the caller chooses freely.

Fix tested on v7.4-rc3, same user: both buffers above are refused (the
write fails with EIO), with no fault and no change to the file or to
the zero page; a never-written writable buffer gets its own page; a
normal writable buffer works as before. Not tested: v6.14 to v7.1 with
the fix, stacked devices.

The patch makes such writes refuse a read-only PI buffer. If those must
keep working, the PI buffer could be bounced instead, always or only
when pinning for writing fails. I can test another fix.

 block/bio-integrity.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index d3df726e0..3ee2d49d8 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -487,6 +487,14 @@ int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
 		return -EINVAL;
 
 	it.count = integrity_bytes;
+
+	/*
+	 * blk_integrity_prepare() remaps the reference tags of a write in
+	 * place, so the PI pages are written. Pin them for writing.
+	 */
+	if (bio_op(bio) == REQ_OP_WRITE && (bi->flags & BLK_INTEGRITY_REF_TAG))
+		it.data_source = ITER_DEST;
+
 	ret = bio_integrity_map_user(bio, &it);
 	if (!ret) {
 		bio_uio_meta_to_bip(bio, meta);

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped
  2026-11-18 15:40 [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped Zakaria Belkadi
@ 2026-11-19 22:44 ` Caleb Sander Mateos
  2026-11-20 15:28 ` Christoph Hellwig
  1 sibling, 0 replies; 5+ messages in thread
From: Caleb Sander Mateos @ 2026-11-19 22:44 UTC (permalink / raw)
  To: Zakaria Belkadi
  Cc: Jens Axboe, linux-block, linux-kernel, Kanchan Joshi, Anuj Gupta,
	florence.schadle, frederic.tronel, louis.rilling, stable

On Wed, Nov 18, 2026 at 7:42 AM Zakaria Belkadi
<zakaria.belkadi@inria.fr> wrote:
>
> For an O_DIRECT write that carries a user protection-information buffer
> (io_uring PI attribute), the PI pages are pinned as a data source,
> without FOLL_WRITE: io_prep_rw_pi() imports the buffer as ITER_SOURCE,
> and bio_integrity_map_user() pins the pages and normally uses them
> directly. It copies them to a kernel buffer only if they are misaligned
> or in too many segments.
>
> If the integrity profile of the device has BLK_INTEGRITY_REF_TAG,
> blk_mq_start_request() -> blk_integrity_prepare() then remaps the
> reference tags in place in those pages. So the kernel stores into
> whatever page backs the buffer, including a page the caller can only
> read.
>
> Write access to one such device is therefore enough to make the kernel
> write into any page the user can map for reading, wherever that page
> comes from. Two examples, both tested: the page cache page of a file
> the user can only read, and the shared zero page.
>
> Pin the pages for writing when the request is a write and the profile
> has a reference tag. A read-only buffer is then refused, and a
> never-written anonymous buffer gets its own page.
>
> Fixes: 3d8b5a22d404 ("block: add support to pass user meta buffer")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zakaria Belkadi <zakaria.belkadi@inria.fr>

Yeah, this looks serious. It's pretty much an arbitrary overwrite
primitive for pages that should be read-only to the process: userspace
can choose the starting address of the metadata buffer so the bytes it
wants to update are part of a PI tuple ref tag field, it can choose
the seed to determine what ref tag value to match, and it can choose
the block device offset to determine what value to write in its place.
It does require userspace to have write access to a metadata-enabled
block device. (Though on kernels with ublk enabled and
UBLK_F_INTEGRITY support, a CAP_SYS_ADMIN process could use ublk to
create such a device...)

> ---
> Resend: my first attempt today went out with an HTML part and was
> rejected by the lists. Sorry for the noise to those who got it
> directly.
>
> Found by a source review assisted by an LLM. I first sent this
> privately to Jens and security@kernel.org on 16 November, because I had
> missed that Documentation/process/security-bugs.rst asks to treat such
> a finding as public. So I resend it here as a normal patch; sorry for
> the duplicate. The patch in the private mail was truncated, please use
> this one.
>
> I reproduced it myself. A reproducer is available on request.
>
> Affected version: mainline v7.4-rc3 (aebb72744764); both cases below
> were observed on it. The path exists since the commit in Fixes, v6.14.
>
> Path (line numbers at v7.4-rc3):
>   io_uring/rw.c:220, io_prep_rw_pi(): for a write, the PI buffer is
>     imported as ITER_SOURCE.
>   block/bio-integrity.c:368, bio_integrity_map_user(): the pages are
>     pinned without FOLL_WRITE (lib/iov_iter.c:1784) and normally used
>     directly.
>   block/blk-mq.c:1377, blk_mq_start_request() -> blk_integrity_prepare()
>     -> blk_set_t10_map_ref() (block/t10-pi.c:483) stores into them:
>         if (ref == virt && ref != ref_tag)
>                 put_unaligned_be32(ref_tag, &pi->ref_tag);
>
> Setup: QEMU guest, emulated NVMe namespace with T10-DIF type 1. Root
> gives one unprivileged user (uid 1000, no supplementary groups, no
> capabilities) ownership of the device node of that namespace, and
> nothing else. That user does one io_uring O_DIRECT write to it.
>
> 1. PI buffer = read-only mapping of a file. The file is on another disk
>    and belongs to root (mode 0444); the user can write neither. The
>    write changed the reference tag stored in the file, as seen through
>    the mapping, through pread() and by root. After dropping the page
>    cache the old content was back: the cached page was changed, not
>    marked dirty.
>
> 2. PI buffer = never-written anonymous memory (shared zero page). On
>    v7.4-rc3 the zero page is read-only, and the kernel oopses with a
>    write fault in blk_integrity_remap(). Up to v7.1 the x86-64 zero
>    page is writable, so the store lands: on stable 6.18.58 the write
>    succeeded, and a new, independent anonymous mapping then read the
>    reference tag where there should be zeros.
>
> These two pages are only examples. The device is the one thing the user
> was given, and it does not limit where the store lands: that depends
> only on what the user maps as the PI buffer.
>
> Conditions: a device whose integrity profile has a reference tag (NVMe
> PI type 1/2; SCSI DIF type 1/2 by the source, not tested), opened for
> writing with O_DIRECT. What is stored is the 4-byte reference tag of
> the device, at the tag positions, not data the caller chooses freely.
>
> Fix tested on v7.4-rc3, same user: both buffers above are refused (the
> write fails with EIO), with no fault and no change to the file or to
> the zero page; a never-written writable buffer gets its own page; a
> normal writable buffer works as before. Not tested: v6.14 to v7.1 with
> the fix, stacked devices.
>
> The patch makes such writes refuse a read-only PI buffer. If those must
> keep working, the PI buffer could be bounced instead, always or only
> when pinning for writing fails. I can test another fix.

CoW pages should get copied to a writable page in
iov_iter_extract_pages(), so that leaves only memory explicitly mapped
read-only into the process. Seems pretty unlikely that any users would
intentionally do that (they would have had to initialize the metadata
while it was writable and then mprotect(PROT_READ) it before issuing
the write I/O). So this approach seems fine to me. Might have made
more sense to disable ref tag remapping for IORING_RW_ATTR_FLAG_PI
I/Os from the start, but would be a UAPI-breaking change to do that
now.

>
>  block/bio-integrity.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/block/bio-integrity.c b/block/bio-integrity.c
> index d3df726e0..3ee2d49d8 100644
> --- a/block/bio-integrity.c
> +++ b/block/bio-integrity.c
> @@ -487,6 +487,14 @@ int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
>                 return -EINVAL;
>
>         it.count = integrity_bytes;
> +
> +       /*
> +        * blk_integrity_prepare() remaps the reference tags of a write in
> +        * place, so the PI pages are written. Pin them for writing.
> +        */
> +       if (bio_op(bio) == REQ_OP_WRITE && (bi->flags & BLK_INTEGRITY_REF_TAG))
> +               it.data_source = ITER_DEST;

Could probably drop the bio_op(bio) == REQ_OP_WRITE check; reads will
also write to the metadata buffer in blk_reftag_remap_complete()
(though they will already have data_source = ITER_DEST).

Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>

> +
>         ret = bio_integrity_map_user(bio, &it);
>         if (!ret) {
>                 bio_uio_meta_to_bip(bio, meta);
>
> base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
> --
> 2.39.5
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped
  2026-11-18 15:40 [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped Zakaria Belkadi
  2026-11-19 22:44 ` Caleb Sander Mateos
@ 2026-11-20 15:28 ` Christoph Hellwig
  2026-11-20 16:22   ` Zakaria Belkadi
  1 sibling, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2026-11-20 15:28 UTC (permalink / raw)
  To: Zakaria Belkadi
  Cc: Jens Axboe, linux-block, linux-kernel, Kanchan Joshi, Anuj Gupta,
	florence.schadle, frederic.tronel, louis.rilling, stable

On Wed, Nov 18, 2026 at 04:40:53PM +0100, Zakaria Belkadi wrote:
> +	/*
> +	 * blk_integrity_prepare() remaps the reference tags of a write in
> +	 * place, so the PI pages are written. Pin them for writing.
> +	 */
> +	if (bio_op(bio) == REQ_OP_WRITE && (bi->flags & BLK_INTEGRITY_REF_TAG))
> +		it.data_source = ITER_DEST;

I don't think we can just pin memory as writable that wasn't given to
us as writable.  So this needs to force bounce buffering, at least when
remapping might be required.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped
  2026-11-20 15:28 ` Christoph Hellwig
@ 2026-11-20 16:22   ` Zakaria Belkadi
  2026-11-20 16:58     ` Keith Busch
  0 siblings, 1 reply; 5+ messages in thread
From: Zakaria Belkadi @ 2026-11-20 16:22 UTC (permalink / raw)
  To: Christoph Hellwig, Caleb Sander Mateos
  Cc: Jens Axboe, linux-block, linux-kernel, Kanchan Joshi, Anuj Gupta,
	florence.schadle, frederic.tronel, louis.rilling, stable

Christoph, Caleb,

Thanks to both of you for the review.

On Fri, Nov 20, 2026 at 07:28:07AM -0800, Christoph Hellwig wrote:
> I don't think we can just pin memory as writable that wasn't given to
> us as writable.  So this needs to force bounce buffering, at least when
> remapping might be required.

Agreed. I'll keep the PI iterator as ITER_SOURCE. In v2,
bio_integrity_map_user() will bounce the user PI buffer for REQ_OP_WRITE
when the integrity profile has BLK_INTEGRITY_REF_TAG. That way the
block layer can remap the tags in its own copy.

Thanks,
Zakaria

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped
  2026-11-20 16:22   ` Zakaria Belkadi
@ 2026-11-20 16:58     ` Keith Busch
  0 siblings, 0 replies; 5+ messages in thread
From: Keith Busch @ 2026-11-20 16:58 UTC (permalink / raw)
  To: Zakaria Belkadi
  Cc: Christoph Hellwig, Caleb Sander Mateos, Jens Axboe, linux-block,
	linux-kernel, Kanchan Joshi, Anuj Gupta, florence.schadle,
	frederic.tronel, louis.rilling, stable

On Fri, Nov 20, 2026 at 05:22:57PM +0100, Zakaria Belkadi wrote:
> Christoph, Caleb,
> 
> Thanks to both of you for the review.
> 
> On Fri, Nov 20, 2026 at 07:28:07AM -0800, Christoph Hellwig wrote:
> > I don't think we can just pin memory as writable that wasn't given to
> > us as writable.  So this needs to force bounce buffering, at least when
> > remapping might be required.
> 
> Agreed. I'll keep the PI iterator as ITER_SOURCE. In v2,
> bio_integrity_map_user() will bounce the user PI buffer for REQ_OP_WRITE
> when the integrity profile has BLK_INTEGRITY_REF_TAG. That way the
> block layer can remap the tags in its own copy.

Can avoid bouncing? I think the remapping flags can be tied to the
user's reftag check request.

If BIP_CHECK_REFTAG is set from the user setting IO_INTEGRITY_CHK_REFTAG
on a write, we can map the PI buffer for writing. The user explicitly
asked to have the ref tags checked by the device, and partitions require
remapping them, so they've told the kernel to write to their buffer.

And if the flag isn't set, blk_integrity_prepare() can skip the remap
for that bio because the request asked to not be checked.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-20 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 15:40 [PATCH RESEND] block: pin user PI pages for writing when ref tags are remapped Zakaria Belkadi
2026-11-19 22:44 ` Caleb Sander Mateos
2026-11-20 15:28 ` Christoph Hellwig
2026-11-20 16:22   ` Zakaria Belkadi
2026-11-20 16:58     ` Keith Busch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox