Commit Graph

45 Commits

Author SHA1 Message Date
Tom Lane 0c071936e9 Revert error-throwing wrappers for the printf family of functions.
This reverts commit 16304a0134, except
for its changes in src/port/snprintf.c; as well as commit
cac18a76bb 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-19 18:19:38 -04:00
Noah Misch 16304a0134 Add error-throwing wrappers for the printf family of functions.
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-18 10:02:31 -04:00
Noah Misch cac18a76bb Permit use of vsprintf() in PostgreSQL code.
The next commit needs it.  Back-patch to 9.0 (all supported versions).
2015-05-18 10:02:31 -04:00
Andres Freund ff8ca3b04c Add missing float.h include to snprintf.c.
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-04 13:27:31 +01:00
Bruce Momjian 29725b3db6 port/snprintf(): fix overflow and do padding
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
2015-02-02 10:00:45 -05:00
Bruce Momjian 0a78320057 pgindent run for 9.4
This includes removing tabs after periods in C comments, which was
applied to back branches, so this change should not effect backpatching.
2014-05-06 12:12:18 -04:00
Tom Lane ac4ef637ad Allow use of "z" flag in our printf calls, and use it where appropriate.
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
2014-01-23 17:18:33 -05:00
Bruce Momjian bf50caf105 pgindent run before PG 9.1 beta 1. 2011-04-10 11:42:00 -04:00
Magnus Hagander 9f2e211386 Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
Bruce Momjian 239d769e7e pgindent run for 9.0, second run 2010-07-06 19:19:02 +00:00
Tom Lane 206b1e558a Fix our printf implementation to follow spec: if a star parameter
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.
2008-03-18 01:49:44 +00:00
Bruce Momjian 9dd3ec6c3b Remove advertising clause from Berkeley BSD-licensed files, per
instructions from Berkeley.
2007-03-26 21:44:11 +00:00
Andrew Dunstan 796a3f373a protect vfprintf from hijacking by Windows gettext just like other members of the *printf family. 2006-11-28 01:12:34 +00:00
Bruce Momjian f99a569a2e pgindent run for 8.2. 2006-10-04 00:30:14 +00:00
Tom Lane 1daac8e165 Document return-value conventions used by this implementation, per
suggestion from Bruce.
2005-12-05 21:57:00 +00:00
Tom Lane 3311c7669a Fix a rather sizable number of problems in our homegrown snprintf, such as
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-12-05 02:39:38 +00:00
Bruce Momjian 1dc3498251 Standard pgindent run for 8.1. 2005-10-15 02:49:52 +00:00
Tom Lane 06e1d62689 Fix a whole bunch of #includes that were either wrong or redundant.
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-07-28 04:03:14 +00:00
Tom Lane b79a718fac Must count '*' characters as potential arguments. 2005-04-14 20:53:09 +00:00
Bruce Momjian 683f60da3d Add mention of why malloc() has to be used in snprintf.c. 2005-03-20 13:54:53 +00:00
Bruce Momjian 8c72526fe9 Department of second thoughts. Remove FRONTEND from snprintf.c because
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-20 03:53:39 +00:00
Bruce Momjian 7111a14fba Factor duplicate snprintf code into functions. 2005-03-17 03:18:14 +00:00
Bruce Momjian 712f053587 Add sprintf support, that were were missing.
Add support for snprintf '+', 'h', and %* length settings.
2005-03-16 21:27:23 +00:00
Bruce Momjian ca66797308 pgindent snprintf.c for consistency. 2005-03-16 15:12:18 +00:00
Bruce Momjian 2306f63dea Fix snprintf for %*$. 2005-03-16 15:11:43 +00:00
Bruce Momjian 888271ed15 Fix snprintf to handle %$ properly by storing and reordering the
arguments.

Nicolai Tufar
2005-03-16 06:00:58 +00:00
Bruce Momjian c82f76e1ac Fix snprintf() to properly handle precision specification for %f. 2005-03-12 04:00:56 +00:00
Bruce Momjian caad817d1c Add fprintf() custom version to libpgport.
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-11 19:13:43 +00:00
Bruce Momjian 3bc6bdf322 Define snprintf() to call pg_snprintf() so our own snprintf-like
implementation doesn't export out via libpq and get used by a user
application.
2005-03-11 17:20:35 +00:00
Bruce Momjian 5592a6cf46 Move snprintf int64 compatibility letters into a NOT_USED block. 2005-03-02 23:56:53 +00:00
Bruce Momjian f8aca046bb Fix for %I64d snprintf. 2005-03-02 15:32:39 +00:00
Bruce Momjian 022ece2cf1 snprintf() %I64d code fix. 2005-03-02 15:07:09 +00:00
Bruce Momjian bb7a3a740c Use our own snprintf() only if NLS is enabled, and support %qd and %I64d. 2005-03-02 14:48:22 +00:00
Bruce Momjian 70d4a9343f Prevent large allocation in snprintf to hold positional parameters.
Allocated size based on format string.
2005-03-02 05:22:22 +00:00
Bruce Momjian 47ea7148e3 Fix snprintf on Win32:
*  If vsnprintf() is not before snprintf() in this file, snprintf()
 *  will call the system vsnprintf() on MinGW.
2005-03-02 03:21:52 +00:00
Bruce Momjian 9989e90490 Make port snprintf.c finally thread-safe. 2005-03-02 00:02:13 +00:00
Bruce Momjian 03b8efa540 Make snprintf() use already-defined int64/uint64 typedefs rather than
defining its own.
2005-03-01 05:47:28 +00:00
Bruce Momjian 295dd338c3 And while we are on it, I would like to submit minor
changes to make snprintf() vsnprintf() and printf()
functions in src/port/snprintf.c thread-safe.

Nicolai Tufar
2005-03-01 00:38:11 +00:00
Bruce Momjian 949ab3c9b3 snprintf.c has no sys/ioctl.h. Trivial patch below:
Magnus Hagander
2005-02-28 14:16:16 +00:00
Bruce Momjian e3ebe2521e Add semicolon so snprintf.c goto has a statement to attach to:
nochar:
    /* nothing */
    ; /* semicolon required because a goto has to be attached to a statement */
2005-02-22 04:57:24 +00:00
Bruce Momjian b4feafb6ff Add support to port/snprintf.c for position parameter specification:
+ # 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.
2005-02-22 03:56:22 +00:00
Bruce Momjian b6b71b85bc Pgindent run for 8.0. 2004-08-29 05:07:03 +00:00
Bruce Momjian 49c534fccb Throw compile error if our non-thread-safe snprintf is used. 2004-01-08 17:15:54 +00:00
PostgreSQL Daemon 55b113257c make sure the $Id tags are converted to $PostgreSQL as well ... 2003-11-29 22:41:33 +00:00
Bruce Momjian 404e9a12a5 Move libc replacement files from src/backend/port to src/port. 2002-07-18 04:13:59 +00:00