Repository navigation
Fix receive buffer overflow - #207
Merged
Merged
Conversation
cbPKT_MAX_SIZE < sizeof(cbPKT_FS_BASIS), so it's possible for a cbPKT_FS_BASIS packet near the end of the receive buffer to result in a read access violation even with padding of sizeof(cbPKT_GENERIC) == cbPKT_MAX_SIZE. The receive buffer must have padding equal to the largest packet on the wire, but whichever packet is the largest changes between protocol versions and cbPKT_MAX_SIZE cannot be the pad length because it is exceeded by cbPKG_FS_BASIS. This fix sets the pad length equal to the buffer length because all packets in future protocol versions are guaranteed to fit within cbCER_UDP_SIZE_MAX.
cboulay
reviewed
Sep 24, 2026
cboulay
left a comment
Collaborator
There was a problem hiding this comment.
Thanks. The DeviceSessionWrapper half is the important fix here: that receive thread (used for 3.11/4.0/4.1 devices) had no padding at all and filled the whole buffer, so any full-struct copy from a packet near the end of a large datagram ran off the stack buffer.
Padding by a full datagram is fine with me. It covers readers and writers (e.g. in-place translation, or any code that writes a full struct at a packet offset), and it doesn't need revisiting each time cbproto adds a bigger packet type.
A few cleanups before merging:
- Trim the comment in
startReceiveThread(). It currently reads like a design discussion ("The previous fix…", "A potential fix is…", thecbPKT_FS_BASIS/cbPKT_UNIT_SELECTIONsizing). The code comment should say what the padding is for; the history and alternatives belong in the commit message, where you already have them. Suggested wording is inline. - Typo ("the en") and a line with trailing whitespace, both inline.
- Stale comment in
device_session_wrapper.h:127. The scratch buffer inreceivePackets()is described as "mirroring the receive thread's own stack buffer of the same size", which is no longer true. Since the out-of-place translators (3.11, 4.0) readsrcthrough struct casts too, consider givingscratchthe samecbCER_UDP_SIZE_MAX * 2padding (still passingcbCER_UDP_SIZE_MAXasrecv_cap); otherwise please at least update that comment.
…ession.cpp Co-authored-by: Chadwick Boulay <chadwick.boulay@gmail.com>
cshmookler
force-pushed
the
fix/receive-buffer-overflow
branch
from
September 28, 2026 14:59
fa3e36d to
8a664e6
Compare
The out-of-place translators (3.11, 4.0) read source packets through struct casts, so the scratch buffer in receivePackets() needs the same full-datagram padding as the receive thread's buffer. It is still filled only to cbCER_UDP_SIZE_MAX.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add more padding to the receive buffer to prevent consumers of the receive buffer from reading past the end of the allocated space.
See comments in code and commits for details.