postgresql 作为定义程序执行的过程中的事务控制语句

643ylb08  于 2023-02-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(122)

我们刚刚开始使用Postgres,并开始探索它的特性。我们注意到的一件事是,如果过程是作为定义者执行的(过程定义中有SECURITY DEFINER),那么在事务中执行提交是有限制的。
我看到了另一篇关于这个的文章,其中一个建议的解决方案是有一个 Package 过程(作为调用者执行),这些事务控制语句可以在过程调用(作为定义者执行)之后完成。我们想要做的是能够在子过程中的事务内控制它,特别是在执行自治事务时。
我们知道为什么Postgres有这个限制吗?
我们尝试控制模式的访问权限,确保只有特定的用户可以执行模式中的函数或过程。我们还设置了会话ID,尝试了用户/组、ACL,并设置了行级安全的表策略,但仍然存在在哪里执行提交的问题。
但是我们希望能够在作为定义者执行的过程中直接控制提交和回滚,而不是从过程调用的调用者控制提交和回滚。

bjp0bcyl

bjp0bcyl1#

这在ExecuteCallStmt中进行了解释:

/*
 * In security definer procedures, we can't allow transaction commands.
 * StartTransaction() insists that the security context stack is empty,
 * and AbortTransaction() resets the security context.  This could be
 * reorganized, but right now it doesn't work.
 */

实际上,StartTransaction具有

/* SecurityRestrictionContext should never be set outside a transaction */
    Assert(s->prevSecContext == 0);

这可以追溯到commit eedb068c0a7474fb11d67d03b0a9e1ded5df82c4

Make standard maintenance operations (including VACUUM, ANALYZE, REINDEX,
and CLUSTER) execute as the table owner rather than the calling user, using
the same privilege-switching mechanism already used for SECURITY DEFINER
functions.  The purpose of this change is to ensure that user-defined
functions used in index definitions cannot acquire the privileges of a
superuser account that is performing routine maintenance.  While a function
used in an index is supposed to be IMMUTABLE and thus not able to do anything
very interesting, there are several easy ways around that restriction; and
even if we could plug them all, there would remain a risk of reading sensitive
information and broadcasting it through a covert channel such as CPU usage.

To prevent bypassing this security measure, execution of SET SESSION
AUTHORIZATION and SET ROLE is now forbidden within a SECURITY DEFINER context.

Thanks to Itagaki Takahiro for reporting this vulnerability.

Security: CVE-2007-6600

因此,其目的是防止权限提升攻击。
您可能想阅读this mailing list thread,它提供了更详细的解释。

相关问题