selection: allows using a wrapper source to filter data

1. Added support for intercepting clipboard selection operations.
2. Wraps data sources to control data provision based on client PID.
3. Added wlr_data_receiver to data source's send to identify the
recipient.
4. Includes new example "clipboard-control.c" to illustrate clipboard
permission management.
5. Added PID and client members to data source and receiver for access
control.
This commit is contained in:
JiDe Zhang 2025-09-19 15:58:58 +08:00
parent 90f9f59041
commit c3d3e6f8bb
18 changed files with 2723 additions and 108 deletions

View file

@ -7,6 +7,7 @@
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
#include "wlr/types/wlr_data_receiver.h"
#include "xwayland/selection.h"
#include "xwayland/xwm.h"
@ -42,6 +43,39 @@ xwm_selection_transfer_create_incoming(struct wlr_xwm_selection *selection) {
return transfer;
}
void xwm_selection_transfer_destroy_incoming(
struct wlr_xwm_selection_transfer *transfer) {
if (!transfer) {
return;
}
xwm_selection_transfer_destroy_property_reply(transfer);
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
if (transfer->incoming_window) {
struct wlr_xwm *xwm = transfer->selection->xwm;
xcb_destroy_window(xwm->xcb_conn, transfer->incoming_window);
xwm_schedule_flush(xwm);
}
if (transfer->wl_client_receiver) {
wl_list_remove(&transfer->receiver_destroy.link);
wlr_data_receiver_destroy(transfer->wl_client_receiver);
}
wl_list_remove(&transfer->link);
free(transfer);
}
static void handle_incoming_receiver_destroy(struct wl_listener *listener, void *data) {
struct wlr_xwm_selection_transfer *transfer =
wl_container_of(listener, transfer, receiver_destroy);
transfer->wl_client_receiver = NULL;
wl_list_remove(&transfer->receiver_destroy.link);
}
struct wlr_xwm_selection_transfer *
xwm_selection_find_incoming_transfer_by_window(
struct wlr_xwm_selection *selection, xcb_window_t window) {
@ -110,7 +144,7 @@ static int write_selection_property_to_wl_client(int fd, uint32_t mask,
ssize_t len = write(fd, property + transfer->property_start, remainder);
if (len == -1) {
wlr_log_errno(WLR_ERROR, "write error to target fd %d", fd);
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
return 0;
}
@ -126,7 +160,7 @@ static int write_selection_property_to_wl_client(int fd, uint32_t mask,
xwm_notify_ready_for_next_incr_chunk(transfer);
} else {
wlr_log(WLR_DEBUG, "transfer complete");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
return 0;
@ -171,7 +205,7 @@ void xwm_get_incr_chunk(struct wlr_xwm_selection_transfer *transfer) {
xwm_write_selection_property_to_wl_client(transfer);
} else {
wlr_log(WLR_DEBUG, "incremental transfer complete");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
}
@ -195,7 +229,7 @@ static void xwm_selection_transfer_get_data(
static void source_send(struct wlr_xwm_selection *selection,
struct wl_array *mime_types, struct wl_array *mime_types_atoms,
const char *requested_mime_type, int fd) {
const char *requested_mime_type, struct wlr_data_receiver *receiver) {
struct wlr_xwm *xwm = selection->xwm;
xcb_atom_t *atoms = mime_types_atoms->data;
@ -215,7 +249,7 @@ static void source_send(struct wlr_xwm_selection *selection,
if (!found) {
wlr_log(WLR_DEBUG, "Cannot send X11 selection to Wayland: "
"unsupported MIME type");
close(fd);
wlr_data_receiver_cancelled(receiver);
return;
}
@ -223,10 +257,14 @@ static void source_send(struct wlr_xwm_selection *selection,
xwm_selection_transfer_create_incoming(selection);
if (!transfer) {
wlr_log(WLR_ERROR, "Cannot create transfer");
close(fd);
wlr_data_receiver_cancelled(receiver);
return;
}
// Listen for receiver destruction to clean up the reference
transfer->receiver_destroy.notify = handle_incoming_receiver_destroy;
wl_signal_add(&receiver->events.destroy, &transfer->receiver_destroy);
xcb_convert_selection(xwm->xcb_conn,
transfer->incoming_window,
selection->atom,
@ -236,8 +274,9 @@ static void source_send(struct wlr_xwm_selection *selection,
xwm_schedule_flush(xwm);
fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
transfer->wl_client_fd = fd;
fcntl(receiver->fd, F_SETFL, O_WRONLY | O_NONBLOCK);
transfer->wl_client_fd = receiver->fd;
transfer->wl_client_receiver = receiver;
}
struct x11_data_source {
@ -250,24 +289,26 @@ static const struct wlr_data_source_impl data_source_impl;
bool data_source_is_xwayland(
struct wlr_data_source *wlr_source) {
return wlr_source->impl == &data_source_impl;
struct wlr_data_source *original = wlr_data_source_get_original(wlr_source);
return original->impl == &data_source_impl;
}
static struct x11_data_source *data_source_from_wlr_data_source(
struct wlr_data_source *wlr_source) {
assert(data_source_is_xwayland(wlr_source));
struct x11_data_source *source = wl_container_of(wlr_source, source, base);
struct wlr_data_source *original = wlr_data_source_get_original(wlr_source);
struct x11_data_source *source = wl_container_of(original, source, base);
return source;
}
static void data_source_send(struct wlr_data_source *wlr_source,
const char *mime_type, int32_t fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct x11_data_source *source =
data_source_from_wlr_data_source(wlr_source);
struct wlr_xwm_selection *selection = source->selection;
source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms,
mime_type, fd);
mime_type, receiver);
}
static void data_source_destroy(struct wlr_data_source *wlr_source) {
@ -293,17 +334,18 @@ static const struct wlr_primary_selection_source_impl
bool primary_selection_source_is_xwayland(
struct wlr_primary_selection_source *wlr_source) {
return wlr_source->impl == &primary_selection_source_impl;
struct wlr_primary_selection_source *original = wlr_primary_selection_source_get_original(wlr_source);
return original->impl == &primary_selection_source_impl;
}
static void primary_selection_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct x11_primary_selection_source *source = wl_container_of(wlr_source, source, base);
struct wlr_xwm_selection *selection = source->selection;
source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms,
mime_type, fd);
mime_type, receiver);
}
static void primary_selection_source_destroy(
@ -407,6 +449,10 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) {
}
wlr_data_source_init(&source->base, &data_source_impl);
// Set client and PID for XWayland data sources
source->base.client = xwm->xwayland->server->client;
source->base.pid = get_x11_window_pid(xwm->xcb_conn, selection->owner);
source->selection = selection;
wl_array_init(&source->mime_types_atoms);
@ -426,6 +472,10 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) {
wlr_primary_selection_source_init(&source->base,
&primary_selection_source_impl);
// Set client and PID for XWayland primary selection sources
source->base.client = xwm->xwayland->server->client;
source->base.pid = get_x11_window_pid(xwm->xcb_conn, selection->owner);
source->selection = selection;
wl_array_init(&source->mime_types_atoms);
@ -460,7 +510,7 @@ void xwm_handle_selection_notify(struct wlr_xwm *xwm,
if (event->property == XCB_ATOM_NONE) {
if (transfer) {
wlr_log(WLR_ERROR, "convert selection failed");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
} else if (event->target == xwm->atoms[TARGETS]) {
// No xwayland surface focused, deny access to clipboard

View file

@ -5,6 +5,7 @@
#include <unistd.h>
#include <wayland-util.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
@ -56,11 +57,22 @@ static int xwm_selection_flush_source_data(
static void xwm_selection_transfer_start_outgoing(
struct wlr_xwm_selection_transfer *transfer);
// Structure to contain wlr_data_receiver and transfer pointer for outgoing transfers
struct xwm_outgoing_receiver {
struct wlr_data_receiver receiver;
struct wlr_xwm_selection_transfer *transfer;
};
void xwm_selection_transfer_destroy_outgoing(
struct wlr_xwm_selection_transfer *transfer) {
wl_list_remove(&transfer->link);
wlr_log(WLR_DEBUG, "Destroying transfer %p", transfer);
// Destroy the receiver if it exists
if (transfer->wl_client_receiver) {
wlr_data_receiver_destroy(transfer->wl_client_receiver);
}
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
wl_array_release(&transfer->source_data);
@ -180,32 +192,54 @@ void xwm_send_incr_chunk(struct wlr_xwm_selection_transfer *transfer) {
}
}
static void xwm_selection_source_send(struct wlr_xwm_selection *selection,
const char *mime_type, int32_t fd) {
static void handle_data_receiver_cancelled(struct wlr_data_receiver *receiver) {
struct xwm_outgoing_receiver *outgoing_receiver =
wl_container_of(receiver, outgoing_receiver, receiver);
struct wlr_xwm_selection_transfer *transfer = outgoing_receiver->transfer;
// Handle failure by destroying the transfer
xwm_selection_transfer_destroy_outgoing(transfer);
}
static void handle_data_receiver_destroy(struct wlr_data_receiver *receiver) {
struct xwm_outgoing_receiver *outgoing_receiver =
wl_container_of(receiver, outgoing_receiver, receiver);
outgoing_receiver->transfer->wl_client_receiver = NULL;
free(outgoing_receiver);
}
static const struct wlr_data_receiver_impl outgoing_receiver_impl = {
.cancelled = handle_data_receiver_cancelled,
.destroy = handle_data_receiver_destroy,
};
static bool xwm_selection_source_send(struct wlr_xwm_selection *selection,
const char *mime_type, struct wlr_data_receiver *receiver) {
if (selection == &selection->xwm->clipboard_selection) {
struct wlr_data_source *source =
selection->xwm->seat->selection_source;
if (source != NULL) {
wlr_data_source_send(source, mime_type, fd);
return;
wlr_data_source_send(source, mime_type, receiver);
return true;
}
} else if (selection == &selection->xwm->primary_selection) {
struct wlr_primary_selection_source *source =
selection->xwm->seat->primary_selection_source;
if (source != NULL) {
wlr_primary_selection_source_send(source, mime_type, fd);
return;
wlr_primary_selection_source_send(source, mime_type, receiver);
return true;
}
} else if (selection == &selection->xwm->dnd_selection) {
struct wlr_data_source *source =
selection->xwm->seat->drag_source;
if (source != NULL) {
wlr_data_source_send(source, mime_type, fd);
return;
wlr_data_source_send(source, mime_type, receiver);
return true;
}
}
wlr_log(WLR_DEBUG, "not sending selection: no selection source available");
return false;
}
static void xwm_selection_transfer_start_outgoing(
@ -276,16 +310,17 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
return false;
}
xwm_selection_transfer_init(transfer, selection);
transfer->request = *req;
wl_array_init(&transfer->source_data);
int p[2];
if (pipe(p) == -1) {
wlr_log_errno(WLR_ERROR, "pipe() failed");
free(transfer);
return false;
}
xwm_selection_transfer_init(transfer, selection);
transfer->request = *req;
wl_array_init(&transfer->source_data);
fcntl(p[0], F_SETFD, FD_CLOEXEC);
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFD, FD_CLOEXEC);
@ -293,10 +328,31 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
transfer->wl_client_fd = p[0];
wlr_log(WLR_DEBUG, "Sending Wayland selection %u to Xwayland window with "
"MIME type %s, target %u, transfer %p", req->target, mime_type,
req->target, transfer);
xwm_selection_source_send(selection, mime_type, p[1]);
// Create and initialize the outgoing receiver
struct xwm_outgoing_receiver *outgoing_receiver = calloc(1, sizeof(*outgoing_receiver));
if (!outgoing_receiver) {
wlr_log(WLR_ERROR, "Failed to allocate outgoing receiver");
close(p[0]);
close(p[1]);
free(transfer);
return false;
}
outgoing_receiver->transfer = transfer;
struct wlr_data_receiver *receiver = &outgoing_receiver->receiver;
wlr_data_receiver_init(receiver, &outgoing_receiver_impl);
receiver->fd = p[1];
receiver->client = selection->xwm->xwayland->server->client;
// Get X11 window PID
if (transfer->request.requestor != XCB_WINDOW_NONE) {
receiver->pid = get_x11_window_pid(selection->xwm->xcb_conn, transfer->request.requestor);
} else {
receiver->pid = 0;
}
transfer->wl_client_receiver = receiver;
// It seems that if we ever try to reply to a selection request after
// another has been sent by the same requestor, the requestor never reads
@ -317,6 +373,14 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
xwm_selection_transfer_start_outgoing(transfer);
wlr_log(WLR_DEBUG, "Sending Wayland selection %u to Xwayland window with "
"MIME type %s, target %u, transfer %p", req->target, mime_type,
req->target, transfer);
// Maybe the transfer object is destroyed following the receiver object afeter
// xwm_selection_source_send, so don't access any member of transfer after this call.
transfer = NULL;
xwm_selection_source_send(selection, mime_type, receiver);
return true;
}

View file

@ -3,12 +3,58 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-server-protocol.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
#include <xcb/res.h>
#include "xwayland/selection.h"
#include "xwayland/xwm.h"
/* Get X11 window PID using XRes extension */
pid_t get_x11_window_pid(xcb_connection_t *conn, xcb_window_t window) {
if (!conn || window == XCB_WINDOW_NONE) {
return 0;
}
// Check if XRes extension is available
xcb_query_extension_cookie_t ext_cookie = xcb_query_extension(conn,
strlen("X-Resource"), "X-Resource");
xcb_query_extension_reply_t *ext_reply = xcb_query_extension_reply(conn, ext_cookie, NULL);
if (!ext_reply || !ext_reply->present) {
free(ext_reply);
return 0;
}
free(ext_reply);
// Create client ID spec for the window
xcb_res_client_id_spec_t spec;
spec.client = window;
spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
// Query client IDs for the window
xcb_res_query_client_ids_cookie_t cookie = xcb_res_query_client_ids(conn, 1, &spec);
xcb_res_query_client_ids_reply_t *reply = xcb_res_query_client_ids_reply(conn, cookie, NULL);
if (!reply) {
return 0;
}
pid_t pid = 0;
xcb_res_client_id_value_iterator_t iter = xcb_res_query_client_ids_ids_iterator(reply);
for (; iter.rem; xcb_res_client_id_value_next(&iter)) {
if (iter.data->spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
uint32_t *value = xcb_res_client_id_value_value(iter.data);
if (value && iter.data->length >= sizeof(uint32_t)) {
pid = *value;
break;
}
}
}
free(reply);
return pid;
}
void xwm_selection_transfer_remove_event_source(
struct wlr_xwm_selection_transfer *transfer) {
if (transfer->event_source != NULL) {
@ -39,26 +85,6 @@ void xwm_selection_transfer_init(struct wlr_xwm_selection_transfer *transfer,
};
}
void xwm_selection_transfer_destroy(
struct wlr_xwm_selection_transfer *transfer) {
if (!transfer) {
return;
}
xwm_selection_transfer_destroy_property_reply(transfer);
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
if (transfer->incoming_window) {
struct wlr_xwm *xwm = transfer->selection->xwm;
xcb_destroy_window(xwm->xcb_conn, transfer->incoming_window);
xwm_schedule_flush(xwm);
}
wl_list_remove(&transfer->link);
free(transfer);
}
xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type) {
if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) {
return xwm->atoms[UTF8_STRING];
@ -256,7 +282,7 @@ void xwm_selection_finish(struct wlr_xwm_selection *selection) {
struct wlr_xwm_selection_transfer *incoming;
wl_list_for_each_safe(incoming, tmp, &selection->incoming, link) {
xwm_selection_transfer_destroy(incoming);
xwm_selection_transfer_destroy_incoming(incoming);
}
xcb_destroy_window(selection->xwm->xcb_conn, selection->window);