]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Fix issue in extendable talloced sbuffs, that would cause the sbuff to slowly shrink
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 18 Apr 2025 20:43:33 +0000 (15:43 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 18 Apr 2025 20:43:33 +0000 (15:43 -0500)
src/lib/server/log.c
src/lib/tls/log.c
src/lib/util/log.c
src/lib/util/sbuff.c
src/lib/util/sbuff.h
src/lib/util/sbuff_tests.c
src/protocols/der/encode.c

index 80c525d612317817f82603d2de1ae935656aa6b8..4a77bb213a9ec46b6fdbf2757fca9ca280a3558f 100644 (file)
@@ -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);
        }
 }
 
index 48bec199f12dc81790e445c135218d3dc96241df..bbab59eed62c0b6fef2babebb2c17edd43fe575e 100644 (file)
@@ -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 */
 }
index 7cd26d1de5d5e9760b52794fab523aab72c053a6..c5a53f7838b68feb45a5710de6f9b89dc55fd90e 100644 (file)
@@ -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);
        }
 }
 
index dd82d642372d68be6168e9b692a18e8b2d0476b4..342ef71c3a55c9d438f3f1d8d3ab48a89b72e8ab 100644 (file)
@@ -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;
index 41a73367dca4b9a990d58102cae7d8ad430cc678..7cda384d870360818d6c2a60625e8df635c62844 100644 (file)
@@ -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);
 
index 343203eb20cedbb407addff346f7785af2471218..cfb2130fd1c8f7bc3a1f812e05e4715ccb33a3ed 100644 (file)
@@ -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);
index ef6f1ce808f5aebecdac92a8c814f067e8865a39..f7db2e46f103d252039f4409d9b0e9b4679d0ca2 100644 (file)
@@ -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