Skip to content

Commit 329d755

Browse files
committed
Use a separate interpreter for each calling SQL userid in plperl and pltcl.
There are numerous methods by which a Perl or Tcl function can subvert the behavior of another such function executed later; for example, by redefining standard functions or operators called by the target function. If the target function is SECURITY DEFINER, or is called by such a function, this means that any ordinary SQL user with Perl or Tcl language usage rights can do essentially anything with the privileges of the target function's owner. To close this security hole, create a separate Perl or Tcl interpreter for each SQL userid under which plperl or pltcl functions are executed within a session. However, all plperlu or pltclu functions run within a session still share a single interpreter, since they all execute at the trust level of a database superuser anyway. Note: this change results in a functionality loss when libperl has been built without the "multiplicity" option: it's no longer possible to call plperl functions under different userids in one session, since such a libperl can't support multiple interpreters in one process. However, such a libperl already failed to support concurrent use of plperl and plperlu, so it's likely that few people use such versions with Postgres. Security: CVE-2010-3433
1 parent 51b69ef commit 329d755

File tree

8 files changed

+704
-374
lines changed

8 files changed

+704
-374
lines changed

doc/src/sgml/installation.sgml

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ su - postgres
185185
recent <productname>Perl</productname> versions, but it was not
186186
in earlier versions, and in any case it is the choice of whomever
187187
installed Perl at your site.
188+
If you intend to make more than incidental use of
189+
<application>PL/Perl</application>, you should ensure that the
190+
<productname>Perl</productname> installation was built with the
191+
<literal>usemultiplicity</> option enabled (<literal>perl -V</>
192+
will show whether this is the case).
188193
</para>
189194

190195
<para>

doc/src/sgml/plperl.sgml

+43-16
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.49.2.2 2010/05/13 16:43:40 aduns
4141
<para>
4242
Users of source packages must specially enable the build of
4343
PL/Perl during the installation process. (Refer to <xref
44-
linkend="install-short"> for more information.) Users of
44+
linkend="installation"> for more information.) Users of
4545
binary packages might find PL/Perl in a separate subpackage.
4646
</para>
4747
</note>
@@ -526,6 +526,23 @@ $$ LANGUAGE plperl;
526526
<literal>return $_SHARED{myquote}-&gt;($_[0]);</literal>
527527
at the expense of readability.)
528528
</para>
529+
530+
<para>
531+
For security reasons, PL/Perl executes functions called by any one SQL role
532+
in a separate Perl interpreter for that role. This prevents accidental or
533+
malicious interference by one user with the behavior of another user's
534+
PL/Perl functions. Each such interpreter has its own value of the
535+
<varname>%_SHARED</varname> variable and other global state. Thus, two
536+
PL/Perl functions will share the same value of <varname>%_SHARED</varname>
537+
if and only if they are executed by the same SQL role. In an application
538+
wherein a single session executes code under multiple SQL roles (via
539+
<literal>SECURITY DEFINER</> functions, use of <command>SET ROLE</>, etc)
540+
you may need to take explicit steps to ensure that PL/Perl functions can
541+
share data via <varname>%_SHARED</varname>. To do that, make sure that
542+
functions that should communicate are owned by the same user, and mark
543+
them <literal>SECURITY DEFINER</>. You must of course take care that
544+
such functions can't be used to do anything unintended.
545+
</para>
529546
</sect1>
530547

531548
<sect1 id="plperl-trusted">
@@ -593,21 +610,31 @@ $$ LANGUAGE plperl;
593610
</para>
594611

595612
<note>
596-
<para>
597-
For security reasons, to stop a leak of privileged operations from
598-
<application>PL/PerlU</> to <application>PL/Perl</>, these two languages
599-
have to run in separate instances of the Perl interpreter. If your
600-
Perl installation has been appropriately compiled, this is not a problem.
601-
However, not all installations are compiled with the requisite flags.
602-
If <productname>PostgreSQL</> detects that this is the case then it will
603-
not start a second interpreter, but instead create an error. In
604-
consequence, in such an installation, you cannot use both
605-
<application>PL/PerlU</> and <application>PL/Perl</> in the same backend
606-
process. The remedy for this is to obtain a Perl installation created
607-
with the appropriate flags, namely either <literal>usemultiplicity</> or
608-
both <literal>usethreads</> and <literal>useithreads</>.
609-
For more details,see the <literal>perlembed</> manual page.
610-
</para>
613+
<para>
614+
While <application>PL/Perl</> functions run in a separate Perl
615+
interpreter for each SQL role, all <application>PL/PerlU</> functions
616+
executed in a given session run in a single Perl interpreter (which is
617+
not any of the ones used for <application>PL/Perl</> functions).
618+
This allows <application>PL/PerlU</> functions to share data freely,
619+
but no communication can occur between <application>PL/Perl</> and
620+
<application>PL/PerlU</> functions.
621+
</para>
622+
</note>
623+
624+
<note>
625+
<para>
626+
Perl cannot support multiple interpreters within one process unless
627+
it was built with the appropriate flags, namely either
628+
<literal>usemultiplicity</> or <literal>useithreads</>.
629+
(<literal>usemultiplicity</> is preferred unless you actually need
630+
to use threads. For more details, see the
631+
<citerefentry><refentrytitle>perlembed</></citerefentry> man page.)
632+
If <application>PL/Perl</> is used with a copy of Perl that was not built
633+
this way, then it is only possible to have one Perl interpreter per
634+
session, and so any one session can only execute either
635+
<application>PL/PerlU</> functions, or <application>PL/Perl</> functions
636+
that are all called by the same SQL role.
637+
</para>
611638
</note>
612639

