git: 3a2ea31568c8 - stable/12 - fflush: correct buffer handling in __sflush

From: Ed Maste <>
Date: Tue, 07 Nov 2023 14:29:47 UTC
The branch stable/12 has been updated by emaste:


commit 3a2ea31568c81f7b29710abd3d3e1ada2fbeb6c9
Author:     Dag-Erling Smørgrav <>
AuthorDate: 2023-08-03 15:13:45 +0000
Commit:     Ed Maste <>
CommitDate: 2023-11-07 14:11:54 +0000

    fflush: correct buffer handling in __sflush
    This fixes CVE-2014-8611 correctly.
    The commit that purported to fix CVE-2014-8611 (805288c2f062) only hid
    it behind another bug.  Two later commits, 86a16ada1ea6 and
    44cf1e5eb470, attempted to address this new bug but mostly just confused
    the issue.  This commit rolls back the three previous changes and fixes
    CVE-2014-8611 correctly.
    The key to understanding the bug (and the fix) is that `_w` has
    different meanings for different stream modes.  If the stream is
    unbuffered, it is always zero.  If the stream is fully buffered, it is
    the amount of space remaining in the buffer (equal to the buffer size
    when the buffer is empty and zero when the buffer is full).  If the
    stream is line-buffered, it is a negative number reflecting the amount
    of data in the buffer (zero when the buffer is empty and negative buffer
    size when the buffer is full).
    At the heart of `fflush()`, we call the stream's write function in a
    loop, where `t` represents the return value from the last call and `n`
    the amount of data that remains to be written.  When the write function
    fails, we need to move the unwritten data to the top of the buffer
    (unless nothing was written) and adjust `_p` (which points to the next
    free location in the buffer) and `_w` accordingly.  These variables have
    already been set to the values they should have after a successful
    flush, so instead of adjusting them down to reflect what was written,
    we're adjusting them up to reflect what remains.
    The bug was that while `_p` was always adjusted, we only adjusted `_w`
    if the stream was fully buffered.  The fix is to also adjust `_w` for
    line-buffered streams.  Everything else is just noise.
    Fixes: 805288c2f062
    Fixes: 86a16ada1ea6
    Fixes: 44cf1e5eb470
    Sponsored by:   Klara, Inc.
    (cherry picked from commit d09a3bf72c0b5f1779c52269671872368c99f02a)
    (cherry picked from commit 92709431b14df6c0687446247ac57cfc189ee827)
    (cherry picked from commit 6cb5690b3495741e9ece6f42ba4a85732932aa83)
    Approved by:    so
 lib/libc/stdio/fflush.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c
index 05e587ecec66..75f145fae6a3 100644
--- a/lib/libc/stdio/fflush.c
+++ b/lib/libc/stdio/fflush.c
@@ -127,14 +127,13 @@ __sflush(FILE *fp)
 	for (; n > 0; n -= t, p += t) {
 		t = _swrite(fp, (char *)p, n);
 		if (t <= 0) {
-			/* Reset _p and _w. */
-			if (p > fp->_p) {
+			if (p > fp->_p)
 				/* Some was written. */
 				memmove(fp->_p, p, n);
-				fp->_p += n;
-				if ((fp->_flags & (__SLBF | __SNBF)) == 0)
-					fp->_w -= n;
-			}
+			/* Reset _p and _w. */
+			fp->_p += n;
+			if ((fp->_flags & __SNBF) == 0)
+				fp->_w -= n;
 			fp->_flags |= __SERR;
 			return (EOF);