Thread: invoker function security issues

invoker function security issues

From
Virender Singla
Date:

I believe functions in Postgres follow a late binding approach and hence nested function dependencies are resolved using search_path at run time. This way a user can override nested functions in its schema and change the behaviour of wrapper functions. However, a more serious issue is when functional Indexes (with nested function calls) are created on a table and then the data inserted in Indexes could be entirely dependent on which user is inserting the data (by overriding nested function).

I performed a couple of test cases where data inserted is dependent on the user overriding nested functions. I understand this is not the best practice to scatter functions/indexes/tables in different different schemas and use such kind schema setup but I still expect Postgres to save us from such data inconsistencies issues by using early binding for functional Indexes. In fact Postgres does that linking for a single function Index (where no nested function are there) and qualifies the function used in the Index with its schema name and also it works in cases where all functions, table, Indexes are present in the same schema.  

However still there are cases where functional Indexes are created on extension functions (For Ex - cube extension) which are present in different schemas and then those cube functions are defined as invoker security type with nested functions calls without any schema qualification.

Issue that would arise with late binding for functional Indexes is that when we are migrating such tables/indexes/data from one database to another (using pg_dump/pg_restore or any other method) data can be changed depending on which user we are using for import.
(These tests i performed using invoker functions, i think definer functions produce correct behavior). One way would be to define search_path for such nested functions.

  1. =========Case1======
  2.  
  3. ##Table and functions are in different schemas.

  4. Session1::
  5. User:Postgres
  6.  
  7. create user idxusr1 with password '*****';
  8. grant idxusr1 to postgres;
  9. create schema idxusr1 AUTHORIZATION idxusr1;
  10.  
  11. create user idxusr2 with password '*****';
  12. grant idxusr2 to postgres;
  13. create schema idxusr2 AUTHORIZATION idxusr2;
  14.  
  15. Session2::
  16. User:idxusr1
  17.  
  18. set search_path to idxusr1,public;
  19.  
  20. CREATE FUNCTION sumcall(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT ($1+$2)';
  21.  
  22. CREATE FUNCTION wrapsum(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT sumcall($1,$2)';

  23. ##create table in another schema
  24.  
  25. create table public.test(n1 int);
  26. create unique index idxtst on public.test(idxusr1.wrapsum(n1,1));
  27.  
  28. grant insert on table public.test to idxusr2;
  29.  
  30. postgres=> insert into test values(1);
  31. INSERT 0 1
  32. postgres=> insert into test values(1);
  33. ERROR: duplicate key value violates unique constraint "idxtst"
  34. DETAIL: Key (wrapsum(n1, 1))=(2) already exists.
  35.  
  36. Session3::
  37. User:idxusr2
  38.  
  39. set search_path to idxusr2,public;
  40.  
  41. CREATE FUNCTION sumcall(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT ($1 - $2)';
  42.  
  43. postgres=> insert into test values(1);
  44. INSERT 0 1
  45. postgres=> insert into test values(1);
  46. ERROR: duplicate key value violates unique constraint "idxtst"
  47. DETAIL: Key (idxusr1.wrapsum(n1, 1))=(0) already exists.
  48.  
  49. ======Case2==========
  50.  
  51.  ##Functions are in different schemas.

  52. Session1::
  53. User:Postgres
  54.  
  55. create user idxusr1 with password '*****';
  56. grant idxusr1 to postgres;
  57. create schema idxusr1 AUTHORIZATION idxusr1;
  58.  
  59. create user idxusr2 with password '*****';
  60. grant idxusr2 to postgres;
  61. create schema idxusr2 AUTHORIZATION idxusr2;
  62.  
  63. Session2::
  64. User:idxusr1
  65.  
  66. set search_path to idxusr1,public;

  67. ##create internal function in own schema and wrapper function in another schema.
  68.  
  69. CREATE FUNCTION sumcall(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT ($1+$2)';
  70.  
  71. CREATE FUNCTION public.wrapsum(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT sumcall($1,$2)';
  72.  
  73. create table test(n1 int);
  74. create unique index idxtst on test(public.wrapsum(n1,1));
  75.  
  76. grant usage on schema idxusr1 to idxusr2;
  77. grant insert on table test to idxusr2;
  78. postgres=> insert into test values(1);
  79. INSERT 0 1
  80. postgres=> insert into test values(1);
  81. ERROR: duplicate key value violates unique constraint "idxtst"
  82. DETAIL: Key (wrapsum(n1, 1))=(2) already exists.
  83.  
  84. Session3::
  85. User:idxusr2
  86.  
  87. set search_path to idxusr2,public;
  88.  
  89. CREATE FUNCTION sumcall(int, int) RETURNS int LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE AS 'SELECT ($1 - $2)';
  90.  
  91. postgres=> insert into idxusr1.test values(1);
  92. INSERT 0 1
  93. postgres=> insert into idxusr1.test values(1);
  94. ERROR: duplicate key value violates unique constraint "idxtst"
  95. DETAIL: Key (wrapsum(n1, 1))=(0) already exists.
  96. postgres=>

Re: invoker function security issues

From
"David G. Johnston"
Date:
On Wed, Jun 8, 2022 at 7:29 AM Virender Singla <virender.cse@gmail.com> wrote:
 but I still expect Postgres to save us from such data inconsistencies issues by using early binding for functional Indexes.

Well, if the functions you are writing are "black boxes" to PostgreSQL this expectation seems unreasonable.  As of v14 at least you have the option to write a SQL standard function definition which is whose parsed expression and dependencies are saved instead of a black box of text.


However still there are cases where functional Indexes are created on extension functions (For Ex - cube extension) which are present in different schemas and then those cube functions are defined as invoker security type with nested functions calls without any schema qualification.

Right, because if you try doing that in a security definer context the lack of a schema qualification will provoke a function not found error due to the sanitized search_path  (IIRC, if it doesn't then the two cases should behave identically...)

One way would be to define search_path for such nested functions.

Which the user is well advised to do, or, better, just schema-qualify all object references.  This can get a bit annoying for operators, in which case an explicit, localized, search_path becomes more appealing.

The tools are available for one to protect themself.  I suspect the historical baggage we carry prevents the server from being more aggressive in enforcing the use of these tools since not all cases where they are not used are problematic and there is lots of legacy code working just fine.  The security lockdown for this dynamic has already happened by basically admitting that the idea of a "public" schema at the front of the default search_path was a poor decision.  And while I see that there is possibly room for improvement here if desired, it is, for me, acceptable for the project to put the responsibility of not executing problematic code in the hands of the DBA.

I'm curious how "EXECUTE" command and dynamic SQL fit into your POV here (specifically in function bodies).  Right now, with "black box inside of black box" mechanics it isn't really an issue but if you want to not keep function bodies as black boxes now dynamic SQL becomes the top-most late binding point.

David J.