agora inbox for pgsql-hackers@postgresql.org  
help / color / mirror / Atom feed
[PATCH v1 2/3] Allow building only trusted or untrusted PL/Perl.
5+ messages / 2 participants
[nested] [flat]

* [PATCH v1 2/3] Allow building only trusted or untrusted PL/Perl.
@ 2022-05-17 20:28 Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Nathan Bossart @ 2022-05-17 20:28 UTC (permalink / raw)

Presently, when the --with-perl configuration option is used, both
trusted and untrusted PL/Perl are built.  However, some users may
only want to build one or the other.  This change introduces an
optional argument that can be used to do so.  If
--with-perl='trusted' is specified, only trusted PL/Perl is built.
If --with-perl='untrusted' is specified, only untrusted PL/Perl is
built.  If --with-perl is given without an argument, both trusted
and untrusted PL/Perl are built.
---
 configure                               | 47 ++++++++++++++++++++++---
 configure.ac                            | 32 ++++++++++++++++-
 contrib/bool_plperl/Makefile            | 24 ++++++++++---
 contrib/hstore_plperl/Makefile          | 24 ++++++++++---
 contrib/jsonb_plperl/Makefile           | 24 ++++++++++---
 doc/src/sgml/installation.sgml          | 23 +++++++++++-
 src/Makefile.global.in                  |  2 ++
 src/include/pg_config.h.in              |  6 ++++
 src/pl/plperl/GNUmakefile               | 37 +++++++++++++------
 src/pl/plperl/expected/plperl_setup.out | 15 +++-----
 src/pl/plperl/expected/plperlu.out      | 27 ++++++++++++++
 src/pl/plperl/plperl.c                  | 10 ++++++
 src/pl/plperl/sql/plperl_setup.sql      | 11 +++---
 src/pl/plperl/sql/plperlu.sql           | 29 +++++++++++++++
 14 files changed, 265 insertions(+), 46 deletions(-)

diff --git a/configure b/configure
index 7dec6b7bf9..faa8b1a2e3 100755
--- a/configure
+++ b/configure
@@ -723,6 +723,8 @@ with_krb_srvnam
 krb_srvtab
 with_gssapi
 with_python
+PERL_UNTRUSTED
+PERL_TRUSTED
 with_perl
 with_tcl
 ICU_LIBS
@@ -1563,7 +1565,8 @@ Optional Packages:
   --with-icu              build with ICU support
   --with-tcl              build Tcl modules (PL/Tcl)
   --with-tclconfig=DIR    tclConfig.sh is in DIR
-  --with-perl             build Perl modules (PL/Perl)
+  --with-perl[=TRUSTWORTHINESS]
+                          build Perl modules (PL/Perl)
   --with-python           build Python modules (PL/Python)
   --with-gssapi           build with GSSAPI support
   --with-krb-srvnam=NAME  default service principal name in Kerberos (GSSAPI)
@@ -8152,26 +8155,62 @@ if test "${with_perl+set}" = set; then :
   withval=$with_perl;
   case $withval in
     yes)
-      :
+
+  PERL_TRUSTED=yes
+  PERL_UNTRUSTED=yes
+
       ;;
     no)
       :
       ;;
     *)
-      as_fn_error $? "no argument expected for --with-perl option" "$LINENO" 5
+      with_perl=yes
+
+  if test "$withval" = trusted ; then
+    PERL_TRUSTED=yes
+    PERL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    PERL_TRUSTED=no
+    PERL_UNTRUSTED=yes
+  else
+    as_fn_error $? "invalid --with-perl value: argument must be omitted or specified as 'trusted' or 'untrusted'" "$LINENO" 5
+  fi
+
       ;;
   esac
 
 else
   with_perl=no
-
 fi
 
 
+
+if test "$with_perl" = yes; then
+
+  if test "$PERL_TRUSTED" = yes ; then
+
+$as_echo "#define USE_PERL 1" >>confdefs.h
+
+  fi
+  if test "$PERL_UNTRUSTED" = yes ; then
+
+$as_echo "#define USE_PERLU 1" >>confdefs.h
+
+  fi
+
+else
+
+  PERL_TRUSTED=no
+  PERL_UNTRUSTED=no
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_perl" >&5
 $as_echo "$with_perl" >&6; }
 
 
+
+
 #
 # Optionally build Python modules (PL/Python)
 #
diff --git a/configure.ac b/configure.ac
index d093fb88dd..0dd440131b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -823,9 +823,39 @@ PGAC_ARG_REQ(with, tclconfig, [DIR], [tclConfig.sh is in DIR])
 # Optionally build Perl modules (PL/Perl)
 #
 AC_MSG_CHECKING([whether to build Perl modules])
-PGAC_ARG_BOOL(with, perl, no, [build Perl modules (PL/Perl)])
+PGAC_ARG_OPTARG(with, perl,
+[TRUSTWORTHINESS], [build Perl modules (PL/Perl)],
+[
+  PERL_TRUSTED=yes
+  PERL_UNTRUSTED=yes
+],
+[
+  if test "$withval" = trusted ; then
+    PERL_TRUSTED=yes
+    PERL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    PERL_TRUSTED=no
+    PERL_UNTRUSTED=yes
+  else
+    AC_MSG_ERROR([invalid --with-perl value: argument must be omitted or specified as 'trusted' or 'untrusted'])
+  fi
+],
+[
+  if test "$PERL_TRUSTED" = yes ; then
+    AC_DEFINE([USE_PERL], 1, [Define to 1 to build with trusted Perl support. (--with-perl='trusted')])
+  fi
+  if test "$PERL_UNTRUSTED" = yes ; then
+    AC_DEFINE([USE_PERLU], 1, [Define to 1 to build with untrusted Perl support. (--with-perl='untrusted')])
+  fi
+],
+[
+  PERL_TRUSTED=no
+  PERL_UNTRUSTED=no
+])
 AC_MSG_RESULT([$with_perl])
 AC_SUBST(with_perl)
+AC_SUBST(PERL_TRUSTED)
+AC_SUBST(PERL_UNTRUSTED)
 
 #
 # Optionally build Python modules (PL/Python)
diff --git a/contrib/bool_plperl/Makefile b/contrib/bool_plperl/Makefile
index efe1de986b..ae23e7e3f1 100644
--- a/contrib/bool_plperl/Makefile
+++ b/contrib/bool_plperl/Makefile
@@ -8,10 +8,12 @@ PGFILEDESC = "bool_plperl - bool transform for plperl"
 
 PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plperl
 
-EXTENSION = bool_plperlu bool_plperl
-DATA = bool_plperlu--1.0.sql bool_plperl--1.0.sql
-
-REGRESS = bool_plperl bool_plperlu
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = bool_plperl
+DATA = bool_plperl--1.0.sql
+REGRESS = bool_plperl
 
 ifdef USE_PGXS
 PG_CONFIG = pg_config
@@ -37,3 +39,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+	override undefine EXTENSION
+	override undefine DATA
+	override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	override EXTENSION += bool_plperlu
+	override DATA += bool_plperlu--1.0.sql
+	override REGRESS += bool_plperlu
+endif
diff --git a/contrib/hstore_plperl/Makefile b/contrib/hstore_plperl/Makefile
index 9065f16408..32af5049a9 100644
--- a/contrib/hstore_plperl/Makefile
+++ b/contrib/hstore_plperl/Makefile
@@ -6,11 +6,13 @@ OBJS = \
 	hstore_plperl.o
 PGFILEDESC = "hstore_plperl - hstore transform for plperl"
 
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = hstore_plperl
+DATA = hstore_plperl--1.0.sql
+REGRESS = hstore_plperl create_transform
 
-EXTENSION = hstore_plperl hstore_plperlu
-DATA = hstore_plperl--1.0.sql hstore_plperlu--1.0.sql
-
-REGRESS = hstore_plperl hstore_plperlu create_transform
 EXTRA_INSTALL = contrib/hstore
 
 ifdef USE_PGXS
@@ -39,3 +41,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+	override undefine EXTENSION
+	override undefine DATA
+	override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	override EXTENSION += hstore_plperlu
+	override DATA += hstore_plperlu--1.0.sql
+	override REGRESS += hstore_plperlu
+endif
diff --git a/contrib/jsonb_plperl/Makefile b/contrib/jsonb_plperl/Makefile
index ba9480e819..218bd51c9e 100644
--- a/contrib/jsonb_plperl/Makefile
+++ b/contrib/jsonb_plperl/Makefile
@@ -8,10 +8,12 @@ PGFILEDESC = "jsonb_plperl - jsonb transform for plperl"
 
 PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plperl
 
-EXTENSION = jsonb_plperlu jsonb_plperl
-DATA = jsonb_plperlu--1.0.sql jsonb_plperl--1.0.sql
-
-REGRESS = jsonb_plperl jsonb_plperlu
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = jsonb_plperl
+DATA = jsonb_plperl--1.0.sql
+REGRESS = jsonb_plperl
 
 SHLIB_LINK += $(filter -lm, $(LIBS))
 
@@ -39,3 +41,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+    override undefine EXTENSION
+    override undefine DATA
+    override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+    override EXTENSION += jsonb_plperlu
+    override DATA += jsonb_plperlu--1.0.sql
+    override REGRESS += jsonb_plperlu
+endif
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index c585078029..4377e9d51a 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -873,10 +873,31 @@ build-postgresql:
       </varlistentry>
 
       <varlistentry>
-       <term><option>--with-perl</option></term>
+       <term><option>--with-perl<optional>=<replaceable>TRUSTWORTHINESS</replaceable></optional></option></term>
        <listitem>
         <para>
          Build the <application>PL/Perl</application> server-side language.