613640
</sect1>

doc/src/sgml/pltcl.sgml

+34-10
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,36 @@ $$ LANGUAGE pltcl;
217217
Sometimes it
218218
is useful to have some global data that is held between two
219219
calls to a function or is shared between different functions.
220-
This is easily done since
221-
all PL/Tcl functions executed in one session share the same
222-
safe Tcl interpreter. So, any global Tcl variable is accessible to
223-
all PL/Tcl function calls and will persist for the duration of the
224-
SQL session. (Note that <application>PL/TclU</> functions likewise share
225-
global data, but they are in a different Tcl interpreter and cannot
226-
communicate with PL/Tcl functions.)
220+
This is easily done in PL/Tcl, but there are some restrictions that
221+
must be understood.
227222
</para>
223+
224+
<para>
225+
For security reasons, PL/Tcl executes functions called by any one SQL
226+
role in a separate Tcl interpreter for that role. This prevents
227+
accidental or malicious interference by one user with the behavior of
228+
another user's PL/Tcl functions. Each such interpreter will have its own
229+
values for any <quote>global</> Tcl variables. Thus, two PL/Tcl
230+
functions will share the same global variables if and only if they are
231+
executed by the same SQL role. In an application wherein a single
232+
session executes code under multiple SQL roles (via <literal>SECURITY
233+
DEFINER</> functions, use of <command>SET ROLE</>, etc) you may need to
234+
take explicit steps to ensure that PL/Tcl functions can share data. To
235+
do that, make sure that functions that should communicate are owned by
236+
the same user, and mark them <literal>SECURITY DEFINER</>. You must of
237+
course take care that such functions can't be used to do anything
238+
unintended.
239+
</para>
240+
241+
<para>
242+
All PL/TclU functions used in a session execute in the same Tcl
243+
interpreter, which of course is distinct from the interpreter(s)
244+
used for PL/Tcl functions. So global data is automatically shared
245+
between PL/TclU functions. This is not considered a security risk
246+
because all PL/TclU functions execute at the same trust level,
247+
namely that of a database superuser.
248+
</para>
249+
228250
<para>
229251
To help protect PL/Tcl functions from unintentionally interfering
230252
with each other, a global
@@ -234,7 +256,9 @@ $$ LANGUAGE pltcl;
234256
<literal>GD</> be used
235257
for persistent private data of a function. Use regular Tcl global
236258
variables only for values that you specifically intend to be shared among
237-
multiple functions.
259+
multiple functions. (Note that the <literal>GD</> arrays are only
260+
global within a particular interpreter, so they do not bypass the
261+
security restrictions mentioned above.)
238262
</para>
239263

240264
<para>
@@ -674,8 +698,8 @@ CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
674698
exists, the module <literal>unknown</> is fetched from the table
675699
and loaded into the Tcl interpreter immediately before the first
676700
execution of a PL/Tcl function in a database session. (This
677-
happens separately for PL/Tcl and PL/TclU, if both are used,
678-
because separate interpreters are used for the two languages.)
701+
happens separately for each Tcl interpreter, if more than one is
702+
used in a session; see <xref linkend="pltcl-global">.)
679703
</para>
680704
<para>
681705
While the <literal>unknown</> module could actually contain any

doc/src/sgml/release-7.4.sgml

+37
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@
3737

3838
<itemizedlist>
3939

