Conversation
This reverts commit 667f0c3. It would also work without it, in the opposite way, it has two issues: 1, it failed to build on macOS, with an error "ld: unknown option: --exclude-libs=libpq.a" 2, after the 12 merge, it triggers a PostgreSQL's error: "libpq is incorrectly linked to backend functions" (cherry picked from commit ceda5994991c17c57331d84d914478d123a2aa18)
Cloudberry linked libpq.a statically into postgres_fdw and added a copy of src/common/link-canary.c compiled with -DFRONTEND, only to make libpq's "libpq is incorrectly linked to backend functions" check pass. That check was right: libpq was calling the backend's src/common functions. With a SCRAM-authenticated remote server, pg_saslprep() returned a palloc'd password that libpq later passed to free() in scram_free() when the connection was closed. The memory context it came from (typically ExecutorState) was gone by then, so any disconnect crashed the backend: postgres_fdw_disconnect(), ALTER SERVER/USER MAPPING, or a cache reset processed at the start of the next statement. The previous commit links libpq.so dynamically, as Greenplum and upstream do; that is safe since 32cde27 hides the backend's copy of the libpq symbols. libpq.so uses its own frontend src/common and its own link canary, so the copy in postgres_fdw.so is no longer used. Remove it. Fixes apache#2490
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2490
What does this PR do?
Fixes a QD crash in postgres_fdw when the remote server uses SCRAM authentication. The backend crashes whenever a cached connection is closed.
Reproduce. On the remote server, give the user-mapping role a SCRAM password and a
scram-sha-256rule inpg_hba.conf. Then:Any path that closes the cached connection crashes in the same way:
postgres_fdw_disconnect()/postgres_fdw_disconnect_all()ALTER SERVER/ALTER USER MAPPINGStartTransactionCommand -> InvalidateSystemCachesExtended -> pgfdw_inval_callback -> PQfinish -> free, reported byCREATE FOREIGN TABLE.With
md5authentication the same steps work.Root cause. postgres_fdw linked
libpq.astatically, with-Bsymbolic.libpq.adoes not contain thesrc/commonfunctions that libpq calls, so they were resolved at run time against the backend's versions, which allocate withpalloc().During SCRAM authentication,
scram_init()stores the result ofpg_saslprep()instate->password. That result was apstrdup()in the current memory context,ExecutorStateof the first query on the connection (postgresBeginForeignScan -> GetConnection). The context is deleted when that query ends, but libpq keeps the pointer for the life of the connection. When the connection is closed,scram_free()callsfree()on it, and glibc aborts withfree(): invalid pointer/munmap_chunk(): invalid pointer, or the process gets SIGSEGV if the page has already been unmapped. All of this was confirmed with gdb on main and on 2.0.0.The static link came from GPDB 667f0c3, when the backend exported its own copy of the libpq functions. After the PG12 merge, libpq's own check reported that link as wrong (
libpq is incorrectly linked to backend functions). Cloudberry then silenced the check by building a copy oflink-canary.cwith-DFRONTENDinto postgres_fdw.Fix. Do what Greenplum and upstream PostgreSQL do.
libpq.sodynamically and drops theFRONTENDwrapper aroundlibpq-fe.h. This is safe since 32cde27 (Greenplum #16234), which hides the backend's copy of the libpq symbols with a linker version script.libpq.souses its own frontendsrc/common, sopg_saslprep()allocates withmalloc(). The only conflict was the extra-Bsymbolicand aGPDB_13_MERGE_FIXMEcomment; both were resolved to the Greenplum side.link-canary.oworkaround. It is Cloudberry-only, and withlibpq.soit is no longer used.dblink and gp_inject_fault already link
libpq.sothe same way.Type of Change
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelTested on a demo cluster built from main, with the cluster itself as the remote server and a
scram-sha-256role:postgres_fdw_disconnect_all(), a sinval cache reset processed while idle, and a cache reset processed instart_xact_command()ofCREATE FOREIGN TABLE. The last one is the stack in terminated by signal 11 Segmentation fault #2490.md5passes in all cases.pg_saslprep()andscram_free()run inlibpq.so.5, andstate->passwordis a livemalloc()chunk when it is freed.postgres_fdw.sonow hasNEEDED libpq.so.5.gp_postgres_fdwandmpp_postgres_fdwproduce the sameregression.diffswith and without this change. Both fail before it as well, because their expected files are stale; they are not run in CI.gp2pg_*needs separate PostgreSQL servers on 5432/5555 and was not run.pg_hba.conf, which pg_regress cannot set up. With the workaround removed, libpq's link canary again rejects any future mislinking of libpq against backendsrc/common. Every postgres_fdw connection would fail, so existing tests would catch it.Impact
User-facing changes: postgres_fdw no longer crashes the backend when it closes a connection to a SCRAM-authenticated server.
Dependencies:
postgres_fdw.sonow depends onlibpq.so.5from the same installation. It is found through the rpath, as for dblink.Checklist
Additional Context
Backport. 2.1.0 and 2.2.0 already hide the libpq symbols in
libpostgres.so, so this should apply there; that has not been tested yet. 2.0.0 does not: itslibpostgres.soexports thePQ*symbols, which take precedence overlibpq.so. Measured on 2.0.0:PQfinish()/pg_fe_scram_free()still come fromlibpostgres.soand still crash.dblink_disconnect().A 2.0.x fix would also need the
libpostgres.sopart of 32cde27.