[BUG] SECURITY DEFINER on call handler makes daemon crash

From: KaiGai Kohei <kaigai(at)ak(dot)jp(dot)nec(dot)com>
To: PostgreSQL-Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [BUG] SECURITY DEFINER on call handler makes daemon crash
Date: 2010-03-19 07:22:45
Message-ID: 4BA32645.7030007@ak.jp.nec.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

It is a bug report in a corner case.

When we assign "SECURITY DEFINER" attribute on plpgsql_call_handler(),
it makes server process crashed.

postgres=# ALTER FUNCTION plpgsql_call_handler() security definer;
ALTER FUNCTION

postgres=# CREATE FUNCTION foo(text) RETURNS text AS $$
BEGIN
RETURN $1 || '_aaa';
END
$$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# select foo('aaa');
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!> \q

[scenario]
1. PostgreSQL calls fmgr_info_cxt_security() to initialize FmgrInfo
of the foo() invocation.

2. This invocation eventually calls fmgr_info_other_lang(), because
foo() is implemented with plpgsql. It also calls fmgr_info() to
fetch the function pointer of the plpgsql_call_handler().

3. However, this invocation returns fmgr_security_definer, because
it is marked as security definer function. Then, it is copied to
FmgrInfo->fn_addr of the foo().

4. Then, at the first call of fmgr_security_definer(), it also calls
fmgr_info_cxt_security() with ignore_security = true, to initialize
secondary FmgrInfo.
However, its fn_addr is initialized to fmgr_security_definer()
because of step (1) - (3).

5. Finally, fmgr_security_definer() calls itself infinity, as long as
stack is available.

I wonder whether the fmgr_info() is an appropriate choice at
fmgr_info_other_lang(), because user intended to call the foo(), not
plpgsql_call_handler(), although it actuall calls language call-handler
in fact.

I think fmgr_info_cxt_security() with ignore_security = true should
be used here, instead.

Thanks,
--
KaiGai Kohei <kaigai(at)ak(dot)jp(dot)nec(dot)com>

Attachment Content-Type Size
pgsql-fix-security-definer.patch application/octect-stream 723 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2010-03-19 08:52:04 Re: Re: [COMMITTERS] pgsql: Make standby server continuously retry restoring the next WAL
Previous Message Gokulakannan Somasundaram 2010-03-19 06:19:27 Re: An idle thought