Add buffer safety checks to prevent out-of-bounds access#96
Add buffer safety checks to prevent out-of-bounds access#96hmueller01 wants to merge 12 commits into
Conversation
- connect(): null-buffer guard to prevent crash if buffer allocation failed - disconnect(): null-buffer guard - loop(): buffer existence and size validation before packet processing - handlePacket(): three ordered boundary checks for topic length, payload offset, and msgId fields - Detailed error messages with context for debugging
|
@TD-er @MarcAntoineCRUE This PR is just about the safety checks (and some needed mods to satisfy new linter). |
|
At first glance I don't see any obvious issues, but since I was just about to go to bed, I will have to look at it tomorrow with some fresh eyes :) |
|
Memory usage change @ d3973b9
Click for full report table
Click for full report CSV |
|
Only found one thing to think about. |
|
Memory usage change @ 32b657c
Click for full report table
Click for full report CSV |
|
Memory usage change @ b67cac8
Click for full report table
Click for full report CSV |
…etBufferSize(), no need to check every time in loop()
|
These branches that the AI (Claude) created are really strange. Now branch
|
|
@TD-er If you find time again, can you please take a last look on it. Thanks. |
| // Cannot set it back to 0 | ||
| // Buffer must be large enough to hold at least a minimal MQTT packet. | ||
| // Note: MQTT_MAX_HEADER_SIZE (5) + protocol (9) + flags (1) + keepalive (2) covers a minmal CONNECT message. | ||
| if (size < MQTT_MAX_HEADER_SIZE + 9 + 1 + 2) { |
There was a problem hiding this comment.
Hmm I guess there might be good reasons to completely disable the buffer to save up all available resources, only to later set the buffer again.
So maybe it is good to accept sizes which are too small and then simply delete the buffer and set its pointer to null.
This value you're now checking against may be a good bottom value to just clear the buffer.
Not sure though what the return value should be for this function.
There was a problem hiding this comment.
Ok, I now allow setting the buffer to 0, but not to lower values than the min buffer size. It this fine for you?
There was a problem hiding this comment.
@TD-er Are you ok with the new implementation?
|
There are quite a few functions in the code that try to access For example:
|
Hmm, this is the question. Do we make every function failsafe by it's own, or do we trust that it was checked before. I see only one case that might fail here: pubsubclient3/src/PubSubClient.cpp Lines 737 to 741 in c856d48 |
|
Memory usage change @ c856d48
Click for full report table
Click for full report CSV |
|
@MarcAntoineCRUE Heard nothing from you. Hope you are doing well. If no further comments I'll merge this soon. |
Applied safety checks introduced by @MarcAntoineCRUE in #91