← Index berkoc · research
Medium reported

Linux kernel drm/qxl: validate dst_offset in apply_reloc and apply_surf_reloc

13-year-old unvalidated dst_offset in QXL DRM driver allows guest-local OOB write into QEMU emulated VRAM aperture.

Vendor
torvalds/linux · linux-kernel
CWE
CWE-787
CVSS
CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H (5.5)
Disclosed

Background

QXL is the paravirtual 2D graphics device that QEMU exposes to Linux guests when the host uses the SPICE remote-desktop protocol. The guest-side kernel driver lives at drivers/gpu/drm/qxl/ in the mainline tree and implements the standard DRM (Direct Rendering Manager) interface on top of the QXL hardware contract. Userspace programs (typically the Xorg qxl DDX, or a Wayland compositor through a generic DRM path) submit command buffers to the kernel driver via ioctl, and the driver translates those into QXL commands that the QEMU process consumes from a shared ring buffer.

The piece relevant to this writeup is the relocation handler. QXL command buffers reference other buffer objects (BOs) by handle, and the kernel driver patches the command stream with the actual VRAM addresses of those BOs before handing the stream to the device. Two helpers do the patching, both in drivers/gpu/drm/qxl/qxl_ioctl.c:

  • apply_reloc() writes an 8-byte address into a target BO at an offset the caller supplies.
  • apply_surf_reloc() writes a 4-byte surface-id into a target BO at an offset the caller supplies.

The target BO and the offset both come from struct drm_qxl_reloc, which is the per-relocation entry inside the DRM_IOCTL_QXL_EXECBUFFER argument. Any process with DRM_AUTH access to the QXL device node can submit relocations. In a typical desktop Linux guest that means any local user with an active display server session.

The driver's VRAM aperture is mapped at boot through io_mapping_create_wc(vram_base, vram_size), where vram_size is the full QXL VRAM region as seen by the guest, on the order of 64 MiB by default. Individual BOs are sub-regions of that aperture. The aperture itself is real PCI BAR memory on the QEMU side, which means writes from the guest kernel land in QEMU's emulation of the QXL VRAM and are subsequently parsed by QEMU's QXL command path.

A second aperture exists for surfaces. The apply_surf_reloc() path targets the surface aperture, which is laid out by the same mechanism but used for different command-stream payloads. Both apertures inherit the same lack of validation, so the patch has to handle both helpers symmetrically. The relocation entry carries a reloc_type discriminator (QXL_RELOC_TYPE_BO or QXL_RELOC_TYPE_SURF) that selects which helper runs, and the call site dispatches on that field. The size of the write differs (8 bytes for BO relocations, 4 bytes for surface relocations), but the missing-validation pattern is identical.

The threat model is local-user attacker against the guest kernel. Any process holding an open file descriptor on /dev/dri/cardN with DRM_AUTH flag set can submit relocations; in a regular desktop guest that includes any user logged into the active display session, and any container or sandbox that the guest administrator permits to access DRM. The bug does not require root, capability tokens, or any guest-internal privilege escalation. The bar to reach the vulnerable code is "be a normal user with a working desktop session," which is below the level at which most defense-in-depth work is conventionally applied.

The bug

apply_reloc() and apply_surf_reloc() write at reloc_page + (info->dst_offset & ~PAGE_MASK), where reloc_page is io_mapping_map_atomic_wc(qdev->vram_mapping, info->dst_bo->offset + (info->dst_offset & PAGE_MASK)). The dst_offset value flows in from userspace through qxl_process_single_command(), which copies the user-supplied drm_qxl_reloc array into an internal qxl_reloc_info structure. The narrowing from __u64 to uint32_t in that copy still leaves the caller a full 32-bit / 4 GiB range, and no comparison against dst_bo->tbo.base.size happens at any point before the writes.

The intent to validate is documented in the source. A comment above apply_reloc() reads:

/* TODO: dst must be validated, i.e. whole bo on vram/surfacesram */

That comment dates to the original QXL driver merge in Linux 3.10 (June 2013). The TODO has survived 13 years of refactoring without anyone implementing it. The bug class is CWE-787, Out-of-bounds Write, and is reachable from any local user with access to the QXL device node.

The io_mapping_map_atomic_wc() call wraps a BUG_ON(offset >= mapping->size) check, but mapping->size is the size of the full QXL VRAM aperture, not the size of the specific BO. Writes that fall outside the resolved BO but within the aperture pass the assertion and land on whatever data occupies those pages. That includes other guest processes' QXL BOs, queued QXL release-command BOs that QEMU is about to read, and ring buffer entries that QEMU will parse next.

The narrowing from __u64 to uint32_t in the user-to-kernel copy is worth flagging because it changes the bug shape from "arbitrary 64-bit offset" to "32-bit offset within a 4 GiB window". On a typical guest the QXL VRAM aperture is much smaller than 4 GiB (64 MiB is the default, with some platforms allocating up to 256 MiB), so the 32-bit window comfortably covers the aperture. The attacker can pick any offset inside the 4 GiB window; offsets that exceed the aperture trigger the BUG_ON in io_mapping_map_atomic_wc, which is itself a guest-side DoS but is not the interesting primitive. The interesting offsets are the ones inside the aperture but outside the resolved BO, which is the wide majority of values in the 32-bit range for any small BO.

A subtle related case is the wrap at the high end of the 32-bit range. If dst_offset is set to a value within sizeof(uint64_t) of U32_MAX, the addition dst_offset + sizeof(uint64_t) wraps to a small value. A naive bounds check of the form dst_offset + width > size would compare the wrapped (small) value against the BO size, pass the check, and let the write proceed. The patch has to guard against the wrap explicitly, which is why the proposed fix performs both dst_offset > U32_MAX - sizeof(...) and dst_offset + sizeof(...) > size in conjunction.

Reproducer

The vulnerable ioctl is DRM_IOCTL_QXL_EXECBUFFER with a drm_qxl_reloc entry whose dst_offset exceeds the size of the BO identified by dst_handle. I have not personally compiled and run a userspace PoC against a vulnerable guest; the analysis came from reading the source and the relocation flow. A working trigger sketch:

struct drm_qxl_reloc reloc = {
    .src_offset  = 0,
    .dst_offset  = 0xFFFF0000,      /* arbitrary; pick any value > dst_bo size */
    .src_handle  = 0,
    .dst_handle  = small_bo_handle, /* a BO with size << 0xFFFF0000 */
    .reloc_type  = QXL_RELOC_TYPE_BO,
};

struct drm_qxl_execbuffer exec = {
    .commands_num = 1,
    .commands     = (uintptr_t)&one_command,
};

ioctl(drm_fd, DRM_IOCTL_QXL_EXECBUFFER, &exec);

The expected effect is an 8-byte write at vram_base + dst_bo->offset + 0xFFFF0000, which is comfortably outside the BO and somewhere inside the aperture. The apply_surf_reloc() path is the same with reloc_type = QXL_RELOC_TYPE_SURF and a 4-byte write width.

The arithmetic overflow case is its own variant. If dst_offset is close to U32_MAX, then dst_offset + sizeof(uint64_t) wraps and the offset-plus-width check (if it existed) would also be required to guard against the wrap. The proposed patch covers both cases.

Impact

The primary primitive is a guest-local out-of-bounds write into the QXL VRAM aperture. The most direct consequence is corruption of state inside the guest's own QXL command pipeline: other DRM clients' BOs, in-flight release commands, ring buffer indices. That alone is a local DoS or local cross-process integrity bug between DRM-authenticated clients on the guest.

The more interesting consequence is the host attack surface. QEMU's QXL emulation parses the same aperture from the host side. Corrupting QXL command structures that QEMU then dereferences moves the bug from a pure guest concern to a priming primitive for guest-to-host chains whose terminal step lives in userspace QEMU. The QXL parser inside QEMU has a long history of CVEs (CVE-2017-7718, CVE-2017-7717, CVE-2019-12155, among others), so a primitive that lets a guest stage attacker-controlled bytes at attacker-controlled QXL-aperture offsets is a reasonable building block for those chains.

I want to be honest about the ceiling here. I have not constructed an end-to-end guest-to-host chain for this specific primitive. The CVSS 3.1 vector AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H (5.5 Medium) reflects the kernel-side view: scope unchanged, local attacker, integrity and availability impact on the guest kernel's memory contents. If a downstream chain demonstrates a host escape, the appropriate scoring would shift to S:C and the score would jump into the High range. The kernel bug stands as a defense-in-depth concern independent of any QEMU chain, which is the framing the patch uses.

The proposed fix

The patch adds an explicit per-relocation bounds check in the fill loop in qxl_process_single_command(), after dst_bo is resolved and before the relocation is appended to reloc_info[]. It rejects out-of-BO offsets and the arithmetic overflow case with -EINVAL. The relevant addition:

/*
 * dst must be validated, i.e. whole bo on vram/surfacesram.
 * Refuse offsets that would write past the end of the BO,
 * or where dst_offset + write_size would overflow uint32_t.
 */
