xserver/Xext/namespace/hook-init-rootwindow.c
Enrico Weigelt, metux IT consult 97278df78a Xext: namespace: fix NULL-deref crash for protocol-created namespaces
A namespace created at runtime via the CreateNamespace protocol request
never got a virtual root window assigned: XnsCreate() left rootWindow at
its calloc'd NULL, since only hookInitRootWindow() (a one-shot
PostInitRootWindowCallback fired once at boot, before any client can
reach the extension) ever populated that field, for the fixed set of
namespaces known from the config file at that point.

Any client placed into such a namespace then crashed the whole server
with a NULL-pointer segfault in hookWindowProperty() on its very first
GetProperty request (Xext/namespace/hook-windowproperty.c:37,
'subj->ns->rootWindow->drawable.id') - which virtually every X11 client
issues during normal startup. Reproduced end-to-end via a real
FlyingTux-managed container app connecting through a freshly created
namespace.

Factor the per-namespace virtual-root-window creation out of
hookInitRootWindow() into XnsCreateVirtualRoot(), and call it from
XnsCreate() too, so a namespace created via the protocol gets the same
virtual root a config-time one gets at boot.

Signed-off-by: Enrico Weigelt, metux IT consult <enrico.weigelt@vnc.biz>
2026-07-03 20:05:08 +02:00

92 lines
2.8 KiB
C

#define HOOK_NAME "initroot"
#include <dix-config.h>
#include <inttypes.h>
#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Xmd.h>
#include "dix/window_priv.h"
#include "namespace.h"
#include "hooks.h"
static inline int setWinStrProp(WindowPtr pWin, Atom name, const char *text) {
return dixChangeWindowProperty(serverClient, pWin, name, XA_STRING,
8, PropModeReplace, strlen(text), text, TRUE);
}
/**
* @brief Create a namespace's virtual root window, parented to the real root.
*
* Used both at boot (for namespaces known from the config file, one pass
* over ns_list from hookInitRootWindow below) and at runtime (for a
* namespace created later via the CreateNamespace protocol request, which
* has no other opportunity to get a virtual root - PostInitRootWindowCallback
* only ever fires once, before any client can reach the extension).
*
* @param realRoot the screen's actual root window to parent the new window to
* @param name the namespace's name, used only for the window's WM_NAME
* @return the new window, or NULL on failure
*/
WindowPtr XnsCreateVirtualRoot(WindowPtr realRoot, const char *name)
{
int rc = 0;
WindowPtr pWin = dixCreateWindow(
FakeClientID(0), realRoot, 0, 0, 23, 23,
0, /* bw */
InputOutput,
0, /* vmask */
NULL, /* vlist */
0, /* depth */
serverClient,
wVisual(realRoot), /* visual */
&rc);
if (!pWin)
return NULL;
Mask mask = pWin->eventMask;
pWin->eventMask = 0; /* subterfuge in case AddResource fails */
if (!AddResource(pWin->drawable.id, X11_RESTYPE_WINDOW, (void *) pWin))
return NULL;
pWin->eventMask = mask;
// set window name
char buf[PATH_MAX] = { 0 };
snprintf(buf, sizeof(buf)-1, "XNS-ROOT:%s", name);
setWinStrProp(pWin, XA_WM_NAME, buf);
return pWin;
}
void hookInitRootWindow(CallbackListPtr *pcbl, void *data, void *screen)
{
ScreenPtr pScreen = (ScreenPtr)screen;
// only act on first screen
if (pScreen->myNum)
return;
/* create the virtual root windows */
WindowPtr realRoot = pScreen->root;
assert(realRoot);
struct Xnamespace *walk;
xorg_list_for_each_entry(walk, &ns_list, entry) {
if (strcmp(walk->name, NS_NAME_ROOT)==0) {
walk->rootWindow = realRoot;
XNS_LOG("<%s> actual root 0x%0llx\n", walk->name, (unsigned long long)walk->rootWindow->drawable.id);
continue;
}
walk->rootWindow = XnsCreateVirtualRoot(realRoot, walk->name);
if (!walk->rootWindow)
FatalError("hookInitRootWindow: cant create per-namespace root window for %s\n", walk->name);
XNS_LOG("<%s> virtual root 0x%0llx\n", walk->name, (unsigned long long)walk->rootWindow->drawable.id);
}
}