Wayland: a keymap that fills its last page without a NUL, sent with a size past its file's end, crashes keyboard_keymap with SIGBUS #206

Open
opened 2026-11-27 08:01:33 +00:00 by jorijnvdgraaf · 0 comments

Summary

Device::keyboard_keymap hands the mapped keymap to xkb_keymap_new_from_string, which reads up to the first NUL, not up to the size the compositor sent. wl_keyboard.keymap promises "a null-terminated string", so this takes a compositor that breaks the promise. Since #167's fix the handler ignores any other keymap it cannot load, but it does not survive this one. Line numbers refer to 148ed01.

Details

  • implementations/Crafter.Graphics-Device.cpp:693: mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0), with the size the compositor gave.
  • :706: xkb_keymap_new_from_string(xkb_context, static_cast<const char*>(map), ...). Nothing bounds the read by size, and nothing compares size with the size of the file.
  • A text that does not fill its last page ends at a NUL anyway, since the rest of that page reads as zeros, and the handler loads or ignores it as it would any keymap. A text that fills its last page without a NUL makes the read run on. When size is larger than the file, the next page of the mapping lies past the end of the file, and reading it raises SIGBUS. When it is not, the read runs past the end of the mapping, into whatever is mapped there (not tried).

Concrete case

The handler's two calls in a standalone program, on a memfd, with libxkbcommon 1.13.2 on Linux 7.2.11: 4096 bytes of x with no NUL, mapped with a size of 8192, die of signal 7 (SIGBUS) in xkb_keymap_new_from_string; 100 bytes of x with no NUL, mapped with a size of 100, return null. Not run through the library or against a compositor.

Suggested fix

Bound the read: xkb_keymap_new_from_buffer(xkb_context, text, strnlen(text, size), ...), after an fstat of the fd that ignores a keymap whose size is past the end of its file.

## Summary `Device::keyboard_keymap` hands the mapped keymap to `xkb_keymap_new_from_string`, which reads up to the first NUL, not up to the `size` the compositor sent. `wl_keyboard.keymap` promises "a null-terminated string", so this takes a compositor that breaks the promise. Since #167's fix the handler ignores any other keymap it cannot load, but it does not survive this one. Line numbers refer to `148ed01`. ## Details - `implementations/Crafter.Graphics-Device.cpp:693`: `mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0)`, with the size the compositor gave. - `:706`: `xkb_keymap_new_from_string(xkb_context, static_cast<const char*>(map), ...)`. Nothing bounds the read by `size`, and nothing compares `size` with the size of the file. - A text that does not fill its last page ends at a NUL anyway, since the rest of that page reads as zeros, and the handler loads or ignores it as it would any keymap. A text that fills its last page without a NUL makes the read run on. When `size` is larger than the file, the next page of the mapping lies past the end of the file, and reading it raises SIGBUS. When it is not, the read runs past the end of the mapping, into whatever is mapped there (not tried). ## Concrete case The handler's two calls in a standalone program, on a memfd, with libxkbcommon 1.13.2 on Linux 7.2.11: 4096 bytes of `x` with no NUL, mapped with a size of 8192, die of signal 7 (SIGBUS) in `xkb_keymap_new_from_string`; 100 bytes of `x` with no NUL, mapped with a size of 100, return null. Not run through the library or against a compositor. ## Suggested fix Bound the read: `xkb_keymap_new_from_buffer(xkb_context, text, strnlen(text, size), ...)`, after an `fstat` of the fd that ignores a keymap whose `size` is past the end of its file.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#206
No description provided.