From: Arran Cudbard-Bell Date: Fri, 18 Apr 2025 20:43:33 +0000 (-0500) Subject: Fix issue in extendable talloced sbuffs, that would cause the sbuff to slowly shrink X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8bb876f5cbdf188e4c2b5c62247fdceb16786d7;p=thirdparty%2Ffreeradius-server.git Fix issue in extendable talloced sbuffs, that would cause the sbuff to slowly shrink --- diff --git a/src/lib/server/log.c b/src/lib/server/log.c index 80c525d6123..4a77bb213a9 100644 --- a/src/lib/server/log.c +++ b/src/lib/server/log.c @@ -1027,7 +1027,7 @@ void log_request_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, /* * Clear out the existing data */ - fr_sbuff_shift(&sbuff, fr_sbuff_used(&m_start)); + fr_sbuff_shift(&sbuff, fr_sbuff_used(&m_start), false); } } diff --git a/src/lib/tls/log.c b/src/lib/tls/log.c index 48bec199f12..bbab59eed62 100644 --- a/src/lib/tls/log.c +++ b/src/lib/tls/log.c @@ -390,7 +390,7 @@ static int tls_log_request_bio_write_cb(BIO *bio, char const *in, int len) /* * Clear out printed data */ - fr_sbuff_shift(&lb->sbuff, fr_sbuff_used(&lb->logged_m)); + fr_sbuff_shift(&lb->sbuff, fr_sbuff_used(&lb->logged_m), false); return len; /* Amount of data written */ } @@ -474,7 +474,7 @@ static int tls_log_global_bio_write_cb(BIO *bio, char const *in, int len) /* * Clear out printed data */ - fr_sbuff_shift(&lb->sbuff, fr_sbuff_used(&lb->logged_m)); + fr_sbuff_shift(&lb->sbuff, fr_sbuff_used(&lb->logged_m), false); return len; /* Amount of data written */ } diff --git a/src/lib/util/log.c b/src/lib/util/log.c index 7cd26d1de5d..c5a53f7838b 100644 --- a/src/lib/util/log.c +++ b/src/lib/util/log.c @@ -236,7 +236,7 @@ void fr_log_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void /* * Clear out the existing data */ - fr_sbuff_shift(&sbuff, fr_sbuff_used(&m_start)); + fr_sbuff_shift(&sbuff, fr_sbuff_used(&m_start), false); } } diff --git a/src/lib/util/sbuff.c b/src/lib/util/sbuff.c index dd82d642372..342ef71c3a5 100644 --- a/src/lib/util/sbuff.c +++ b/src/lib/util/sbuff.c @@ -185,6 +185,8 @@ void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len) * @param[in] sbuff to shift. * @param[in] shift the contents of the buffer this many bytes * towards the start of the buffer. + * @param[in] move_end If the buffer is used for reading, then this should be true + * so we cannot read passed the end of valid data. * @return * - 0 the shift failed due to constraining pointers. * - >0 the number of bytes we managed to shift pointers @@ -192,7 +194,7 @@ void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len) * existing contents of the buffer, and fill the free * space at the end of the buffer with additional data. */ -size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift) +size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift, bool move_end) { fr_sbuff_t *sbuff_i; char *buff, *end; /* Current start */ @@ -243,7 +245,7 @@ size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift) sbuff_i->start -= min(max_shift, sbuff_i->start - buff); sbuff_i->p -= max_shift; - sbuff_i->end -= max_shift; + if (move_end) sbuff_i->end -= max_shift; sbuff_i->shifted += (max_shift - (start - sbuff_i->start)); for (m_i = sbuff_i->m; m_i; m_i = m_i->next) m_i->p -= max_shift; } @@ -303,7 +305,7 @@ size_t fr_sbuff_extend_file(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, * * Note: p and markers are constraints here. */ - fctx->shifted += fr_sbuff_shift(sbuff, shift); + fctx->shifted += fr_sbuff_shift(sbuff, shift, true); } available = fctx->buff_end - sbuff->end; diff --git a/src/lib/util/sbuff.h b/src/lib/util/sbuff.h index 41a73367dca..7cda384d870 100644 --- a/src/lib/util/sbuff.h +++ b/src/lib/util/sbuff.h @@ -595,7 +595,7 @@ do { \ void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len); -size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift); +size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift, bool move_end); size_t fr_sbuff_extend_file(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension); diff --git a/src/lib/util/sbuff_tests.c b/src/lib/util/sbuff_tests.c index 343203eb20c..cfb2130fd1c 100644 --- a/src/lib/util/sbuff_tests.c +++ b/src/lib/util/sbuff_tests.c @@ -1042,9 +1042,9 @@ static void test_talloc_extend_with_shift(void) TEST_CHECK(fr_sbuff_init_talloc(NULL, &sbuff, &tctx, 4, 8) == &sbuff); TEST_CHECK(fr_sbuff_in_strcpy(&sbuff, "0123") == 4); TEST_CHECK(fr_sbuff_in_strcpy(&sbuff, "5678") == 4); - TEST_CHECK(fr_sbuff_shift(&sbuff, 4) == 4); + TEST_CHECK(fr_sbuff_shift(&sbuff, 4, false) == 4); TEST_CHECK(fr_sbuff_in_strcpy(&sbuff, "AAAA") == 4); - TEST_CHECK(fr_sbuff_shift(&sbuff, 8) == 8); + TEST_CHECK(fr_sbuff_shift(&sbuff, 8, false) == 8); TEST_CHECK(fr_sbuff_in_strcpy(&sbuff, "BBBBBBBB") == 8); talloc_free(sbuff.buff); diff --git a/src/protocols/der/encode.c b/src/protocols/der/encode.c index ef6f1ce808f..f7db2e46f10 100644 --- a/src/protocols/der/encode.c +++ b/src/protocols/der/encode.c @@ -951,7 +951,7 @@ static ssize_t fr_der_encode_utc_time(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, U /* * Remove the century from the year */ - fr_sbuff_shift(&time_sbuff, 2); + fr_sbuff_shift(&time_sbuff, 2, false); /* * Trim the time string of any unwanted characters