Skip to content

Commit e58f042

Browse files
committed
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
1 parent b544dcd commit e58f042

File tree

15 files changed

+296
-85
lines changed

15 files changed

+296
-85
lines changed

src/include/port.h

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,11 @@ extern unsigned char pg_tolower(unsigned char ch);
155155
extern unsigned char pg_ascii_toupper(unsigned char ch);
156156
extern unsigned char pg_ascii_tolower(unsigned char ch);
157157

158-
#ifdef USE_REPL_SNPRINTF
159-
160158
/*
161-
* Versions of libintl >= 0.13 try to replace printf() and friends with
162-
* macros to their own versions that understand the %$ format. We do the
163-
* same, so disable their macros, if they exist.
159+
* Capture macro-compatible calls to printf() and friends, and redirect them
160+
* to wrappers that throw errors in lieu of reporting failure in a return
161+
* value. Versions of libintl >= 0.13 similarly redirect to versions that
162+
* understand the %$ format, so disable libintl macros first.
164163
*/
165164
#ifdef vsnprintf
166165
#undef vsnprintf
@@ -184,6 +183,55 @@ extern unsigned char pg_ascii_tolower(unsigned char ch);
184183
#undef printf
185184
#endif
186185

186+
extern int
187+
vsnprintf_throw_on_fail(char *str, size_t count, const char *fmt, va_list args)
188+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
189+
extern int
190+
snprintf_throw_on_fail(char *str, size_t count, const char *fmt,...)
191+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
192+
extern int
193+
vsprintf_throw_on_fail(char *str, const char *fmt, va_list args)
194+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
195+
extern int
196+
sprintf_throw_on_fail(char *str, const char *fmt,...)
197+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
198+
extern int
199+
vfprintf_throw_on_fail(FILE *stream, const char *fmt, va_list args)
200+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
201+
extern int
202+
fprintf_throw_on_fail(FILE *stream, const char *fmt,...)
203+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
204+
extern int
205+
printf_throw_on_fail(const char *fmt,...)
206+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
207+
208+
/*
209+
* The GCC-specific code below prevents the __attribute__(... 'printf')
210+
* above from being replaced, and this is required because gcc doesn't
211+
* know anything about printf_throw_on_fail.
212+
*/
213+
#ifdef __GNUC__
214+
#define vsnprintf(...) vsnprintf_throw_on_fail(__VA_ARGS__)
215+
#define snprintf(...) snprintf_throw_on_fail(__VA_ARGS__)
216+
#define vsprintf(...) vsprintf_throw_on_fail(__VA_ARGS__)
217+
#define sprintf(...) sprintf_throw_on_fail(__VA_ARGS__)
218+
#define vfprintf(...) vfprintf_throw_on_fail(__VA_ARGS__)
219+
#define fprintf(...) fprintf_throw_on_fail(__VA_ARGS__)
220+
#define printf(...) printf_throw_on_fail(__VA_ARGS__)
221+
#else
222+
#define vsnprintf vsnprintf_throw_on_fail
223+
#define snprintf snprintf_throw_on_fail
224+
#define vsprintf vsprintf_throw_on_fail
225+
#define sprintf sprintf_throw_on_fail
226+
#define vfprintf vfprintf_throw_on_fail
227+
#define fprintf fprintf_throw_on_fail
228+
#define printf printf_throw_on_fail
229+
#endif
230+
231+
#ifdef USE_REPL_SNPRINTF
232+
233+
/* Code outside syswrap.c should not call these. */
234+
187235
extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
188236
extern int
189237
pg_snprintf(char *str, size_t count, const char *fmt,...)
@@ -204,28 +252,6 @@ pg_printf(const char *fmt,...)
204252
/* This extension allows gcc to check the format string */
205253
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
206254

207-
/*
208-
* The GCC-specific code below prevents the __attribute__(... 'printf')
209-
* above from being replaced, and this is required because gcc doesn't
210-
* know anything about pg_printf.
211-
*/
212-
#ifdef __GNUC__
213-
#define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
214-
#define snprintf(...) pg_snprintf(__VA_ARGS__)
215-
#define vsprintf(...) pg_vsprintf(__VA_ARGS__)
216-
#define sprintf(...) pg_sprintf(__VA_ARGS__)
217-
#define vfprintf(...) pg_vfprintf(__VA_ARGS__)
218-
#define fprintf(...) pg_fprintf(__VA_ARGS__)
219-
#define printf(...) pg_printf(__VA_ARGS__)
220-
#else
221-
#define vsnprintf pg_vsnprintf
222-
#define snprintf pg_snprintf
223-
#define vsprintf pg_vsprintf
224-
#define sprintf pg_sprintf
225-
#define vfprintf pg_vfprintf
226-
#define fprintf pg_fprintf
227-
#define printf pg_printf
228-
#endif
229255
#endif /* USE_REPL_SNPRINTF */
230256

231257
#if defined(WIN32)

src/interfaces/ecpg/compatlib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ submake-pgtypeslib:
4545
# Shared library stuff
4646
include $(top_srcdir)/src/Makefile.shlib
4747

