1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>43.5. Trusted and Untrusted PL/Perl</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plperl-global.html" title="43.4. Global Values in PL/Perl" /><link rel="next" href="plperl-triggers.html" title="43.6. PL/Perl Triggers" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">43.5. Trusted and Untrusted PL/Perl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-global.html" title="43.4. Global Values in PL/Perl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 43. PL/Perl — Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 43. PL/Perl — Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-triggers.html" title="43.6. PL/Perl Triggers">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPERL-TRUSTED"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.5. Trusted and Untrusted PL/Perl <a href="#PLPERL-TRUSTED" class="id_link">#</a></h2></div></div></div><a id="id-1.8.10.13.2" class="indexterm"></a><p>
3 Normally, PL/Perl is installed as a <span class="quote">“<span class="quote">trusted</span>”</span> programming
4 language named <code class="literal">plperl</code>. In this setup, certain Perl
5 operations are disabled to preserve security. In general, the
6 operations that are restricted are those that interact with the
7 environment. This includes file handle operations,
8 <code class="literal">require</code>, and <code class="literal">use</code> (for
9 external modules). There is no way to access internals of the
10 database server process or to gain OS-level access with the
11 permissions of the server process,
12 as a C function can do. Thus, any unprivileged database user can
13 be permitted to use this language.
14 </p><div class="warning"><h3 class="title">Warning</h3><p>
15 Trusted PL/Perl relies on the Perl <code class="literal">Opcode</code> module to
18 <a class="ulink" href="https://perldoc.perl.org/Opcode#WARNING" target="_top">documents</a>
19 that the module is not effective for the trusted PL/Perl use case. If
20 your security needs are incompatible with the uncertainty in that warning,
21 consider executing <code class="literal">REVOKE USAGE ON LANGUAGE plperl FROM
24 Here is an example of a function that will not work because file
25 system operations are not allowed for security reasons:
26 </p><pre class="programlisting">
27 CREATE FUNCTION badfunc() RETURNS integer AS $$
28 my $tmpfile = "/tmp/badfile";
29 open my $fh, '>', $tmpfile
30 or elog(ERROR, qq{could not open the file "$tmpfile": $!});
31 print $fh "Testing writing to a file\n";
32 close $fh or elog(ERROR, qq{could not close the file "$tmpfile": $!});
36 The creation of this function will fail as its use of a forbidden
37 operation will be caught by the validator.
39 Sometimes it is desirable to write Perl functions that are not
40 restricted. For example, one might want a Perl function that sends
41 mail. To handle these cases, PL/Perl can also be installed as an
42 <span class="quote">“<span class="quote">untrusted</span>”</span> language (usually called
43 <span class="application">PL/PerlU</span><a id="id-1.8.10.13.6.3" class="indexterm"></a>).
44 In this case the full Perl language is available. When installing the
45 language, the language name <code class="literal">plperlu</code> will select
46 the untrusted PL/Perl variant.
48 The writer of a <span class="application">PL/PerlU</span> function must take care that the function
49 cannot be used to do anything unwanted, since it will be able to do
50 anything that could be done by a user logged in as the database
51 administrator. Note that the database system allows only database
52 superusers to create functions in untrusted languages.
54 If the above function was created by a superuser using the language
55 <code class="literal">plperlu</code>, execution would succeed.
57 In the same way, anonymous code blocks written in Perl can use
58 restricted operations if the language is specified as
59 <code class="literal">plperlu</code> rather than <code class="literal">plperl</code>, but the caller
61 </p><div class="note"><h3 class="title">Note</h3><p>
62 While <span class="application">PL/Perl</span> functions run in a separate Perl
63 interpreter for each SQL role, all <span class="application">PL/PerlU</span> functions
64 executed in a given session run in a single Perl interpreter (which is
65 not any of the ones used for <span class="application">PL/Perl</span> functions).
66 This allows <span class="application">PL/PerlU</span> functions to share data freely,
67 but no communication can occur between <span class="application">PL/Perl</span> and
68 <span class="application">PL/PerlU</span> functions.
69 </p></div><div class="note"><h3 class="title">Note</h3><p>
70 Perl cannot support multiple interpreters within one process unless
71 it was built with the appropriate flags, namely either
72 <code class="literal">usemultiplicity</code> or <code class="literal">useithreads</code>.
73 (<code class="literal">usemultiplicity</code> is preferred unless you actually need
74 to use threads. For more details, see the
75 <span class="citerefentry"><span class="refentrytitle">perlembed</span></span> man page.)
76 If <span class="application">PL/Perl</span> is used with a copy of Perl that was not built
77 this way, then it is only possible to have one Perl interpreter per
78 session, and so any one session can only execute either
79 <span class="application">PL/PerlU</span> functions, or <span class="application">PL/Perl</span> functions
80 that are all called by the same SQL role.
81 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plperl-global.html" title="43.4. Global Values in PL/Perl">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html" title="Chapter 43. PL/Perl — Perl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-triggers.html" title="43.6. PL/Perl Triggers">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.4. Global Values in PL/Perl </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 43.6. PL/Perl Triggers</td></tr></table></div></body></html>