Blame SOURCES/0006-Xext-free-the-XvRTVideoNotify-when-turning-off-from-.patch

a4dcf1
From a42635ee3c01f71a49052d83a372933504c9db04 Mon Sep 17 00:00:00 2001
a4dcf1
From: Peter Hutterer <peter.hutterer@who-t.net>
a4dcf1
Date: Wed, 30 Nov 2022 11:20:40 +1000
a4dcf1
Subject: [PATCH xserver 6/7] Xext: free the XvRTVideoNotify when turning off
a4dcf1
 from the same client
a4dcf1
a4dcf1
This fixes a use-after-free bug:
a4dcf1
a4dcf1
When a client first calls XvdiSelectVideoNotify() on a drawable with a
a4dcf1
TRUE onoff argument, a struct XvVideoNotifyRec is allocated. This struct
a4dcf1
is added twice to the resources:
a4dcf1
  - as the drawable's XvRTVideoNotifyList. This happens only once per
a4dcf1
    drawable, subsequent calls append to this list.
a4dcf1
  - as the client's XvRTVideoNotify. This happens for every client.
a4dcf1
a4dcf1
The struct keeps the ClientPtr around once it has been added for a
a4dcf1
client. The idea, presumably, is that if the client disconnects we can remove
a4dcf1
all structs from the drawable's list that match the client (by resetting
a4dcf1
the ClientPtr to NULL), but if the drawable is destroyed we can remove
a4dcf1
and free the whole list.
a4dcf1
a4dcf1
However, if the same client then calls XvdiSelectVideoNotify() on the
a4dcf1
same drawable with a FALSE onoff argument, only the ClientPtr on the
a4dcf1
existing struct was set to NULL. The struct itself remained in the
a4dcf1
client's resources.
a4dcf1
a4dcf1
If the drawable is now destroyed, the resource system invokes
a4dcf1
XvdiDestroyVideoNotifyList which frees the whole list for this drawable
a4dcf1
- including our struct. This function however does not free the resource
a4dcf1
for the client since our ClientPtr is NULL.
a4dcf1
a4dcf1
Later, when the client is destroyed and the resource system invokes
a4dcf1
XvdiDestroyVideoNotify, we unconditionally set the ClientPtr to NULL. On
a4dcf1
a struct that has been freed previously. This is generally frowned upon.
a4dcf1
a4dcf1
Fix this by calling FreeResource() on the second call instead of merely
a4dcf1
setting the ClientPtr to NULL. This removes the struct from the client
a4dcf1
resources (but not from the list), ensuring that it won't be accessed
a4dcf1
again when the client quits.
a4dcf1
a4dcf1
Note that the assignment tpn->client = NULL; is superfluous since the
a4dcf1
XvdiDestroyVideoNotify function will do this anyway. But it's left for
a4dcf1
clarity and to match a similar invocation in XvdiSelectPortNotify.
a4dcf1
a4dcf1
CVE-2022-46342, ZDI-CAN 19400
a4dcf1
a4dcf1
This vulnerability was discovered by:
a4dcf1
Jan-Niklas Sohn working with Trend Micro Zero Day Initiative
a4dcf1
a4dcf1
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
a4dcf1
Acked-by: Olivier Fourdan <ofourdan@redhat.com>
a4dcf1
---
a4dcf1
 Xext/xvmain.c | 4 +++-
a4dcf1
 1 file changed, 3 insertions(+), 1 deletion(-)
a4dcf1
a4dcf1
diff --git a/Xext/xvmain.c b/Xext/xvmain.c
a4dcf1
index f627471938..2a08f8744a 100644
a4dcf1
--- a/Xext/xvmain.c
a4dcf1
+++ b/Xext/xvmain.c
a4dcf1
@@ -811,8 +811,10 @@ XvdiSelectVideoNotify(ClientPtr client, DrawablePtr pDraw, BOOL onoff)
a4dcf1
         tpn = pn;
a4dcf1
         while (tpn) {
a4dcf1
             if (tpn->client == client) {
a4dcf1
-                if (!onoff)
a4dcf1
+                if (!onoff) {
a4dcf1
                     tpn->client = NULL;
a4dcf1
+                    FreeResource(tpn->id, XvRTVideoNotify);
a4dcf1
+                }
a4dcf1
                 return Success;
a4dcf1
             }
a4dcf1
             if (!tpn->client)
a4dcf1
--
a4dcf1
2.38.1