Fix use-after-free and memory leak in mqtt_pit.c#20
Open
juandiego-bmu wants to merge 2 commits intohoneynet:mainfrom
Open
Fix use-after-free and memory leak in mqtt_pit.c#20juandiego-bmu wants to merge 2 commits intohoneynet:mainfrom
juandiego-bmu wants to merge 2 commits intohoneynet:mainfrom
Conversation
When disconnectClient() is called inside the packet processing switch, it frees the client struct. But break only exits the switch, not the for loop — so the code after the loop still accesses the freed client (reading bytesWrittenToBuffer, calling memmove on client->buffer). Added a clientDisconnected flag that breaks out of both the switch and the for loop, and guards the post-loop buffer cleanup so it only runs when the client is still alive. Also added free(arr) to sendPubrel() which was leaking a small allocation on every call. Fixes honeynet#19
lookupClient() can return NULL if the fd is not found in the hash table. The code on line 873 used the return value immediately without checking, which would cause a NULL pointer dereference. Now we safely remove the fd from epoll and close it if the client is not found.
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.
Fixes the use-after-free and the
sendPubrel()memory leak reported in #19.Use-after-free in packet processing loop
When
disconnectClient()is called inside the switch (DISCONNECT, CONNECT failure, PUBCOMP, PING), it frees the client struct. Butbreakonly exits the switch, not theforloop — so after the loop, lines like:are accessing freed memory. Also, if there are multiple MQTT packets in one TCP segment, the loop keeps iterating and reads
client->bufferagain.Fix: added a
clientDisconnectedflag that breaks out of both the switch and the for loop, and wraps the post-loop buffer cleanup inif (!clientDisconnected).Memory leak in
sendPubrel()sendPubrel()callsmallocbut neverfrees the buffer. Addedfree(arr)before both return paths, matching the pattern used bysendConnack.Fixes #19