linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: linux-man@vger.kernel.org
Cc: Alejandro Colomar <alx@kernel.org>, serge@hallyn.com
Subject: [PATCH 0/2] Split *s*printf(3) from printf(3), and document the snprintf(,-1,) is UB
Date: Fri, 5 Dec 2025 21:14:08 +0100	[thread overview]
Message-ID: <cover.1764964289.git.alx@kernel.org> (raw)

Hi!

Serge and I were reviewing some code, and we realized that it seems
quite easy to induce a buffer overflow using snprintf(3).

We solved our problems by checking that the size is non-negative in
a wrapper inline function.

	// vstprintf - va_list string truncate print formatted
	inline int
	vstprintf(char *restrict s, ssize_t size, const char *restrict fmt, va_list ap)
	{
		int  len;

		if (size <= 0 || size > INT_MAX) {
			errno = EOVERFLOW;
			return -1;
		}
		len = vsnprintf(s, size, fmt, ap);
		if (len == -1) {
			if (errno == EOVERFLOW)
				errno = E2BIG;
			return -1;
		}
		if (len >= size) {
			errno = E2BIG;
			return -1;
		}

		return len;
	}

[v]snprintf(3) seems to be even worse than I thought it was.
Let's document this.


Alejandro Colomar (2):
  man/man3/*printf.3: Split *s*printf(3) from printf(3)
  man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is
    essentially UB

 man/man3/printf.3    | 223 +---------------------------
 man/man3/snprintf.3  | 337 ++++++++++++++++++++++++++++++++++++++++++-
 man/man3/sprintf.3   |   2 +-
 man/man3/vsnprintf.3 |   2 +-
 man/man3/vsprintf.3  |   2 +-
 5 files changed, 346 insertions(+), 220 deletions(-)

Range-diff:
-:  --------- > 1:  a654dcfc2 man/man3/*printf.3: Split *s*printf(3) from printf(3)
-:  --------- > 2:  d00ed9434 man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is essentially UB

base-commit: 2a5edb7e59ae024d1969e7379c078a58b5f50b80
-- 
2.51.0


             reply	other threads:[~2025-12-05 20:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-05 20:14 Alejandro Colomar [this message]
2025-12-05 20:14 ` [PATCH 1/2] man/man3/*printf.3: Split *s*printf(3) from printf(3) Alejandro Colomar
2025-12-05 20:14 ` [PATCH 2/2] man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is essentially UB Alejandro Colomar
2025-12-05 20:46   ` Alejandro Colomar
2025-12-06 13:32   ` Alejandro Colomar
2025-12-06 17:33     ` Serge Hallyn
2025-12-06 17:45       ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1764964289.git.alx@kernel.org \
    --to=alx@kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=serge@hallyn.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).