An off-by-one error exists in sbuf_vprintf()'s use of SBUF_HASROOM() when an
authorlstewart <lstewart@FreeBSD.org>
Fri, 18 Aug 2017 02:06:28 +0000 (02:06 +0000)
committerlstewart <lstewart@FreeBSD.org>
Fri, 18 Aug 2017 02:06:28 +0000 (02:06 +0000)
commit5b9d72a33b5232ebdf492f85a0cf0206d950fe73
treea8a043334d777b568e297a7a3c86def0b177638e
parentb2837014591b6bc2351c6a0c194bd9b0c9d1917b
An off-by-one error exists in sbuf_vprintf()'s use of SBUF_HASROOM() when an
sbuf is filled to capacity by vsnprintf(), the loop exits without error, and
the sbuf is not marked as auto-extendable.

SBUF_HASROOM() evaluates true if there is room for one or more non-NULL
characters, but in the case that the sbuf was filled exactly to capacity,
SBUF_HASROOM() evaluates false. Consequently, sbuf_vprintf() incorrectly
assigns an ENOMEM error to the sbuf when in fact everything is fine, in turn
poisoning the buffer for all subsequent operations.

Correct by moving the ENOMEM assignment into the loop where it can be made
unambiguously.

As a related safety net change, explicitly check for the zero bytes drained
case in sbuf_drain() and set EDEADLK as the error. This avoids an infinite loop
in sbuf_vprintf() if a drain function were to inadvertently return a value of
zero to sbuf_drain().

Reviewed by: cem, jtl, gallatin
MFC after: 2 weeks
Sponsored by: Netflix, Inc.
Differential Revision: https://reviews.freebsd.org/D8535
sys/kern/subr_sbuf.c