2021-05-19 07:39:33 +02:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "ui/clipboard.h"
|
2022-09-12 12:24:51 +02:00
|
|
|
#include "trace.h"
|
2021-05-19 07:39:33 +02:00
|
|
|
|
|
|
|
static NotifierList clipboard_notifiers =
|
|
|
|
NOTIFIER_LIST_INITIALIZER(clipboard_notifiers);
|
|
|
|
|
2021-08-05 15:57:02 +02:00
|
|
|
static QemuClipboardInfo *cbinfo[QEMU_CLIPBOARD_SELECTION__COUNT];
|
|
|
|
|
2021-05-19 07:39:33 +02:00
|
|
|
void qemu_clipboard_peer_register(QemuClipboardPeer *peer)
|
|
|
|
{
|
2021-07-19 17:42:15 +02:00
|
|
|
notifier_list_add(&clipboard_notifiers, &peer->notifier);
|
2021-05-19 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_peer_unregister(QemuClipboardPeer *peer)
|
|
|
|
{
|
2021-08-05 15:57:05 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < QEMU_CLIPBOARD_SELECTION__COUNT; i++) {
|
|
|
|
qemu_clipboard_peer_release(peer, i);
|
|
|
|
}
|
2021-07-19 17:42:15 +02:00
|
|
|
notifier_remove(&peer->notifier);
|
2021-05-19 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 15:57:03 +02:00
|
|
|
bool qemu_clipboard_peer_owns(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
QemuClipboardInfo *info = qemu_clipboard_info(selection);
|
|
|
|
|
|
|
|
return info && info->owner == peer;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:57:04 +02:00
|
|
|
void qemu_clipboard_peer_release(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
g_autoptr(QemuClipboardInfo) info = NULL;
|
|
|
|
|
|
|
|
if (qemu_clipboard_peer_owns(peer, selection)) {
|
|
|
|
/* set empty clipboard info */
|
|
|
|
info = qemu_clipboard_info_new(NULL, selection);
|
|
|
|
qemu_clipboard_update(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 13:19:13 +02:00
|
|
|
bool qemu_clipboard_check_serial(QemuClipboardInfo *info, bool client)
|
|
|
|
{
|
2022-09-12 12:24:51 +02:00
|
|
|
bool ok;
|
|
|
|
|
2021-07-21 13:19:13 +02:00
|
|
|
if (!info->has_serial ||
|
|
|
|
!cbinfo[info->selection] ||
|
|
|
|
!cbinfo[info->selection]->has_serial) {
|
2022-09-12 12:24:51 +02:00
|
|
|
trace_clipboard_check_serial(-1, -1, true);
|
2021-07-21 13:19:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client) {
|
2022-09-12 12:24:52 +02:00
|
|
|
ok = info->serial >= cbinfo[info->selection]->serial;
|
2021-07-21 13:19:13 +02:00
|
|
|
} else {
|
2022-09-12 12:24:52 +02:00
|
|
|
ok = info->serial > cbinfo[info->selection]->serial;
|
2021-07-21 13:19:13 +02:00
|
|
|
}
|
2022-09-12 12:24:51 +02:00
|
|
|
|
|
|
|
trace_clipboard_check_serial(cbinfo[info->selection]->serial, info->serial, ok);
|
|
|
|
return ok;
|
2021-07-21 13:19:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 07:39:33 +02:00
|
|
|
void qemu_clipboard_update(QemuClipboardInfo *info)
|
|
|
|
{
|
2024-01-24 11:57:49 +01:00
|
|
|
uint32_t type;
|
2021-07-19 17:42:15 +02:00
|
|
|
QemuClipboardNotify notify = {
|
|
|
|
.type = QEMU_CLIPBOARD_UPDATE_INFO,
|
|
|
|
.info = info,
|
|
|
|
};
|
2021-08-05 15:57:02 +02:00
|
|
|
assert(info->selection < QEMU_CLIPBOARD_SELECTION__COUNT);
|
|
|
|
|
2024-01-24 11:57:49 +01:00
|
|
|
for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) {
|
|
|
|
/*
|
|
|
|
* If data is missing, the clipboard owner's 'request' callback needs to
|
|
|
|
* be set. Otherwise, there is no way to get the clipboard data and
|
|
|
|
* qemu_clipboard_request() cannot be called.
|
|
|
|
*/
|
|
|
|
if (info->types[type].available && !info->types[type].data) {
|
|
|
|
assert(info->owner && info->owner->request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:42:15 +02:00
|
|
|
notifier_list_notify(&clipboard_notifiers, ¬ify);
|
2021-08-05 15:57:02 +02:00
|
|
|
|
2022-02-14 12:59:17 +01:00
|
|
|
if (cbinfo[info->selection] != info) {
|
|
|
|
qemu_clipboard_info_unref(cbinfo[info->selection]);
|
|
|
|
cbinfo[info->selection] = qemu_clipboard_info_ref(info);
|
|
|
|
}
|
2021-08-05 15:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info(QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
assert(selection < QEMU_CLIPBOARD_SELECTION__COUNT);
|
|
|
|
|
|
|
|
return cbinfo[selection];
|
2021-05-19 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info_new(QemuClipboardPeer *owner,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
QemuClipboardInfo *info = g_new0(QemuClipboardInfo, 1);
|
|
|
|
|
|
|
|
info->owner = owner;
|
|
|
|
info->selection = selection;
|
|
|
|
info->refcount = 1;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info_ref(QemuClipboardInfo *info)
|
|
|
|
{
|
|
|
|
info->refcount++;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_info_unref(QemuClipboardInfo *info)
|
|
|
|
{
|
|
|
|
uint32_t type;
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->refcount--;
|
|
|
|
if (info->refcount > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) {
|
|
|
|
g_free(info->types[type].data);
|
|
|
|
}
|
|
|
|
g_free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_request(QemuClipboardInfo *info,
|
|
|
|
QemuClipboardType type)
|
|
|
|
{
|
|
|
|
if (info->types[type].data ||
|
|
|
|
info->types[type].requested ||
|
|
|
|
!info->types[type].available ||
|
|
|
|
!info->owner)
|
|
|
|
return;
|
|
|
|
|
2024-01-24 11:57:49 +01:00
|
|
|
assert(info->owner->request);
|
|
|
|
|
2021-05-19 07:39:33 +02:00
|
|
|
info->types[type].requested = true;
|
|
|
|
info->owner->request(info, type);
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:49:56 +02:00
|
|
|
void qemu_clipboard_reset_serial(void)
|
|
|
|
{
|
|
|
|
QemuClipboardNotify notify = { .type = QEMU_CLIPBOARD_RESET_SERIAL };
|
2022-09-12 12:24:54 +02:00
|
|
|
int i;
|
2021-07-19 17:49:56 +02:00
|
|
|
|
2022-09-12 12:24:54 +02:00
|
|
|
for (i = 0; i < QEMU_CLIPBOARD_SELECTION__COUNT; i++) {
|
|
|
|
QemuClipboardInfo *info = qemu_clipboard_info(i);
|
|
|
|
if (info) {
|
|
|
|
info->serial = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 17:49:56 +02:00
|
|
|
notifier_list_notify(&clipboard_notifiers, ¬ify);
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:39:33 +02:00
|
|
|
void qemu_clipboard_set_data(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardInfo *info,
|
|
|
|
QemuClipboardType type,
|
|
|
|
uint32_t size,
|
2021-06-16 16:19:54 +02:00
|
|
|
const void *data,
|
2021-05-19 07:39:33 +02:00
|
|
|
bool update)
|
|
|
|
{
|
|
|
|
if (!info ||
|
|
|
|
info->owner != peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(info->types[type].data);
|
ui/clipboard: mark type as not available when there is no data
With VNC, a client can send a non-extended VNC_MSG_CLIENT_CUT_TEXT
message with len=0. In qemu_clipboard_set_data(), the clipboard info
will be updated setting data to NULL (because g_memdup(data, size)
returns NULL when size is 0). If the client does not set the
VNC_ENCODING_CLIPBOARD_EXT feature when setting up the encodings, then
the 'request' callback for the clipboard peer is not initialized.
Later, because data is NULL, qemu_clipboard_request() can be reached
via vdagent_chr_write() and vdagent_clipboard_recv_request() and
there, the clipboard owner's 'request' callback will be attempted to
be called, but that is a NULL pointer.
In particular, this can happen when using the KRDC (22.12.3) VNC
client.
Another scenario leading to the same issue is with two clients (say
noVNC and KRDC):
The noVNC client sets the extension VNC_FEATURE_CLIPBOARD_EXT and
initializes its cbpeer.
The KRDC client does not, but triggers a vnc_client_cut_text() (note
it's not the _ext variant)). There, a new clipboard info with it as
the 'owner' is created and via qemu_clipboard_set_data() is called,
which in turn calls qemu_clipboard_update() with that info.
In qemu_clipboard_update(), the notifier for the noVNC client will be
called, i.e. vnc_clipboard_notify() and also set vs->cbinfo for the
noVNC client. The 'owner' in that clipboard info is the clipboard peer
for the KRDC client, which did not initialize the 'request' function.
That sounds correct to me, it is the owner of that clipboard info.
Then when noVNC sends a VNC_MSG_CLIENT_CUT_TEXT message (it did set
the VNC_FEATURE_CLIPBOARD_EXT feature correctly, so a check for it
passes), that clipboard info is passed to qemu_clipboard_request() and
the original segfault still happens.
Fix the issue by handling updates with size 0 differently. In
particular, mark in the clipboard info that the type is not available.
While at it, switch to g_memdup2(), because g_memdup() is deprecated.
Cc: qemu-stable@nongnu.org
Fixes: CVE-2023-6683
Reported-by: Markus Frank <m.frank@proxmox.com>
Suggested-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Tested-by: Markus Frank <m.frank@proxmox.com>
Message-ID: <20240124105749.204610-1-f.ebner@proxmox.com>
2024-01-24 11:57:48 +01:00
|
|
|
if (size) {
|
|
|
|
info->types[type].data = g_memdup2(data, size);
|
|
|
|
info->types[type].size = size;
|
|
|
|
info->types[type].available = true;
|
|
|
|
} else {
|
|
|
|
info->types[type].data = NULL;
|
|
|
|
info->types[type].size = 0;
|
|
|
|
info->types[type].available = false;
|
|
|
|
}
|
2021-05-19 07:39:33 +02:00
|
|
|
|
|
|
|
if (update) {
|
|
|
|
qemu_clipboard_update(info);
|
|
|
|
}
|
|
|
|
}
|