This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit 6da2885685478c18f3ad47329d84855f2c1e2388 Author: Andrew Stitcher <[email protected]> AuthorDate: Tue Nov 3 17:25:16 2026 -0500 PROTON-3010: Bound the TLS buffer capacity setters Buffers are addressed by a buff_ptr (index+1 held in a uint16_t), so no array can hold more than UINT16_MAX of them. The four public capacity setters took a size_t and stored it as a size_t (32/64 bits) unchecked. The free list loop for (buff_ptr i = 1; i <= count; i++) could wrap at UINT16_MAX (65535): i returned to 0, the loop never terminated and it wrote through buffers[-1]. Narrow the setters to uint16_t so an out of range capacity cannot be expressed at the call site, add a buff_count typedef for the ten counts bounded by the same range, and rewrite the loop so that it neither wraps at the top nor writes anything for a zero count. This changes the signature of four published functions. The TLS API is documented as unsettled, the library is still SOVERSION 0, and the only known consumer (skupper-router) does not call them. Add tests for these setters. Assisted-By: Claude Opus 5 <[email protected]> --- c/include/proton/tls.h | 10 +++--- c/src/tls/openssl.c | 63 +++++++++++++++++---------------- c/tests/tls_test.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 35 deletions(-) diff --git a/c/include/proton/tls.h b/c/include/proton/tls.h index f5e70ef8d..f1d1f7d95 100644 --- a/c/include/proton/tls.h +++ b/c/include/proton/tls.h @@ -479,10 +479,12 @@ PN_TLS_EXTERN uint32_t pn_tls_get_last_encrypt_output_buffer_size(pn_tls_t*); // Configurable. Zero implies "use default". No effect after pn_tls_start(). -PN_TLS_EXTERN void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t*, size_t s); -PN_TLS_EXTERN void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t*, size_t s); -PN_TLS_EXTERN void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t*, size_t s); -PN_TLS_EXTERN void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t*, size_t s); +// The engine addresses its buffers by a 16 bit index, so UINT16_MAX is the +// largest capacity that can be requested. +PN_TLS_EXTERN void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t*, uint16_t s); +PN_TLS_EXTERN void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t*, uint16_t s); +PN_TLS_EXTERN void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t*, uint16_t s); +PN_TLS_EXTERN void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t*, uint16_t s); // Process as much unencrypted data to encrypted result data as possible. // Also process as much undecrypted data to decryptedcrypted result data as possible. diff --git a/c/src/tls/openssl.c b/c/src/tls/openssl.c index f26a5f3e0..764123db9 100644 --- a/c/src/tls/openssl.c +++ b/c/src/tls/openssl.c @@ -95,6 +95,7 @@ typedef enum { typedef uint16_t buff_ptr; // This is always the index+1 so that 0 can be special +typedef buff_ptr buff_count; // A number of buffers; bounded by the largest buff_ptr typedef struct pbuffer_t { uintptr_t context; @@ -138,17 +139,17 @@ struct pn_tls_t { pbuffer_t *dresult_buffers; pbuffer_t *encrypt_buffers; pbuffer_t *decrypt_buffers; - size_t eresult_buffer_count; - size_t dresult_buffer_count; - size_t encrypt_buffer_count; - size_t decrypt_buffer_count; - - uint16_t encrypt_buffer_empty_count; - uint16_t decrypt_buffer_empty_count; - uint16_t eresult_encrypted_count; - uint16_t eresult_empty_count; - uint16_t dresult_decrypted_count; - uint16_t dresult_empty_count; + buff_count eresult_buffer_count; + buff_count dresult_buffer_count; + buff_count encrypt_buffer_count; + buff_count decrypt_buffer_count; + + buff_count encrypt_buffer_empty_count; + buff_count decrypt_buffer_empty_count; + buff_count eresult_encrypted_count; + buff_count eresult_empty_count; + buff_count dresult_decrypted_count; + buff_count dresult_empty_count; // Lists within encrypt_buffers buff_ptr encrypt_first_empty; // unordered: no encryp_last_empty @@ -223,24 +224,24 @@ static int pn_tls_alpn_cb(SSL *ssn, const unsigned char **out, unsigned char *ou static void release_buffers(pn_tls_t * ); +// Chain count buffers into a single free list; a count of zero yields an empty list. +// Note that i is compared against count rather than running to it inclusive, so it +// cannot wrap when count is the largest value a buff_count can hold. +static void initialize_free_list(pbuffer_t *buffers, buff_count count) { + assert(count); // Callers always supply at least the default number of buffers. + for (buff_count i = 0; i < count; i++) { + // next is a buff_ptr (index+1), so buffer i chains to i+2 and the last ends the list. + buffers[i].next = (i + 1 == count) ? 0 : i + 2; + buffers[i].type = buff_empty; + } +} + static void tls_initialize_buffers(pn_tls_t *tls) { // Link together free lists - for (buff_ptr i = 1; i<=tls->encrypt_buffer_count; i++) { - tls->encrypt_buffers[i-1].next = i==tls->encrypt_buffer_count ? 0 : i+1; - tls->encrypt_buffers[i-1].type = buff_empty; - } - for (buff_ptr i = 1; i<=tls->decrypt_buffer_count; i++) { - tls->decrypt_buffers[i-1].next = i==tls->decrypt_buffer_count ? 0 : i+1; - tls->decrypt_buffers[i-1].type = buff_empty; - } - for (buff_ptr i = 1; i<=tls->eresult_buffer_count; i++) { - tls->eresult_buffers[i-1].next = i==tls->eresult_buffer_count ? 0 : i+1; - tls->eresult_buffers[i-1].type = buff_empty; - } - for (buff_ptr i = 1; i<=tls->dresult_buffer_count; i++) { - tls->dresult_buffers[i-1].next = i==tls->dresult_buffer_count ? 0 : i+1; - tls->dresult_buffers[i-1].type = buff_empty; - } + initialize_free_list(tls->encrypt_buffers, tls->encrypt_buffer_count); + initialize_free_list(tls->decrypt_buffers, tls->decrypt_buffer_count); + initialize_free_list(tls->eresult_buffers, tls->eresult_buffer_count); + initialize_free_list(tls->dresult_buffers, tls->dresult_buffer_count); tls->encrypt_buffer_empty_count = tls->encrypt_buffer_count; tls->decrypt_buffer_empty_count = tls->decrypt_buffer_count; @@ -2185,16 +2186,16 @@ bool pn_tls_is_decrypt_output_pending(pn_tls_t *tls) } -void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t *tls, size_t s) { +void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t *tls, uint16_t s) { if (!tls->started) tls->encrypt_buffer_count = s; } -void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t *tls, size_t s) { +void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t *tls, uint16_t s) { if (!tls->started) tls->decrypt_buffer_count = s; } -void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t *tls, size_t s) { +void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t *tls, uint16_t s) { if (!tls->started) tls->eresult_buffer_count = s; } -void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t *tls, size_t s) { +void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t *tls, uint16_t s) { if (!tls->started) tls->dresult_buffer_count = s; } diff --git a/c/tests/tls_test.cpp b/c/tests/tls_test.cpp index fab25a2cc..1e009bad2 100644 --- a/c/tests/tls_test.cpp +++ b/c/tests/tls_test.cpp @@ -29,8 +29,10 @@ #include <errno.h> #endif +#include <cstdint> #include <cstring> #include <filesystem> +#include <vector> using namespace pn_test; using Catch::Matchers::Contains; @@ -548,3 +550,96 @@ TEST_CASE("buffer release on pn_tls_stop()") { REQUIRE(take_count == give_count); } + +// The engine addresses buffers with a 16 bit index, so UINT16_MAX is both the +// largest capacity a caller can request and the boundary at which the free list +// construction in tls_initialize_buffers() can overflow that index. +TEST_CASE("maximum buffer capacity") { + TestPeer client(false); + client.config = default_client_config(); + REQUIRE(client.config); + client.tls = pn_tls(client.config); + REQUIRE(client.tls); + pn_tls_t *tls = client.tls; + + pn_tls_set_encrypt_input_buffer_max_capacity(tls, UINT16_MAX); + pn_tls_set_decrypt_input_buffer_max_capacity(tls, UINT16_MAX); + pn_tls_set_encrypt_output_buffer_max_capacity(tls, UINT16_MAX); + pn_tls_set_decrypt_output_buffer_max_capacity(tls, UINT16_MAX); + + client.init(); // pn_tls_start(): builds the four free lists + + REQUIRE(pn_tls_get_encrypt_input_buffer_capacity(tls) == UINT16_MAX); + REQUIRE(pn_tls_get_decrypt_input_buffer_capacity(tls) == UINT16_MAX); + REQUIRE(pn_tls_get_encrypt_output_buffer_capacity(tls) == UINT16_MAX); + REQUIRE(pn_tls_get_decrypt_output_buffer_capacity(tls) == UINT16_MAX); + + // Walk the whole free list: all UINT16_MAX buffers must be accepted, which + // only holds if every link including the terminator was built correctly. + std::vector<pn_raw_buffer_t> bufs(UINT16_MAX); + for (auto &b : bufs) b = new_rbuf(1); + REQUIRE(pn_tls_give_encrypt_input_buffers(tls, bufs.data(), bufs.size()) == UINT16_MAX); + REQUIRE(pn_tls_get_encrypt_input_buffer_capacity(tls) == 0); + + // A full list accepts nothing more. + pn_raw_buffer_t extra = new_rbuf(1); + REQUIRE(pn_tls_give_encrypt_input_buffers(tls, &extra, 1) == 0); + free_rbuf(extra); + + pn_tls_stop(tls); + size_t returned = 0; + while (size_t n = pn_tls_take_encrypt_input_buffers(tls, bufs.data(), bufs.size())) { + for (size_t i = 0; i < n; i++) free_rbuf(bufs[i]); + returned += n; + } + REQUIRE(returned == UINT16_MAX); +} + +// Zero means "use the default", so a zero capacity must not leave the engine +// with no buffers at all. +TEST_CASE("zero buffer capacity means default") { + TestPeer client(false); + client.config = default_client_config(); + REQUIRE(client.config); + client.tls = pn_tls(client.config); + REQUIRE(client.tls); + pn_tls_t *tls = client.tls; + + pn_tls_set_encrypt_input_buffer_max_capacity(tls, 0); + pn_tls_set_decrypt_input_buffer_max_capacity(tls, 0); + pn_tls_set_encrypt_output_buffer_max_capacity(tls, 0); + pn_tls_set_decrypt_output_buffer_max_capacity(tls, 0); + + client.init(); + + // The documented default is 4 of each. + REQUIRE(pn_tls_get_encrypt_input_buffer_capacity(tls) == 4); + REQUIRE(pn_tls_get_decrypt_input_buffer_capacity(tls) == 4); + REQUIRE(pn_tls_get_encrypt_output_buffer_capacity(tls) == 4); + REQUIRE(pn_tls_get_decrypt_output_buffer_capacity(tls) == 4); +} + +// A capacity of one exercises the shortest possible free list: a single buffer +// that must both start and terminate the chain. +TEST_CASE("single buffer capacity") { + TestPeer client(false); + client.config = default_client_config(); + REQUIRE(client.config); + client.tls = pn_tls(client.config); + REQUIRE(client.tls); + pn_tls_t *tls = client.tls; + + pn_tls_set_encrypt_input_buffer_max_capacity(tls, 1); + client.init(); + + REQUIRE(pn_tls_get_encrypt_input_buffer_capacity(tls) == 1); + + pn_raw_buffer_t bufs[2] = {new_rbuf(1), new_rbuf(1)}; + REQUIRE(pn_tls_give_encrypt_input_buffers(tls, bufs, 2) == 1); // only one fits + REQUIRE(pn_tls_get_encrypt_input_buffer_capacity(tls) == 0); + free_rbuf(bufs[1]); + + pn_tls_stop(tls); + REQUIRE(pn_tls_take_encrypt_input_buffers(tls, bufs, 2) == 1); + free_rbuf(bufs[0]); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
