freenode
Kernel & Low-Level

Linux kernel can overwrite read-only pages via PI ref tags

An O_DIRECT write to a metadata-capable block device can store into any page the caller can map for reading; the proposed fix copies the buffer first.

A Linux block-layer bug lets a process with write access to a protection-information block device make the kernel store into pages that process can only read, including another file's page-cache page and the shared zero page. Zakaria Belkadi reported the flaw on the kernel mailing list after reproducing both cases on mainline 7.4-rc3. The path has existed since user metadata buffers were added in 6.14. He asked for a stable backport.

On an O_DIRECT write that carries a user PI buffer, as with io_uring's PI attribute, the kernel pins those pages as a data source and normally uses them directly. If the device's integrity profile uses reference tags, the block layer then rewrites those tags in place before the I/O starts. The store lands in whatever page backs the buffer, with no check that the caller could write it. Write access to one such device is enough; the target page can come from anywhere the process can map for reading.

Caleb Sander Mateos called it an arbitrary overwrite of pages that should be read-only to the process. Userspace chooses the metadata buffer address so the bytes it wants to change sit in a reference-tag field, chooses the seed that the existing tag must match, and chooses the device offset that determines the value written in its place. He noted that on kernels with ublk integrity support, a CAP_SYS_ADMIN process could create such a device itself.

Belkadi's first patch would have pinned the pages for writing when the request is a write and the profile has a reference tag, so a read-only buffer would be refused. Christoph Hellwig rejected that: the kernel should not pin as writable memory that was not handed over as writable, and should bounce the buffer instead, at least when remapping might be required. Belkadi agreed. A follow-up will keep the user iterator read-only and copy the PI buffer into kernel memory for those writes, so tag remapping happens on the copy.

Keith Busch asked whether the copy could be skipped unless the user had requested a reference-tag check, on the view that asking for the check implies accepting a rewrite. Hellwig said no: remapping is an internal detail, and requesting a device check is not consent to violate the mapping direction. He did allow skipping both the bounce and the remap when that flag is unset, if nothing else breaks. Sander Mateos pointed out that remapping currently depends only on the device reporting Type 1 or Type 2 PI, not on the per-request check flag, so conditioning it would change user-visible behavior. He argued for always bounce-buffering as the vulnerability fix and optimizing later. Busch agreed the interface change undercuts the shorter path.