← Back to PostgreSQL, page 5

postgresql-0086 · Hard senior

Why is a SECURITY DEFINER function dangerous without a fixed search_path, and how is it hardened?

A SECURITY DEFINER function runs with its owner's privileges. If it refers to unqualified names and does not set search_path, a caller who can create objects in a schema earlier on the path can substitute their own function or table and run code with the owner's rights. Harden it with SET search_path = pg_catalog, pg_temp (or a trusted schema) in the function definition, qualified names, and REVOKE EXECUTE FROM PUBLIC.

Full answer

Setup for the example:

sql
CREATE SCHEMA vault;
CREATE TABLE vault.secrets (id int, value text);
CREATE TABLE secrets (id int, value text);
CREATE FUNCTION count_secrets_unsafe() RETURNS bigint LANGUAGE sql SECURITY DEFINER AS $$ SELECT count(*) FROM secrets $$;
CREATE FUNCTION count_secrets_safe() RETURNS bigint LANGUAGE sql SECURITY DEFINER
  SET search_path = pg_catalog, pg_temp AS $$ SELECT count(*) FROM vault.secrets $$;
REVOKE EXECUTE ON FUNCTION count_secrets_safe() FROM PUBLIC;
sql
SELECT proname, prosecdef, coalesce(proconfig::text, 'no search_path set') AS config FROM pg_proc WHERE proname LIKE 'count_secrets%' ORDER BY proname;

PostgreSQL returns:

text
       proname        | prosecdef |                config
----------------------+-----------+-------------------------------------
 count_secrets_safe   | t         | {"search_path=pg_catalog, pg_temp"}
 count_secrets_unsafe | t         | no search_path set
(2 rows)
sql
SELECT p.proname FROM pg_proc p WHERE p.prosecdef AND p.pronamespace = current_schema()::regnamespace AND (p.proconfig IS NULL OR NOT EXISTS (SELECT 1 FROM unnest(p.proconfig) c WHERE c LIKE 'search_path=%'));

Result on PostgreSQL 17:

text
       proname
----------------------
 count_secrets_unsafe
(1 row)
sql
DROP SCHEMA vault CASCADE;

Result:

text
NOTICE:  drop cascades to table vault.secrets
DROP SCHEMA

The catalog shows that both functions are security definers, but only the safe one carries its own search_path setting, with pg_temp listed last so temporary objects cannot shadow anything. The second query is an audit that lists security-definer functions without a fixed search_path, here the unsafe one. The actual attack needs a second, less privileged role and was not run here: that role creates a table or function named like the referenced object in a schema it controls and calls the function with that schema first on its path. Besides the fixed path, schema-qualify names in the body and grant EXECUTE only to the roles that need it. The last run removes the demo's vault schema.

sql
CREATE SCHEMA vault;
CREATE TABLE vault.secrets (id int, value text);
CREATE TABLE secrets (id int, value text);
CREATE FUNCTION count_secrets_unsafe() RETURNS bigint LANGUAGE sql SECURITY DEFINER AS $$ SELECT count(*) FROM secrets $$;
CREATE FUNCTION count_secrets_safe() RETURNS bigint LANGUAGE sql SECURITY DEFINER
  SET search_path = pg_catalog, pg_temp AS $$ SELECT count(*) FROM vault.secrets $$;
REVOKE EXECUTE ON FUNCTION count_secrets_safe() FROM PUBLIC;

SELECT proname, prosecdef, coalesce(proconfig::text, 'no search_path set') AS config FROM pg_proc WHERE proname LIKE 'count_secrets%' ORDER BY proname;

SELECT p.proname FROM pg_proc p WHERE p.prosecdef AND p.pronamespace = current_schema()::regnamespace AND (p.proconfig IS NULL OR NOT EXISTS (SELECT 1 FROM unnest(p.proconfig) c WHERE c LIKE 'search_path=%'));

DROP SCHEMA vault CASCADE;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • SECURITY DEFINER runs with owner rights
  • Unqualified names follow the caller's search_path
  • Set search_path on the function
  • Put pg_temp last
  • Revoke EXECUTE from PUBLIC

Common mistakes

  • Creating security definer functions without SET search_path
  • Leaving EXECUTE granted to PUBLIC

Follow-ups

  • How did PostgreSQL 15 change the public schema to reduce this risk?
  • When is SECURITY DEFINER the right tool?

Tips

  • Audit with the catalog