+         <replaceable>TRUSTWORTHINESS</replaceable> is an optional argument and,
+         if provided, must be one of:
+        </para>
+        <itemizedlist>
+         <listitem>
+          <para>
+           <option>trusted</option> to build only trusted
+           <application>PL/Perl</application>
+          </para>
+         </listitem>
+         <listitem>
+          <para>
+           <option>untrusted</option> to build only untrusted
+           <application>PL/Perl</application>
+           (<application>PL/PerlU</application>)
+          </para>
+         </listitem>
+        </itemizedlist>
+        <para>
+         If <replaceable>TRUSTWORTHINESS</replaceable> is not specified, both
+         trusted and untrusted <application>PL/Perl</application> will be built.
         </para>
        </listitem>
       </varlistentry>
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 051718e4fe..53f367ca7a 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -526,6 +526,8 @@ GENHTML = @GENHTML@
 
 DEF_PGPORT = @default_port@
 WANTED_LANGUAGES = @WANTED_LANGUAGES@
+PERL_TRUSTED = @PERL_TRUSTED@
+PERL_UNTRUSTED = @PERL_UNTRUSTED@
 
 
 ##########################################################################
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index cdd742cb55..2779f5f671 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -931,6 +931,12 @@
 /* Define to 1 to build with PAM support. (--with-pam) */
 #undef USE_PAM
 
+/* Define to 1 to build with trusted Perl support. (--with-perl='trusted') */
+#undef USE_PERL
+
+/* Define to 1 to build with untrusted Perl support. (--with-perl='untrusted') */
+#undef USE_PERLU
+
 /* Define to 1 to use software CRC-32C implementation (slicing-by-8). */
 #undef USE_SLICING_BY_8_CRC32C
 
diff --git a/src/pl/plperl/GNUmakefile b/src/pl/plperl/GNUmakefile
index a2e6410f53..d01201bbac 100644
--- a/src/pl/plperl/GNUmakefile
+++ b/src/pl/plperl/GNUmakefile
@@ -27,8 +27,13 @@ NAME = plperl
 
 OBJS = plperl.o SPI.o Util.o $(WIN32RES)
 
-DATA = plperl.control plperl--1.0.sql \
-       plperlu.control plperlu--1.0.sql
+ifeq ($(PERL_TRUSTED), yes)
+	DATA += plperl.control plperl--1.0.sql
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	DATA += plperlu.control plperlu--1.0.sql
+endif
 
 PERLCHUNKS = plc_perlboot.pl plc_trusted.pl
 
@@ -56,14 +61,26 @@ endif # win32
 SHLIB_LINK = $(perl_embed_ldflags)
 
 REGRESS_OPTS = --dbname=$(PL_TESTDB)
-REGRESS = plperl_setup plperl plperl_lc plperl_trigger plperl_shared \
-	plperl_elog plperl_util plperl_init plperlu plperl_array \
-	plperl_call plperl_transaction
-# if Perl can support two interpreters in one backend,
-# test plperl-and-plperlu cases
-ifneq ($(PERL),)
-ifeq ($(shell $(PERL) -V:usemultiplicity), usemultiplicity='define';)
-	REGRESS += plperl_plperlu
+
+ifeq ($(PERL_TRUSTED), yes)
+	REGRESS = plperl_setup plperl plperl_lc plperl_trigger plperl_shared \
+		plperl_elog plperl_util plperl_init plperl_array plperl_call \
+		plperl_transaction
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	REGRESS += plperlu
+endif
+
+ifeq ($(PERL_TRUSTED), yes)
+ifeq ($(PERL_UNTRUSTED), yes)
+	# if Perl can support two interpreters in one backend,
+	# test plperl-and-plperlu cases
+	ifneq ($(PERL),)
+		ifeq ($(shell $(PERL) -V:usemultiplicity), usemultiplicity='define';)
+			REGRESS += plperl_plperlu
+		endif
+	endif
 endif
 endif
 
diff --git a/src/pl/plperl/expected/plperl_setup.out b/src/pl/plperl/expected/plperl_setup.out
index 5234febefd..d0682325b2 100644
--- a/src/pl/plperl/expected/plperl_setup.out
+++ b/src/pl/plperl/expected/plperl_setup.out
@@ -1,18 +1,15 @@
 --
--- Install the plperl and plperlu extensions
+-- Install plperl
 --
 -- Before going ahead with the to-be-tested installations, verify that
--- a non-superuser is allowed to install plperl (but not plperlu) when
--- suitable permissions have been granted.
+-- a non-superuser is allowed to install plperl when suitable permissions
+-- have been granted.
 CREATE USER regress_user1;
 CREATE USER regress_user2;
 SET ROLE regress_user1;
 CREATE EXTENSION plperl;  -- fail
 ERROR:  permission denied to create extension "plperl"
 HINT:  Must have CREATE privilege on current database to create this extension.
-CREATE EXTENSION plperlu;  -- fail
-ERROR:  permission denied to create extension "plperlu"
-HINT:  Must be superuser to create this extension.
 RESET ROLE;
 DO $$
 begin
@@ -22,9 +19,6 @@ end;
 $$;
 SET ROLE regress_user1;
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;  -- fail
-ERROR:  permission denied to create extension "plperlu"
-HINT:  Must be superuser to create this extension.
 CREATE SCHEMA plperl_setup_scratch;
 SET search_path = plperl_setup_scratch;
 GRANT ALL ON SCHEMA plperl_setup_scratch TO regress_user2;
@@ -68,6 +62,5 @@ RESET ROLE;
 DROP OWNED BY regress_user1;
 DROP USER regress_user1;
 DROP USER regress_user2;
--- Now install the versions that will be used by subsequent test scripts.
+-- Now install the version that will be used by subsequent test scripts.
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;
diff --git a/src/pl/plperl/expected/plperlu.out b/src/pl/plperl/expected/plperlu.out
index a3edb38497..f759466a90 100644
--- a/src/pl/plperl/expected/plperlu.out
+++ b/src/pl/plperl/expected/plperlu.out
@@ -1,5 +1,32 @@
+-- Before going ahead with the to-be-tested installations, verify that
+-- only superusers can install plperlu.
+CREATE USER regress_user1;
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+ERROR:  permission denied to create extension "plperlu"
+HINT:  Must be superuser to create this extension.
+RESET ROLE;
+DO $$
+begin
+  execute format('grant create on database %I to regress_user1',
+                 current_database());
+end;
+$$;
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+ERROR:  permission denied to create extension "plperlu"
+HINT:  Must be superuser to create this extension.
+RESET ROLE;
+DO $$
+begin
+  execute format('revoke create on database %I from regress_user1',
+                 current_database());
+end;
+$$;
+DROP ROLE regress_user1;
 -- Use ONLY plperlu tests here. For plperl/plerlu combined tests
 -- see plperl_plperlu.sql
+CREATE EXTENSION plperlu;
 -- This test tests setting on_plperlu_init after loading plperl
 LOAD 'plperl';
 -- Test plperl.on_plperlu_init gets run
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 9bc6793a30..ff5f9641d5 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -445,6 +445,7 @@ _PG_init(void)
 	 * OK since the worst result would be an error.  Your code oughta pass
 	 * use_strict anyway ;-)
 	 */
+#ifdef USE_PERL
 	DefineCustomStringVariable("plperl.on_plperl_init",
 							   gettext_noop("Perl initialization code to execute once when plperl is first used."),
 							   NULL,
@@ -452,7 +453,9 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+#endif  /* USE_PERL */
 
+#ifdef USE_PERLU
 	DefineCustomStringVariable("plperl.on_plperlu_init",
 							   gettext_noop("Perl initialization code to execute once when plperlu is first used."),
 							   NULL,
@@ -460,6 +463,7 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+#endif  /* USE_PERLU */
 
 	MarkGUCPrefixReserved("plperl");
 
@@ -2039,6 +2043,7 @@ plperl_validator_internal(PG_FUNCTION_ARGS, bool trusted)
  * There are three externally visible pieces to plperl: plperl_call_handler,
  * plperl_inline_handler, and plperl_validator.
  */
+#ifdef USE_PERL
 
 PG_FUNCTION_INFO_V1(plperl_call_handler);
 
@@ -2064,11 +2069,14 @@ plperl_validator(PG_FUNCTION_ARGS)
 	return plperl_validator_internal(fcinfo, true);
 }
 
+#endif  /* USE_PERL */
+
 
 /*
  * plperlu likewise requires three externally visible functions:
  * plperlu_call_handler, plperlu_inline_handler, and plperlu_validator.
  */
+#ifdef USE_PERLU
 
 PG_FUNCTION_INFO_V1(plperlu_call_handler);
 
@@ -2095,6 +2103,8 @@ plperlu_validator(PG_FUNCTION_ARGS)
 	return plperl_validator_internal(fcinfo, false);
 }
 
