1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-25 01:02:05 +03:00

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
This commit is contained in:
Noah Misch
2015-05-18 10:02:31 -04:00
parent cac18a76bb
commit 16304a0134
16 changed files with 300 additions and 93 deletions

View File

@ -48,6 +48,7 @@ submake-pgtypeslib:
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
# XXX This library uses no symbols from snprintf.c.
snprintf.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .

View File

@ -5,6 +5,7 @@
/pgstrcasecmp.c
/snprintf.c
/strlcpy.c
/syswrap.c
/thread.c
/win32setlocale.c
/isinf.c

View File

@ -26,7 +26,7 @@ override CFLAGS += $(PTHREAD_CFLAGS)
LIBS := $(filter-out -lpgport, $(LIBS))
OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o memory.o \
connect.o misc.o path.o pgstrcasecmp.o \
connect.o misc.o path.o pgstrcasecmp.o syswrap.o \
$(filter snprintf.o strlcpy.o win32setlocale.o isinf.o, $(LIBOBJS)) $(WIN32RES)
# thread.c is needed only for non-WIN32 implementation of path.c
@ -55,7 +55,7 @@ include $(top_srcdir)/src/Makefile.shlib
# necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file.
path.c pgstrcasecmp.c snprintf.c strlcpy.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
path.c pgstrcasecmp.c snprintf.c strlcpy.c syswrap.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
misc.o: misc.c $(top_builddir)/src/port/pg_config_paths.h
@ -72,6 +72,6 @@ uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS)
rm -f path.c pgstrcasecmp.c snprintf.c strlcpy.c thread.c win32setlocale.c isinf.c
rm -f path.c pgstrcasecmp.c snprintf.c strlcpy.c syswrap.c thread.c win32setlocale.c isinf.c
maintainer-clean: distclean maintainer-clean-lib

View File

@ -4,3 +4,4 @@
/pgstrcasecmp.c
/rint.c
/snprintf.c
/syswrap.c

View File

@ -30,7 +30,7 @@ SHLIB_LINK += -lm
SHLIB_EXPORTS = exports.txt
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
pgstrcasecmp.o \
pgstrcasecmp.o syswrap.o \
$(filter rint.o snprintf.o, $(LIBOBJS)) $(WIN32RES)
all: all-lib
@ -43,7 +43,7 @@ include $(top_srcdir)/src/Makefile.shlib
# necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file.
pgstrcasecmp.c rint.c snprintf.c: % : $(top_srcdir)/src/port/%
pgstrcasecmp.c rint.c snprintf.c syswrap.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
install: all installdirs install-lib
@ -53,6 +53,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib
clean distclean: clean-lib
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c syswrap.c
maintainer-clean: distclean maintainer-clean-lib

View File

@ -13,6 +13,7 @@
/strerror.c
/strlcpy.c
/system.c
/syswrap.c
/thread.c
/win32error.c
/win32setlocale.c

View File

@ -36,7 +36,7 @@ OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
libpq-events.o
# libpgport C files we always use
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
thread.o
syswrap.o thread.o
# libpgport C files that are needed if identified by configure
OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o win32error.o win32setlocale.o, $(LIBOBJS))
# backend/libpq
@ -93,7 +93,7 @@ backend_src = $(top_srcdir)/src/backend
# For some libpgport modules, this only happens if configure decides
# the module is needed (see filter hack in OBJS, above).
chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c syswrap.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
ip.c md5.c: % : $(backend_src)/libpq/%
@ -145,7 +145,7 @@ clean distclean: clean-lib
# Might be left over from a Win32 client-only build
rm -f pg_config_paths.h
rm -f inet_net_ntop.c noblock.c pgstrcasecmp.c pqsignal.c thread.c
rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c system.c snprintf.c strerror.c strlcpy.c win32error.c win32setlocale.c
rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c system.c snprintf.c strerror.c strlcpy.c syswrap.c win32error.c win32setlocale.c
rm -f pgsleep.c
rm -f md5.c ip.c
rm -f encnames.c wchar.c

View File

@ -107,6 +107,7 @@ CLEAN :
-@erase "$(INTDIR)\pgsleep.obj"
-@erase "$(INTDIR)\open.obj"
-@erase "$(INTDIR)\system.obj"
-@erase "$(INTDIR)\syswrap.obj"
-@erase "$(INTDIR)\win32error.obj"
-@erase "$(OUTDIR)\$(OUTFILENAME).lib"
-@erase "$(OUTDIR)\$(OUTFILENAME)dll.lib"
@ -151,6 +152,7 @@ LIB32_OBJS= \
"$(INTDIR)\pgsleep.obj" \
"$(INTDIR)\open.obj" \
"$(INTDIR)\system.obj" \
"$(INTDIR)\syswrap.obj" \
"$(INTDIR)\win32error.obj" \
"$(INTDIR)\pthread-win32.obj"
@ -302,6 +304,11 @@ LINK32_FLAGS = -Gn -L$(BCB)\lib;$(INTDIR); -x -Tpd -v
$(CPP_PROJ) /I"." ..\..\port\system.c
<<
"$(INTDIR)\syswrap.obj" : ..\..\port\syswrap.c
$(CPP) @<<
$(CPP_PROJ) ..\..\port\syswrap.c
<<
"$(INTDIR)\win32error.obj" : ..\..\port\win32error.c
$(CPP) @<<
$(CPP_PROJ) /I"." ..\..\port\win32error.c

View File

@ -114,6 +114,7 @@ CLEAN :
-@erase "$(INTDIR)\pgsleep.obj"
-@erase "$(INTDIR)\open.obj"
-@erase "$(INTDIR)\system.obj"
-@erase "$(INTDIR)\syswrap.obj"
-@erase "$(INTDIR)\win32error.obj"
-@erase "$(INTDIR)\win32setlocale.obj"
-@erase "$(OUTDIR)\$(OUTFILENAME).lib"
@ -164,6 +165,7 @@ LIB32_OBJS= \
"$(INTDIR)\pgsleep.obj" \
"$(INTDIR)\open.obj" \
"$(INTDIR)\system.obj" \
"$(INTDIR)\syswrap.obj" \
"$(INTDIR)\win32error.obj" \
"$(INTDIR)\win32setlocale.obj" \
"$(INTDIR)\pthread-win32.obj"
@ -348,6 +350,11 @@ LINK32_OBJS= \
$(CPP_PROJ) /I"." ..\..\port\system.c
<<
"$(INTDIR)\syswrap.obj" : ..\..\port\syswrap.c
$(CPP) @<<
$(CPP_PROJ) ..\..\port\syswrap.c
<<
"$(INTDIR)\win32error.obj" : ..\..\port\win32error.c
$(CPP) @<<
$(CPP_PROJ) /I"." ..\..\port\win32error.c