Skip to content

Commit 638300f

Browse files
committed
Disallow execution of SPI functions during plperl function compilation.
Perl can be convinced to execute user-defined code during compilation of a plperl function (or at least a plperlu function). That's not such a big problem as long as the activity is confined within the Perl interpreter, and it's not clear we could do anything about that anyway. However, if such code tries to use plperl's SPI functions, we have a bigger problem. In the first place, those functions are likely to crash because current_call_data->prodesc isn't set up yet. In the second place, because it isn't set up, we lack critical info such as whether the function is supposed to be read-only. And in the third place, this path allows code execution during function validation, which is strongly discouraged because of the potential for security exploits. Hence, reject execution of the SPI functions until compilation is finished. While here, add check_spi_usage_allowed() calls to various functions that hadn't gotten the memo about checking that. I think that perhaps plperl_sv_to_literal may have been intentionally omitted on the grounds that it was safe at the time; but if so, the addition of transforms functionality changed that. The others are more recently added and seem to be flat-out oversights. Per report from Mark Murawski. Back-patch to all supported branches. Discussion: https://postgr.es/m/[email protected]
1 parent cd83cb9 commit 638300f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/pl/plperl/plperl.c

+31-2
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static plperl_proc_desc *compile_plperl_function(Oid fn_oid,
264264

265265
static SV *plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated);
266266
static SV *plperl_hash_from_datum(Datum attr);
267+
static void check_spi_usage_allowed(void);
267268
static SV *plperl_ref_from_pg_array(Datum arg, Oid typid);
268269
static SV *split_array(plperl_array_info *info, int first, int last, int nest);
269270
static SV *make_array_ref(plperl_array_info *info, int first, int last);
@@ -1429,13 +1430,15 @@ plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod,
14291430
char *
14301431
plperl_sv_to_literal(SV *sv, char *fqtypename)
14311432
{
1432-
Datum str = CStringGetDatum(fqtypename);
1433-
Oid typid = DirectFunctionCall1(regtypein, str);
1433+
Oid typid;
14341434
Oid typoutput;
14351435
Datum datum;
14361436
bool typisvarlena,
14371437
isnull;
14381438

1439+
check_spi_usage_allowed();
1440+
1441+
typid = DirectFunctionCall1(regtypein, CStringGetDatum(fqtypename));
14391442
if (!OidIsValid(typid))
14401443
ereport(ERROR,
14411444
(errcode(ERRCODE_UNDEFINED_OBJECT),
@@ -3097,6 +3100,21 @@ check_spi_usage_allowed(void)
30973100
/* simple croak as we don't want to involve PostgreSQL code */
30983101
croak("SPI functions can not be used in END blocks");
30993102
}
3103+
3104+
/*
3105+
* Disallow SPI usage if we're not executing a fully-compiled plperl
3106+
* function. It might seem impossible to get here in that case, but there
3107+
* are cases where Perl will try to execute code during compilation. If
3108+
* we proceed we are likely to crash trying to dereference the prodesc
3109+
* pointer. Working around that might be possible, but it seems unwise
3110+
* because it'd allow code execution to happen while validating a
3111+
* function, which is undesirable.
3112+
*/
3113+
if (current_call_data == NULL || current_call_data->prodesc == NULL)
3114+
{
3115+
/* simple croak as we don't want to involve PostgreSQL code */
3116+
croak("SPI functions can not be used during function compilation");
3117+
}
31003118
}
31013119

31023120

@@ -3217,6 +3235,8 @@ plperl_return_next(SV *sv)
32173235
{
32183236
MemoryContext oldcontext = CurrentMemoryContext;
32193237

3238+
check_spi_usage_allowed();
3239+
32203240
PG_TRY();
32213241
{
32223242
plperl_return_next_internal(sv);
@@ -3961,6 +3981,8 @@ plperl_spi_commit(void)
39613981
{
39623982
MemoryContext oldcontext = CurrentMemoryContext;
39633983

3984+
check_spi_usage_allowed();
3985+
39643986
PG_TRY();
39653987
{
39663988
SPI_commit();
@@ -3986,6 +4008,8 @@ plperl_spi_rollback(void)
39864008
{
39874009
MemoryContext oldcontext = CurrentMemoryContext;
39884010

4011+
check_spi_usage_allowed();
4012+
39894013
PG_TRY();
39904014
{
39914015
SPI_rollback();
@@ -4023,6 +4047,11 @@ plperl_util_elog(int level, SV *msg)
40234047
MemoryContext oldcontext = CurrentMemoryContext;
40244048
char *volatile cmsg = NULL;
40254049

4050+
/*
4051+
* We intentionally omit check_spi_usage_allowed() here, as this seems
4052+
* safe to allow even in the contexts that that function rejects.
4053+
*/
4054+
40264055
PG_TRY();
40274056
{
40284057
cmsg = sv2cstr(msg);

0 commit comments

Comments
 (0)