48+
# XXX This library uses no symbols from snprintf.c.
4849
snprintf.c: % : $(top_srcdir)/src/port/%
4950
rm -f $@ && $(LN_S) $< .
5051

src/interfaces/ecpg/ecpglib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
/path.c
88
/pgstrcasecmp.c
99
/strlcpy.c
10+
/syswrap.c
1011
/thread.c

src/interfaces/ecpg/ecpglib/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ override CFLAGS += $(PTHREAD_CFLAGS)
2525
LIBS := $(filter-out -lpgport, $(LIBS))
2626

2727
OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o memory.o \
28-
connect.o misc.o path.o pgstrcasecmp.o \
28+
connect.o misc.o path.o pgstrcasecmp.o syswrap.o \
2929
$(filter snprintf.o strlcpy.o win32setlocale.o isinf.o, $(LIBOBJS))
3030

3131
# thread.c is needed only for non-WIN32 implementation of path.c
@@ -57,7 +57,7 @@ include $(top_srcdir)/src/Makefile.shlib
5757
# necessarily use the same object files as the backend uses. Instead,
5858
# symlink the source files in here and build our own object file.
5959

60-
path.c pgstrcasecmp.c snprintf.c strlcpy.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
60+
path.c pgstrcasecmp.c snprintf.c strlcpy.c syswrap.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
6161
rm -f $@ && $(LN_S) $< .
6262

6363
misc.o: misc.c $(top_builddir)/src/port/pg_config_paths.h
@@ -74,6 +74,6 @@ uninstall: uninstall-lib
7474

7575
clean distclean: clean-lib
7676
rm -f $(OBJS)
77-
rm -f path.c pgstrcasecmp.c snprintf.c strlcpy.c thread.c win32setlocale.c
77+
rm -f path.c pgstrcasecmp.c snprintf.c strlcpy.c syswrap.c thread.c win32setlocale.c
7878

7979
maintainer-clean: distclean maintainer-clean-lib

src/interfaces/ecpg/pgtypeslib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/exports.list
66

77
/pgstrcasecmp.c
8+
/syswrap.c

src/interfaces/ecpg/pgtypeslib/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SHLIB_LINK += -lm
2929
SHLIB_EXPORTS = exports.txt
3030

3131
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
32-
pgstrcasecmp.o \
32+
pgstrcasecmp.o syswrap.o \
3333
$(filter rint.o snprintf.o, $(LIBOBJS))
3434

3535
all: all-lib
@@ -42,7 +42,7 @@ include $(top_srcdir)/src/Makefile.shlib
4242
# necessarily use the same object files as the backend uses. Instead,
4343
# symlink the source files in here and build our own object file.
4444

45-
pgstrcasecmp.c rint.c snprintf.c: % : $(top_srcdir)/src/port/%
45+
pgstrcasecmp.c rint.c snprintf.c syswrap.c: % : $(top_srcdir)/src/port/%
4646
rm -f $@ && $(LN_S) $< .
4747

4848
install: all installdirs install-lib
@@ -52,6 +52,6 @@ installdirs: installdirs-lib
5252
uninstall: uninstall-lib
5353

5454
clean distclean: clean-lib
55-
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c
55+
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c syswrap.c
5656

5757
maintainer-clean: distclean maintainer-clean-lib

src/interfaces/libpq/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/snprintf.c
1212
/strerror.c
1313
/strlcpy.c
14+
/syswrap.c
1415
/thread.c
1516
/win32error.c
1617
/win32setlocale.c

src/interfaces/libpq/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
3535
fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
3636
libpq-events.o
3737
# libpgport C files we always use
38-
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o thread.o
38+
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o syswrap.o thread.o
3939
# libpgport C files that are needed if identified by configure
4040
OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o snprintf.o strerror.o strlcpy.o win32error.o win32setlocale.o, $(LIBOBJS))
4141
# backend/libpq
@@ -88,7 +88,7 @@ backend_src = $(top_srcdir)/src/backend
8888
# For some libpgport modules, this only happens if configure decides
8989
# the module is needed (see filter hack in OBJS, above).
9090

91-
chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c pgsleep.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
91+
chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c pgsleep.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c syswrap.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
9292
rm -f $@ && $(LN_S) $< .
9393

9494
ip.c md5.c: % : $(backend_src)/libpq/%
@@ -145,7 +145,7 @@ clean distclean: clean-lib
145145
# Might be left over from a Win32 client-only build
146146
rm -f pg_config_paths.h
147147
rm -f inet_net_ntop.c noblock.c pgstrcasecmp.c thread.c
148-
rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c snprintf.c strerror.c strlcpy.c win32error.c win32setlocale.c
148+
rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c snprintf.c strerror.c strlcpy.c syswrap.c win32error.c win32setlocale.c
149149
rm -f pgsleep.c
150150
rm -f md5.c ip.c
151151
rm -f encnames.c wchar.c

