]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MAJOR: buf: make the shift distance check less strict in b_move()
authorWilly Tarreau <w@1wt.eu>
Mon, 16 Nov 2026 15:21:26 +0000 (16:21 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 17:38:15 +0000 (18:38 +0100)
Yuqi Qiu and Xiang Li from Nankai University (AOSP Lab) found during
code analysis that the check "BUG_ON(-shift > size)" in b_move() is
incorrect and can trigger too easily. The test as written like this
to only consider a one-byte minimum move, but the function could also
be called either with a larger move and a large offset, or with no data
and a larger move, typically the result of a full buffer deletion. What
matters is that the moved data plus the shift do not go larger than the
buffer.

Let's fix the test to consider both the length and the distance to save
it from triggering too easily.

The test was added in 2.6-dev3 with commit 8873b85bd9 ("DEBUG: buf: add
BUG_ON_HOT() to most buffer management functions") so this fix must be
backported to all stable versions.

src/buf.c

index 32388d6d389bb22227814f3b24d99853bbac761d..6dd592eb9faaf08e6bc792e2751a0f682cd2ac85 100644 (file)
--- a/src/buf.c
+++ b/src/buf.c
@@ -409,11 +409,11 @@ size_t b_ncat(struct buffer *dst, const struct buffer *src, size_t count)
 /* Moves <len> bytes from absolute position <src> of buffer <b> by <shift>
  * bytes, while supporting wrapping of both the source and the destination.
  * The position is relative to the buffer's origin and may overlap with the
- * target position. The <shift>'s absolute value must be strictly lower than
- * the buffer's size. The main purpose is to aggregate data block during
- * parsing while removing unused delimiters. The buffer's length is not
- * modified, and the caller must take care of size adjustments and holes by
- * itself.
+ * target position. The <shift>'s absolute value plus the <len> being moved
+ * must be lower than or equal to the buffer's size. The main purpose is to
+ * aggregate data block during parsing while removing unused delimiters. The
+ * buffer's length is not modified, and the caller must take care of size
+ * adjustments and holes by itself.
  */
 void b_move(const struct buffer *b, size_t src, size_t len, ssize_t shift)
 {
@@ -428,7 +428,7 @@ void b_move(const struct buffer *b, size_t src, size_t len, ssize_t shift)
                dst -= size;
 
        if (shift < 0) {
-               BUG_ON(-shift >= size);
+               BUG_ON(-shift + len > size);
                /* copy from left to right */
                for (; (cnt = len); len -= cnt) {
                        if (cnt > size - src)
@@ -446,7 +446,7 @@ void b_move(const struct buffer *b, size_t src, size_t len, ssize_t shift)
                }
        }
        else if (shift > 0) {
-               BUG_ON(shift >= size);
+               BUG_ON(shift + len > size);
                /* copy from right to left */
                for (; (cnt = len); len -= cnt) {
                        size_t src_end = src + len;