[PATCH] Prevent replacement of a function if it's used in an index expression and is not IMMUTABLE
[PATCH] Prevent replacement of a function if it's used in an index expression and is not IMMUTABLE
Hi hackers,
The purpose of this patch is to prevent replacing a function via `CREATE OR REPLACE FUNCTION` with a new definition that is not marked as `IMMUTABLE`, if the existing function is referenced by an index expression.
Replacing such functions may lead to index corruption or runtime semantic inconsistencies, especially when the function’s output is not stable for the same input.
This patch is motivated by our analysis of both recent and earlier PostgreSQL security vulnerabilities, notably:
- CVE-2020-25695: A privilege escalation issue caused by non-IMMUTABLE expressions.
- CVE-2024-1713: A similar privilege escalation issue related to the `plv8` extension, akin to CVE-2020-25695.
Although these CVE vulnerabilities have been fixed, we believe this patch enforces a stricter rule that further enhances PostgreSQL’s robustness:
If a function is used in an index, it can only be replaced if it is declared as `IMMUTABLE`.
This strategy aligns with PostgreSQL’s established assumption that `IMMUTABLE` functions are safe to use in indexes and their behavior should remain consistent after updates.
We hope this contribution benefits the community, and we welcome your valuable feedback.
Sincerely,
xiaojiluo (Tencent Yunding Lab)
The purpose of this patch is to prevent replacing a function via `CREATE OR REPLACE FUNCTION` with a new definition that is not marked as `IMMUTABLE`, if the existing function is referenced by an index expression.
Replacing such functions may lead to index corruption or runtime semantic inconsistencies, especially when the function’s output is not stable for the same input.
This patch is motivated by our analysis of both recent and earlier PostgreSQL security vulnerabilities, notably:
- CVE-2020-25695: A privilege escalation issue caused by non-IMMUTABLE expressions.
- CVE-2024-1713: A similar privilege escalation issue related to the `plv8` extension, akin to CVE-2020-25695.
Although these CVE vulnerabilities have been fixed, we believe this patch enforces a stricter rule that further enhances PostgreSQL’s robustness:
If a function is used in an index, it can only be replaced if it is declared as `IMMUTABLE`.
This strategy aligns with PostgreSQL’s established assumption that `IMMUTABLE` functions are safe to use in indexes and their behavior should remain consistent after updates.
We hope this contribution benefits the community, and we welcome your valuable feedback.
Sincerely,
xiaojiluo (Tencent Yunding Lab)
Re: [PATCH] Prevent replacement of a function if it's used in an index expression and is not IMMUTABLE
От:
jian he <jian.universality@gmail.com>
Дата:
On Mon, Jun 30, 2025 at 5:34 PM sundayjiang(蒋浩天)
wrote:
>
> The purpose of this patch is to prevent replacing a function via `CREATE OR REPLACE FUNCTION` with a new definition that is not marked as `IMMUTABLE`, if the existing function is referenced by an index expression.
>
> Replacing such functions may lead to index corruption or runtime semantic inconsistencies, especially when the function’s output is not stable for the same input.
>
> If a function is used in an index, it can only be replaced if it is declared as `IMMUTABLE`.
>
looking at the right above code ``if (oldproc->prokind != prokind)``
+ if(oldproc->prokind == PROKIND_FUNCTION && volatility !=
PROVOLATILE_IMMUTABLE){
we can change it to
+ if(prokind == PROKIND_FUNCTION && oldproc->provolatile ==
PROVOLATILE_IMMUTABLE &&
+ volatility != PROVOLATILE_IMMUTABLE)
+ {
curly brace generally begins with a new line.
if (index_found)
+ ereport(ERROR,
+ (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
+ errmsg("cannot replace function \"%s\" with a non-IMMUTABLE function
because it is used by an index",
+ procedureName)));
Here, errcode ERRCODE_FEATURE_NOT_SUPPORTED would be more appropriate.
you can add a simple test in src/test/regress/sql/create_function_sql.sql
for example:
CREATE OR REPLACE FUNCTION get_a_int(int default 2) RETURNS int
IMMUTABLE AS 'select $1' LANGUAGE sql;
create table t1(a int);
create index on t1((get_a_int(a)));
CREATE OR REPLACE FUNCTION get_a_int(int default 2) RETURNS int AS
'select $1' LANGUAGE sql;