Skip to content
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

fix(esp_http_server): prevent concurrent access to socket used in async http requests (IDFGH-13263) #14196

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/esp_http_server/src/httpd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ static int httpd_process_session(struct sock_db *session, void *context)
return 1;
}

// session is busy in an async task, do not process here.
if (session->for_async_req) {
return 1;
}

process_session_context_t *ctx = (process_session_context_t *)context;
int fd = session->fd;

Expand Down
3 changes: 2 additions & 1 deletion components/esp_http_server/src/httpd_sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static int enum_function(struct sock_db *session, void *context)
case HTTPD_TASK_INIT:
session->fd = -1;
session->ctx = NULL;
session->for_async_req = false;
break;
// Get active session
case HTTPD_TASK_GET_ACTIVE:
Expand All @@ -87,7 +88,7 @@ static int enum_function(struct sock_db *session, void *context)
break;
// Set descriptor
case HTTPD_TASK_SET_DESCRIPTOR:
if (session->fd != -1) {
if (session->fd != -1 && !session->for_async_req) {
FD_SET(session->fd, ctx->fdset);
if (session->fd > ctx->max_fd) {
ctx->max_fd = session->fd;
Expand Down
6 changes: 4 additions & 2 deletions components/esp_http_server/src/httpd_txrx.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,11 @@ esp_err_t httpd_req_async_handler_begin(httpd_req_t *r, httpd_req_t **out)
}
memcpy(async_aux->resp_hdrs, r_aux->resp_hdrs, hd->config.max_resp_headers * sizeof(struct resp_hdr));

// Prevent the main thread from reading the rest of the request after the handler returns.
r_aux->remaining_len = 0;

// mark socket as "in use"
struct httpd_req_aux *ra = r->aux;
ra->sd->for_async_req = true;
r_aux->sd->for_async_req = true;

*out = async;

Expand Down
Loading