Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commited0cdf0

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 sharedlibrary, when dynamically loaded into the backend, to call the backendversions of src/port and src/common functions rather than the frontendversions that are actually linked into the shlib. This is definitelynot what we want, because the frontend versions often behave slightlydifferently. Up to now it's been "slight" enough that nobody noticed;but with the addition of SCRAM support functions in src/common, we'reobserving crashes due to the difference between palloc and mallocmemory allocation rules, as reported in bug #15367 from Jeremy Evans.The purpose of this patch is to create a direct test for this type ofmis-linking, so that we know whether any given platform requires extrameasures to prevent using the wrong functions. If the test fails, itwill lead to connection failures in the contrib/postgres_fdw regressiontest. At the moment, *BSD platforms using ELF format are known to havethe problem and can be expected to fail; but we need to know whetheranything else does, and we need a reliable ongoing check for futureplatforms.Actually fixing the problem will be the subject of later commit(s).Discussion:https://postgr.es/m/153626613985.23143.4743626885618266803@wrigleys.postgresql.org
1 parentc85ad9c commited0cdf0

File tree

8 files changed

+83
-5
lines changed

8 files changed

+83
-5
lines changed

‎src/backend/bootstrap/bootstrap.c

Lines changed: 8 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 36 additions & 0 deletions
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+
#ifdefFRONTEND
32+
return true;
33+
#else
34+
return false;
35+
#endif
36+
}

‎src/include/common/link-canary.h

Lines changed: 17 additions & 0 deletions
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+
#ifndefLINK_CANARY_H
13+
#defineLINK_CANARY_H
14+
15+
externboolpg_link_canary_is_frontend(void);
16+
17+
#endif/* LINK_CANARY_H */

‎src/interfaces/libpq/.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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.olink-canary.omd5.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.ccrypt.cerand48.cgetaddrinfo.cgetpeereid.cinet_aton.cinet_net_ntop.cnoblock.copen.csystem.cpgsleep.cpg_strong_random.cpgstrcasecmp.cpqsignal.csnprintf.cstrerror.cstrlcpy.cstrnlen.cthread.cwin32error.cwin32setlocale.c:% :$(top_srcdir)/src/port/%
107107
rm -f$@&&$(LN_S)$<.
108108

109-
ip.cmd5.cbase64.cscram-common.csha2.csha2_openssl.csaslprep.cunicode_norm.c:% :$(top_srcdir)/src/common/%
109+
ip.cmd5.cbase64.clink-canary.cscram-common.csha2.csha2_openssl.csaslprep.cunicode_norm.c:% :$(top_srcdir)/src/common/%
110110
rm -f$@&&$(LN_S)$<.
111111

112112
encnames.cwchar.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.clink-canary.cscram-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

Lines changed: 14 additions & 0 deletions
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
gotoconnect_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+
gotoconnect_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

Lines changed: 2 additions & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp