postgres.git / summary / log / commit / refs
commit 0bf1ac6bef44bc2ec4bf66e481c172139265f476
Author: Andrew Dunstan <andrew@dunslane.net>
Commit: Noah Misch <noah@leadboat.com>
Date: Mon Nov 09 14:20:35 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Fix type confusion in PL/Perl's get_perl_array_ref().
get_perl_array_ref() used sv_isa(sv, "PostgreSQL::InServer::ARRAY") to
recognize PL/Perl's own array wrapper objects, then cast SvRV(sv) to
HV * and looked up the "array" key. But sv_isa() only reports which
package a reference was blessed into, and blessing is an unprivileged
Perl operation that constrains nothing about what is blessed. A
function in trusted plperl could therefore return
my $x = "hi"; return bless \$x, 'PostgreSQL::InServer::ARRAY';
whereupon hv_fetch_string() would read a hash bucket array out of the
body of a plain scalar and dereference it. Since plperl is trusted,
any role with CREATE on a database can reach this.
Fix, per a suggestion from Tom Lane, by requiring SvTYPE(SvRV(sv)) to
be SVt_PVHV before consulting sv_isa(). A reference blessed into that
package but pointing at anything else now falls through to the caller's
existing handling for other references, which looks through the
reference; the example above fails with "malformed array literal". Add
a regression test next to the existing forged-object case.
It's not clear if this is exploitable, but out of an abundance of
caution we treat it as if it is.
Reported-by: Zheng Yu <zheng@depthfirst.com>
Backpatch-through: 14
Security: CVE-2026-116309
src/pl/plperl/expected/plperl_array.out | 9 +++++++++
src/pl/plperl/plperl.c | 8 +++++++-
src/pl/plperl/sql/plperl_array.sql | 8 ++++++++
3 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/src/pl/plperl/expected/plperl_array.out b/src/pl/plperl/expected/plperl_array.out
index f5803e10a6e..2469c707ccf 100644
--- a/src/pl/plperl/expected/plperl_array.out
+++ b/src/pl/plperl/expected/plperl_array.out
@@ -281,3 +281,12 @@ $$ LANGUAGE plperl;
SELECT perl_forged_array();
ERROR: could not get array reference from PostgreSQL::InServer::ARRAY object
CONTEXT: PL/Perl function "perl_forged_array"
+-- Test a forged PostgreSQL::InServer::ARRAY object that isn't a hash at all
+CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$
+ my $x = "hi";
+ return bless \$x, "PostgreSQL::InServer::ARRAY";
+$$ LANGUAGE plperl;
+SELECT perl_forged_array();
+ERROR: malformed array literal: "hi"
+DETAIL: Array value must start with "{" or dimension information.
+CONTEXT: PL/Perl function "perl_forged_array"
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index dcade2b4969..2db599e22d3 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -1150,7 +1150,13 @@ get_perl_array_ref(SV *sv)
{
if (SvTYPE(SvRV(sv)) == SVt_PVAV)
return sv;
- else if (sv_isa(sv, "PostgreSQL::InServer::ARRAY"))
+
+ /*
+ * sv_isa() only reports the package the reference was blessed into,
+ * so check the referent's type before casting it to HV.
+ */
+ else if (SvTYPE(SvRV(sv)) == SVt_PVHV &&
+ sv_isa(sv, "PostgreSQL::InServer::ARRAY"))
{
HV *hv = (HV *) SvRV(sv);
SV **sav = hv_fetch_string(hv, "array");
diff --git a/src/pl/plperl/sql/plperl_array.sql b/src/pl/plperl/sql/plperl_array.sql
index cd1d7e34c50..db745d0a23a 100644
--- a/src/pl/plperl/sql/plperl_array.sql
+++ b/src/pl/plperl/sql/plperl_array.sql
@@ -213,3 +213,11 @@ CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$
$$ LANGUAGE plperl;
SELECT perl_forged_array();
+
+-- Test a forged PostgreSQL::InServer::ARRAY object that isn't a hash at all
+CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$
+ my $x = "hi";
+ return bless \$x, "PostgreSQL::InServer::ARRAY";
+$$ LANGUAGE plperl;
+
+SELECT perl_forged_array();
[parent: 410b7056e9d7]