Re: Residual cleanups for tied objects in PL/Perl

Поиск
Список
Период
Сортировка
Искать

Re: Residual cleanups for tied objects in PL/Perl

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andrey Rachitskiy  writes:
> A tied hash does not store the value in HeVAL(). hv_iternext() leaves that
> pointer unset. Tom's NULL check therefore always takes the SQL NULL path,
> even when FETCH would return a real value. hv_iterval() plus SvGETMAGIC()
> is what actually runs FETCH.

> The same applies to a tied array. av_len() stops the infinite loop, but
> av_fetch() still returns a magic SV whose SvOK() is false until GETMAGIC.
> Without that, SETOF also yields NULLs.

Hmm.  I confirm that adding SvGETMAGIC() makes the tied-array test
work (didn't try the hash case).  But to my previous point, where
else would we need to call it, if we're going to try to support
such cases?  Also, what exactly are the preconditions for that
function?  Looking at the perl header files here, it looks like
sv being nonnull is required and sufficient ... but your patch
is inconsistent about checking that.

			regards, tom lane


Re: Residual cleanups for tied objects in PL/Perl

От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:
Вложения:

пн, 17 авг. 2026 г. в 01:26, Tom Lane <tgl@sss.pgh.pa.us>:
But to my previous point, where
else would we need to call it, if we're going to try to support
such cases?  Also, what exactly are the preconditions for that
function?

If we want a tied hash or array to convert like an ordinary one, GETMAGIC
is still needed before SvOK() / SvROK() in plperl_sv_to_datum() and
jsonb_plperl's SV_to_JsonbValue().  plperl_build_tuple_result() still
uses HeVAL() and will crash on a tied hash the same way hstore_plperl
did.  That loop also calls hek2cstr(), which uses FREETMPS, so
hek2cstr() has to run before hv_iterval().

The trigger path does not return a hash.  After "MODIFY",
plperl_modify_tuple() walks $_TD->{new} with HeVAL().  PL/Perl already
filled that hash from the tuple, so it is not tied.  A crash would
need the trigger to tie that hash itself, for example

tie %{$_TD->{new}}, 'SomeTie';
$_TD->{new}{v} = 'from_tie';
return 'MODIFY';

Looking at the perl header files here, it looks like
sv being nonnull is required and sufficient ... but your patch
is inconsistent about checking that.

Yes.  Non-NULL is required and sufficient.

The extra NULL checks on the hstore side are not needed after
hv_iterval().  I left the old HeVAL() guards in because the point was
to show that FETCH results are available, and why a NULL check on
HeVAL() always yields SQL NULL.  The if (svp) in the SETOF loop is
only because av_fetch() returns NULL for a missing element. 

> That seems odd, given that we use Tie::Hash and Tie::StdHash in
> src/pl/plperl/plc_trusted.pl

Trusted plperl replaces require with pp_require_safe, which only
succeeds if the module is already in %INC. Otherwise it dies with
"Unable to load Tie/Array.pm into plperl". tie itself is allowed.
plperlu can load the modules as usual.

Proof:
```
\set ON_ERROR_STOP off

CREATE EXTENSION IF NOT EXISTS plperl;
CREATE EXTENSION IF NOT EXISTS plperlu;

\echo === 1) plperl: use Tie::Array ===
CREATE OR REPLACE FUNCTION try_use_tie_array() RETURNS text
LANGUAGE plperl AS $$
use Tie::Array;
return 'loaded';
$$;
SELECT try_use_tie_array();

\echo === 3) plperl: tie with inline class (no use) ===
CREATE OR REPLACE FUNCTION try_tie_inline() RETURNS text
LANGUAGE plperl AS $$
{
package PLPerlProofTie;
sub TIEARRAY { bless [], $_[0] }
sub STORE { $_[0][$_[1]] = $_[2] }
sub FETCH { $_[0][$_[1]] }
sub FETCHSIZE { scalar @{$_[0]} }
}
my @a;
tie @a, 'PLPerlProofTie';
$a[0] = 'ok';
return $a[0];
$$;
SELECT try_tie_inline();

\echo === 4) plperlu: use Tie::Array ===
CREATE OR REPLACE FUNCTION try_use_tie_array_u() RETURNS text
LANGUAGE plperlu AS $$
use Tie::Array;
return 'loaded';
$$;
SELECT try_use_tie_array_u();
```

Re: Residual cleanups for tied objects in PL/Perl

От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:
Вложения:
вт, 18 авг. 2026 г. в 10:27, Andrey Rachitskiy <pl0h0yp1@gmail.com>:

Hi, Tom!
Here's a patch to try to clean that up.  I think it is wrong that
the extension modules don't use hek2cstr, so I made them do so.
(But we probably shouldn't back-patch that: it's a user-visible
behavioral change and we've not gotten actual field complaints AFAIR.)

Agreed
path looks good  for me 
 
 
Tom, I've put together a SvGETMAGIC version on top of your last patch. Thoughts?

--
Regards,
Rachitskiy Andrey

Re: Residual cleanups for tied objects in PL/Perl

От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:


вт, 18 авг. 2026 г. в 20:17, Tom Lane <tgl@sss.pgh.pa.us>:
I've not read the whole thing, but I noticed

 /*
  * Convert a HE (hash entry) key to a cstr in the current database encoding.
  * The result is palloc'd.
+ *
+ * Uses FREETMPS, so call this before hv_iterval() if both are needed.
  */
 static inline char *
 hek2cstr(HE *he)

and you had a similar comment in your earlier draft.  I think this is
nonsense though.  hek2cstr does the full ENTER; SAVETMPS; FREETMPS;
LEAVE; dance, so unless I totally misunderstand that API, it's
destroying only its own temps not any pre-existing ones.  Also,
if there actually is an ordering dependency there, how is the existing
coding working (where the two callers of hek2cstr do so in randomly
different orders?)  I changed that ordering in my patch only for
cosmetic reasons, not because I expect it to make a difference.

You are right. The comment can be dropped.

I had read FREETMPS as discarding every mortal in sight. It does not. SAVETMPS records the current index on the temp stack. FREETMPS only drops SVs pushed after that mark. The nested ENTER/SAVETMPS/FREETMPS/LEAVE in hek2cstr() therefore frees the mortal from HeSVKEY_force(), and nothing that the caller already held.

--
Regards,
Rachitskiy Andrey

Re: Residual cleanups for tied objects in PL/Perl

От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:

ср, 19 авг. 2026 г. в 23:09, Tom Lane <tgl@sss.pgh.pa.us>:
Pushed after some mostly-cosmetic fiddling, and I incorporated
your test cases from upthread.  Thanks for working on this!


Dear Tom,

I would like to express my sincere gratitude. Thank you for the review and merging. 
It is a great honor to work with you and have the opportunity to learn from such a professional. 
This is an invaluable experience.

--
Regards,
Rachitskiy Andrey

Re: Residual cleanups for tied objects in PL/Perl

От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
On Wed, 19 Aug 2026 at 23:10, Tom Lane  wrote:
>
> I wrote:
> > I plan to study your patch more closely tomorrow.
>
> Pushed after some mostly-cosmetic fiddling, and I incorporated
> your test cases from upthread.  Thanks for working on this!
>
>                         regards, tom lane
>


HI!
I was running some random fuzzing recently, and I bumped into another
related issue. Not sure if this is worth fixing, but maybe it is worth
reporting. No modules required. A trusted plperl function that returns
a SETOF result as a tied array whose class does not implement
FETCHSIZE exits. Fails on current master 1f7013fe089 and 19beta4
b73d13c32c8.


CREATE EXTENSION plperl;

CREATE OR REPLACE FUNCTION pl_mf_tr() RETURNS SETOF int LANGUAGE plperl AS $$
  package MF;
  sub TIEARRAY { my $c = shift; bless { }, $c }
  sub FETCH { 42 }
  my @a; tie @a, 'MF';
  return \@a;
$$;

SELECT count(*) FROM pl_mf_tr();


Breakpoint 1, __GI_exit (status=255) at ./stdlib/exit.c:137
warning: 137 ./stdlib/exit.c: No such file or directory
(gdb) bt
#0  __GI_exit (status=255) at ./stdlib/exit.c:137
#1  0x0000713157470bef in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#2  0x0000713157470cc6 in Perl_my_failure_exit () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#3  0x000071315756cef3 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#4  0x00007131574fd71b in Perl_croak_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#5  0x00007131574fd72d in Perl_die_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#6  0x000071315757b0b0 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#7  0x000071315751ec7e in Perl_runops_standard () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#8  0x00007131574691af in Perl_call_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#9  0x00007131574fa38a in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#10 0x00007131574fd763 in Perl_vcroak () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#11 0x00007131574fe2ea in Perl_croak () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#12 0x0000713157478eff in Perl_gv_fetchmethod_pvn_flags () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#13 0x00007131574790fc in Perl_gv_fetchmethod_sv_flags () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#14 0x000071315752f7a7 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#15 0x000071315751ec7e in Perl_runops_standard () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#16 0x00007131574691af in Perl_call_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#17 0x0000713157504368 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#18 0x0000713157504733 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#19 0x00007131575019f8 in Perl_mg_size () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#20 0x00007131643797ce in Perl_av_count (av=0x5d7778b83290,
my_perl=0x5d7778b49cf0) at
/usr/lib/x86_64-linux-gnu/perl/5.38/CORE/inline.h:61
#21 Perl_av_count (av=0x5d7778b83290, my_perl=0x5d7778b49cf0) at
/usr/lib/x86_64-linux-gnu/perl/5.38/CORE/inline.h:56
#22 plperl_func_handler (fcinfo=) at plperl.c:2461
#23 plperl_call_handler (fcinfo=0x5d7778c26630) at plperl.c:1864



-- 
Best regards,
Kirill Reshke


Re: Residual cleanups for tied objects in PL/Perl

От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:

ср, 30 сент. 2026 г. в 21:18, Kirill Reshke <reshkekirill@gmail.com>:
On Wed, 19 Aug 2026 at 23:10, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> I wrote:
> > I plan to study your patch more closely tomorrow.
>
> Pushed after some mostly-cosmetic fiddling, and I incorporated
> your test cases from upthread.  Thanks for working on this!
>
>                         regards, tom lane
>


HI!
I was running some random fuzzing recently, and I bumped into another
related issue. Not sure if this is worth fixing, but maybe it is worth
reporting. No modules required. A trusted plperl function that returns
a SETOF result as a tied array whose class does not implement
FETCHSIZE exits. Fails on current master 1f7013fe089 and 19beta4
b73d13c32c8.


CREATE EXTENSION plperl;

CREATE OR REPLACE FUNCTION pl_mf_tr() RETURNS SETOF int LANGUAGE plperl AS $$
  package MF;
  sub TIEARRAY { my $c = shift; bless { }, $c }
  sub FETCH { 42 }
  my @a; tie @a, 'MF';
  return \@a;
$$;

SELECT count(*) FROM pl_mf_tr();


Breakpoint 1, __GI_exit (status=255) at ./stdlib/exit.c:137
warning: 137 ./stdlib/exit.c: No such file or directory
(gdb) bt
#0  __GI_exit (status=255) at ./stdlib/exit.c:137
#1  0x0000713157470bef in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#2  0x0000713157470cc6 in Perl_my_failure_exit () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#3  0x000071315756cef3 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#4  0x00007131574fd71b in Perl_croak_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#5  0x00007131574fd72d in Perl_die_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#6  0x000071315757b0b0 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#7  0x000071315751ec7e in Perl_runops_standard () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#8  0x00007131574691af in Perl_call_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#9  0x00007131574fa38a in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#10 0x00007131574fd763 in Perl_vcroak () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#11 0x00007131574fe2ea in Perl_croak () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#12 0x0000713157478eff in Perl_gv_fetchmethod_pvn_flags () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#13 0x00007131574790fc in Perl_gv_fetchmethod_sv_flags () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#14 0x000071315752f7a7 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#15 0x000071315751ec7e in Perl_runops_standard () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#16 0x00007131574691af in Perl_call_sv () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#17 0x0000713157504368 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#18 0x0000713157504733 in ?? () from /lib/x86_64-linux-gnu/libperl.so.5.38
#19 0x00007131575019f8 in Perl_mg_size () from
/lib/x86_64-linux-gnu/libperl.so.5.38
#20 0x00007131643797ce in Perl_av_count (av=0x5d7778b83290,
my_perl=0x5d7778b49cf0) at
/usr/lib/x86_64-linux-gnu/perl/5.38/CORE/inline.h:61
#21 Perl_av_count (av=0x5d7778b83290, my_perl=0x5d7778b49cf0) at
/usr/lib/x86_64-linux-gnu/perl/5.38/CORE/inline.h:56
#22 plperl_func_handler (fcinfo=<optimized out>) at plperl.c:2461
#23 plperl_call_handler (fcinfo=0x5d7778c26630) at plperl.c:1864



--
Best regards,
Kirill Reshke

Hi, Kirill!

I, Tom, and the security team are aware of it. I found this a while ago. I proposed a patch on how to close this issue (the sec team may have their own solution). I think the issue will be resolved in the near future.


--
Regards,
Rachitskiy Andrey
FAQ