2 43.5. Trusted and Untrusted PL/Perl #
4 Normally, PL/Perl is installed as a “trusted” programming language
5 named plperl. In this setup, certain Perl operations are disabled to
6 preserve security. In general, the operations that are restricted are
7 those that interact with the environment. This includes file handle
8 operations, require, and use (for external modules). There is no way to
9 access internals of the database server process or to gain OS-level
10 access with the permissions of the server process, as a C function can
11 do. Thus, any unprivileged database user can be permitted to use this
16 Trusted PL/Perl relies on the Perl Opcode module to preserve security.
17 Perl documents that the module is not effective for the trusted PL/Perl
18 use case. If your security needs are incompatible with the uncertainty
19 in that warning, consider executing REVOKE USAGE ON LANGUAGE plperl
22 Here is an example of a function that will not work because file system
23 operations are not allowed for security reasons:
24 CREATE FUNCTION badfunc() RETURNS integer AS $$
25 my $tmpfile = "/tmp/badfile";
26 open my $fh, '>', $tmpfile
27 or elog(ERROR, qq{could not open the file "$tmpfile": $!});
28 print $fh "Testing writing to a file\n";
29 close $fh or elog(ERROR, qq{could not close the file "$tmpfile": $!});
33 The creation of this function will fail as its use of a forbidden
34 operation will be caught by the validator.
36 Sometimes it is desirable to write Perl functions that are not
37 restricted. For example, one might want a Perl function that sends
38 mail. To handle these cases, PL/Perl can also be installed as an
39 “untrusted” language (usually called PL/PerlU). In this case the full
40 Perl language is available. When installing the language, the language
41 name plperlu will select the untrusted PL/Perl variant.
43 The writer of a PL/PerlU function must take care that the function
44 cannot be used to do anything unwanted, since it will be able to do
45 anything that could be done by a user logged in as the database
46 administrator. Note that the database system allows only database
47 superusers to create functions in untrusted languages.
49 If the above function was created by a superuser using the language
50 plperlu, execution would succeed.
52 In the same way, anonymous code blocks written in Perl can use
53 restricted operations if the language is specified as plperlu rather
54 than plperl, but the caller must be a superuser.
58 While PL/Perl functions run in a separate Perl interpreter for each SQL
59 role, all PL/PerlU functions executed in a given session run in a
60 single Perl interpreter (which is not any of the ones used for PL/Perl
61 functions). This allows PL/PerlU functions to share data freely, but no
62 communication can occur between PL/Perl and PL/PerlU functions.
66 Perl cannot support multiple interpreters within one process unless it
67 was built with the appropriate flags, namely either usemultiplicity or
68 useithreads. (usemultiplicity is preferred unless you actually need to
69 use threads. For more details, see the perlembed man page.) If PL/Perl
70 is used with a copy of Perl that was not built this way, then it is
71 only possible to have one Perl interpreter per session, and so any one
72 session can only execute either PL/PerlU functions, or PL/Perl
73 functions that are all called by the same SQL role.