README.sql_security.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. SQL SECURITY.
  2. Implements capability to run executable objects regarding SQL SECURITY clause.
  3. SQL Standard (2003, 2011) Feature.
  4. Author:
  5. Red Soft, roman.simakov(at)red-soft.ru
  6. Syntax is:
  7. CREATE TABLE <TABLENAME> (...) [SQL SECURITY {DEFINER | INVOKER}]
  8. ALTER TABLE <TABLENAME> ... [{ALTER SQL SECURITY {DEFINER | INVOKER} | DROP SQL SECURITY}]
  9. CREATE [OR ALTER] FUNCTION <FUNCTIONNAME> ... [SQL SECURITY {DEFINER | INVOKER}] AS ...
  10. CREATE [OR ALTER] PROCEDURE <PROCEDURENAME> ... [SQL SECURITY {DEFINER | INVOKER}] AS ...
  11. CREATE [OR ALTER] TRIGGER <TRIGGERNAME> ... [SQL SECURITY {DEFINER | INVOKER} | DROP SQL SECURITY] [AS ...]
  12. CREATE [OR ALTER] PACKAGE <PACKAGENAME> [SQL SECURITY {DEFINER | INVOKER}] AS ...
  13. ALTER DATABASE SET DEFAULT SQL SECURITY {DEFINER | INVOKER}
  14. Description:
  15. Makes it possible to execute some objects with permissions of either definer or invoker.
  16. By default INVOKER is used to keep backward compatibility. You can change this behavior and be more compatible
  17. with SQL STANDARD by using ALTER DATABASE SET DEFAULT SQL SECURITY statement.
  18. If INVOKER is specified a current set of privileges of the current user will be used.
  19. If DEFINER - a set of privileges of object owner will be used to check an access to database objects
  20. used by this object.
  21. Trigger inherits SQL SECURITY option from TABLE but can overwrite it by explicit specifying. If SQL SECURITY option
  22. will be changed for table, existing triggers without explicitly specified option will not use new value immediately
  23. it will take effect next time trigger will be loaded into metadata cache.
  24. For procedures and functions defined in package explicit SQL SECURITY clause is prohibit.
  25. In stored procedures, functions or triggers you may check which user if really effective and which privileges
  26. are applying to accessed objects by using the system context variable EFFECTIVE_USER from SYSTEM namespace.
  27. select RDB$GET_CONTEXT('SYSTEM', 'EFFECTIVE_USER') from RDB$DATABASE;
  28. Note: now the same object may be called in different security contexts and requires different privileges.
  29. For example we have:
  30. - a stored procedure INV with SQL SECURITY INVOKER which insert records in a table T
  31. - a stored procedure DEF with SQL SECURITY DEFINER defined by SYSDBA
  32. If a user U calls INV an access to T will require an INSERT privile to be granted to U (and EXECUTE on INV of course).
  33. In this case U is EFFECTIVE_USER due INV running.
  34. If user U calls DEF an access to T will require an INSERT privilege to be granted to SYSDBA (end EXECUTE on DEF).
  35. In this case SYSDBA is EFFECTIVE_USER due INV running as well as due DEF running.
  36. Example 1. It's enough to grant only SELECT privilege to user US for table T.
  37. In case of INVOKER it will require also EXECUTE for function F.
  38. set term ^;
  39. create function f() returns int
  40. as
  41. begin
  42. return 3;
  43. end^
  44. set term ;^
  45. create table t (i integer, c computed by (i + f())) sql security definer;
  46. insert into t values (2);
  47. grant select on table t to user us;
  48. commit;
  49. connect 'localhost:/tmp/7.fdb' user us password 'pas';
  50. select * from t;
  51. Example 2. It's enough to grant EXECUTE privilege to user US for function F.
  52. In case of INVOKER it will require also INSERT for table T.
  53. set term ^;
  54. create function f (i integer) returns int sql security definer
  55. as
  56. begin
  57. insert into t values (:i);
  58. return i + 1;
  59. end^
  60. set term ;^
  61. grant execute on function f to user us;
  62. commit;
  63. connect 'localhost:/tmp/59.fdb' user us password 'pas';
  64. select f(3) from rdb$database;
  65. Example 3. It's enough to grant only EXECUTE privilege to user US for procedure P.
  66. In case of INVOKER it will require also INSERT for table T to either user US or procedure P.
  67. set term ^;
  68. create procedure p (i integer) sql security definer
  69. as
  70. begin
  71. insert into t values (:i);
  72. end^
  73. set term ;^
  74. grant execute on procedure p to user us;
  75. commit;
  76. connect 'localhost:/tmp/17.fdb' user us password 'pas';
  77. execute procedure p(1);
  78. Example 4. It's enough to grant only INSERT privilege to user US for table TR.
  79. In case of INVOKER it will require also INSERT for table T to user US.
  80. create table tr (i integer);
  81. create table t (i integer);
  82. set term ^;
  83. create trigger tr_ins for tr after insert sql security definer
  84. as
  85. begin
  86. insert into t values (NEW.i);
  87. end^
  88. set term ;^
  89. grant insert on table tr to user us;
  90. commit;
  91. connect 'localhost:/tmp/29.fdb' user us password 'pas';
  92. insert into tr values(2);
  93. the same result if specify SQL SECURITY DEFINER for table TR.
  94. create table tr (i integer) sql security definer;
  95. create table t (i integer);
  96. set term ^;
  97. create trigger tr_ins for tr after insert
  98. as
  99. begin
  100. insert into t values (NEW.i);
  101. end^
  102. set term ;^
  103. grant insert on table tr to user us;
  104. commit;
  105. connect 'localhost:/tmp/29.fdb' user us password 'pas';
  106. insert into tr values(2);
  107. Example 5. It's enough to grant only EXECUTE privilege to user US for package PK.
  108. In case of INVOKER it will require also INSERT for table T to user US.
  109. create table t (i integer);
  110. set term ^;
  111. create package pk sql security definer
  112. as
  113. begin
  114. function f(i integer) returns int;
  115. end^
  116. create package body pk
  117. as
  118. begin
  119. function f(i integer) returns int
  120. as
  121. begin
  122. insert into t values (:i);
  123. return i + 1;
  124. end
  125. end^
  126. set term ;^
  127. grant execute on package pk to user us;
  128. commit;
  129. connect 'localhost:/tmp/69.fdb' user us password 'pas';
  130. select pk.f(3) from rdb$database;
  131. Example 6. Altering explicit option SQL SECURITY for triggers.
  132. To remove explicit SQL SECURITY OPTION from trigger you can execute:
  133. alter trigger tr_ins drop sql security;
  134. To set it again to SQL SECURITY INVOKER you can:
  135. alter trigger tr_ins sql security invoker;