Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ed0cdf0

Browse files
committed
Install a check for mis-linking of src/port and src/common functions.
On ELF-based platforms (and maybe others?) it's possible for a shared library, when dynamically loaded into the backend, to call the backend versions of src/port and src/common functions rather than the frontend versions that are actually linked into the shlib. This is definitely not what we want, because the frontend versions often behave slightly differently. Up to now it's been "slight" enough that nobody noticed; but with the addition of SCRAM support functions in src/common, we're observing crashes due to the difference between palloc and malloc memory allocation rules, as reported in bug #15367 from Jeremy Evans. The purpose of this patch is to create a direct test for this type of mis-linking, so that we know whether any given platform requires extra measures to prevent using the wrong functions. If the test fails, it will lead to connection failures in the contrib/postgres_fdw regression test. At the moment, *BSD platforms using ELF format are known to have the problem and can be expected to fail; but we need to know whether anything else does, and we need a reliable ongoing check for future platforms. Actually fixing the problem will be the subject of later commit(s). Discussion: https://postgr.es/m/153626613985.23143.4743626885618266803@wrigleys.postgresql.org
1 parent c85ad9c commit ed0cdf0

File tree

8 files changed

+83
-5
lines changed

8 files changed

+83
-5
lines changed

src/backend/bootstrap/bootstrap.c

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "catalog/index.h"
2525
#include "catalog/pg_collation.h"
2626
#include "catalog/pg_type.h"
27+
#include "common/link-canary.h"
2728
#include "libpq/pqsignal.h"
2829
#include "miscadmin.h"
2930
#include "nodes/makefuncs.h"
@@ -502,6 +503,13 @@ BootstrapModeMain(void)
502503
Assert(!IsUnderPostmaster);
503504
Assert(IsBootstrapProcessingMode());
504505

506+
/*
507+
* To ensure that src/common/link-canary.c is linked into the backend, we
508+
* must call it from somewhere. Here is as good as anywhere.
509+
*/
510+
if (pg_link_canary_is_frontend())
511+
elog(ERROR, "backend is incorrectly linked to frontend functions");
512+
505513
/*
506514
* Do backend-like initialization for bootstrap mode
507515
*/

src/common/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
4141
LIBS += $(PTHREAD_LIBS)
4242

4343
OBJS_COMMON = base64.o config_info.o controldata_utils.o exec.o file_perm.o \
44-
ip.o keywords.o md5.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o \
44+
ip.o keywords.o link-canary.o md5.o pg_lzcompress.o \
45+
pgfnames.o psprintf.o relpath.o \
4546
rmtree.o saslprep.o scram-common.o string.o unicode_norm.o \
4647
username.o wait_error.o
4748

src/common/link-canary.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*-------------------------------------------------------------------------
2+
* link-canary.c
3+
* Detect whether src/common functions came from frontend or backend.
4+
*
5+
* Copyright (c) 2018, PostgreSQL Global Development Group
6+
*
7+
* IDENTIFICATION
8+
* src/common/link-canary.c
9+
*
10+
*-------------------------------------------------------------------------
11+
*/
12+
#include "c.h"
13+
14+
#include "common/link-canary.h"
15+
16+
/*
17+
* This function just reports whether this file was compiled for frontend
18+
* or backend environment. We need this because in some systems, mainly
19+
* ELF-based platforms, it is possible for a shlib (such as libpq) loaded
20+
* into the backend to call a backend function named XYZ in preference to
21+
* the shlib's own function XYZ. That's bad if the two functions don't
22+
* act identically. This exact situation comes up for many functions in
23+
* src/common and src/port, where the same function names exist in both
24+
* libpq and the backend but they don't act quite identically. To verify
25+
* that appropriate measures have been taken to prevent incorrect symbol
26+
* resolution, libpq should test that this function returns true.
27+
*/
28+
bool
29+
pg_link_canary_is_frontend(void)
30+
{
31+
#ifdef FRONTEND
32+
return true;
33+
#else
34+
return false;
35+
#endif
36+
}

