From 2cf9249d2bf3d0227f4ec8d4158816f08bbaa9ad Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Fri, 17 Apr 2020 14:15:54 +0200 Subject: x11: Fix race conditions in XSetSelection Some clients like X11 LibreOffice clear the selection prior to copying some content there. This selection clear is correctly seen by our clipboard manager as a hint to take ownership and preserve the last copied content, all while LO is issuing other XSetSelection with the new clipboard content. Our use of META_CURRENT_TIME turns this into a race condition, as there's both LO and our clipboard manager trying to do XSetSelection(), from our side it's all up to the order in which the requests arrive to the X server. In order to break the tie, keep the selection timestamp from the XFixes event (i.e. the timestamp set by the XSetSelection external call that is unsetting the clipboard) and ensure it is used for our own XSetSelection call replacing the selection. In this same situation, it will make the X server deem the request too old, and let LO win. If the compositor-side XSetSelection event does not happen in result to a XFixes selection notify event, the current event time will be used, and META_CURRENT_TIME as a last resort. Closes: https://gitlab.gnome.org/GNOME/mutter/-/issues/1113 https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1205 --- src/x11/meta-x11-display-private.h | 1 + src/x11/meta-x11-selection.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/x11/meta-x11-display-private.h b/src/x11/meta-x11-display-private.h index 36d4bad38..44b42159e 100644 --- a/src/x11/meta-x11-display-private.h +++ b/src/x11/meta-x11-display-private.h @@ -127,6 +127,7 @@ struct _MetaX11Display struct { Window xwindow; + uint32_t ownership_timestamp; MetaSelectionSource *owners[META_N_SELECTION_TYPES]; GCancellable *cancellables[META_N_SELECTION_TYPES]; diff --git a/src/x11/meta-x11-selection.c b/src/x11/meta-x11-selection.c index b2d524f35..015408a3c 100644 --- a/src/x11/meta-x11-selection.c +++ b/src/x11/meta-x11-selection.c @@ -322,6 +322,7 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display, if (!atom_to_selection_type (xdisplay, event->selection, &selection_type)) return FALSE; + x11_display->selection.ownership_timestamp = event->selection_timestamp; selection = meta_display_get_selection (meta_get_display ()); if (x11_display->selection.cancellables[selection_type]) @@ -360,6 +361,8 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display, data); } + x11_display->selection.ownership_timestamp = 0; + return TRUE; } @@ -381,6 +384,7 @@ notify_selection_owner (MetaX11Display *x11_display, MetaSelectionSource *new_owner) { Display *xdisplay = x11_display->xdisplay; + uint32_t timestamp; if (new_owner && !META_IS_SELECTION_SOURCE_X11 (new_owner)) { @@ -390,13 +394,17 @@ notify_selection_owner (MetaX11Display *x11_display, g_clear_object (&x11_display->selection.cancellables[selection_type]); } + timestamp = x11_display->selection.ownership_timestamp ? + x11_display->selection.ownership_timestamp : + meta_display_get_current_time (x11_display->display); + /* If the owner is non-X11, claim the selection on our selection * window, so X11 apps can interface with it. */ XSetSelectionOwner (xdisplay, selection_to_atom (selection_type, xdisplay), x11_display->selection.xwindow, - META_CURRENT_TIME); + timestamp); } } -- cgit v1.2.1