forked from me-no-dev/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 106
fix(http): prevent use-after-free from synchronous abort/close() - v2 #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,6 @@ static inline bool isParamChar(char c) { | |
| return ((c) && ((c) != '{') && ((c) != '[') && ((c) != '&') && ((c) != '=')); | ||
| } | ||
|
|
||
| static void doNotDelete(AsyncWebServerRequest *) {} | ||
|
|
||
| using namespace asyncsrv; | ||
|
|
||
| enum { | ||
|
|
@@ -108,8 +106,6 @@ AsyncWebServerRequest::~AsyncWebServerRequest() { | |
| _response = nullptr; | ||
| } | ||
|
|
||
| _this.reset(); | ||
|
|
||
| if (_tempObject != NULL) { | ||
| free(_tempObject); | ||
| } | ||
|
|
@@ -124,6 +120,8 @@ AsyncWebServerRequest::~AsyncWebServerRequest() { | |
| } | ||
|
|
||
| void AsyncWebServerRequest::_onData(void *buf, size_t len) { | ||
| std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function | ||
|
|
||
| // SSL/TLS handshake detection | ||
| #ifndef ASYNC_TCP_SSL_ENABLED | ||
| if (_parseState == PARSE_REQ_START && len && ((uint8_t *)buf)[0] == 0x16) { // 0x16 indicates a Handshake message (SSL/TLS). | ||
|
|
@@ -240,13 +238,15 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len) { | |
| } | ||
|
|
||
| void AsyncWebServerRequest::_onPoll() { | ||
| std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function | ||
| // os_printf("p\n"); | ||
| if (_response && _client && _client->canSend()) { | ||
| _response->_ack(this, 0, 0); | ||
| } | ||
| } | ||
|
|
||
| void AsyncWebServerRequest::_onAck(size_t len, uint32_t time) { | ||
| std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function | ||
| // os_printf("a:%u:%u\n", len, time); | ||
| if (!_response) { | ||
| return; | ||
|
|
@@ -269,6 +269,7 @@ void AsyncWebServerRequest::_onError(int8_t error) { | |
| } | ||
|
|
||
| void AsyncWebServerRequest::_onTimeout(uint32_t time) { | ||
| std::shared_ptr<AsyncWebServerRequest> self = _this; // Ensure we stay in scope over the function | ||
| (void)time; | ||
| // os_printf("TIMEOUT: %u, state: %s\n", time, _client->stateToString()); | ||
| _client->close(); | ||
|
|
@@ -280,10 +281,16 @@ void AsyncWebServerRequest::onDisconnect(ArDisconnectHandler fn) { | |
|
|
||
| void AsyncWebServerRequest::_onDisconnect() { | ||
| async_ws_log_v("onDisconnect() cb for request: %s", _url.c_str()); | ||
| // Take a local copy of _this before resetting the member: if _this is the | ||
| // last owning shared_ptr, resetting it would destruct *this while this | ||
| // member function is still executing. The local copy keeps us alive until | ||
| // the function returns, then the shared_ptr destructor runs safely. | ||
| std::shared_ptr<AsyncWebServerRequest> self = _this; | ||
| if (_onDisconnectfn) { | ||
| _onDisconnectfn(); | ||
| } | ||
| _server->_handleDisconnect(this); | ||
| _this.reset(); // release shared pointer to this request, allowing for object destruction | ||
| // 'self' goes out of scope here — if it was the last owner, the request is deleted | ||
|
Comment on lines
+284
to
+293
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect and unnecessary. Sorry I have not had a chance to post the detailed explanation yet. |
||
| } | ||
|
|
||
| void AsyncWebServerRequest::_addGetParams(const String ¶ms) { | ||
|
|
@@ -1054,9 +1061,6 @@ AsyncWebServerRequestPtr AsyncWebServerRequest::pause() { | |
| return _this; | ||
| } | ||
| client()->setRxTimeout(0); | ||
| // this shared ptr will hold the request pointer until it gets destroyed following a disconnect. | ||
| // this is just used as a holder providing weak observers, so the deleter is a no-op. | ||
| _this = std::shared_ptr<AsyncWebServerRequest>(this, doNotDelete); | ||
| _paused = true; | ||
| return _this; | ||
| } | ||
|
|
@@ -1065,7 +1069,6 @@ void AsyncWebServerRequest::abort() { | |
| if (!_sent) { | ||
| _sent = true; | ||
| _paused = false; | ||
| _this.reset(); | ||
| async_ws_log_v("Abort request: %s", _url.c_str()); | ||
| _client->abort(); | ||
| } | ||
|
|
@@ -1327,7 +1330,9 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch | |
| r->addHeader(T_WWW_AUTH, header.c_str()); | ||
| } else { | ||
| async_ws_log_e("Failed to allocate"); | ||
| delete r; | ||
| abort(); | ||
| return; | ||
| } | ||
|
|
||
| break; | ||
|
|
@@ -1351,7 +1356,9 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch | |
| r->addHeader(T_WWW_AUTH, header.c_str()); | ||
| } else { | ||
| async_ws_log_e("Failed to allocate"); | ||
| delete r; | ||
| abort(); | ||
| return; | ||
| } | ||
| } | ||
| break; | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a waste of resources and should be omitted as it is not necessary for this function.