Abort normalizer sessions on OOM and guard reentrant delivery callbacks
This commit is contained in:
1 parent
8da16b603c
commit
116b11ca0b
2 files changed
+97
-130
No files matched your search
@@ -192,6 +192,7 @@ void etcp_int_recv(struct ll_queue* queue, void* arg) {
|
||||
uint8_t id = e->dgram[0];
|
||||
struct UTUN_INSTANCE* inst = conn->instance;
|
||||
if (!inst) { queue_dgram_free(e); queue_entry_free(e); queue_resume_callback(queue); return; }
|
||||
conn->callbacks_running++;
|
||||
if (inst->api_bindings.callbacks[id]) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_TRAFFIC, "dispatch id=0x%02x conn=%s len=%zu", id, conn->log_name, e->len);
|
||||
inst->api_bindings.callbacks[id](conn, e);
|
||||
@@ -199,5 +200,6 @@ void etcp_int_recv(struct ll_queue* queue, void* arg) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_TRAFFIC, "dispatch id=0x%02x (fallback) conn=%s len=%zu", id, conn->log_name, e->len);
|
||||
inst->api_bindings.callbacks[ETCP_ID_DATA](conn, e);
|
||||
} else { queue_dgram_free(e); queue_entry_free(e); }
|
||||
queue_resume_callback(queue);
|
||||
if (!conn->close_requested) queue_resume_callback(queue);
|
||||
conn->callbacks_running--;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ static void packer_cb(struct ll_queue* q, void* arg);
|
||||
static void pn_flush_cb(void* arg);
|
||||
static void etcp_input_ready_cb(struct ll_queue* q, void* arg);
|
||||
static void pn_unpacker_cb(struct ll_queue* q, void* arg);
|
||||
static void pn_send_to_etcp(struct PKTNORM* pn);
|
||||
static int pn_send_to_etcp(struct PKTNORM* pn);
|
||||
|
||||
// Initialization
|
||||
struct PKTNORM* pn_init(struct ETCP_CONN* etcp) {
|
||||
@@ -81,60 +81,13 @@ struct PKTNORM* pn_init(struct ETCP_CONN* etcp) {
|
||||
|
||||
// Deinitialization
|
||||
void pn_deinit(struct PKTNORM* pn) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
if (!pn) return;
|
||||
|
||||
// Unregister from routing module
|
||||
if (pn->etcp) {
|
||||
routing_del_conn(pn->etcp);
|
||||
}
|
||||
|
||||
// Drain and free queues
|
||||
if (pn->input) {
|
||||
struct ll_entry* entry;
|
||||
while ((entry = queue_data_get(pn->input)) != NULL) {
|
||||
if (entry->dgram) {
|
||||
u_free(entry->dgram);
|
||||
}
|
||||
queue_entry_free(entry);
|
||||
}
|
||||
queue_free(pn->input);
|
||||
}
|
||||
if (pn->output) {
|
||||
struct ll_entry* entry;
|
||||
while ((entry = queue_data_get(pn->output)) != NULL) {
|
||||
if (entry->dgram) {
|
||||
u_free(entry->dgram);
|
||||
}
|
||||
queue_entry_free(entry);
|
||||
}
|
||||
queue_free(pn->output);
|
||||
}
|
||||
|
||||
if (pn->flush_timer) {
|
||||
uasync_call_soon_cancel(pn->ua, pn->flush_timer);
|
||||
}
|
||||
|
||||
if (pn->data) {
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "pn_deinit: free pn->data=%p ptr=%d", pn->data, pn->data_ptr);
|
||||
memory_pool_free(pn->etcp->instance->data_pool, pn->data);
|
||||
}
|
||||
if (pn->recvpart) {
|
||||
queue_dgram_free(pn->recvpart);
|
||||
queue_entry_free(pn->recvpart);
|
||||
}
|
||||
|
||||
// Cancel waiter and clear callbacks on ETCP queues to prevent use-after-free
|
||||
// during etcp_connection_close() when drain_and_free_fragment_queue() is called
|
||||
if (pn->etcp) {
|
||||
if (pn->etcp->input_queue) {
|
||||
queue_waiter_cancel(pn->etcp->input_queue, &pn->input_waiter_handle);
|
||||
}
|
||||
if (pn->etcp->output_queue) {
|
||||
queue_set_callback(pn->etcp->output_queue, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] normalizer deinit", pn->etcp->log_name);
|
||||
routing_del_conn(pn->etcp);
|
||||
queue_set_callback(pn->etcp->output_queue, NULL, NULL);
|
||||
pn_reset(pn);
|
||||
if (pn->input) queue_free(pn->input);
|
||||
if (pn->output) queue_free(pn->output);
|
||||
u_free(pn);
|
||||
}
|
||||
|
||||
@@ -155,6 +108,11 @@ void pn_unpacker_reset_state(struct PKTNORM* pn) {
|
||||
void pn_reset(struct PKTNORM* pn) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
if (!pn) return;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] normalizer discard: input=%d output=%d packed=%u partial_rx=%u",
|
||||
pn->etcp->log_name, queue_entry_count(pn->input), queue_entry_count(pn->output), pn->data_ptr,
|
||||
pn->recvpart ? pn->recvpart->len : 0);
|
||||
queue_waiter_cancel(pn->etcp->input_queue, &pn->input_waiter_handle);
|
||||
queue_clear(pn->input); queue_clear(pn->output);
|
||||
|
||||
// Cancel flush timer
|
||||
if (pn->flush_timer) {
|
||||
@@ -219,24 +177,31 @@ void pn_packer_send(struct PKTNORM* pn, uint8_t* data, uint16_t len) {
|
||||
static void packer_cb(struct ll_queue* q, void* arg) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
struct PKTNORM* pn = (struct PKTNORM*)arg;
|
||||
if (!pn) return;
|
||||
if (!pn || pn->etcp->close_requested || pn->etcp->reinit_pending || pn->etcp->session_required) return;
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "input_q->pn: waiting etcp input threshold");
|
||||
queue_waiter_wait(pn->etcp->input_queue, &pn->input_waiter_handle, etcp_input_ready_cb, pn);
|
||||
pn->etcp->callbacks_running++;
|
||||
if (queue_waiter_wait(pn->etcp->input_queue, &pn->input_waiter_handle, etcp_input_ready_cb, pn) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer waiter OOM; restarting stream", pn->etcp->log_name);
|
||||
etcp_conn_fatal_reinit(pn->etcp, "normalizer waiter OOM");
|
||||
}
|
||||
pn->etcp->callbacks_running--;
|
||||
}
|
||||
|
||||
// Helper to send block to ETCP as ETCP_FRAGMENT
|
||||
static void pn_send_to_etcp(struct PKTNORM* pn) {
|
||||
static int pn_send_to_etcp(struct PKTNORM* pn) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
if (!pn || !pn->data || pn->data_ptr == 0) return;
|
||||
if (!pn || !pn->data || pn->data_ptr == 0) return 0;
|
||||
uint32_t reset = pn->etcp->reset_count;
|
||||
|
||||
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "pn_send_to_etcp");
|
||||
// Allocate ETCP_FRAGMENT from io_pool
|
||||
struct ETCP_FRAGMENT* frag = (struct ETCP_FRAGMENT*)queue_entry_new_from_pool(pn->etcp->io_pool);
|
||||
if (!frag) {// drop data
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "send to etcp alloc error");
|
||||
if (!frag) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer fragment OOM bytes=%u; restarting stream",
|
||||
pn->etcp->log_name, pn->data_ptr);
|
||||
pn->alloc_errors++;
|
||||
pn->data_ptr = 0;
|
||||
return;
|
||||
etcp_conn_fatal_reinit(pn->etcp, "normalizer TX fragment OOM");
|
||||
return -1;
|
||||
}
|
||||
|
||||
frag->seq = 0;
|
||||
@@ -255,22 +220,28 @@ static void pn_send_to_etcp(struct PKTNORM* pn) {
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "pn->etcp: size=%d memlen=%d frag_size=%d", frag->ll.len, frag->ll.memlen, pn->frag_size);
|
||||
if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP)) log_dump(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP, "NORM->ETCP", frag->ll.dgram, frag->ll.len);
|
||||
|
||||
queue_data_put(pn->etcp->input_queue, (struct ll_entry*)frag);
|
||||
if (queue_data_put(pn->etcp->input_queue, (struct ll_entry*)frag) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer TX queue rejected fragment; restarting stream", pn->etcp->log_name);
|
||||
etcp_conn_fatal_reinit(pn->etcp, "normalizer TX queue failure");
|
||||
return -1;
|
||||
}
|
||||
return pn->etcp->close_requested || pn->etcp->reinit_pending || pn->etcp->reset_count != reset ? -1 : 0;
|
||||
}
|
||||
|
||||
// Internal: Renew sndpart buffer
|
||||
static void pn_buf_renew(struct PKTNORM* pn) {
|
||||
static int pn_buf_renew(struct PKTNORM* pn) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
if (pn->data) {
|
||||
int remain = pn->data_size - pn->data_ptr;
|
||||
if (remain < 3) pn_send_to_etcp(pn);
|
||||
if (remain < 3 && pn_send_to_etcp(pn) < 0) return -1;
|
||||
}
|
||||
if (!pn->data) {
|
||||
pn->data = memory_pool_alloc(pn->etcp->instance->data_pool);
|
||||
if (!pn->data) {
|
||||
pn->alloc_errors++;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "memory_pool_alloc failed");
|
||||
return;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer buffer OOM; restarting stream", pn->etcp->log_name);
|
||||
etcp_conn_fatal_reinit(pn->etcp, "normalizer TX buffer OOM");
|
||||
return -1;
|
||||
}
|
||||
int size=pn->etcp->instance->data_pool->object_size;
|
||||
if (size>pn->frag_size) size=pn->frag_size;
|
||||
@@ -278,12 +249,16 @@ static void pn_buf_renew(struct PKTNORM* pn) {
|
||||
pn->data_ptr=0;
|
||||
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "new bufer size=%d bytes",size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Internal: Process input when etcp->input_queue is ready (empty)
|
||||
static void etcp_input_ready_cb(struct ll_queue* q, void* arg) {
|
||||
struct PKTNORM* pn = (struct PKTNORM*)arg;
|
||||
if (!pn || !pn->etcp) return;
|
||||
if (!pn || !pn->etcp || pn->etcp->close_requested || pn->etcp->reinit_pending || pn->etcp->session_required) return;
|
||||
struct ETCP_CONN* conn = pn->etcp;
|
||||
uint32_t reset = conn->reset_count;
|
||||
conn->callbacks_running++;
|
||||
// if (pn->etcp->initialized==0) return;// начинаем обработку очередей только когда готов etcp
|
||||
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "before get");
|
||||
@@ -292,6 +267,7 @@ static void etcp_input_ready_cb(struct ll_queue* q, void* arg) {
|
||||
if (!in_dgram) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Q EMPTY");
|
||||
queue_resume_callback(pn->input);
|
||||
conn->callbacks_running--;
|
||||
return;
|
||||
}
|
||||
pn->in_total_pkts++;
|
||||
@@ -299,7 +275,7 @@ static void etcp_input_ready_cb(struct ll_queue* q, void* arg) {
|
||||
|
||||
if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP)) log_dump(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP, "->NORM", in_dgram->dgram, in_dgram->len);
|
||||
|
||||
pn_buf_renew(pn);
|
||||
if (pn_buf_renew(pn) < 0) goto exit;
|
||||
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "new pkt hdrpos=%d",pn->data_ptr);
|
||||
if (!pn->data) goto exit; // Allocation failed
|
||||
pn->data[pn->data_ptr++] = in_dgram->len & 0xFF;
|
||||
@@ -314,13 +290,15 @@ static void etcp_input_ready_cb(struct ll_queue* q, void* arg) {
|
||||
memcpy(pn->data + pn->data_ptr, in_dgram->dgram + in_ptr, remain);
|
||||
pn->data_ptr += remain;
|
||||
in_ptr += remain;
|
||||
pn_buf_renew(pn);
|
||||
if (!pn->data) goto exit;
|
||||
if (pn_buf_renew(pn) < 0) goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
queue_dgram_free(in_dgram);
|
||||
queue_entry_free(in_dgram);
|
||||
if (conn->close_requested || conn->reinit_pending || conn->reset_count != reset) {
|
||||
conn->callbacks_running--; return;
|
||||
}
|
||||
|
||||
// Cancel flush timer if active
|
||||
if (pn->flush_timer) {
|
||||
@@ -331,9 +309,14 @@ exit:
|
||||
// Set flush timer if no more input
|
||||
if (queue_entry_count(pn->input) == 0) {
|
||||
pn->flush_timer = uasync_call_soon(pn->ua/*, pn->tx_wait_time*/, pn, pn_flush_cb);
|
||||
if (!pn->flush_timer) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer flush scheduling OOM; restarting stream", conn->log_name);
|
||||
etcp_conn_fatal_reinit(conn, "normalizer flush OOM");
|
||||
}
|
||||
}
|
||||
|
||||
queue_resume_callback(pn->input);
|
||||
conn->callbacks_running--;
|
||||
}
|
||||
|
||||
// Internal: Flush callback on timeout
|
||||
@@ -343,85 +326,67 @@ static void pn_flush_cb(void* arg) {
|
||||
if (!pn) return;
|
||||
|
||||
pn->flush_timer = NULL;
|
||||
if (pn->etcp->close_requested || pn->etcp->reinit_pending || pn->etcp->session_required) return;
|
||||
pn->etcp->callbacks_running++;
|
||||
pn_send_to_etcp(pn);
|
||||
pn->etcp->callbacks_running--;
|
||||
}
|
||||
|
||||
|
||||
// Internal: Unpacker callback (assembles fragments into original packets)
|
||||
static void pn_unpacker_cb(struct ll_queue* q, void* arg) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "");
|
||||
struct PKTNORM* pn = (struct PKTNORM*)arg;
|
||||
if (!pn) return;
|
||||
|
||||
while (1) {
|
||||
void* data = queue_data_get(pn->etcp->output_queue);
|
||||
if (!data) break;
|
||||
|
||||
struct ETCP_FRAGMENT* frag = (struct ETCP_FRAGMENT*)data; // Since data is ll_entry*
|
||||
struct PKTNORM* pn = arg;
|
||||
if (!pn || pn->etcp->close_requested || pn->etcp->reinit_pending) return;
|
||||
struct ETCP_CONN* conn = pn->etcp;
|
||||
uint32_t reset = conn->reset_count;
|
||||
conn->callbacks_running++;
|
||||
while (!conn->close_requested && !conn->reinit_pending && conn->reset_count == reset) {
|
||||
struct ETCP_FRAGMENT* frag = (struct ETCP_FRAGMENT*)queue_data_get(q);
|
||||
if (!frag) break;
|
||||
uint8_t* payload = frag->ll.dgram;
|
||||
uint16_t len = frag->ll.len;
|
||||
uint16_t ptr = 0;
|
||||
if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP)) log_dump(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP, "ETCP->NORM", payload, len);
|
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "unpacking fragment len=%d", len);
|
||||
|
||||
while (ptr < len) {
|
||||
uint16_t len = frag->ll.len, ptr = 0;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] unpack fragment seq=%u bytes=%u", conn->log_name, frag->seq, len);
|
||||
while (ptr < len && !conn->close_requested && !conn->reinit_pending && conn->reset_count == reset) {
|
||||
if (!pn->recvpart) {
|
||||
// Need length header for new packet
|
||||
if (len - ptr < 2) {
|
||||
pn->logic_errors++;
|
||||
// Incomplete header, reset
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "reset state");
|
||||
// pn_unpacker_reset_state(pn);
|
||||
etcp_conn_fatal_reinit(pn->etcp, "normalizer desync");
|
||||
break;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] short normalizer header seq=%u offset=%u len=%u",
|
||||
conn->log_name, frag->seq, ptr, len);
|
||||
etcp_conn_fatal_reinit(conn, "normalizer desync"); break;
|
||||
}
|
||||
uint16_t part_size = payload[ptr] | (payload[ptr + 1] << 8);
|
||||
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "new fragment pkt_len=%d (at %d)", part_size, ptr);
|
||||
ptr += 2;
|
||||
|
||||
if (part_size<1 || part_size>PKTNORM_MAX_DGRAM_SIZE) {
|
||||
uint16_t size = payload[ptr] | (payload[ptr + 1] << 8);
|
||||
if (!size || size > PKTNORM_MAX_DGRAM_SIZE) {
|
||||
pn->logic_errors++;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "PART_SIZE ERROR!!! %d", part_size);
|
||||
etcp_conn_fatal_reinit(pn->etcp, "bad fragment size");
|
||||
break;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] invalid normalizer size=%u seq=%u", conn->log_name, size, frag->seq);
|
||||
etcp_conn_fatal_reinit(conn, "bad fragment size"); break;
|
||||
}
|
||||
|
||||
pn->recvpart = ll_alloc_lldgram(part_size);
|
||||
pn->recvpart = ll_alloc_lldgram(size);
|
||||
if (!pn->recvpart) {
|
||||
pn->alloc_errors++;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "ll_alloc_lldgram failed");
|
||||
break;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer RX OOM seq=%u offset=%u size=%u; restarting stream",
|
||||
conn->log_name, frag->seq, ptr, size);
|
||||
etcp_conn_fatal_reinit(conn, "normalizer RX OOM"); break;
|
||||
}
|
||||
pn->recvpart->len = 0;
|
||||
ptr += 2;
|
||||
}
|
||||
|
||||
uint16_t rem = pn->recvpart->memlen - pn->recvpart->len;// осталось собрать байт
|
||||
uint16_t avail = len - ptr;// доступно байт сейчас
|
||||
uint16_t cp = (rem < avail) ? rem : avail;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "copy: remain=%d avail=%d in_ptr=%d out_ptr=%d", rem, avail, ptr, pn->recvpart->len);
|
||||
memcpy(pn->recvpart->dgram + pn->recvpart->len, payload + ptr, cp);
|
||||
pn->recvpart->len += cp;
|
||||
ptr += cp;
|
||||
|
||||
uint16_t remain = pn->recvpart->memlen - pn->recvpart->len;
|
||||
uint16_t copy = remain < len - ptr ? remain : len - ptr;
|
||||
memcpy(pn->recvpart->dgram + pn->recvpart->len, payload + ptr, copy);
|
||||
pn->recvpart->len += copy; ptr += copy;
|
||||
if (pn->recvpart->len == pn->recvpart->memlen) {
|
||||
uint32_t recv_len = pn->recvpart->len;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "unpacked dgram (size=%d)", recv_len);
|
||||
if (debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP)) log_dump(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP, "NORM->", pn->recvpart->dgram, recv_len);
|
||||
// Зануляем recvpart ДО queue_data_put: etcp_int_recv может ре-энтрантно вызвать
|
||||
// reinit → pn_unpacker_reset_state → free(pn->recvpart), что даст double-free.
|
||||
struct ll_entry* out = pn->recvpart;
|
||||
pn->recvpart = NULL;
|
||||
queue_data_put(pn->output, out);
|
||||
pn->out_total_pkts++;
|
||||
pn->out_total_bytes += recv_len;
|
||||
pn->out_total_pkts++; pn->out_total_bytes += out->len;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] normalizer deliver bytes=%u", conn->log_name, out->len);
|
||||
if (queue_data_put(pn->output, out) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] normalizer output rejected message; restarting stream", conn->log_name);
|
||||
etcp_conn_fatal_reinit(conn, "normalizer RX queue failure");
|
||||
}
|
||||
// Callback приложения мог сбросить поток или запросить close; условие цикла проверяется до чтения старых данных.
|
||||
}
|
||||
}
|
||||
|
||||
// Free the fragment - dgram was malloc'd in pn_send_to_etcp
|
||||
memory_pool_free(pn->etcp->instance->data_pool, frag->ll.dgram);
|
||||
memory_pool_free(pn->etcp->io_pool, frag);
|
||||
queue_dgram_free(&frag->ll); queue_entry_free(&frag->ll);
|
||||
}
|
||||
|
||||
queue_resume_callback(q);
|
||||
if (!conn->close_requested) queue_resume_callback(q);
|
||||
conn->callbacks_running--;
|
||||
}
|
||||
Reference in new issue
Block a user