Bug 14771 - add length sanity check to snprintf
Summary: add length sanity check to snprintf
Status: NEW
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 29379 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-10-26 10:06 UTC by Florian Weimer
Modified: 2022-07-19 17:27 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Weimer 2012-10-26 10:06:34 UTC
We should reject impossibly large size arguments for snprintf, vsprintf.  This is similar to bug 13592.  Passing (size_t)-1 to snprintf to emulate the sprintf behavior might actually be valid code, so this would have to be restricted to -D_FORTIFY_SOURCE mode.

This is prompted by <https://lists.exim.org/lurker/message/20121026.080330.74b9147b.en.html> (CVE-2012-5671).
Comment 1 Ondrej Bilka 2013-10-08 15:55:39 UTC
Did you post a patch since sending this bug?
Comment 2 Rich Felker 2013-10-08 23:07:28 UTC
snprintf is required by POSIX to return a negative value and set errno to EOVERFLOW if the n argument is greater than INT_MAX. Actually I find it difficult to see how this requirement is compatible with ISO C, which makes no such requirement or allowance for what would otherwise be a spurious error, so perhaps this should be filed as a bug against POSIX; an interpretation is needed, at least. But assuming the requirement in POSIX stands, it's a bug for glibc not to report an error when n is greater than INT_MAX.
Comment 3 Rich Felker 2013-10-08 23:18:16 UTC
I've reported the issue with the possible conflict between the standards on the Austin Group tracker here:

http://austingroupbugs.net/view.php?id=761
Comment 4 Florian Weimer 2013-10-18 11:24:48 UTC
The Austin Group has decided not to updated POSIX.
Comment 5 Florian Weimer 2013-10-18 12:31:21 UTC
Patch posted: https://sourceware.org/ml/libc-alpha/2013-10/msg00630.html
Comment 6 jsm-csl@polyomino.org.uk 2013-10-18 13:52:42 UTC
The Austin Group appears to have failed to address the conflict with C11 
semantics.  Is the Austin Group / WG14 liaison taking this up with WG14, 
if the Austin Group view is that the C11 specifications are defective?

A fortification check obviously doesn't address the POSIX semantics, so a 
separate bug would need opening for those if this one is considered to be 
about fortification only.
Comment 7 Joseph Myers 2022-07-19 17:27:44 UTC
*** Bug 29379 has been marked as a duplicate of this bug. ***