40+
<listitem>
41+
<para>
42+
Use a separate interpreter for each calling SQL userid in PL/Perl and
43+
PL/Tcl (Tom Lane)
44+
</para>
45+
46+
<para>
47+
This change prevents security problems that can be caused by subverting
48+
Perl or Tcl code that will be executed later in the same session under
49+
another SQL user identity (for example, within a <literal>SECURITY
50+
DEFINER</> function). Most scripting languages offer numerous ways that
51+
that might be done, such as redefining standard functions or operators
52+
called by the target function. Without this change, any SQL user with
53+
Perl or Tcl language usage rights can do essentially anything with the
54+
SQL privileges of the target function's owner.
55+
</para>
56+
57+
<para>
58+
The cost of this change is that intentional communication among Perl
59+
and Tcl functions becomes more difficult. To provide an escape hatch,
60+
PL/PerlU and PL/TclU functions continue to use only one interpreter
61+
per session. This is not considered a security issue since all such
62+
functions execute at the trust level of a database superuser already.
63+
</para>
64+
65+
<para>
66+
It is likely that third-party procedural languages that claim to offer
67+
trusted execution have similar security issues. We advise contacting
68+
the authors of any PL you are depending on for security-critical
69+
purposes.
70+
</para>
71+
72+
<para>
73+
Our thanks to Tim Bunce for pointing out this issue (CVE-2010-3433).
74+
</para>
75+
</listitem>
76+
4077
<listitem>
4178
<para>
4279
Prevent possible crashes in <function>pg_get_expr()</> by disallowing

doc/src/sgml/release-8.0.sgml

+37
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@
3737

3838
<itemizedlist>
3939

40+
<listitem>
41+
<para>
42+
Use a separate interpreter for each calling SQL userid in PL/Perl and
43+
PL/Tcl (Tom Lane)
44+
</para>
45+
46+
<para>
47+
This change prevents security problems that can be caused by subverting
48+
Perl or Tcl code that will be executed later in the same session under
49+
another SQL user identity (for example, within a <literal>SECURITY
50+
DEFINER</> function). Most scripting languages offer numerous ways that
51+
that might be done, such as redefining standard functions or operators
52+
called by the target function. Without this change, any SQL user with
53+
Perl or Tcl language usage rights can do essentially anything with the
54+
SQL privileges of the target function's owner.
55+
</para>
56+
57+
<para>
58+
The cost of this change is that intentional communication among Perl
59+
and Tcl functions becomes more difficult. To provide an escape hatch,
60+
PL/PerlU and PL/TclU functions continue to use only one interpreter
61+
per session. This is not considered a security issue since all such
62+
functions execute at the trust level of a database superuser already.
63+
</para>
64+
65+
<para>
66+
It is likely that third-party procedural languages that claim to offer
67+
trusted execution have similar security issues. We advise contacting
68+
the authors of any PL you are depending on for security-critical
69+
purposes.
70+
</para>
71+
72+
<para>
73+
Our thanks to Tim Bunce for pointing out this issue (CVE-2010-3433).
74+
</para>
75+
</listitem>
76+
4077
<listitem>
4178
<para>
4279
Prevent possible crashes in <function>pg_get_expr()</> by disallowing

doc/src/sgml/release-8.1.sgml

+37
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@
3737

3838
<itemizedlist>
3939

40+
<listitem>
41+
<para>
42+
Use a separate interpreter for each calling SQL userid in PL/Perl and
43+
PL/Tcl (Tom Lane)
44+
</para>
45+
46+
<para>
47+
This change prevents security problems that can be caused by subverting
48+
Perl or Tcl code that will be executed later in the same session under
49+
another SQL user identity (for example, within a <literal>SECURITY
50+
DEFINER</> function). Most scripting languages offer numerous ways that
51+
that might be done, such as redefining standard functions or operators
52+
called by the target function. Without this change, any SQL user with
53+
Perl or Tcl language usage rights can do essentially anything with the
54+
SQL privileges of the target function's owner.
55+
</para>
56+
57+
<para>
58+
The cost of this change is that intentional communication among Perl
59+
and Tcl functions becomes more difficult. To provide an escape hatch,
60+
PL/PerlU and PL/TclU functions continue to use only one interpreter
61+
per session. This is not considered a security issue since all such
62+
functions execute at the trust level of a database superuser already.
63+
</para>
64+
65+
<para>
66+
It is likely that third-party procedural languages that claim to offer
67+
trusted execution have similar security issues. We advise contacting
68+
the authors of any PL you are depending on for security-critical
69+
purposes.
70+
</para>
71+
72+
<para>
73+
Our thanks to Tim Bunce for pointing out this issue (CVE-2010-3433).
74+
</para>
75+
</listitem>
76+
4077
<listitem>
4178
<para>
4279
Prevent possible crashes in <function>pg_get_expr()</> by disallowing

0 commit comments

Comments
 (0)