From 67f27a908fdc4f24ca9ac52572ac000a5738187f Mon Sep 17 00:00:00 2001 From: boreddevnl Date: Tue, 21 Apr 2026 16:40:53 +0200 Subject: [PATCH] fix(net): guard network_cleanup() with TCP connection ownership token --- src/net/network.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/net/network.c b/src/net/network.c index 42a1630..16340d3 100644 --- a/src/net/network.c +++ b/src/net/network.c @@ -24,6 +24,7 @@ static struct pbuf *tcp_recv_queue = NULL; static int tcp_connect_done = 0; static int tcp_connect_error = 0; static int tcp_closed = 0; +static uint32_t tcp_owner_pid = 0; static err_t tcp_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { (void)arg; (void)tpcb; (void)err; @@ -158,6 +159,10 @@ void network_force_unlock(void) { } void network_cleanup(void) { + extern uint32_t process_get_current_pid(void); + uint32_t my_pid = process_get_current_pid(); + if (tcp_owner_pid != 0 && tcp_owner_pid != my_pid) return; + asm volatile("cli"); if (tcp_recv_queue) { pbuf_free(tcp_recv_queue); @@ -167,6 +172,7 @@ void network_cleanup(void) { tcp_abort(current_tcp_pcb); current_tcp_pcb = NULL; } + tcp_owner_pid = 0; network_processing = 0; asm volatile("sti"); } @@ -208,6 +214,9 @@ int network_tcp_connect(const ipv4_address_t *ip, uint16_t port) { current_tcp_pcb = tcp_new(); if (!current_tcp_pcb) { network_processing = 0; return -1; } + + extern uint32_t process_get_current_pid(void); + tcp_owner_pid = process_get_current_pid(); ip4_addr_t dest_addr; IP4_ADDR(&dest_addr, ip->bytes[0], ip->bytes[1], ip->bytes[2], ip->bytes[3]);