+#endif  /* USE_PERLU */
+
 
 /*
  * Uses mkfunc to create a subroutine whose text is
diff --git a/src/pl/plperl/sql/plperl_setup.sql b/src/pl/plperl/sql/plperl_setup.sql
index a89cf56617..eb29cacdac 100644
--- a/src/pl/plperl/sql/plperl_setup.sql
+++ b/src/pl/plperl/sql/plperl_setup.sql
@@ -1,10 +1,10 @@
 --
--- Install the plperl and plperlu extensions
+-- Install plperl
 --
 
 -- Before going ahead with the to-be-tested installations, verify that
--- a non-superuser is allowed to install plperl (but not plperlu) when
--- suitable permissions have been granted.
+-- a non-superuser is allowed to install plperl when suitable permissions
+-- have been granted.
 
 CREATE USER regress_user1;
 CREATE USER regress_user2;
@@ -12,7 +12,6 @@ CREATE USER regress_user2;
 SET ROLE regress_user1;
 
 CREATE EXTENSION plperl;  -- fail
-CREATE EXTENSION plperlu;  -- fail
 
 RESET ROLE;
 
@@ -26,7 +25,6 @@ $$;
 SET ROLE regress_user1;
 
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;  -- fail
 CREATE SCHEMA plperl_setup_scratch;
 SET search_path = plperl_setup_scratch;
 GRANT ALL ON SCHEMA plperl_setup_scratch TO regress_user2;
@@ -68,6 +66,5 @@ DROP OWNED BY regress_user1;
 DROP USER regress_user1;
 DROP USER regress_user2;
 
--- Now install the versions that will be used by subsequent test scripts.
+-- Now install the version that will be used by subsequent test scripts.
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;
diff --git a/src/pl/plperl/sql/plperlu.sql b/src/pl/plperl/sql/plperlu.sql
index be43df5d90..1e050e8237 100644
--- a/src/pl/plperl/sql/plperlu.sql
+++ b/src/pl/plperl/sql/plperlu.sql
@@ -1,5 +1,34 @@
+-- Before going ahead with the to-be-tested installations, verify that
+-- only superusers can install plperlu.
+CREATE USER regress_user1;
+
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+RESET ROLE;
+
+DO $$
+begin
+  execute format('grant create on database %I to regress_user1',
+                 current_database());
+end;
+$$;
+
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+RESET ROLE;
+
+DO $$
+begin
+  execute format('revoke create on database %I from regress_user1',
+                 current_database());
+end;
+$$;
+
+DROP ROLE regress_user1;
+
 -- Use ONLY plperlu tests here. For plperl/plerlu combined tests
 -- see plperl_plperlu.sql
+CREATE EXTENSION plperlu;
 
 -- This test tests setting on_plperlu_init after loading plperl
 LOAD 'plperl';
-- 
2.25.1


--ReaqsoxgOBHFXBhH
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0003-Allow-building-only-trusted-or-untrusted-PL-Tcl.patch"



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* [PATCH v1 2/3] Allow building only trusted or untrusted PL/Perl.
@ 2022-05-17 20:28 Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Nathan Bossart @ 2022-05-17 20:28 UTC (permalink / raw)

Presently, when the --with-perl configuration option is used, both
trusted and untrusted PL/Perl are built.  However, some users may
only want to build one or the other.  This change introduces an
optional argument that can be used to do so.  If
--with-perl='trusted' is specified, only trusted PL/Perl is built.
If --with-perl='untrusted' is specified, only untrusted PL/Perl is
built.  If --with-perl is given without an argument, both trusted
and untrusted PL/Perl are built.
---
 configure                               | 47 ++++++++++++++++++++++---
 configure.ac                            | 32 ++++++++++++++++-
 contrib/bool_plperl/Makefile            | 24 ++++++++++---
 contrib/hstore_plperl/Makefile          | 24 ++++++++++---
 contrib/jsonb_plperl/Makefile           | 24 ++++++++++---
 doc/src/sgml/installation.sgml          | 23 +++++++++++-
 src/Makefile.global.in                  |  2 ++
 src/include/pg_config.h.in              |  6 ++++
 src/pl/plperl/GNUmakefile               | 37 +++++++++++++------
 src/pl/plperl/expected/plperl_setup.out | 15 +++-----
 src/pl/plperl/expected/plperlu.out      | 27 ++++++++++++++
 src/pl/plperl/plperl.c                  | 10 ++++++
 src/pl/plperl/sql/plperl_setup.sql      | 11 +++---
 src/pl/plperl/sql/plperlu.sql           | 29 +++++++++++++++
 14 files changed, 265 insertions(+), 46 deletions(-)

diff --git a/configure b/configure
index 7dec6b7bf9..faa8b1a2e3 100755
--- a/configure
+++ b/configure
@@ -723,6 +723,8 @@ with_krb_srvnam
 krb_srvtab
 with_gssapi
 with_python
+PERL_UNTRUSTED
+PERL_TRUSTED
 with_perl
 with_tcl
 ICU_LIBS
@@ -1563,7 +1565,8 @@ Optional Packages:
   --with-icu              build with ICU support
   --with-tcl              build Tcl modules (PL/Tcl)
   --with-tclconfig=DIR    tclConfig.sh is in DIR
-  --with-perl             build Perl modules (PL/Perl)
+  --with-perl[=TRUSTWORTHINESS]
+                          build Perl modules (PL/Perl)
   --with-python           build Python modules (PL/Python)
   --with-gssapi           build with GSSAPI support
   --with-krb-srvnam=NAME  default service principal name in Kerberos (GSSAPI)
@@ -8152,26 +8155,62 @@ if test "${with_perl+set}" = set; then :
   withval=$with_perl;
   case $withval in
     yes)
-      :
+
+  PERL_TRUSTED=yes
+  PERL_UNTRUSTED=yes
+
       ;;
     no)
       :
       ;;
     *)
-      as_fn_error $? "no argument expected for --with-perl option" "$LINENO" 5
+      with_perl=yes
+
+  if test "$withval" = trusted ; then
+    PERL_TRUSTED=yes
+    PERL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    PERL_TRUSTED=no
+    PERL_UNTRUSTED=yes
+  else
+    as_fn_error $? "invalid --with-perl value: argument must be omitted or specified as 'trusted' or 'untrusted'" "$LINENO" 5
+  fi
+
       ;;
   esac
 
 else
   with_perl=no
-
 fi
 
 
+
+if test "$with_perl" = yes; then
+
+  if test "$PERL_TRUSTED" = yes ; then
+
+$as_echo "#define USE_PERL 1" >>confdefs.h
+
+  fi
+  if test "$PERL_UNTRUSTED" = yes ; then
+
+$as_echo "#define USE_PERLU 1" >>confdefs.h
+
+  fi
+
+else
+
+  PERL_TRUSTED=no
+  PERL_UNTRUSTED=no
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_perl" >&5
 $as_echo "$with_perl" >&6; }
 
 
+
+
 #
 # Optionally build Python modules (PL/Python)
 #
diff --git a/configure.ac b/configure.ac
index d093fb88dd..0dd440131b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -823,9 +823,39 @@ PGAC_ARG_REQ(with, tclconfig, [DIR], [tclConfig.sh is in DIR])
 # Optionally build Perl modules (PL/Perl)
 #
 AC_MSG_CHECKING([whether to build Perl modules])
-PGAC_ARG_BOOL(with, perl, no, [build Perl modules (PL/Perl)])
+PGAC_ARG_OPTARG(with, perl,
+[TRUSTWORTHINESS], [build Perl modules (PL/Perl)],
+[
+  PERL_TRUSTED=yes
+  PERL_UNTRUSTED=yes
+],
+[
+  if test "$withval" = trusted ; then
+    PERL_TRUSTED=yes
+    PERL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    PERL_TRUSTED=no
+    PERL_UNTRUSTED=yes
+  else
+    AC_MSG_ERROR([invalid --with-perl value: argument must be omitted or specified as 'trusted' or 'untrusted'])
+  fi
+],
+[
+  if test "$PERL_TRUSTED" = yes ; then
+    AC_DEFINE([USE_PERL], 1, [Define to 1 to build with trusted Perl support. (--with-perl='trusted')])
+  fi
+  if test "$PERL_UNTRUSTED" = yes ; then
+    AC_DEFINE([USE_PERLU], 1, [Define to 1 to build with untrusted Perl support. (--with-perl='untrusted')])
+  fi
+],
+[
+  PERL_TRUSTED=no
+  PERL_UNTRUSTED=no
+])
 AC_MSG_RESULT([$with_perl])
 AC_SUBST(with_perl)
+AC_SUBST(PERL_TRUSTED)
+AC_SUBST(PERL_UNTRUSTED)
 
 #
 # Optionally build Python modules (PL/Python)
diff --git a/contrib/bool_plperl/Makefile b/contrib/bool_plperl/Makefile
index efe1de986b..ae23e7e3f1 100644
--- a/contrib/bool_plperl/Makefile
+++ b/contrib/bool_plperl/Makefile
@@ -8,10 +8,12 @@ PGFILEDESC = "bool_plperl - bool transform for plperl"
 
 PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plperl
 
-EXTENSION = bool_plperlu bool_plperl
-DATA = bool_plperlu--1.0.sql bool_plperl--1.0.sql
-
-REGRESS = bool_plperl bool_plperlu
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = bool_plperl
+DATA = bool_plperl--1.0.sql
+REGRESS = bool_plperl
 
 ifdef USE_PGXS
 PG_CONFIG = pg_config
@@ -37,3 +39,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+	override undefine EXTENSION
+	override undefine DATA
+	override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	override EXTENSION += bool_plperlu
+	override DATA += bool_plperlu--1.0.sql
+	override REGRESS += bool_plperlu
+endif
diff --git a/contrib/hstore_plperl/Makefile b/contrib/hstore_plperl/Makefile
index 9065f16408..32af5049a9 100644
--- a/contrib/hstore_plperl/Makefile
+++ b/contrib/hstore_plperl/Makefile
@@ -6,11 +6,13 @@ OBJS = \
 	hstore_plperl.o
 PGFILEDESC = "hstore_plperl - hstore transform for plperl"
 
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = hstore_plperl
+DATA = hstore_plperl--1.0.sql
+REGRESS = hstore_plperl create_transform
 
-EXTENSION = hstore_plperl hstore_plperlu
-DATA = hstore_plperl--1.0.sql hstore_plperlu--1.0.sql
-
-REGRESS = hstore_plperl hstore_plperlu create_transform
 EXTRA_INSTALL = contrib/hstore
 
 ifdef USE_PGXS
@@ -39,3 +41,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+	override undefine EXTENSION
+	override undefine DATA
+	override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	override EXTENSION += hstore_plperlu
+	override DATA += hstore_plperlu--1.0.sql
+	override REGRESS += hstore_plperlu
+endif
diff --git a/contrib/jsonb_plperl/Makefile b/contrib/jsonb_plperl/Makefile
index ba9480e819..218bd51c9e 100644
--- a/contrib/jsonb_plperl/Makefile
+++ b/contrib/jsonb_plperl/Makefile
@@ -8,10 +8,12 @@ PGFILEDESC = "jsonb_plperl - jsonb transform for plperl"
 
 PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plperl
 
-EXTENSION = jsonb_plperlu jsonb_plperl
-DATA = jsonb_plperlu--1.0.sql jsonb_plperl--1.0.sql
-
-REGRESS = jsonb_plperl jsonb_plperlu
+# We do not yet know whether we are building with trusted PL/Perl, untrusted
+# PL/Perl, or both, so for now we assume we are just building trusted PL/Perl.
+# We'll adjust these later on if this assumption was accurate.
+EXTENSION = jsonb_plperl
+DATA = jsonb_plperl--1.0.sql
+REGRESS = jsonb_plperl
 
 SHLIB_LINK += $(filter -lm, $(LIBS))
 
@@ -39,3 +41,17 @@ endif
 
 # As with plperl we need to include the perl_includespec directory last.
 override CPPFLAGS := $(CPPFLAGS) $(perl_embed_ccflags) $(perl_includespec)
+
+# Since we now know whether we are building trusted PL/Perl, untrusted PL/Perl,
+# or both, we should adjust the relevant variables accordingly.
+ifeq ($(PERL_TRUSTED), no)
+    override undefine EXTENSION
+    override undefine DATA
+    override undefine REGRESS
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+    override EXTENSION += jsonb_plperlu
+    override DATA += jsonb_plperlu--1.0.sql
+    override REGRESS += jsonb_plperlu
+endif
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index c585078029..4377e9d51a 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -873,10 +873,31 @@ build-postgresql:
       </varlistentry>
 
       <varlistentry>
-       <term><option>--with-perl</option></term>
+       <term><option>--with-perl<optional>=<replaceable>TRUSTWORTHINESS</replaceable></optional></option></term>
        <listitem>
         <para>
          Build the <application>PL/Perl</application> server-side language.
+         <replaceable>TRUSTWORTHINESS</replaceable> is an optional argument and,
+         if provided, must be one of:
+        </para>
+        <itemizedlist>
+         <listitem>
+          <para>
+           <option>trusted</option> to build only trusted
+           <application>PL/Perl</application>
+          </para>
+         </listitem>
+         <listitem>
+          <para>
+           <option>untrusted</option> to build only untrusted
+           <application>PL/Perl</application>
+           (<application>PL/PerlU</application>)
+          </para>
+         </listitem>
+        </itemizedlist>
+        <para>
+         If <replaceable>TRUSTWORTHINESS</replaceable> is not specified, both
+         trusted and untrusted <application>PL/Perl</application> will be built.
         </para>
        </listitem>
       </varlistentry>
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 051718e4fe..53f367ca7a 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -526,6 +526,8 @@ GENHTML = @GENHTML@
 
 DEF_PGPORT = @default_port@
 WANTED_LANGUAGES = @WANTED_LANGUAGES@
+PERL_TRUSTED = @PERL_TRUSTED@
+PERL_UNTRUSTED = @PERL_UNTRUSTED@
 
 
 ##########################################################################
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index cdd742cb55..2779f5f671 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -931,6 +931,12 @@
 /* Define to 1 to build with PAM support. (--with-pam) */
 #undef USE_PAM
 
