summaryrefslogtreecommitdiffstats
path: root/src/pl/plperl/plperl_opmask.pl
blob: 4972043572670b0e0d088d42bbc7a31a67c8ab7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!perl

# Copyright (c) 2021-2022, PostgreSQL Global Development Group

use strict;
use warnings;

use Opcode qw(opset opset_to_ops opdesc);

my $plperl_opmask_h = shift
  or die "Usage: $0 <output_filename.h>\n";

my $plperl_opmask_tmp = $plperl_opmask_h . "tmp";
END { unlink $plperl_opmask_tmp }

open my $fh, ">", "$plperl_opmask_tmp"
  or die "Could not write to $plperl_opmask_tmp: $!";

printf $fh "#define PLPERL_SET_OPMASK(opmask) \\\n";
printf $fh "  memset(opmask, 1, MAXO);\t/* disable all */ \\\n";
printf $fh "  /* then allow some... */                       \\\n";

my @allowed_ops = (

	# basic set of opcodes
	qw[:default :base_math !:base_io sort time],

	# require is safe because we redirect the opcode
	# entereval is safe as the opmask is now permanently set
	# caller is safe because the entire interpreter is locked down
	qw[require entereval caller],

	# These are needed for utf8_heavy.pl:
	# dofile is safe because we redirect the opcode like require above
	# print is safe because the only writable filehandles are STDOUT & STDERR
	# prtf (printf) is safe as it's the same as print + sprintf
	qw[dofile print prtf],

	# Disallow these opcodes that are in the :base_orig optag
	# (included in :default) but aren't considered sufficiently safe
	qw[!dbmopen !setpgrp !setpriority],

	# custom is not deemed a likely security risk as it can't be generated from
	# perl so would only be seen if the DBA had chosen to load a module that
	# used it. Even then it's unlikely to be seen because it's typically
	# generated by compiler plugins that operate after PL_op_mask checks.
	# But we err on the side of caution and disable it
	qw[!custom],);

printf $fh "  /* ALLOWED: @allowed_ops */ \\\n";

foreach my $opname (opset_to_ops(opset(@allowed_ops)))
{
	printf $fh qq{  opmask[OP_%-12s] = 0;\t/* %s */ \\\n},
	  uc($opname), opdesc($opname);
}
printf $fh "								/* end */\n";

close $fh
  or die "Error closing $plperl_opmask_tmp: $!";

rename $plperl_opmask_tmp, $plperl_opmask_h
  or die "Error renaming $plperl_opmask_tmp to $plperl_opmask_h: $!";

exit 0;