Fix long compose result out-of-bounds write #114612

Open
Phoenix-Katsch wants to merge 1 commits from Phoenix-Katsch/blender:fix-compose-oob-write into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
First-time contributor

xkb_compose_state_get_utf8 returns how many UTF-8 bytes the result would have taken up, excluding the NUL terminator, before truncation. When the compose result is over six bytes long, xkb_compose_state_feed_and_get_utf8 writes too many bytes into r_utf8_buf.

I've chosen to add a guard before memcpy, passing the keypress through unhandled instead. I don't see this changing much; this is an edge case anyway.

Note that the old code never wrote a NUL terminator to r_utf8_buf; this was never a bug because the buffer it wrote to was already initialized by the caller (keyboard_handle_key). I've changed the code to copy the entire buffer contents instead; I can't see this causing any issues.

I considered using strncpy (or equivalently: just changing the memcpy size) instead of adding a guard, but that could have cut Unicode codepoints in half and I don't know what ramifications that could have had; it seemed better to simply bail instead. With what little testing I did, it seems like key events are only expected to hold a single Unicode codepoint and simply ignore the rest of the buffer; so perhaps that would've been okay.

There are alternative approaches here. One way would be to expand a single compose sequence into multiple key events, one for each Unicode codepoint of the compose result.

Another way would be to instead change composition to send IME events, but it looks like these are behind the WITH_INPUT_IME feature flag, meaning that disabling IME would also disable composition unless the old code path is kept around.

`xkb_compose_state_get_utf8` returns how many UTF-8 bytes the result would have taken up, excluding the NUL terminator, *before* truncation. When the compose result is over six bytes long, `xkb_compose_state_feed_and_get_utf8` writes too many bytes into `r_utf8_buf`. I've chosen to add a guard before `memcpy`, passing the keypress through unhandled instead. I don't see this changing much; this is an edge case anyway. Note that the old code never wrote a NUL terminator to `r_utf8_buf`; this was never a bug because the buffer it wrote to was already initialized by the caller (`keyboard_handle_key`). I've changed the code to copy the entire buffer contents instead; I can't see this causing any issues. I considered using `strncpy` (or equivalently: just changing the `memcpy` size) instead of adding a guard, but that could have cut Unicode codepoints in half and I don't know what ramifications that could have had; it seemed better to simply bail instead. With what little testing I did, it seems like key events are only expected to hold a single Unicode codepoint and simply ignore the rest of the buffer; so perhaps that would've been okay. There are alternative approaches here. One way would be to expand a single compose sequence into multiple key events, one for each Unicode codepoint of the compose result. Another way would be to instead change composition to send IME events, but it looks like these are behind the `WITH_INPUT_IME` feature flag, meaning that disabling IME would also disable composition unless the old code path is kept around.
Phoenix-Katsch added 1 commit 2023-11-08 01:56:14 +01:00
aa71dc2882 Fix long compose result out-of-bounds write
`xkb_compose_state_get_utf8` returns how many UTF-8 bytes the
result would have taken up, excluding the NUL terminator, *before*
truncation. When the compose result is over six bytes long,
`xkb_compose_state_feed_and_get_utf8` writes too many bytes into
`r_utf8_buf`.

I've chosen to add a guard before `memcpy`, passing the keypress through
unhandled instead. I don't see this changing much; this is an edge case
anyway.

Note that the old code never wrote a NUL terminator to `r_utf8_buf`;
this was never a bug because the buffer it wrote to was already
initialized by the caller (`keyboard_handle_key`). I've changed the code
to copy the entire buffer contents instead; I can't see this causing any
issues.

I considered using `strncpy` instead of adding a guard, but that could
have cut Unicode codepoints in half and I don't know what ramifications
that could have had; it seemed better to simply bail instead. With what
little testing I did, it seems like key events are only expected to hold
a single Unicode codepoint and simply ignore the rest of the buffer; so
perhaps that would've been okay.

There are alternative approaches here. One way would be to expand a
single compose sequence into multiple key events, one for each Unicode
codepoint of the compose result.

Another way would be to instead change composition to send IME events,
but it looks like these are behind the `WITH_INPUT_IME` feature flag,
meaning that disabling IME would also disable composition unless the old
code path is kept around.
Iliya Katushenock added the
Module
Core
label 2023-11-08 06:25:19 +01:00
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u fix-compose-oob-write:Phoenix-Katsch-fix-compose-oob-write
git checkout Phoenix-Katsch-fix-compose-oob-write
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
1 Participants
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: blender/blender#114612
No description provided.