+/* Define to 1 to build with trusted Perl support. (--with-perl='trusted') */
+#undef USE_PERL
+
+/* Define to 1 to build with untrusted Perl support. (--with-perl='untrusted') */
+#undef USE_PERLU
+
 /* Define to 1 to use software CRC-32C implementation (slicing-by-8). */
 #undef USE_SLICING_BY_8_CRC32C
 
diff --git a/src/pl/plperl/GNUmakefile b/src/pl/plperl/GNUmakefile
index a2e6410f53..d01201bbac 100644
--- a/src/pl/plperl/GNUmakefile
+++ b/src/pl/plperl/GNUmakefile
@@ -27,8 +27,13 @@ NAME = plperl
 
 OBJS = plperl.o SPI.o Util.o $(WIN32RES)
 
-DATA = plperl.control plperl--1.0.sql \
-       plperlu.control plperlu--1.0.sql
+ifeq ($(PERL_TRUSTED), yes)
+	DATA += plperl.control plperl--1.0.sql
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	DATA += plperlu.control plperlu--1.0.sql
+endif
 
 PERLCHUNKS = plc_perlboot.pl plc_trusted.pl
 
@@ -56,14 +61,26 @@ endif # win32
 SHLIB_LINK = $(perl_embed_ldflags)
 
 REGRESS_OPTS = --dbname=$(PL_TESTDB)
-REGRESS = plperl_setup plperl plperl_lc plperl_trigger plperl_shared \
-	plperl_elog plperl_util plperl_init plperlu plperl_array \
-	plperl_call plperl_transaction
-# if Perl can support two interpreters in one backend,
-# test plperl-and-plperlu cases
-ifneq ($(PERL),)
-ifeq ($(shell $(PERL) -V:usemultiplicity), usemultiplicity='define';)
-	REGRESS += plperl_plperlu
+
+ifeq ($(PERL_TRUSTED), yes)
+	REGRESS = plperl_setup plperl plperl_lc plperl_trigger plperl_shared \
+		plperl_elog plperl_util plperl_init plperl_array plperl_call \
+		plperl_transaction
+endif
+
+ifeq ($(PERL_UNTRUSTED), yes)
+	REGRESS += plperlu
+endif
+
+ifeq ($(PERL_TRUSTED), yes)
+ifeq ($(PERL_UNTRUSTED), yes)
+	# if Perl can support two interpreters in one backend,
+	# test plperl-and-plperlu cases
+	ifneq ($(PERL),)
+		ifeq ($(shell $(PERL) -V:usemultiplicity), usemultiplicity='define';)
+			REGRESS += plperl_plperlu
+		endif
+	endif
 endif
 endif
 
diff --git a/src/pl/plperl/expected/plperl_setup.out b/src/pl/plperl/expected/plperl_setup.out
index 5234febefd..d0682325b2 100644
--- a/src/pl/plperl/expected/plperl_setup.out
+++ b/src/pl/plperl/expected/plperl_setup.out
@@ -1,18 +1,15 @@
 --
--- Install the plperl and plperlu extensions
+-- Install plperl
 --
 -- Before going ahead with the to-be-tested installations, verify that
--- a non-superuser is allowed to install plperl (but not plperlu) when
--- suitable permissions have been granted.
+-- a non-superuser is allowed to install plperl when suitable permissions
+-- have been granted.
 CREATE USER regress_user1;
 CREATE USER regress_user2;
 SET ROLE regress_user1;
 CREATE EXTENSION plperl;  -- fail
 ERROR:  permission denied to create extension "plperl"
 HINT:  Must have CREATE privilege on current database to create this extension.
-CREATE EXTENSION plperlu;  -- fail
-ERROR:  permission denied to create extension "plperlu"
-HINT:  Must be superuser to create this extension.
 RESET ROLE;
 DO $$
 begin
@@ -22,9 +19,6 @@ end;
 $$;
 SET ROLE regress_user1;
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;  -- fail
-ERROR:  permission denied to create extension "plperlu"
-HINT:  Must be superuser to create this extension.
 CREATE SCHEMA plperl_setup_scratch;
 SET search_path = plperl_setup_scratch;
 GRANT ALL ON SCHEMA plperl_setup_scratch TO regress_user2;
@@ -68,6 +62,5 @@ RESET ROLE;
 DROP OWNED BY regress_user1;
 DROP USER regress_user1;
 DROP USER regress_user2;
--- Now install the versions that will be used by subsequent test scripts.
+-- Now install the version that will be used by subsequent test scripts.
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;
diff --git a/src/pl/plperl/expected/plperlu.out b/src/pl/plperl/expected/plperlu.out
index a3edb38497..f759466a90 100644
--- a/src/pl/plperl/expected/plperlu.out
+++ b/src/pl/plperl/expected/plperlu.out
@@ -1,5 +1,32 @@
+-- Before going ahead with the to-be-tested installations, verify that
+-- only superusers can install plperlu.
+CREATE USER regress_user1;
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+ERROR:  permission denied to create extension "plperlu"
+HINT:  Must be superuser to create this extension.
+RESET ROLE;
+DO $$
+begin
+  execute format('grant create on database %I to regress_user1',
+                 current_database());
+end;
+$$;
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+ERROR:  permission denied to create extension "plperlu"
+HINT:  Must be superuser to create this extension.
+RESET ROLE;
+DO $$
+begin
+  execute format('revoke create on database %I from regress_user1',
+                 current_database());
+end;
+$$;
+DROP ROLE regress_user1;
 -- Use ONLY plperlu tests here. For plperl/plerlu combined tests
 -- see plperl_plperlu.sql
+CREATE EXTENSION plperlu;
 -- This test tests setting on_plperlu_init after loading plperl
 LOAD 'plperl';
 -- Test plperl.on_plperlu_init gets run
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 9bc6793a30..ff5f9641d5 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -445,6 +445,7 @@ _PG_init(void)
 	 * OK since the worst result would be an error.  Your code oughta pass
 	 * use_strict anyway ;-)
 	 */
+#ifdef USE_PERL
 	DefineCustomStringVariable("plperl.on_plperl_init",
 							   gettext_noop("Perl initialization code to execute once when plperl is first used."),
 							   NULL,
@@ -452,7 +453,9 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+#endif  /* USE_PERL */
 
+#ifdef USE_PERLU
 	DefineCustomStringVariable("plperl.on_plperlu_init",
 							   gettext_noop("Perl initialization code to execute once when plperlu is first used."),
 							   NULL,
@@ -460,6 +463,7 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+#endif  /* USE_PERLU */
 
 	MarkGUCPrefixReserved("plperl");
 
@@ -2039,6 +2043,7 @@ plperl_validator_internal(PG_FUNCTION_ARGS, bool trusted)
  * There are three externally visible pieces to plperl: plperl_call_handler,
  * plperl_inline_handler, and plperl_validator.
  */
+#ifdef USE_PERL
 
 PG_FUNCTION_INFO_V1(plperl_call_handler);
 
@@ -2064,11 +2069,14 @@ plperl_validator(PG_FUNCTION_ARGS)
 	return plperl_validator_internal(fcinfo, true);
 }
 
+#endif  /* USE_PERL */
+
 
 /*
  * plperlu likewise requires three externally visible functions:
  * plperlu_call_handler, plperlu_inline_handler, and plperlu_validator.
  */
+#ifdef USE_PERLU
 
 PG_FUNCTION_INFO_V1(plperlu_call_handler);
 
@@ -2095,6 +2103,8 @@ plperlu_validator(PG_FUNCTION_ARGS)
 	return plperl_validator_internal(fcinfo, false);
 }
 
