mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2024-09-17 20:24:24 +00:00
altcp_tls_mbedtls: fixed memory leak introduced by delayed deallocation
This commit is contained in:
parent
0581a77731
commit
6add16e36b
@ -186,6 +186,10 @@ altcp_mbedtls_lower_recv(void *arg, struct altcp_pcb *inner_conn, struct pbuf *p
|
|||||||
{
|
{
|
||||||
altcp_mbedtls_state_t *state;
|
altcp_mbedtls_state_t *state;
|
||||||
struct altcp_pcb *conn = (struct altcp_pcb *)arg;
|
struct altcp_pcb *conn = (struct altcp_pcb *)arg;
|
||||||
|
|
||||||
|
LWIP_ASSERT("no err expected", err == ERR_OK);
|
||||||
|
LWIP_UNUSED_ARG(err);
|
||||||
|
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
/* no connection given as arg? should not happen, but prevent pbuf/conn leaks */
|
/* no connection given as arg? should not happen, but prevent pbuf/conn leaks */
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
@ -205,29 +209,24 @@ altcp_mbedtls_lower_recv(void *arg, struct altcp_pcb *inner_conn, struct pbuf *p
|
|||||||
return ERR_CLSD;
|
return ERR_CLSD;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* handle NULL pbufs or other errors */
|
/* handle NULL pbuf (connection closed) */
|
||||||
if ((p == NULL) || (err != ERR_OK)) {
|
if (p == NULL) {
|
||||||
err_t local_err = ERR_OK;
|
/* remote host sent FIN, remember this (SSL state is destroyed
|
||||||
if (p == NULL) {
|
when both sides are closed only!) */
|
||||||
/* remote host sent FIN, remember this (SSL state is destroyed
|
state->flags |= ALTCP_MBEDTLS_FLAGS_RX_CLOSE_QUEUED;
|
||||||
when both sides are closed only!) */
|
|
||||||
state->flags |= ALTCP_MBEDTLS_FLAGS_RX_CLOSE_QUEUED;
|
|
||||||
}
|
|
||||||
if ((state->flags & (ALTCP_MBEDTLS_FLAGS_HANDSHAKE_DONE|ALTCP_MBEDTLS_FLAGS_UPPER_CALLED)) ==
|
if ((state->flags & (ALTCP_MBEDTLS_FLAGS_HANDSHAKE_DONE|ALTCP_MBEDTLS_FLAGS_UPPER_CALLED)) ==
|
||||||
(ALTCP_MBEDTLS_FLAGS_HANDSHAKE_DONE|ALTCP_MBEDTLS_FLAGS_UPPER_CALLED)) {
|
(ALTCP_MBEDTLS_FLAGS_HANDSHAKE_DONE|ALTCP_MBEDTLS_FLAGS_UPPER_CALLED)) {
|
||||||
/* need to notify upper layer (e.g. 'accept' called or 'connect' succeeded) */
|
/* need to notify upper layer (e.g. 'accept' called or 'connect' succeeded) */
|
||||||
if ((err == ERR_OK) && ((state->rx != NULL) || (state->rx_app != NULL))) {
|
if ((state->rx != NULL) || (state->rx_app != NULL)) {
|
||||||
LWIP_ASSERT("p == NULL", p == NULL);
|
/* this is a normal close (FIN) but we have unprocessed data, so delay the FIN */
|
||||||
/* this is a normal close (FIN) but we have unprocessed data */
|
|
||||||
altcp_mbedtls_handle_rx_appldata(conn, state);
|
altcp_mbedtls_handle_rx_appldata(conn, state);
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
if (conn->recv) {
|
if (conn->recv) {
|
||||||
local_err = conn->recv(conn->arg, conn, p, err);
|
err_t local_err = conn->recv(conn->arg, conn, NULL, ERR_OK);
|
||||||
if ((local_err != ERR_OK) && (p != NULL)) {
|
if (local_err == ERR_ABRT) {
|
||||||
pbuf_free(p);
|
return ERR_ABRT;
|
||||||
}
|
}
|
||||||
p = NULL;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* before connection setup is done: call 'err' */
|
/* before connection setup is done: call 'err' */
|
||||||
@ -235,14 +234,12 @@ altcp_mbedtls_lower_recv(void *arg, struct altcp_pcb *inner_conn, struct pbuf *p
|
|||||||
conn->err(conn->arg, ERR_CLSD);
|
conn->err(conn->arg, ERR_CLSD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (p) {
|
|
||||||
pbuf_free(p);
|
|
||||||
}
|
|
||||||
altcp_close(conn);
|
altcp_close(conn);
|
||||||
|
state->flags |= ALTCP_MBEDTLS_FLAGS_RX_CLOSED;
|
||||||
if (conn->state && ((state->flags & ALTCP_MBEDTLS_FLAGS_CLOSED) == ALTCP_MBEDTLS_FLAGS_CLOSED)) {
|
if (conn->state && ((state->flags & ALTCP_MBEDTLS_FLAGS_CLOSED) == ALTCP_MBEDTLS_FLAGS_CLOSED)) {
|
||||||
altcp_mbedtls_dealloc(conn);
|
altcp_mbedtls_dealloc(conn);
|
||||||
}
|
}
|
||||||
return local_err;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we come here, the connection is in good state (handshake phase or application data phase).
|
/* If we come here, the connection is in good state (handshake phase or application data phase).
|
||||||
@ -415,14 +412,13 @@ altcp_mbedtls_handle_rx_appldata(struct altcp_pcb *conn, altcp_mbedtls_state_t *
|
|||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
if (err == ERR_ABRT) {
|
if (err == ERR_ABRT) {
|
||||||
/* recv callback needs to return this as the pcb is deallocated */
|
/* recv callback needs to return this as the pcb is deallocated */
|
||||||
return err;
|
return ERR_ABRT;
|
||||||
}
|
}
|
||||||
/* we hide all other errors as we retry feeding the pbuf to the app later */
|
/* we hide all other errors as we retry feeding the pbuf to the app later */
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while (ret > 0);
|
||||||
while (ret > 0);
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +499,7 @@ altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len)
|
|||||||
|
|
||||||
/** Poll callback from lower connection (i.e. TCP)
|
/** Poll callback from lower connection (i.e. TCP)
|
||||||
* Just pass this on to the application.
|
* Just pass this on to the application.
|
||||||
* @todo: retry sending if
|
* @todo: retry sending?
|
||||||
*/
|
*/
|
||||||
static err_t
|
static err_t
|
||||||
altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn)
|
altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn)
|
||||||
@ -513,7 +509,10 @@ altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn)
|
|||||||
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
|
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
|
||||||
/* check if there's unreceived rx data */
|
/* check if there's unreceived rx data */
|
||||||
if (conn->state) {
|
if (conn->state) {
|
||||||
altcp_mbedtls_handle_rx_appldata(conn, (altcp_mbedtls_state_t *)conn->state);
|
if (altcp_mbedtls_handle_rx_appldata(conn, (altcp_mbedtls_state_t *)conn->state) ==
|
||||||
|
ERR_ABRT) {
|
||||||
|
return ERR_ABRT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (conn->poll) {
|
if (conn->poll) {
|
||||||
return conn->poll(conn->arg, conn);
|
return conn->poll(conn->arg, conn);
|
||||||
@ -832,10 +831,6 @@ altcp_mbedtls_close(struct altcp_pcb *conn)
|
|||||||
}
|
}
|
||||||
state = (altcp_mbedtls_state_t*)conn->state;
|
state = (altcp_mbedtls_state_t*)conn->state;
|
||||||
if (state != NULL) {
|
if (state != NULL) {
|
||||||
if (state->rx != NULL) {
|
|
||||||
pbuf_free(state->rx);
|
|
||||||
state->rx = NULL;
|
|
||||||
}
|
|
||||||
state->flags |= ALTCP_MBEDTLS_FLAGS_TX_CLOSED;
|
state->flags |= ALTCP_MBEDTLS_FLAGS_TX_CLOSED;
|
||||||
if (state->flags & ALTCP_MBEDTLS_FLAGS_RX_CLOSED) {
|
if (state->flags & ALTCP_MBEDTLS_FLAGS_RX_CLOSED) {
|
||||||
altcp_mbedtls_dealloc(conn);
|
altcp_mbedtls_dealloc(conn);
|
||||||
|
Loading…
Reference in New Issue
Block a user