src/include/common/link-canary.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* link-canary.h
4+
* Detect whether src/common functions came from frontend or backend.
5+
*
6+
* Copyright (c) 2018, PostgreSQL Global Development Group
7+
*
8+
* src/include/common/link-canary.h
9+
*
10+
*-------------------------------------------------------------------------
11+
*/
12+
#ifndef LINK_CANARY_H
13+
#define LINK_CANARY_H
14+
15+
extern bool pg_link_canary_is_frontend(void);
16+
17+
#endif /* LINK_CANARY_H */

src/interfaces/libpq/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/ip.c
2626
/md5.c
2727
/base64.c
28+
/link-canary.c
2829
/scram-common.c
2930
/sha2.c
3031
/sha2_openssl.c

src/interfaces/libpq/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ endif
4949
# src/backend/utils/mb
5050
OBJS += encnames.o wchar.o
5151
# src/common
52-
OBJS += base64.o ip.o md5.o scram-common.o saslprep.o unicode_norm.o
52+
OBJS += base64.o ip.o link-canary.o md5.o scram-common.o saslprep.o unicode_norm.o
5353

5454
ifeq ($(with_openssl),yes)
5555
OBJS += fe-secure-openssl.o fe-secure-common.o sha2_openssl.o
@@ -106,7 +106,7 @@ backend_src = $(top_srcdir)/src/backend
106106
chklocale.c crypt.c erand48.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pg_strong_random.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
107107
rm -f $@ && $(LN_S) $< .
108108

109-
ip.c md5.c base64.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c: % : $(top_srcdir)/src/common/%
109+
ip.c md5.c base64.c link-canary.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c: % : $(top_srcdir)/src/common/%
110110
rm -f $@ && $(LN_S) $< .
111111

112112
encnames.c wchar.c: % : $(backend_src)/utils/mb/%
@@ -156,7 +156,7 @@ clean distclean: clean-lib
156156
rm -f pg_config_paths.h
157157
# Remove files we (may have) symlinked in from src/port and other places
158158
rm -f chklocale.c crypt.c erand48.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pg_strong_random.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32error.c win32setlocale.c
159-
rm -f ip.c md5.c base64.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c
159+
rm -f ip.c md5.c base64.c link-canary.c scram-common.c sha2.c sha2_openssl.c saslprep.c unicode_norm.c
160160
rm -f encnames.c wchar.c
161161

162162
maintainer-clean: distclean maintainer-clean-lib

src/interfaces/libpq/fe-connect.c

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
7171
#endif
7272

7373
#include "common/ip.h"
74+
#include "common/link-canary.h"
7475
#include "common/scram-common.h"
7576
#include "mb/pg_wchar.h"
7677
#include "port/pg_bswap.h"
@@ -1748,6 +1749,19 @@ connectDBStart(PGconn *conn)
17481749
if (!conn->options_valid)
17491750
goto connect_errReturn;
17501751

1752+
/*
1753+
* Check for bad linking to backend-internal versions of src/common
1754+
* functions (see comments in link-canary.c for the reason we need this).
1755+
* Nobody but developers should see this message, so we don't bother
1756+
* translating it.
1757+
*/
1758+
if (!pg_link_canary_is_frontend())
1759+
{
1760+
printfPQExpBuffer(&conn->errorMessage,
1761+
"libpq is incorrectly linked to backend functions\n");
1762+
goto connect_errReturn;
1763+
}
1764+
17511765
/* Ensure our buffers are empty */
17521766
conn->inStart = conn->inCursor = conn->inEnd = 0;
17531767
conn->outCount = 0;

src/tools/msvc/Mkvcbuild.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ sub mkvcbuild
116116

117117
our @pgcommonallfiles = qw(
118118
base64.c config_info.c controldata_utils.c exec.c file_perm.c ip.c
119-
keywords.c md5.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
119+
keywords.c link-canary.c md5.c
120+
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
120121
saslprep.c scram-common.c string.c unicode_norm.c username.c
121122
wait_error.c);
122123

0 commit comments

Comments
 (0)