+#endif  /* USE_PERLU */
+
 
 /*
  * Uses mkfunc to create a subroutine whose text is
diff --git a/src/pl/plperl/sql/plperl_setup.sql b/src/pl/plperl/sql/plperl_setup.sql
index a89cf56617..eb29cacdac 100644
--- a/src/pl/plperl/sql/plperl_setup.sql
+++ b/src/pl/plperl/sql/plperl_setup.sql
@@ -1,10 +1,10 @@
 --
--- Install the plperl and plperlu extensions
+-- Install plperl
 --
 
 -- Before going ahead with the to-be-tested installations, verify that
--- a non-superuser is allowed to install plperl (but not plperlu) when
--- suitable permissions have been granted.
+-- a non-superuser is allowed to install plperl when suitable permissions
+-- have been granted.
 
 CREATE USER regress_user1;
 CREATE USER regress_user2;
@@ -12,7 +12,6 @@ CREATE USER regress_user2;
 SET ROLE regress_user1;
 
 CREATE EXTENSION plperl;  -- fail
-CREATE EXTENSION plperlu;  -- fail
 
 RESET ROLE;
 
@@ -26,7 +25,6 @@ $$;
 SET ROLE regress_user1;
 
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;  -- fail
 CREATE SCHEMA plperl_setup_scratch;
 SET search_path = plperl_setup_scratch;
 GRANT ALL ON SCHEMA plperl_setup_scratch TO regress_user2;
@@ -68,6 +66,5 @@ DROP OWNED BY regress_user1;
 DROP USER regress_user1;
 DROP USER regress_user2;
 
--- Now install the versions that will be used by subsequent test scripts.
+-- Now install the version that will be used by subsequent test scripts.
 CREATE EXTENSION plperl;
-CREATE EXTENSION plperlu;
diff --git a/src/pl/plperl/sql/plperlu.sql b/src/pl/plperl/sql/plperlu.sql
index be43df5d90..1e050e8237 100644
--- a/src/pl/plperl/sql/plperlu.sql
+++ b/src/pl/plperl/sql/plperlu.sql
@@ -1,5 +1,34 @@
+-- Before going ahead with the to-be-tested installations, verify that
+-- only superusers can install plperlu.
+CREATE USER regress_user1;
+
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+RESET ROLE;
+
+DO $$
+begin
+  execute format('grant create on database %I to regress_user1',
+                 current_database());
+end;
+$$;
+
+SET ROLE regress_user1;
+CREATE EXTENSION plperlu;  -- fail
+RESET ROLE;
+
+DO $$
+begin
+  execute format('revoke create on database %I from regress_user1',
+                 current_database());
+end;
+$$;
+
+DROP ROLE regress_user1;
+
 -- Use ONLY plperlu tests here. For plperl/plerlu combined tests
 -- see plperl_plperlu.sql
+CREATE EXTENSION plperlu;
 
 -- This test tests setting on_plperlu_init after loading plperl
 LOAD 'plperl';
-- 
2.25.1


--ReaqsoxgOBHFXBhH
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0003-Allow-building-only-trusted-or-untrusted-PL-Tcl.patch"



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* [PATCH v1 3/3] Allow building only trusted or untrusted PL/Tcl.
@ 2022-05-18 21:33 Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Nathan Bossart @ 2022-05-18 21:33 UTC (permalink / raw)

Presently, when the --with-tcl configuration option is used, both
trusted and untrusted PL/Tcl are built.  However, some users may
only want to build one or the other.  This change introduces an
optional argument that can be used to do so.  If
--with-tcl='trusted' is specified, only trusted PL/Tcl is built.
If --with-tcl='untrusted' is specified, only untrusted PL/Tcl is
built.  If --with-tcl is given without an argument, both trusted
and untrusted PL/Tcl are built.
---
 configure                      | 47 +++++++++++++++++++++++++++++++---
 configure.ac                   | 32 ++++++++++++++++++++++-
 doc/src/sgml/installation.sgml | 23 ++++++++++++++++-
 src/Makefile.global.in         |  2 ++
 src/include/pg_config.h.in     |  6 +++++
 src/pl/tcl/Makefile            | 12 ++++++---
 src/pl/tcl/pltcl.c             | 16 ++++++++++--
 7 files changed, 126 insertions(+), 12 deletions(-)

diff --git a/configure b/configure
index faa8b1a2e3..046fc4dcf0 100755
--- a/configure
+++ b/configure
@@ -726,6 +726,8 @@ with_python
 PERL_UNTRUSTED
 PERL_TRUSTED
 with_perl
+TCL_UNTRUSTED
+TCL_TRUSTED
 with_tcl
 ICU_LIBS
 ICU_CFLAGS
@@ -1563,7 +1565,8 @@ Optional Packages:
   --with-CC=CMD           set compiler (deprecated)
   --with-llvm             build with LLVM based JIT support
   --with-icu              build with ICU support
-  --with-tcl              build Tcl modules (PL/Tcl)
+  --with-tcl[=TRUSTWORTHINESS]
+                          build Tcl modules (PL/Tcl)
   --with-tclconfig=DIR    tclConfig.sh is in DIR
   --with-perl[=TRUSTWORTHINESS]
                           build Perl modules (PL/Perl)
@@ -8097,26 +8100,62 @@ if test "${with_tcl+set}" = set; then :
   withval=$with_tcl;
   case $withval in
     yes)
-      :
+
+  TCL_TRUSTED=yes
+  TCL_UNTRUSTED=yes
+
       ;;
     no)
       :
       ;;
     *)
-      as_fn_error $? "no argument expected for --with-tcl option" "$LINENO" 5
+      with_tcl=yes
+
+  if test "$withval" = trusted ; then
+    TCL_TRUSTED=yes
+    TCL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    TCL_TRUSTED=no
+    TCL_UNTRUSTED=yes
+  else
+    as_fn_error $? "invalid --with-tcl value: argument must be omitted or specified as 'trusted' or 'untrusted'" "$LINENO" 5
+  fi
+
       ;;
   esac
 
 else
   with_tcl=no
-
 fi
 
 
+
+if test "$with_tcl" = yes; then
+
+  if test "$TCL_TRUSTED" = yes ; then
+
+$as_echo "#define USE_TCL 1" >>confdefs.h
+
+  fi
+  if test "$TCL_UNTRUSTED" = yes ; then
+
+$as_echo "#define USE_TCLU 1" >>confdefs.h
+
+  fi
+
+else
+
+  TCL_TRUSTED=no
+  TCL_UNTRUSTED=no
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_tcl" >&5
 $as_echo "$with_tcl" >&6; }
 
 
+
+
 # We see if the path to the Tcl/Tk configuration scripts is specified.
 # This will override the use of tclsh to find the paths to search.
 
diff --git a/configure.ac b/configure.ac
index 0dd440131b..8e7d96a6fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -810,9 +810,39 @@ fi
 # Optionally build Tcl modules (PL/Tcl)
 #
 AC_MSG_CHECKING([whether to build with Tcl])
-PGAC_ARG_BOOL(with, tcl, no, [build Tcl modules (PL/Tcl)])
+PGAC_ARG_OPTARG(with, tcl,
+[TRUSTWORTHINESS], [build Tcl modules (PL/Tcl)],
+[
+  TCL_TRUSTED=yes
+  TCL_UNTRUSTED=yes
+],
+[
+  if test "$withval" = trusted ; then
+    TCL_TRUSTED=yes
+    TCL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    TCL_TRUSTED=no
+    TCL_UNTRUSTED=yes
+  else
+    AC_MSG_ERROR([invalid --with-tcl value: argument must be omitted or specified as 'trusted' or 'untrusted'])
+  fi
+],
+[
+  if test "$TCL_TRUSTED" = yes ; then
+    AC_DEFINE([USE_TCL], 1, [Define to 1 to build with trusted Tcl support. (--with-tcl='trusted')])
+  fi
+  if test "$TCL_UNTRUSTED" = yes ; then
+    AC_DEFINE([USE_TCLU], 1, [Define to 1 to build with untrusted Tcl support. (--with-tcl='untrusted')])
+  fi
+],
+[
+  TCL_TRUSTED=no
+  TCL_UNTRUSTED=no
+])
 AC_MSG_RESULT([$with_tcl])
 AC_SUBST([with_tcl])
+AC_SUBST(TCL_TRUSTED)
+AC_SUBST(TCL_UNTRUSTED)
 
 # We see if the path to the Tcl/Tk configuration scripts is specified.
 # This will override the use of tclsh to find the paths to search.
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 4377e9d51a..8d96152dd2 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -912,10 +912,31 @@ build-postgresql:
       </varlistentry>
 
       <varlistentry>
-       <term><option>--with-tcl</option></term>
+       <term><option>--with_tcl<optional>=<replaceable>TRUSTWORTHINESS</replaceable></optional></option></term>
        <listitem>
         <para>
          Build the <application>PL/Tcl</application> server-side language.
+         <replaceable>TRUSTWORTHINESS</replaceable> is an optional argument and,
+         if provided, must be one of:
+        </para>
+        <itemizedlist>
+         <listitem>
+          <para>
+           <option>trusted</option> to build only trusted
+           <application>PL/Tcl</application>
+          </para>
+         </listitem>
+         <listitem>
+          <para>
+           <option>untrusted</option> to build only untrusted
+           <application>PL/Tcl</application>
+           (<application>PL/TclU</application>)
+          </para>
+         </listitem>
+        </itemizedlist>
+        <para>
+         If <replaceable>TRUSTWORTHINESS</replaceable> is not specified, both
+         trusted and untrusted <application>PL/Tcl</application> will be built.
         </para>
        </listitem>
       </varlistentry>
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 53f367ca7a..0262ee6d96 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -528,6 +528,8 @@ DEF_PGPORT = @default_port@
 WANTED_LANGUAGES = @WANTED_LANGUAGES@
 PERL_TRUSTED = @PERL_TRUSTED@
 PERL_UNTRUSTED = @PERL_UNTRUSTED@
+TCL_TRUSTED = @TCL_TRUSTED@
+TCL_UNTRUSTED = @TCL_UNTRUSTED@
 
 
 ##########################################################################
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 2779f5f671..c7eb244050 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -955,6 +955,12 @@
 /* Define to select SysV-style shared memory. */
 #undef USE_SYSV_SHARED_MEMORY
 
