summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton <anton@picapica.im>2026-11-11 06:50:53 +0400
committerAnton <anton@picapica.im>2026-11-11 06:50:53 +0400
commitd49be45ae3a3abb2503bd4d9f1577381991fd67f (patch)
treec95f4d36141413568afe35bacc20aa3d415ec7a7
parent6baf0cced94e4ce5281fae9c90191dc8b703bb6e (diff)
downloadpica-pica-d49be45ae3a3abb2503bd4d9f1577381991fd67f.tar.gz
pica-pica-d49be45ae3a3abb2503bd4d9f1577381991fd67f.zip
pica-client: fix use after free and introduce timeouts
-rw-r--r--PICA_client.c78
-rw-r--r--PICA_client.h13
-rw-r--r--PICA_media.c11
3 files changed, 82 insertions, 20 deletions
diff --git a/PICA_client.c b/PICA_client.c
index d213e55..cf153c2 100644
--- a/PICA_client.c
+++ b/PICA_client.c
@@ -2861,8 +2861,17 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
while(d)
{
+ struct PICA_directc2c *dnext = d->next;
+
if (d->state == PICA_DIRECTC2C_CONNSTATE_ACTIVE)
{
+ /* d leaves the listener list either way - adopted by a c2c or
+ * closed. Splice it out first, using the next pointer captured
+ * above, so nothing is read back out of a freed node.
+ */
+ *pdprev = dnext;
+ d->next = NULL;
+
if ((c2c = find_matching_c2c(c2n, d)) && (PICA_verify_peer_cert_common(&d->peer_cert, d->ssl, c2c->peer_id) == PICA_OK))
{
if (c2c->direct)
@@ -2875,6 +2884,7 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (ret != PICA_OK)
{
PICA_close_directc2c(d);
+ c2c->direct = NULL;
c2c->directc2c_state = PICA_DIRECTC2C_STATE_INACTIVE;
}
else
@@ -2882,21 +2892,19 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
callbacks.direct_c2c_established_cb(c2c->peer_id, SSL_get_cipher_name(d->ssl));
c2c->directc2c_state = PICA_DIRECTC2C_STATE_ACTIVE;
}
-
- //remove from listener's list
- *pdprev = d->next;
- d->next = 0;
}
else
{
- //close and remove from listener's list
- *pdprev = d->next;
PICA_close_directc2c(d);
}
+ /* pdprev stays put: d was spliced out above */
}
- pdprev = &d->next;
- d = d->next;
- //-- killptr?
+ else
+ {
+ pdprev = &d->next;
+ }
+
+ d = dnext;
}
}
//process outgoing connections
@@ -3016,10 +3024,27 @@ static void listener_add_connection(struct PICA_listener *lst, SOCKET s, const s
{
PICA_TRACEFUNC
struct PICA_directc2c *nc;
+ unsigned int count = 0;
int ret;
+ /* Bound the number of half-open incoming connections, so a peer cannot
+ * exhaust memory/descriptors by opening many and never finishing them.
+ */
+ for (nc = lst->accepted_connections; nc; nc = nc->next)
+ if (++count >= PICA_DIRECTC2C_MAX_ACCEPTED)
+ {
+ CLOSE(s);
+ return;
+ }
+
nc = calloc(1, sizeof(struct PICA_directc2c));
+ if (!nc)
+ {
+ CLOSE(s);
+ return;
+ }
+
nc->is_outgoing = PICA_DIRECTC2C_INCOMING;
nc->sck = s;
/* Kept for the media connection: it is the only place the peer's address
@@ -3030,6 +3055,14 @@ static void listener_add_connection(struct PICA_listener *lst, SOCKET s, const s
nc->addr = *peer_addr;
nc->ssl = SSL_new(lst->acc->ctx);
nc->state = PICA_DIRECTC2C_CONNSTATE_NEW;
+ nc->accepted_tmst = time(0);
+
+ if (!nc->ssl)
+ {
+ CLOSE(s);
+ free(nc);
+ return;
+ }
SSL_set_fd(nc->ssl, nc->sck);
@@ -3038,18 +3071,17 @@ static void listener_add_connection(struct PICA_listener *lst, SOCKET s, const s
ret = SSL_accept(nc->ssl);
- if (ret == 0)
+ if (ret <= 0)
{
- free(nc);
- return;
- }
-
- if (ret < 0)
- {
- ret = SSL_get_error(nc->ssl, ret);
-
- if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE)
+ /* Only a WANT_READ/WANT_WRITE means the handshake is merely unfinished;
+ * anything else is a real failure. Free the SSL object and close the
+ * socket on the failure path - dropping only the struct would leak both.
+ */
+ if (!(ret < 0 && (SSL_get_error(nc->ssl, ret) == SSL_ERROR_WANT_READ
+ || SSL_get_error(nc->ssl, ret) == SSL_ERROR_WANT_WRITE)))
{
+ SSL_free(nc->ssl);
+ CLOSE(nc->sck);
free(nc);
return;
}
@@ -3134,6 +3166,14 @@ static int process_listener(struct PICA_listener *lst, fd_set *rfds, fd_set *wfd
{
kill_ptr = conn;
}
+ else if (conn->peer_cert == NULL
+ && (time(0) - conn->accepted_tmst) > PICA_DIRECTC2C_ACCEPT_TIMEOUT)
+ {
+ /* peer_cert is set only once the handshake completes, so a NULL
+ * one past the timeout is a connection that never finished TLS.
+ */
+ kill_ptr = conn;
+ }
if (!kill_ptr)
pprevconn = &conn->next;
diff --git a/PICA_client.h b/PICA_client.h
index 22acafc..2c0b95f 100644
--- a/PICA_client.h
+++ b/PICA_client.h
@@ -142,6 +142,13 @@ typedef u_short in_port_t;
#define PICA_C2C_ACTIVATE_TIMEOUT 30
+/* How long an incoming direct connection may take to finish its TLS handshake
+ * before it is reaped, and how many half-open incoming connections may sit in
+ * a listener at once - both bound a peer's ability to tie up the listener.
+ */
+#define PICA_DIRECTC2C_ACCEPT_TIMEOUT 30
+#define PICA_DIRECTC2C_MAX_ACCEPTED 32
+
#define PICA_C2C_INCOMING 0
#define PICA_C2C_OUTGOING 1
@@ -270,6 +277,12 @@ struct PICA_directc2c
enum PICA_directc2c_connection_state state;
+ /* When this incoming connection was accepted, used to reap it if its TLS
+ * handshake never completes. Only meaningful for accepted (incoming)
+ * connections held in a listener's accepted_connections list.
+ */
+ time_t accepted_tmst;
+
struct PICA_directc2c *next;
};
diff --git a/PICA_media.c b/PICA_media.c
index b0f33cf..6d68c0b 100644
--- a/PICA_media.c
+++ b/PICA_media.c
@@ -515,6 +515,7 @@ static void media_accept(struct PICA_media_channel *m)
static void media_read(struct PICA_media_channel *m)
{
+ struct PICA_c2c *c2c = m->c2c;
int ret;
/* One SSL_read() per datagram, so this loop runs until the socket has
@@ -547,7 +548,15 @@ static void media_read(struct PICA_media_channel *m)
* on a c2c connection, bad data here says nothing about the peer,
* anyone able to reach the port can send it.
*/
- PICA_media_process_messages(m->read_buf, &m->read_pos, m->c2c);
+ PICA_media_process_messages(m->read_buf, &m->read_pos, c2c);
+
+ /* Processing a datagram can close this media channel (e.g. a failed
+ * ping reply calls media_fail(), which frees m). If it did, m is now
+ * dangling - stop before touching it again. m is only compared as an
+ * address here, never dereferenced.
+ */
+ if (c2c->media != m)
+ return;
m->read_pos = 0;
}