From 2822958bbfa6ab7b0fc003cbbb31ec121b22b1c6 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 26 Aug 2026 17:04:03 +1000 Subject: [PATCH] xkb: allocate names->keys to MAP_LENGTH in XkbAllocNames Commit a3171732d ("xkb: Always use MAP_LENGTH keymap size") converted most XKB allocation functions to use MAP_LENGTH (256) instead of max_key_code + 1. It also removed the per-array reallocarray() calls in XkbChangeKeycodeRange(), replacing them with a memset up to MAP_LENGTH. However, XkbAllocNames() was missed and still allocates names->keys to max_key_code + 1. When a keycodes component with max_key_code < 255 is loaded (e.g. sun(type6) with max_key_code=132) and then a SetMap request extends maxKeyCode to 255, XkbChangeKeycodeRange() memsets names->keys from max_key_code to MAP_LENGTH-1, writing past the undersized allocation. Fix by allocating names->keys to MAP_LENGTH, consistent with all other keymap arrays after a3171732d. This vulnerability was discovered by: Anonymous working with TrendAI Zero Day Initiative ZDI-CAN-31941 CVE-2026-93520 Fixes: a3171732da50 ("xkb: Always use MAP_LENGTH keymap size") Signed-off-by: Peter Hutterer Assisted-by: Claude:claude-opus-4-6 Part-of: (cherry picked from commit b941a473e06f26a407a9adb15759be79f7418e86) --- Xext/xkeyboard/XKBAlloc.c | 2 +- test/pyxtest/test_xkb.py | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Xext/xkeyboard/XKBAlloc.c b/Xext/xkeyboard/XKBAlloc.c index b23fadad9e..c606dbe4a8 100644 --- a/Xext/xkeyboard/XKBAlloc.c +++ b/Xext/xkeyboard/XKBAlloc.c @@ -151,7 +151,7 @@ XkbAllocNames(XkbDescPtr xkb, unsigned which, int nTotalRG, int nTotalAliases) (!XkbIsLegalKeycode(xkb->max_key_code)) || (xkb->max_key_code < xkb->min_key_code)) return BadValue; - names->keys = calloc((xkb->max_key_code + 1), sizeof(XkbKeyNameRec)); + names->keys = calloc(MAP_LENGTH, sizeof(XkbKeyNameRec)); if (names->keys == NULL) return BadAlloc; } diff --git a/test/pyxtest/test_xkb.py b/test/pyxtest/test_xkb.py index 719afb4f39..ad881aa3e4 100644 --- a/test/pyxtest/test_xkb.py +++ b/test/pyxtest/test_xkb.py @@ -457,6 +457,81 @@ class TestXkbSetGeometry: ) +class TestXkbChangeKeycodeRange: + """Tests for XKB ChangeKeycodeRange vulnerabilities.""" + + @pytest.mark.asan + def test_names_keys_oob_write(self, xserver, xkb_xclient): + """ + ZDI-CAN-31941: Incomplete fix regression in XkbChangeKeycodeRange. + + Commit a3171732d ("xkb: Always use MAP_LENGTH keymap size") + converted most XKB allocations to use MAP_LENGTH (256) but missed + XkbAllocNames(), which still allocates names->keys to + max_key_code + 1. XkbChangeKeycodeRange() assumes MAP_LENGTH and + memsets up to index 255, overflowing the undersized buffer. + + The test loads a keycodes component with max_key_code < 255 via + GetKbdByName, then sends SetMap with maxKeyCode=255 to trigger the + grow branch in XkbChangeKeycodeRange. + """ + xclient, opcode = xkb_xclient + + # XkbGBN_AllComponentsMask + XkbGBN_AllComponentsMask = 0x00FF + + # Build GetKbdByName request with sun(type6) keycodes + # Component names: keymap(empty), keycodes=sun(type6), types(empty), + # compat(empty), symbols(empty), geometry(empty) + kc_name = b"sun(type6)" + names = ( + b"\x00" # keymap (empty) + + bytes([len(kc_name)]) + + kc_name # keycodes + + b"\x00" # types (empty) + + b"\x00" # compat (empty) + + b"\x00" # symbols (empty) + + b"\x00" # geometry (empty) + ) + + req = xkb.GetKbdByNameRequest( + opcode=opcode, + need=XkbGBN_AllComponentsMask, + want=XkbGBN_AllComponentsMask, + load=1, + payload=names, + ) + xclient.send_request(req) + resp = xclient.recv_response(timeout=5.0) + + if isinstance(resp, X11Error): + pytest.skip( + f"GetKbdByName failed with error {resp.error_code} - " + "sun(type6) keycodes may not be available" + ) + + assert xserver.is_alive, "Server crashed during GetKbdByName" + + # Now send SetMap with maxKeyCode=255 to trigger XkbChangeKeycodeRange + # present=0 means no actual map data, just triggers the grow + setmap_body = struct.pack("keys OOB write: " + "XkbAllocNames allocates max_key_code+1 but memset uses MAP_LENGTH" + ) + + class TestXkbSetCompatMap: """Tests for XKB SetCompatMap vulnerabilities."""