invoker function security issues

Поиск
Список
Период
Сортировка
От Virender Singla
Тема invoker function security issues
Дата
Msg-id CAM6Zo8wpK+DEknNNvVfGj6wF2GCPzS0n55q9k0=1JTXkYazNtQ@mail.gmail.com
обсуждение исходный текст
Ответы Re: invoker function security issues  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-hackers

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=>

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Robert Haas
Дата:
Сообщение: Re: Collation version tracking for macOS
Следующее
От: Martin Butter
Дата:
Сообщение: => operator for named parameters in open cursor