+/* Define to 1 to build with trusted Tcl support. (--with-tcl='trusted') */
+#undef USE_TCL
+
+/* Define to 1 to build with untrusted Tcl support. (--with-tcl='untrusted') */
+#undef USE_TCLU
+
 /* Define to select unnamed POSIX semaphores. */
 #undef USE_UNNAMED_POSIX_SEMAPHORES
 
diff --git a/src/pl/tcl/Makefile b/src/pl/tcl/Makefile
index 25e65189b6..842d425969 100644
--- a/src/pl/tcl/Makefile
+++ b/src/pl/tcl/Makefile
@@ -26,11 +26,15 @@ OBJS = \
 	$(WIN32RES) \
 	pltcl.o
 
-DATA = pltcl.control pltcl--1.0.sql \
-       pltclu.control pltclu--1.0.sql
+ifeq ($(TCL_TRUSTED), yes)
+	DATA += pltcl.control pltcl--1.0.sql
+	REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=pltcl
+	REGRESS = pltcl_setup pltcl_queries pltcl_trigger pltcl_call pltcl_start_proc pltcl_subxact pltcl_unicode pltcl_transaction
+endif
 
-REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=pltcl
-REGRESS = pltcl_setup pltcl_queries pltcl_trigger pltcl_call pltcl_start_proc pltcl_subxact pltcl_unicode pltcl_transaction
+ifeq ($(TCL_UNTRUSTED), yes)
+	DATA += pltclu.control pltclu--1.0.sql
+endif
 
 # Tcl on win32 ships with import libraries only for Microsoft Visual C++,
 # which are not compatible with mingw gcc. Therefore we need to build a
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 0dd6d8ab2c..57c5fc79fb 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -459,6 +459,7 @@ _PG_init(void)
 	/************************************************************
 	 * Define PL/Tcl's custom GUCs
 	 ************************************************************/
+#ifdef USE_TCL
 	DefineCustomStringVariable("pltcl.start_proc",
 							   gettext_noop("PL/Tcl function to call once when pltcl is first used."),
 							   NULL,
@@ -466,6 +467,10 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+	MarkGUCPrefixReserved("pltcl");
+#endif	/* USE_TCL */
+
+#ifdef USE_TCLU
 	DefineCustomStringVariable("pltclu.start_proc",
 							   gettext_noop("PL/TclU function to call once when pltclu is first used."),
 							   NULL,
@@ -473,9 +478,8 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
-
-	MarkGUCPrefixReserved("pltcl");
 	MarkGUCPrefixReserved("pltclu");
+#endif	/* USE_TCLU */
 
 	pltcl_pm_init_done = true;
 }
@@ -690,6 +694,8 @@ start_proc_error_callback(void *arg)
  *				  call this function for execution of
  *				  PL/Tcl procedures.
  **********************************************************************/
+#ifdef USE_TCL
+
 PG_FUNCTION_INFO_V1(pltcl_call_handler);
 
 /* keep non-static */
@@ -699,6 +705,10 @@ pltcl_call_handler(PG_FUNCTION_ARGS)
 	return pltcl_handler(fcinfo, true);
 }
 
+#endif /* USE_TCL */
+
+#ifdef USE_TCLU
+
 /*
  * Alternative handler for unsafe functions
  */
@@ -711,6 +721,8 @@ pltclu_call_handler(PG_FUNCTION_ARGS)
 	return pltcl_handler(fcinfo, false);
 }
 
+#endif	/* USE_TCLU */
+
 
 /**********************************************************************
  * pltcl_handler()		- Handler for function and trigger calls, for
-- 
2.25.1


--ReaqsoxgOBHFXBhH--





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* [PATCH v1 3/3] Allow building only trusted or untrusted PL/Tcl.
@ 2022-05-18 21:33 Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Nathan Bossart @ 2022-05-18 21:33 UTC (permalink / raw)

Presently, when the --with-tcl configuration option is used, both
trusted and untrusted PL/Tcl are built.  However, some users may
only want to build one or the other.  This change introduces an
optional argument that can be used to do so.  If
--with-tcl='trusted' is specified, only trusted PL/Tcl is built.
If --with-tcl='untrusted' is specified, only untrusted PL/Tcl is
built.  If --with-tcl is given without an argument, both trusted
and untrusted PL/Tcl are built.
---
 configure                      | 47 +++++++++++++++++++++++++++++++---
 configure.ac                   | 32 ++++++++++++++++++++++-
 doc/src/sgml/installation.sgml | 23 ++++++++++++++++-
 src/Makefile.global.in         |  2 ++
 src/include/pg_config.h.in     |  6 +++++
 src/pl/tcl/Makefile            | 12 ++++++---
 src/pl/tcl/pltcl.c             | 16 ++++++++++--
 7 files changed, 126 insertions(+), 12 deletions(-)

diff --git a/configure b/configure
index faa8b1a2e3..046fc4dcf0 100755
--- a/configure
+++ b/configure
@@ -726,6 +726,8 @@ with_python
 PERL_UNTRUSTED
 PERL_TRUSTED
 with_perl
+TCL_UNTRUSTED
+TCL_TRUSTED
 with_tcl
 ICU_LIBS
 ICU_CFLAGS
@@ -1563,7 +1565,8 @@ Optional Packages:
   --with-CC=CMD           set compiler (deprecated)
   --with-llvm             build with LLVM based JIT support
   --with-icu              build with ICU support
-  --with-tcl              build Tcl modules (PL/Tcl)
+  --with-tcl[=TRUSTWORTHINESS]
+                          build Tcl modules (PL/Tcl)
   --with-tclconfig=DIR    tclConfig.sh is in DIR
   --with-perl[=TRUSTWORTHINESS]
                           build Perl modules (PL/Perl)
@@ -8097,26 +8100,62 @@ if test "${with_tcl+set}" = set; then :
   withval=$with_tcl;
   case $withval in
     yes)
-      :
+
+  TCL_TRUSTED=yes
+  TCL_UNTRUSTED=yes
+
       ;;
     no)
       :
       ;;
     *)
-      as_fn_error $? "no argument expected for --with-tcl option" "$LINENO" 5
+      with_tcl=yes
+
+  if test "$withval" = trusted ; then
+    TCL_TRUSTED=yes
+    TCL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    TCL_TRUSTED=no
+    TCL_UNTRUSTED=yes
+  else
+    as_fn_error $? "invalid --with-tcl value: argument must be omitted or specified as 'trusted' or 'untrusted'" "$LINENO" 5
+  fi
+
       ;;
   esac
 
 else
   with_tcl=no
-
 fi
 
 
+
+if test "$with_tcl" = yes; then
+
+  if test "$TCL_TRUSTED" = yes ; then
+
+$as_echo "#define USE_TCL 1" >>confdefs.h
+
+  fi
+  if test "$TCL_UNTRUSTED" = yes ; then
+
+$as_echo "#define USE_TCLU 1" >>confdefs.h
+
+  fi
+
+else
+
+  TCL_TRUSTED=no
+  TCL_UNTRUSTED=no
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_tcl" >&5
 $as_echo "$with_tcl" >&6; }
 
 
+
+
 # We see if the path to the Tcl/Tk configuration scripts is specified.
 # This will override the use of tclsh to find the paths to search.
 
diff --git a/configure.ac b/configure.ac
index 0dd440131b..8e7d96a6fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -810,9 +810,39 @@ fi
 # Optionally build Tcl modules (PL/Tcl)
 #
 AC_MSG_CHECKING([whether to build with Tcl])
-PGAC_ARG_BOOL(with, tcl, no, [build Tcl modules (PL/Tcl)])
+PGAC_ARG_OPTARG(with, tcl,
+[TRUSTWORTHINESS], [build Tcl modules (PL/Tcl)],
+[
+  TCL_TRUSTED=yes
+  TCL_UNTRUSTED=yes
+],
+[
+  if test "$withval" = trusted ; then
+    TCL_TRUSTED=yes
+    TCL_UNTRUSTED=no
+  elif test "$withval" = untrusted ; then
+    TCL_TRUSTED=no
+    TCL_UNTRUSTED=yes
+  else
+    AC_MSG_ERROR([invalid --with-tcl value: argument must be omitted or specified as 'trusted' or 'untrusted'])
+  fi
+],
+[
+  if test "$TCL_TRUSTED" = yes ; then
+    AC_DEFINE([USE_TCL], 1, [Define to 1 to build with trusted Tcl support. (--with-tcl='trusted')])
+  fi
+  if test "$TCL_UNTRUSTED" = yes ; then
+    AC_DEFINE([USE_TCLU], 1, [Define to 1 to build with untrusted Tcl support. (--with-tcl='untrusted')])
+  fi
+],
+[
+  TCL_TRUSTED=no
+  TCL_UNTRUSTED=no
+])
 AC_MSG_RESULT([$with_tcl])
 AC_SUBST([with_tcl])
+AC_SUBST(TCL_TRUSTED)
+AC_SUBST(TCL_UNTRUSTED)
 
 # We see if the path to the Tcl/Tk configuration scripts is specified.
 # This will override the use of tclsh to find the paths to search.
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 4377e9d51a..8d96152dd2 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -912,10 +912,31 @@ build-postgresql:
       </varlistentry>
 
       <varlistentry>
-       <term><option>--with-tcl</option></term>
+       <term><option>--with_tcl<optional>=<replaceable>TRUSTWORTHINESS</replaceable></optional></option></term>
        <listitem>
         <para>
          Build the <application>PL/Tcl</application> server-side language.
+         <replaceable>TRUSTWORTHINESS</replaceable> is an optional argument and,
+         if provided, must be one of:
+        </para>
+        <itemizedlist>
+         <listitem>
+          <para>
+           <option>trusted</option> to build only trusted
+           <application>PL/Tcl</application>
+          </para>
+         </listitem>
+         <listitem>
+          <para>
+           <option>untrusted</option> to build only untrusted
+           <application>PL/Tcl</application>
+           (<application>PL/TclU</application>)
+          </para>
+         </listitem>
+        </itemizedlist>
+        <para>
+         If <replaceable>TRUSTWORTHINESS</replaceable> is not specified, both
+         trusted and untrusted <application>PL/Tcl</application> will be built.
         </para>
        </listitem>
       </varlistentry>
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 53f367ca7a..0262ee6d96 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -528,6 +528,8 @@ DEF_PGPORT = @default_port@
 WANTED_LANGUAGES = @WANTED_LANGUAGES@
 PERL_TRUSTED = @PERL_TRUSTED@
 PERL_UNTRUSTED = @PERL_UNTRUSTED@
+TCL_TRUSTED = @TCL_TRUSTED@
+TCL_UNTRUSTED = @TCL_UNTRUSTED@
 
 
 ##########################################################################
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 2779f5f671..c7eb244050 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -955,6 +955,12 @@
 /* Define to select SysV-style shared memory. */
 #undef USE_SYSV_SHARED_MEMORY
 