src/interfaces/libpq/bcc32.mak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ CLEAN :
106106
-@erase "$(INTDIR)\dirmod.obj"
107107
-@erase "$(INTDIR)\pgsleep.obj"
108108
-@erase "$(INTDIR)\open.obj"
109+
-@erase "$(INTDIR)\syswrap.obj"
109110
-@erase "$(INTDIR)\win32error.obj"
110111
-@erase "$(OUTDIR)\$(OUTFILENAME).lib"
111112
-@erase "$(OUTDIR)\$(OUTFILENAME)dll.lib"
@@ -149,6 +150,7 @@ LIB32_OBJS= \
149150
"$(INTDIR)\dirmod.obj" \
150151
"$(INTDIR)\pgsleep.obj" \
151152
"$(INTDIR)\open.obj" \
153+
"$(INTDIR)\syswrap.obj" \
152154
"$(INTDIR)\win32error.obj" \
153155
"$(INTDIR)\pthread-win32.obj"
154156

@@ -287,6 +289,11 @@ LINK32_FLAGS = -Gn -L$(BCB)\lib;$(INTDIR); -x -Tpd -v
287289
$(CPP_PROJ) /I"." ..\..\port\open.c
288290
<<
289291

292+
"$(INTDIR)\syswrap.obj" : ..\..\port\syswrap.c
293+
$(CPP) @<<
294+
$(CPP_PROJ) ..\..\port\syswrap.c
295+
<<
296+
290297
"$(INTDIR)\win32error.obj" : ..\..\port\win32error.c
291298
$(CPP) @<<
292299
$(CPP_PROJ) /I"." ..\..\port\win32error.c

src/interfaces/libpq/win32.mak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ CLEAN :
113113
-@erase "$(INTDIR)\dirmod.obj"
114114
-@erase "$(INTDIR)\pgsleep.obj"
115115
-@erase "$(INTDIR)\open.obj"
116+
-@erase "$(INTDIR)\syswrap.obj"
116117
-@erase "$(INTDIR)\win32error.obj"
117118
-@erase "$(INTDIR)\win32setlocale.obj"
118119
-@erase "$(OUTDIR)\$(OUTFILENAME).lib"
@@ -159,6 +160,7 @@ LIB32_OBJS= \
159160
"$(INTDIR)\dirmod.obj" \
160161
"$(INTDIR)\pgsleep.obj" \
161162
"$(INTDIR)\open.obj" \
163+
"$(INTDIR)\syswrap.obj" \
162164
"$(INTDIR)\win32error.obj" \
163165
"$(INTDIR)\win32setlocale.obj" \
164166
"$(INTDIR)\pthread-win32.obj"
@@ -327,6 +329,11 @@ LINK32_OBJS= \
327329
$(CPP_PROJ) /I"." ..\..\port\open.c
328330
<<
329331

332+
"$(INTDIR)\syswrap.obj" : ..\..\port\syswrap.c
333+
$(CPP) @<<
334+
$(CPP_PROJ) ..\..\port\syswrap.c
335+
<<
336+
330337
"$(INTDIR)\win32error.obj" : ..\..\port\win32error.c
331338
$(CPP) @<<
332339
$(CPP_PROJ) /I"." ..\..\port\win32error.c

src/pl/plperl/plperl.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
* So we undefine them here and redefine them after it's done its dirty deed.
4040
*/
4141

42-
#ifdef USE_REPL_SNPRINTF
4342
#undef snprintf
4443
#undef vsnprintf
45-
#endif
4644

4745

4846
/* required for perl API */
@@ -51,21 +49,19 @@
5149
#include "XSUB.h"
5250

5351
/* put back our snprintf and vsnprintf */
54-
#ifdef USE_REPL_SNPRINTF
5552
#ifdef snprintf
5653
#undef snprintf
5754
#endif
5855
#ifdef vsnprintf
5956
#undef vsnprintf
6057
#endif
6158
#ifdef __GNUC__
62-
#define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
63-
#define snprintf(...) pg_snprintf(__VA_ARGS__)
59+
#define vsnprintf(...) vsnprintf_throw_on_fail(__VA_ARGS__)
60+
#define snprintf(...) snprintf_throw_on_fail(__VA_ARGS__)
6461
#else
65-
#define vsnprintf pg_vsnprintf
66-
#define snprintf pg_snprintf
62+
#define vsnprintf vsnprintf_throw_on_fail
63+
#define snprintf snprintf_throw_on_fail
6764
#endif /* __GNUC__ */
68-
#endif /* USE_REPL_SNPRINTF */
6965

7066
/* perl version and platform portability */
7167
#define NEED_eval_pv

src/port/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LIBS += $(PTHREAD_LIBS)
3232

3333
OBJS = $(LIBOBJS) chklocale.o dirmod.o exec.o inet_net_ntop.o noblock.o \
3434
path.o pgcheckdir.o pgmkdirp.o pgsleep.o pgstrcasecmp.o \
35-
qsort.o qsort_arg.o sprompt.o thread.o
35+
qsort.o qsort_arg.o sprompt.o syswrap.o thread.o
3636

3737
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
3838
OBJS_SRV = $(OBJS:%.o=%_srv.o)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy