From: Willy Tarreau Date: Mon, 16 Nov 2026 15:21:26 +0000 (+0100) Subject: BUG/MAJOR: buf: make the shift distance check less strict in b_move() X-Git-Tag: v3.5-dev13~25 X-Git-Url: https://git.kaiwu.me/web//%22?a=commitdiff_plain;h=91f333d5807e4f92e6dd05de4e2738592d28307f;p=haproxy.git BUG/MAJOR: buf: make the shift distance check less strict in b_move() 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. --- diff --git a/src/buf.c b/src/buf.c index 32388d6d3..6dd592eb9 100644 --- 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 bytes from absolute position of buffer by * 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 '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 's absolute value plus the 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;