summaryrefslogtreecommitdiff
path: root/src/port/snprintf.c
AgeCommit message (Collapse)Author
2015-05-19Revert error-throwing wrappers for the printf family of functions.Tom Lane
This reverts commit 16304a013432931e61e623c8d85e9fe24709d9ba, except for its changes in src/port/snprintf.c; as well as commit cac18a76bb6b08f1ecc2a85e46c9d2ab82dd9d23 which is no longer needed. Fujii Masao reported that the previous commit caused failures in psql on OS X, since if one exits the pager program early while viewing a query result, psql sees an EPIPE error from fprintf --- and the wrapper function thought that was reason to panic. (It's a bit surprising that the same does not happen on Linux.) Further discussion among the security list concluded that the risk of other such failures was far too great, and that the one-size-fits-all approach to error handling embodied in the previous patch is unlikely to be workable. This leaves us again exposed to the possibility of the type of failure envisioned in CVE-2015-3166. However, that failure mode is strictly hypothetical at this point: there is no concrete reason to believe that an attacker could trigger information disclosure through the supposed mechanism. In the first place, the attack surface is fairly limited, since so much of what the backend does with format strings goes through stringinfo.c or psprintf(), and those already had adequate defenses. In the second place, even granting that an unprivileged attacker could control the occurrence of ENOMEM with some precision, it's a stretch to believe that he could induce it just where the target buffer contains some valuable information. So we concluded that the risk of non-hypothetical problems induced by the patch greatly outweighs the security risks. We will therefore revert, and instead undertake closer analysis to identify specific calls that may need hardening, rather than attempt a universal solution. We have kept the portion of the previous patch that improved snprintf.c's handling of errors when it calls the platform's sprintf(). That seems to be an unalloyed improvement. Security: CVE-2015-3166
2015-05-18Add error-throwing wrappers for the printf family of functions.Noah Misch
All known standard library implementations of these functions can fail with ENOMEM. A caller neglecting to check for failure would experience missing output, information exposure, or a crash. Check return values within wrappers and code, currently just snprintf.c, that bypasses the wrappers. The wrappers do not return after an error, so their callers need not check. Back-patch to 9.0 (all supported versions). Popular free software standard library implementations do take pains to bypass malloc() in simple cases, but they risk ENOMEM for floating point numbers, positional arguments, large field widths, and large precisions. No specification demands such caution, so this commit regards every call to a printf family function as a potential threat. Injecting the wrappers implicitly is a compromise between patch scope and design goals. I would prefer to edit each call site to name a wrapper explicitly. libpq and the ECPG libraries would, ideally, convey errors to the caller rather than abort(). All that would be painfully invasive for a back-patched security fix, hence this compromise. Security: CVE-2015-3166
2015-05-18Permit use of vsprintf() in PostgreSQL code.Noah Misch
The next commit needs it. Back-patch to 9.0 (all supported versions).
2015-02-04Add missing float.h include to snprintf.c.Andres Freund
On windows _isnan() (which isnan() is redirected to in port/win32.h) is declared in float.h, not math.h. Per buildfarm animal currawong. Backpatch to all supported branches.
2015-02-02port/snprintf(): fix overflow and do paddingBruce Momjian
Prevent port/snprintf() from overflowing its local fixed-size buffer and pad to the desired number of digits with zeros, even if the precision is beyond the ability of the native sprintf(). port/snprintf() is only used on systems that lack a native snprintf(). Reported by Bruce Momjian. Patch by Tom Lane. Backpatch to all supported versions. Security: CVE-2015-0242
2014-05-06pgindent run for 9.4Bruce Momjian
This includes removing tabs after periods in C comments, which was applied to back branches, so this change should not effect backpatching.
2014-01-23Allow use of "z" flag in our printf calls, and use it where appropriate.Tom Lane
Since C99, it's been standard for printf and friends to accept a "z" size modifier, meaning "whatever size size_t has". Up to now we've generally dealt with printing size_t values by explicitly casting them to unsigned long and using the "l" modifier; but this is really the wrong thing on platforms where pointers are wider than longs (such as Win64). So let's start using "z" instead. To ensure we can do that on all platforms, teach src/port/snprintf.c to understand "z", and add a configure test to force use of that implementation when the platform's version doesn't handle "z". Having done that, modify a bunch of places that were using the unsigned-long hack to use "z" instead. This patch doesn't pretend to have gotten everyplace that could benefit, but it catches many of them. I made an effort in particular to ensure that all uses of the same error message text were updated together, so as not to increase the number of translatable strings. It's possible that this change will result in format-string warnings from pre-C99 compilers. We might have to reconsider if there are any popular compilers that will warn about this; but let's start by seeing what the buildfarm thinks. Andres Freund, with a little additional work by me
2011-04-10pgindent run before PG 9.1 beta 1.Bruce Momjian
2010-09-20Remove cvs keywords from all files.Magnus Hagander
2010-07-06pgindent run for 9.0, second runBruce Momjian
2008-03-18Fix our printf implementation to follow spec: if a star parameterTom Lane
value for a precision is negative, act as though precision weren't specified at all, that is the whole .* part of the format spec should be ignored. Our previous coding took it as .0 which is certainly wrong. Per report from Kris Jurka and local testing. Possibly this should be back-patched, but it would be good to get some more testing first; in any case there are no known cases where there's really a problem on the backend side.
2007-03-26Remove advertising clause from Berkeley BSD-licensed files, perBruce Momjian
instructions from Berkeley.
2006-11-28protect vfprintf from hijacking by Windows gettext just like other members ↵Andrew Dunstan
of the *printf family.
2006-10-04pgindent run for 8.2.Bruce Momjian
2005-12-05Document return-value conventions used by this implementation, perTom Lane
suggestion from Bruce.
2005-12-05Fix a rather sizable number of problems in our homegrown snprintf, such asTom Lane
incorrect implementation of argument reordering, arbitrary limit of output size for sprintf and fprintf, willingness to access more bytes than "%.Ns" specification allows, wrong formatting of LONGLONG_MIN, various field-padding bugs and omissions. I believe it now accurately implements a subset of the Single Unix Spec requirements (remaining unimplemented features are documented, too). Bruce Momjian and Tom Lane.
2005-10-15Standard pgindent run for 8.1.Bruce Momjian
2005-07-28Fix a whole bunch of #includes that were either wrong or redundant.Tom Lane
The first rule of portability for us is 'thou shalt have no other gods before c.h', and a whole lot of these files were either not including c.h at all, or including random system headers beforehand, either of which sins can mess up largefile support nicely. Once you have included c.h, there is no need to re-include what it includes, either.
2005-04-14Must count '*' characters as potential arguments.Tom Lane
2005-03-20Add mention of why malloc() has to be used in snprintf.c.Bruce Momjian
2005-03-20Department of second thoughts. Remove FRONTEND from snprintf.c becauseBruce Momjian
snprintf is called before the memory system is started. We have to just malloc/free. There are no elogs in the code so we should be fine.
2005-03-17Factor duplicate snprintf code into functions.Bruce Momjian
2005-03-16Add sprintf support, that were were missing.Bruce Momjian
Add support for snprintf '+', 'h', and %* length settings.
2005-03-16pgindent snprintf.c for consistency.Bruce Momjian
2005-03-16Fix snprintf for %*$.Bruce Momjian
2005-03-16Fix snprintf to handle %$ properly by storing and reordering theBruce Momjian
arguments. Nicolai Tufar
2005-03-12Fix snprintf() to properly handle precision specification for %f.Bruce Momjian
2005-03-11Add fprintf() custom version to libpgport.Bruce Momjian
Document use of macros for pg_printf functions. Bump major versions of all interfaces to handle movement of get_progname from libpq to libpgport in 8.0, and probably other libpgport changes in 8.1.
2005-03-11Define snprintf() to call pg_snprintf() so our own snprintf-likeBruce Momjian
implementation doesn't export out via libpq and get used by a user application.
2005-03-02Move snprintf int64 compatibility letters into a NOT_USED block.Bruce Momjian
2005-03-02Fix for %I64d snprintf.Bruce Momjian
2005-03-02snprintf() %I64d code fix.Bruce Momjian
2005-03-02Use our own snprintf() only if NLS is enabled, and support %qd and %I64d.Bruce Momjian
2005-03-02Prevent large allocation in snprintf to hold positional parameters.Bruce Momjian
Allocated size based on format string.
2005-03-02Fix snprintf on Win32:Bruce Momjian
* If vsnprintf() is not before snprintf() in this file, snprintf() * will call the system vsnprintf() on MinGW.
2005-03-02Make port snprintf.c finally thread-safe.Bruce Momjian
2005-03-01Make snprintf() use already-defined int64/uint64 typedefs rather thanBruce Momjian
defining its own.
2005-03-01And while we are on it, I would like to submit minorBruce Momjian
changes to make snprintf() vsnprintf() and printf() functions in src/port/snprintf.c thread-safe. Nicolai Tufar
2005-02-28snprintf.c has no sys/ioctl.h. Trivial patch below:Bruce Momjian
Magnus Hagander
2005-02-22Add semicolon so snprintf.c goto has a statement to attach to:Bruce Momjian
nochar: /* nothing */ ; /* semicolon required because a goto has to be attached to a statement */
2005-02-22Add support to port/snprintf.c for position parameter specification:Bruce Momjian
+ # Determine if printf supports %1$ argument selection, e.g. %5$ selects + # the fifth argument after the printf print string. + # This is not in the C99 standard, but in the Single Unix Specification (SUS). + # It is used in our langauge translation strings. Nicolai Tufar with configure changes by Bruce.
2004-08-29Pgindent run for 8.0.Bruce Momjian
2004-01-08Throw compile error if our non-thread-safe snprintf is used.Bruce Momjian
2003-11-29make sure the $Id tags are converted to $PostgreSQL as well ...PostgreSQL Daemon
2002-07-18Move libc replacement files from src/backend/port to src/port.Bruce Momjian