+/* Define to 1 to build with trusted Tcl support. (--with-tcl='trusted') */
+#undef USE_TCL
+
+/* Define to 1 to build with untrusted Tcl support. (--with-tcl='untrusted') */
+#undef USE_TCLU
+
 /* Define to select unnamed POSIX semaphores. */
 #undef USE_UNNAMED_POSIX_SEMAPHORES
 
diff --git a/src/pl/tcl/Makefile b/src/pl/tcl/Makefile
index 25e65189b6..842d425969 100644
--- a/src/pl/tcl/Makefile
+++ b/src/pl/tcl/Makefile
@@ -26,11 +26,15 @@ OBJS = \
 	$(WIN32RES) \
 	pltcl.o
 
-DATA = pltcl.control pltcl--1.0.sql \
-       pltclu.control pltclu--1.0.sql
+ifeq ($(TCL_TRUSTED), yes)
+	DATA += pltcl.control pltcl--1.0.sql
+	REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=pltcl
+	REGRESS = pltcl_setup pltcl_queries pltcl_trigger pltcl_call pltcl_start_proc pltcl_subxact pltcl_unicode pltcl_transaction
+endif
 
-REGRESS_OPTS = --dbname=$(PL_TESTDB) --load-extension=pltcl
-REGRESS = pltcl_setup pltcl_queries pltcl_trigger pltcl_call pltcl_start_proc pltcl_subxact pltcl_unicode pltcl_transaction
+ifeq ($(TCL_UNTRUSTED), yes)
+	DATA += pltclu.control pltclu--1.0.sql
+endif
 
 # Tcl on win32 ships with import libraries only for Microsoft Visual C++,
 # which are not compatible with mingw gcc. Therefore we need to build a
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 0dd6d8ab2c..57c5fc79fb 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -459,6 +459,7 @@ _PG_init(void)
 	/************************************************************
 	 * Define PL/Tcl's custom GUCs
 	 ************************************************************/
+#ifdef USE_TCL
 	DefineCustomStringVariable("pltcl.start_proc",
 							   gettext_noop("PL/Tcl function to call once when pltcl is first used."),
 							   NULL,
@@ -466,6 +467,10 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
+	MarkGUCPrefixReserved("pltcl");
+#endif	/* USE_TCL */
+
+#ifdef USE_TCLU
 	DefineCustomStringVariable("pltclu.start_proc",
 							   gettext_noop("PL/TclU function to call once when pltclu is first used."),
 							   NULL,
@@ -473,9 +478,8 @@ _PG_init(void)
 							   NULL,
 							   PGC_SUSET, 0,
 							   NULL, NULL, NULL);
-
-	MarkGUCPrefixReserved("pltcl");
 	MarkGUCPrefixReserved("pltclu");
+#endif	/* USE_TCLU */
 
 	pltcl_pm_init_done = true;
 }
@@ -690,6 +694,8 @@ start_proc_error_callback(void *arg)
  *				  call this function for execution of
  *				  PL/Tcl procedures.
  **********************************************************************/
+#ifdef USE_TCL
+
 PG_FUNCTION_INFO_V1(pltcl_call_handler);
 
 /* keep non-static */
@@ -699,6 +705,10 @@ pltcl_call_handler(PG_FUNCTION_ARGS)
 	return pltcl_handler(fcinfo, true);
 }
 
+#endif /* USE_TCL */
+
+#ifdef USE_TCLU
+
 /*
  * Alternative handler for unsafe functions
  */
@@ -711,6 +721,8 @@ pltclu_call_handler(PG_FUNCTION_ARGS)
 	return pltcl_handler(fcinfo, false);
 }
 
+#endif	/* USE_TCLU */
+
 
 /**********************************************************************
  * pltcl_handler()		- Handler for function and trigger calls, for
-- 
2.25.1


--ReaqsoxgOBHFXBhH--





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* [PATCH v4 3/8] Introduce multiple shmem segments for shared buffers
@ 2025-03-15 15:38 Dmitrii Dolgov <9erthalion6@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dmitrii Dolgov @ 2025-03-15 15:38 UTC (permalink / raw)

Add more shmem segments to split shared buffers into following chunks:
* BUFFERS_SHMEM_SEGMENT: contains buffer blocks
* BUFFER_DESCRIPTORS_SHMEM_SEGMENT: contains buffer descriptors
* BUFFER_IOCV_SHMEM_SEGMENT: contains condition variables for buffers
* CHECKPOINT_BUFFERS_SHMEM_SEGMENT: contains checkpoint buffer ids
* STRATEGY_SHMEM_SEGMENT: contains buffer strategy status

Size of the corresponding shared data directly depends on NBuffers,
meaning that if we would like to change NBuffers, they have to be
resized correspondingly. Placing each of them in a separate shmem
segment allows to achieve that.

There are some asumptions made about each of shmem segments upper size
limit. The buffer blocks have the largest, while the rest claim less
extra room for resize. Ideally those limits have to be deduced from the
maximum allowed shared memory.
---
 src/backend/port/sysv_shmem.c          | 24 +++++++-
 src/backend/storage/buffer/buf_init.c  | 79 +++++++++++++++++---------
 src/backend/storage/buffer/buf_table.c |  6 +-
 src/backend/storage/buffer/freelist.c  |  5 +-
 src/backend/storage/ipc/ipci.c         |  2 +-
 src/include/storage/bufmgr.h           |  2 +-
 src/include/storage/pg_shmem.h         | 24 +++++++-
 7 files changed, 105 insertions(+), 37 deletions(-)

diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index a0f03ff868f..f46d9d5d9cd 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -147,10 +147,18 @@ static int next_free_segment = 0;
  *
  * The reserved space pointer is calculated to slice up the total reserved
  * space into fixed fractions of address space for each segment, as specified
- * in the SHMEM_RESIZE_RATIO array.
+ * in the SHMEM_RESIZE_RATIO array. E.g. we allow BUFFERS_SHMEM_SEGMENT to take
+ * up to 60% of the whole space when resizing, based on the fact that it most
+ * likely will be the main consumer of this memory. Those numbers are pulled
+ * out of thin air for now, makes sense to evaluate them more precise.
  */
-static double SHMEM_RESIZE_RATIO[1] = {
-	1.0, 									/* MAIN_SHMEM_SLOT */
+static double SHMEM_RESIZE_RATIO[6] = {
+	0.1,    /* MAIN_SHMEM_SEGMENT */
+	0.6,    /* BUFFERS_SHMEM_SEGMENT */
+	0.1,    /* BUFFER_DESCRIPTORS_SHMEM_SEGMENT */
+	0.1,    /* BUFFER_IOCV_SHMEM_SEGMENT */
+	0.05,   /* CHECKPOINT_BUFFERS_SHMEM_SEGMENT */
+	0.05,   /* STRATEGY_SHMEM_SEGMENT */
 };
 
 /*
@@ -182,6 +190,16 @@ MappingName(int shmem_segment)
 	{
 		case MAIN_SHMEM_SEGMENT:
 			return "main";
+		case BUFFERS_SHMEM_SEGMENT:
+			return "buffers";
+		case BUFFER_DESCRIPTORS_SHMEM_SEGMENT:
+			return "descriptors";
+		case BUFFER_IOCV_SHMEM_SEGMENT:
+			return "iocv";
+		case CHECKPOINT_BUFFERS_SHMEM_SEGMENT:
+			return "checkpoint";
+		case STRATEGY_SHMEM_SEGMENT:
+			return "strategy";
 		default:
 			return "unknown";
 	}
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index ed1dc488a42..bd68b69ee98 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -62,7 +62,10 @@ CkptSortItem *CkptBufferIds;
  * Initialize shared buffer pool
  *
  * This is called once during shared-memory initialization (either in the
- * postmaster, or in a standalone backend).
+ * postmaster, or in a standalone backend). Size of data structures initialized
+ * here depends on NBuffers, and to be able to change NBuffers without a
+ * restart we store each structure into a separate shared memory segment, which
+ * could be resized on demand.
  */
 void
 BufferManagerShmemInit(void)
@@ -74,22 +77,22 @@ BufferManagerShmemInit(void)
 
 	/* Align descriptors to a cacheline boundary. */
 	BufferDescriptors = (BufferDescPadded *)
-		ShmemInitStruct("Buffer Descriptors",
+		ShmemInitStructInSegment("Buffer Descriptors",
 						NBuffers * sizeof(BufferDescPadded),
-						&foundDescs);
+						&foundDescs, BUFFER_DESCRIPTORS_SHMEM_SEGMENT);
 
 	/* Align buffer pool on IO page size boundary. */
 	BufferBlocks = (char *)
 		TYPEALIGN(PG_IO_ALIGN_SIZE,
-				  ShmemInitStruct("Buffer Blocks",
+				  ShmemInitStructInSegment("Buffer Blocks",
 								  NBuffers * (Size) BLCKSZ + PG_IO_ALIGN_SIZE,
-								  &foundBufs));
+								  &foundBufs, BUFFERS_SHMEM_SEGMENT));
 
 	/* Align condition variables to cacheline boundary. */
 	BufferIOCVArray = (ConditionVariableMinimallyPadded *)
