[PATCH v2] ui/dbus: validate clipboard selection in Release handler

marcandre.lureau@redhat.com posted 1 patch 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261019131143.2555126-2-marcandre.lureau@redhat.com
Maintainers: "Marc-André Lureau" <marcandre.lureau@redhat.com>
ui/dbus-clipboard.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
[PATCH v2] ui/dbus: validate clipboard selection in Release handler
Posted by marcandre.lureau@redhat.com 1 month ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

The clipboard "Release" D-Bus method handler passes the caller provided
selection to qemu_clipboard_peer_release() without any bounds check,
unlike the "Grab" and "Request" handlers.

An out-of-range selection reaches qemu_clipboard_info(), which asserts
selection < QEMU_CLIPBOARD_SELECTION__COUNT and aborts QEMU.

Reject invalid selections with an error, following the same pattern as
the other clipboard handlers.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4717
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 ui/dbus-clipboard.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/ui/dbus-clipboard.c b/ui/dbus-clipboard.c
index 90318384ee48..44764fff8081 100644
--- a/ui/dbus-clipboard.c
+++ b/ui/dbus-clipboard.c
@@ -346,11 +346,22 @@ dbus_clipboard_release(
     GDBusMethodInvocation *invocation,
     gint arg_selection)
 {
+    QemuClipboardSelection s = arg_selection;
+
     if (!dbus_clipboard_check_caller(dpy, invocation)) {
         return DBUS_METHOD_INVOCATION_HANDLED;
     }
 
-    qemu_clipboard_peer_release(&dpy->clipboard_peer, arg_selection);
+    if (s >= QEMU_CLIPBOARD_SELECTION__COUNT) {
+        g_dbus_method_invocation_return_error(
+            invocation,
+            DBUS_DISPLAY_ERROR,
+            DBUS_DISPLAY_ERROR_FAILED,
+            "Invalid clipboard selection: %d", arg_selection);
+        return DBUS_METHOD_INVOCATION_HANDLED;
+    }
+
+    qemu_clipboard_peer_release(&dpy->clipboard_peer, s);
 
     qemu_dbus_display1_clipboard_complete_release(dpy->clipboard, invocation);
     return DBUS_METHOD_INVOCATION_HANDLED;
-- 
2.56.0.116.g6de20f6092dc


Re: [PATCH v2] ui/dbus: validate clipboard selection in Release handler
Posted by Karl Mehltretter 1 week, 4 days ago
On Mon, 19 Oct 2026 at 17:11, marcandre.lureau@redhat.com wrote:
> The clipboard "Release" D-Bus method handler passes the caller provided
> selection to qemu_clipboard_peer_release() without any bounds check,
> unlike the "Grab" and "Request" handlers.
>
> An out-of-range selection reaches qemu_clipboard_info(), which asserts
> selection < QEMU_CLIPBOARD_SELECTION__COUNT and aborts QEMU.
>
> Reject invalid selections with an error, following the same pattern as
> the other clipboard handlers.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4717
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

With this patch, Release(3) from the reproducer in the issue returns an
error and QEMU keeps running. Tested on an x86_64 host with
qemu-system-x86_64 -machine q35,accel=tcg -display dbus,gl=off.

Tested-by: Karl Mehltretter <kmehltretter@gmail.com>

Karl