Fix raw TLS overflow for large uploads

This commit is contained in:
Sergey Lyubka authored and Sergio R. Caprile committed 2024-07-03 16:40:04 -03:00
1 parent 5ad2b0531a
commit 509fbcc4f8
2 files changed
+10 -2

No files matched your search

+5 -1
View File
@@ -7522,6 +7522,9 @@ static void read_conn(struct mg_connection *c) {
size_t len = c->recv.size - c->recv.len; size_t len = c->recv.size - c->recv.len;
long n = -1; long n = -1;
if (c->is_tls) { if (c->is_tls) {
// Do not read to the raw TLS buffer if it already has enough.
// This is to prevent overflowing c->rtls if our reads are slow
if (c->rtls.len < 16 * 1024) { // 16k is the max TLS message size
if (!ioalloc(c, &c->rtls)) return; if (!ioalloc(c, &c->rtls)) return;
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
c->rtls.size - c->rtls.len); c->rtls.size - c->rtls.len);
@@ -7531,8 +7534,9 @@ static void read_conn(struct mg_connection *c) {
} else { } else {
if (n > 0) c->rtls.len += (size_t) n; if (n > 0) c->rtls.len += (size_t) n;
if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) mg_tls_handshake(c);
n = c->is_tls_hs ? (long) MG_IO_WAIT : mg_tls_recv(c, buf, len);
} }
}
n = c->is_tls_hs ? (long) MG_IO_WAIT : mg_tls_recv(c, buf, len);
} else { } else {
n = recv_raw(c, buf, len); n = recv_raw(c, buf, len);
} }
+5 -1
View File
@@ -276,6 +276,9 @@ static void read_conn(struct mg_connection *c) {
size_t len = c->recv.size - c->recv.len; size_t len = c->recv.size - c->recv.len;
long n = -1; long n = -1;
if (c->is_tls) { if (c->is_tls) {
// Do not read to the raw TLS buffer if it already has enough.
// This is to prevent overflowing c->rtls if our reads are slow
if (c->rtls.len < 16 * 1024) { // 16k is the max TLS message size
if (!ioalloc(c, &c->rtls)) return; if (!ioalloc(c, &c->rtls)) return;
n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len], n = recv_raw(c, (char *) &c->rtls.buf[c->rtls.len],
c->rtls.size - c->rtls.len); c->rtls.size - c->rtls.len);
@@ -285,8 +288,9 @@ static void read_conn(struct mg_connection *c) {
} else { } else {
if (n > 0) c->rtls.len += (size_t) n; if (n > 0) c->rtls.len += (size_t) n;
if (c->is_tls_hs) mg_tls_handshake(c); if (c->is_tls_hs) mg_tls_handshake(c);
n = c->is_tls_hs ? (long) MG_IO_WAIT : mg_tls_recv(c, buf, len);
} }
}
n = c->is_tls_hs ? (long) MG_IO_WAIT : mg_tls_recv(c, buf, len);
} else { } else {
n = recv_raw(c, buf, len); n = recv_raw(c, buf, len);
} }