if (reloc_info[i].type == QXL_RELOC_TYPE_BO &&
    (reloc_info[i].dst_offset > U32_MAX - sizeof(uint64_t) ||
     reloc_info[i].dst_offset + sizeof(uint64_t) >
     reloc_info[i].dst_bo->tbo.base.size)) {
        ret = -EINVAL;
        goto out_free_bos;
}
if (reloc_info[i].type == QXL_RELOC_TYPE_SURF &&
    (reloc_info[i].dst_offset > U32_MAX - sizeof(uint32_t) ||
     reloc_info[i].dst_offset + sizeof(uint32_t) >
     reloc_info[i].dst_bo->tbo.base.size)) {
        ret = -EINVAL;
        goto out_free_bos;
}

Two checks per relocation type. The overflow check fires first because evaluating dst_offset + sizeof(...) on the right-hand side after the wrap would otherwise compare a wrapped value against tbo.base.size. The patch uses U32_MAX rather than (uint32_t)~0 for readability and consistency with the rest of drivers/gpu/drm/qxl/.

The patch is intentionally minimal. It does not refactor the relocation loop, does not introduce a helper, and does not touch the read-side paths. Larger refactors are easier to defer for someone with QXL maintainer-context than to bundle into a 13-year-old-bug-fix patch from a first-time contributor.

Timeline

  • 2026-05-14 02:07 CEST: Initial hardening-report email sent to virtualization@lists.linux.dev, dri-devel@lists.freedesktop.org, spice-devel@lists.freedesktop.org, and security@kernel.org. The first version was framed as a hardening note rather than a regular patch, because the issue is reachable only from a locally authenticated user and the upstream policy on security@kernel.org for non-host-escape kernel bugs is unclear.
  • 2026-05-14 06:57 CEST: Willy Tarreau (w@1wt.eu) replied. His message contained two pieces of guidance. First, the report should be reformatted as a regular [PATCH] and sent to the QXL maintainers through the normal flow; security@kernel.org is unnecessary because the issue is already public on the virtualization list. Second, if the patch is accepted the author will be credited for both finding and fixing the bug, which is the standard kernel attribution model.
  • 2026-05-14 10:28 CEST: Acknowledgement-mail to Willy Tarreau, indicating that a [PATCH]-formatted version would follow and offering a Suggested-by: tag on the eventual commit.
  • 2026-05-14 10:52 CEST: Willy replied to decline the Suggested-by: tag. His exact wording: "I don't need to appear in suggested-by, it's for those suggesting a solution, which I didn't do. It's your fix, no need to share the credit with others like me who only did triage." That guidance is now recorded as a kernel-etiquette note for future patches.
  • 2026-05-14 12:08 CEST: Final [PATCH] drm/qxl: validate dst_offset in apply_reloc and apply_surf_reloc against BO size sent to kraxel@redhat.com (Gerd Hoffmann), airlied@redhat.com (Dave Airlie), cc the same three lists. No Suggested-by:, no cc to Willy.
  • 2026-05-16: No reply from Gerd Hoffmann yet. The typical first-review window on drm-misc patches is one to two weeks. A gentle ping after roughly two weeks is normal; pinging earlier is considered impatient.

Coordination notes

What worked: the public-list-first route was correct for this class of bug. The QXL driver attack surface is guest-local and well-documented; there is no embargo benefit because the vulnerable code has been visible in the source tree since 2013 and any actor who cared has already had over a decade to look. Filing through security@kernel.org would have introduced friction without protecting anyone.

What I learned about kernel etiquette: the Suggested-by: tag is reserved for people who proposed the specific code change, not for triage or channel-redirection. Over-attribution is read as inaccurate by senior reviewers and is gently corrected. Other relevant tags to know: Signed-off-by: is the patch author (you), Reviewed-by: is someone who actually reviewed, Reported-by: is the original bug reporter, Co-developed-by: is a co-author. When in doubt, fewer tags is better.

What is still open: maintainer ACK from Gerd Hoffmann. The patch is sitting in the standard review queue. Acceptance would land the fix in drm-misc-next, then drm-next, then a Linus mainline pull, with eventual stable backports if the maintainer flags it. The realistic earliest mainline target is Linux 6.16; 6.17 is more likely.

References

  • Patch thread on lore.kernel.org: https://lore.kernel.org/all/1778753260.e30adb21c660@berkoc.com/
  • Marc.info mirror: https://marc.info/?l=linux-kernel&m=177875571412645
  • drivers/gpu/drm/qxl/qxl_ioctl.c in mainline at the time of disclosure
  • CWE-787: Out-of-bounds Write
  • QXL specification (Red Hat): the QXL device contract that the kernel driver implements
  • Linux kernel Documentation/process/submitting-patches.rst for the tag etiquette referenced above