-		ShmemInitStruct("Buffer IO Condition Variables",
+		ShmemInitStructInSegment("Buffer IO Condition Variables",
 						NBuffers * sizeof(ConditionVariableMinimallyPadded),
-						&foundIOCV);
+						&foundIOCV, BUFFER_IOCV_SHMEM_SEGMENT);
 
 	/*
 	 * The array used to sort to-be-checkpointed buffer ids is located in
@@ -99,8 +102,9 @@ BufferManagerShmemInit(void)
 	 * painful.
 	 */
 	CkptBufferIds = (CkptSortItem *)
-		ShmemInitStruct("Checkpoint BufferIds",
-						NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
+		ShmemInitStructInSegment("Checkpoint BufferIds",
+						NBuffers * sizeof(CkptSortItem), &foundBufCkpt,
+						CHECKPOINT_BUFFERS_SHMEM_SEGMENT);
 
 	if (foundDescs || foundBufs || foundIOCV || foundBufCkpt)
 	{
@@ -156,33 +160,54 @@ BufferManagerShmemInit(void)
  * BufferManagerShmemSize
  *
  * compute the size of shared memory for the buffer pool including
- * data pages, buffer descriptors, hash tables, etc.
+ * data pages, buffer descriptors, hash tables, etc. based on the
+ * shared memory segment. The main segment must not allocate anything
+ * related to buffers, every other segment will receive part of the
+ * data.
  */
 Size
-BufferManagerShmemSize(void)
+BufferManagerShmemSize(int shmem_segment)
 {
 	Size		size = 0;
 
-	/* size of buffer descriptors */
-	size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
-	/* to allow aligning buffer descriptors */
-	size = add_size(size, PG_CACHE_LINE_SIZE);
+	if (shmem_segment == MAIN_SHMEM_SEGMENT)
+		return size;
 
-	/* size of data pages, plus alignment padding */
-	size = add_size(size, PG_IO_ALIGN_SIZE);
-	size = add_size(size, mul_size(NBuffers, BLCKSZ));
+	if (shmem_segment == BUFFER_DESCRIPTORS_SHMEM_SEGMENT)
+	{
+		/* size of buffer descriptors */
+		size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
+		/* to allow aligning buffer descriptors */
+		size = add_size(size, PG_CACHE_LINE_SIZE);
+	}
 
-	/* size of stuff controlled by freelist.c */
-	size = add_size(size, StrategyShmemSize());
+	if (shmem_segment == BUFFERS_SHMEM_SEGMENT)
+	{
+		/* size of data pages, plus alignment padding */
+		size = add_size(size, PG_IO_ALIGN_SIZE);
+		size = add_size(size, mul_size(NBuffers, BLCKSZ));
+	}
 
-	/* size of I/O condition variables */
-	size = add_size(size, mul_size(NBuffers,
-								   sizeof(ConditionVariableMinimallyPadded)));
-	/* to allow aligning the above */
-	size = add_size(size, PG_CACHE_LINE_SIZE);
+	if (shmem_segment == STRATEGY_SHMEM_SEGMENT)
+	{
+		/* size of stuff controlled by freelist.c */
+		size = add_size(size, StrategyShmemSize());
+	}
 
-	/* size of checkpoint sort array in bufmgr.c */
-	size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
+	if (shmem_segment == BUFFER_IOCV_SHMEM_SEGMENT)
+	{
+		/* size of I/O condition variables */
+		size = add_size(size, mul_size(NBuffers,
+									   sizeof(ConditionVariableMinimallyPadded)));
+		/* to allow aligning the above */
+		size = add_size(size, PG_CACHE_LINE_SIZE);
+	}
+
+	if (shmem_segment == CHECKPOINT_BUFFERS_SHMEM_SEGMENT)
+	{
+		/* size of checkpoint sort array in bufmgr.c */
+		size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
+	}
 
 	return size;
 }
diff --git a/src/backend/storage/buffer/buf_table.c b/src/backend/storage/buffer/buf_table.c
index a50955d5286..a9952b36eba 100644
--- a/src/backend/storage/buffer/buf_table.c
+++ b/src/backend/storage/buffer/buf_table.c
@@ -22,6 +22,7 @@
 #include "postgres.h"
 
 #include "storage/buf_internals.h"
+#include "storage/pg_shmem.h"
 
 /* entry for buffer lookup hashtable */
 typedef struct
@@ -59,10 +60,11 @@ InitBufTable(int size)
 	info.entrysize = sizeof(BufferLookupEnt);
 	info.num_partitions = NUM_BUFFER_PARTITIONS;
 
-	SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
+	SharedBufHash = ShmemInitHashInSegment("Shared Buffer Lookup Table",
 								  size, size,
 								  &info,
-								  HASH_ELEM | HASH_BLOBS | HASH_PARTITION);
+								  HASH_ELEM | HASH_BLOBS | HASH_PARTITION,
+								  STRATEGY_SHMEM_SEGMENT);
 }
 
 /*
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index 336715b6c63..81543cb5ced 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -19,6 +19,7 @@
 #include "port/atomics.h"
 #include "storage/buf_internals.h"
 #include "storage/bufmgr.h"
+#include "storage/pg_shmem.h"
 #include "storage/proc.h"
 
 #define INT_ACCESS_ONCE(var)	((int)(*((volatile int *)&(var))))
@@ -491,9 +492,9 @@ StrategyInitialize(bool init)
 	 * Get or create the shared strategy control block
 	 */
 	StrategyControl = (BufferStrategyControl *)
-		ShmemInitStruct("Buffer Strategy Status",
+		ShmemInitStructInSegment("Buffer Strategy Status",
 						sizeof(BufferStrategyControl),
-						&found);
+						&found, STRATEGY_SHMEM_SEGMENT);
 
 	if (!found)
 	{
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 076888c0172..9d00b80b4f8 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -113,7 +113,7 @@ CalculateShmemSize(int *num_semaphores, int shmem_segment)
 											 sizeof(ShmemIndexEnt)));
 	size = add_size(size, dsm_estimate_size());
 	size = add_size(size, DSMRegistryShmemSize());
-	size = add_size(size, BufferManagerShmemSize());
+	size = add_size(size, BufferManagerShmemSize(shmem_segment));
 	size = add_size(size, LockManagerShmemSize());
 	size = add_size(size, PredicateLockShmemSize());
 	size = add_size(size, ProcGlobalShmemSize());
diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h
index f2192ceb271..1977001e533 100644
--- a/src/include/storage/bufmgr.h
+++ b/src/include/storage/bufmgr.h
@@ -308,7 +308,7 @@ extern bool EvictUnpinnedBuffer(Buffer buf);
 
 /* in buf_init.c */
 extern void BufferManagerShmemInit(void);
-extern Size BufferManagerShmemSize(void);
+extern Size BufferManagerShmemSize(int);
 
 /* in localbuf.c */
 extern void AtProcExit_LocalBuffers(void);
diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h
index 4a83e255652..c5009a1cd73 100644
--- a/src/include/storage/pg_shmem.h
+++ b/src/include/storage/pg_shmem.h
@@ -52,7 +52,7 @@ typedef struct ShmemSegment
 } ShmemSegment;
 
 /* Number of available segments for anonymous memory mappings */
-#define ANON_MAPPINGS 1
+#define ANON_MAPPINGS 6
 
 extern PGDLLIMPORT ShmemSegment Segments[ANON_MAPPINGS];
 
@@ -107,7 +107,29 @@ extern void PGSharedMemoryDetach(void);
 extern void GetHugePageSize(Size *hugepagesize, int *mmap_flags);
 void *ReserveAnonymousMemory(Size reserve_size);
 
+/*
+ * To be able to dynamically resize largest parts of the data stored in shared
+ * memory, we split it into multiple shared memory mappings segments. Each
+ * segment contains only certain part of the data, which size depends on
+ * NBuffers.
+ */
+
 /* The main segment, contains everything except buffer blocks and related data. */
 #define MAIN_SHMEM_SEGMENT 0
 
+/* Buffer blocks */
+#define BUFFERS_SHMEM_SEGMENT 1
+
+/* Buffer descriptors */
+#define BUFFER_DESCRIPTORS_SHMEM_SEGMENT 2
+
+/* Condition variables for buffers */
+#define BUFFER_IOCV_SHMEM_SEGMENT 3
+
+/* Checkpoint BufferIds */
+#define CHECKPOINT_BUFFERS_SHMEM_SEGMENT 4
+
+/* Buffer strategy status */
+#define STRATEGY_SHMEM_SEGMENT 5
+
 #endif							/* PG_SHMEM_H */
-- 
2.45.1


--vninua6xybvzgrci
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v4-0004-Introduce-pending-flag-for-GUC-assign-hooks.patch"



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2025-03-15 15:38 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17 20:28 [PATCH v1 2/3] Allow building only trusted or untrusted PL/Perl. Nathan Bossart <nathandbossart@gmail.com>
2022-05-17 20:28 [PATCH v1 2/3] Allow building only trusted or untrusted PL/Perl. Nathan Bossart <nathandbossart@gmail.com>
2022-05-18 21:33 [PATCH v1 3/3] Allow building only trusted or untrusted PL/Tcl. Nathan Bossart <nathandbossart@gmail.com>
2022-05-18 21:33 [PATCH v1 3/3] Allow building only trusted or untrusted PL/Tcl. Nathan Bossart <nathandbossart@gmail.com>
2025-03-15 15:38 [PATCH v4 3/8] Introduce multiple shmem segments for shared buffers Dmitrii Dolgov <9erthalion6@gmail.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox