summaryrefslogtreecommitdiffstats
path: root/dwm.c
diff options
context:
space:
mode:
authorChris Down <chris@chrisdown.name>2026-01-07 22:02:00 +0800
committerHiltjo Posthuma <hiltjo@codemadness.org>2026-01-10 11:27:23 +0100
commit244fa852fe2775cf52a3901966cd6d8700df8227 (patch)
tree49fb524eb09ffd76d16cc0c709daa2888c2f9d9c /dwm.c
parentdrw.c: drw_scm_free: call free inside (diff)
downloaddwm-244fa852fe2775cf52a3901966cd6d8700df8227.tar.gz
dwm-244fa852fe2775cf52a3901966cd6d8700df8227.zip
dwm: Fix heap buffer overflow in getatomprop
When getatomprop() is called, it invokes XGetWindowProperty() to retrieve an Atom. If the property exists but has zero elements (length 0), Xlib returns Success and sets p to a valid, non-NULL memory address containing a single null byte. However, dl (that is, the number of items) is 0. dwm blindly casts p to Atom* and dereferences it. While Xlib guarantees that p is safe to read as a string (that is, it is null-terminated), it does _not_ guarantee it is safe to read as an Atom (an unsigned long). The Atom type is a typedef for unsigned long. Reading an Atom (which thus will either likely be 4 or 8 bytes) from a 1-byte allocated buffer results in a heap buffer overflow. Since property content is user controlled, this allows any client to trigger an out of bounds read simply by setting a property with format 32 and length 0. An example client which reliably crashes dwm under ASAN: #include <X11/Xlib.h> #include <X11/Xatom.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { Display *d; Window root, w; Atom net_wm_state; d = XOpenDisplay(NULL); if (!d) return 1; root = DefaultRootWindow(d); w = XCreateSimpleWindow(d, root, 10, 10, 200, 200, 1, 0, 0); net_wm_state = XInternAtom(d, "_NET_WM_STATE", False); if (net_wm_state == None) return 1; XChangeProperty(d, w, net_wm_state, XA_ATOM, 32, PropModeReplace, NULL, 0); XMapWindow(d, w); XSync(d, False); sleep(1); XCloseDisplay(d); return 0; } In order to avoid this, check that the number of items returned is greater than zero before dereferencing the pointer.
Diffstat (limited to '')
-rw-r--r--dwm.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/dwm.c b/dwm.c
index 4f345ee..8f4fa75 100644
--- a/dwm.c
+++ b/dwm.c
@@ -870,7 +870,8 @@ getatomprop(Client *c, Atom prop)
870 870
871 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, 871 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
872 &da, &di, &dl, &dl, &p) == Success && p) { 872 &da, &di, &dl, &dl, &p) == Success && p) {
873 atom = *(Atom *)p; 873 if (dl > 0)
874 atom = *(Atom *)p;
874 XFree(p); 875 XFree(p);
875 } 876 }
876 return atom; 877 return atom;