Skip to content

Commit 40a6160

Browse files
committed
MDEV-7574 Security definer views don't work with CONNECT ODBC tables
Instead of checking user's privileges with check_access(), use the cached value in table->grant.privilege instead - it is correctly set to the invoker or definer, depending on SQL SECURITY mode. Continue to use check_access() for DDLs when table->grant.privilege may be not set (but these cases are only possible on tables, never for views). (patch originally by Alexander Barkov)
1 parent 121f3e4 commit 40a6160

13 files changed

+1620
-20
lines changed

storage/connect/ha_connect.cc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4020,7 +4020,27 @@ bool ha_connect::check_privileges(THD *thd, PTOS options, char *dbn)
40204020
case TAB_MAC:
40214021
case TAB_WMI:
40224022
case TAB_OEM:
4023-
return check_access(thd, FILE_ACL, db, NULL, NULL, 0, 0);
4023+
#ifdef NO_EMBEDDED_ACCESS_CHECKS
4024+
return false;
4025+
#endif
4026+
/*
4027+
If table or table->mdl_ticket is NULL - it's a DLL, e.g. CREATE TABLE.
4028+
if the table has an MDL_EXCLUSIVE lock - it's a DDL too, e.g. the
4029+
insert step of CREATE ... SELECT.
4030+
4031+
Otherwise it's a DML, the table was normally opened, locked,
4032+
privilege were already checked, and table->grant.privilege is set.
4033+
With SQL SECURITY DEFINER, table->grant.privilege has definer's privileges.
4034+
*/
4035+
if (!table || !table->mdl_ticket || table->mdl_ticket->get_type() == MDL_EXCLUSIVE)
4036+
return check_access(thd, FILE_ACL, db, NULL, NULL, 0, 0);
4037+
if (table->grant.privilege & FILE_ACL)
4038+
return false;
4039+
status_var_increment(thd->status_var.access_denied_errors);
4040+
my_error(access_denied_error_code(thd->password), MYF(0),
4041+
thd->security_ctx->priv_user, thd->security_ctx->priv_host,
4042+
(thd->password ? ER(ER_YES) : ER(ER_NO)));
4043+
return true;
40244044

40254045
// This is temporary until a solution is found
40264046
case TAB_TBL:
@@ -6159,10 +6179,6 @@ bool ha_connect::FileExists(const char *fn, bool bf)
61596179
int n;
61606180
struct stat info;
61616181

6162-
if (check_access(ha_thd(), FILE_ACL, table->s->db.str,
6163-
NULL, NULL, 0, 0))
6164-
return true;
6165-
61666182
#if defined(__WIN__)
61676183
s= "\\";
61686184
#else // !__WIN__

storage/connect/mysql-test/connect/r/grant.result

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
4646
SELECT user();
4747
user()
4848
root@localhost
49-
CREATE VIEW v1 AS SELECT * FROM t1;
49+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
5050
SELECT user();
5151
user()
5252
user@localhost
@@ -130,7 +130,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
130130
SELECT user();
131131
user()
132132
root@localhost
133-
CREATE VIEW v1 AS SELECT * FROM t1;
133+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
134134
SELECT user();
135135
user()
136136
user@localhost
@@ -224,7 +224,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
224224
SELECT user();
225225
user()
226226
root@localhost
227-
CREATE VIEW v1 AS SELECT * FROM t1;
227+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
228228
SELECT user();
229229
user()
230230
user@localhost
@@ -318,7 +318,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
318318
SELECT user();
319319
user()
320320
root@localhost
321-
CREATE VIEW v1 AS SELECT * FROM t1;
321+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
322322
SELECT user();
323323
user()
324324
user@localhost
@@ -412,7 +412,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
412412
SELECT user();
413413
user()
414414
root@localhost
415-
CREATE VIEW v1 AS SELECT * FROM t1;
415+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
416416
SELECT user();
417417
user()
418418
user@localhost
@@ -506,7 +506,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
506506
SELECT user();
507507
user()
508508
root@localhost
509-
CREATE VIEW v1 AS SELECT * FROM t1;
509+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
510510
SELECT user();
511511
user()
512512
user@localhost

storage/connect/mysql-test/connect/r/grant2.result

Lines changed: 690 additions & 0 deletions
Large diffs are not rendered by default.

storage/connect/mysql-test/connect/r/ini_grant.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
5959
SELECT user();
6060
user()
6161
root@localhost
62-
CREATE VIEW v1 AS SELECT * FROM t1;
62+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
6363
SELECT user();
6464
user()
6565
user@localhost

storage/connect/mysql-test/connect/r/mysql_grant.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
4040
SELECT user();
4141
user()
4242
root@localhost
43-
CREATE VIEW v1 AS SELECT * FROM t1;
43+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
4444
SELECT user();
4545
user()
4646
user@localhost

storage/connect/mysql-test/connect/r/odbc_sqlite3_grant.result

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
4949
CREATE VIEW v1 AS SELECT * FROM t1;
5050
ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
5151
# Testing a VIEW created with FILE privileges but accessed with no FILE
52+
# using SQL SECIRITY INVOKER
5253
SELECT user();
5354
user()
5455
root@localhost
55-
CREATE VIEW v1 AS SELECT * FROM t1;
56+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
5657
SELECT user();
5758
user()
5859
user@localhost
@@ -64,6 +65,19 @@ UPDATE v1 SET a=123;
6465
ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
6566
DELETE FROM v1;
6667
ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
68+
# Testing a VIEW created with FILE privileges but accessed with no FILE
69+
# using SQL SECIRITY DEFINER
70+
DROP VIEW v1;
71+
SELECT user();
72+
user()
73+
root@localhost
74+
CREATE SQL SECURITY DEFINER VIEW v1 AS SELECT * FROM t1;
75+
SELECT user();
76+
user()
77+
user@localhost
78+
SELECT * FROM v1 WHERE a='test1';
79+
a
80+
test1
6781
SELECT user();
6882
user()
6983
root@localhost

storage/connect/mysql-test/connect/r/xml_grant.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ERROR 28000: Access denied for user 'user'@'localhost' (using password: NO)
6363
SELECT user();
6464
user()
6565
root@localhost
66-
CREATE VIEW v1 AS SELECT * FROM t1;
66+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
6767
SELECT user();
6868
user()
6969
user@localhost

storage/connect/mysql-test/connect/t/grant.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CREATE VIEW v1 AS SELECT * FROM t1;
5353
--echo # Testing a VIEW created with FILE privileges but accessed with no FILE
5454
--connection default
5555
SELECT user();
56-
CREATE VIEW v1 AS SELECT * FROM t1;
56+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
5757
--connection user
5858
SELECT user();
5959
--error ER_ACCESS_DENIED_ERROR

storage/connect/mysql-test/connect/t/grant.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ CREATE VIEW v1 AS SELECT * FROM t1;
4949
--echo # Testing a VIEW created with FILE privileges but accessed with no FILE
5050
--connection default
5151
SELECT user();
52-
CREATE VIEW v1 AS SELECT * FROM t1;
52+
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT * FROM t1;
5353
--connection user
5454
SELECT user();
5555
--error ER_ACCESS_DENIED_ERROR

0 commit comments

Comments
 (0)