Skip to content

Commit 92a523e

Browse files
author
Alexander Barkov
committed
MDEV-7280 DATABASE: CREATE OR REPLACE
A clean-up: require CREATE+DROP privileges for "CREATE OR REPLACE DATABASE", instead of just CREATE privilege.
1 parent 31c7458 commit 92a523e

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Tests for checking permission denied on CREATE OR REPLACE if DROP
3+
# access is revoked
4+
#
5+
# These statements do not need special tests for CREATE OR REPLACE,
6+
# because they do not have separate permissions for create and drop:
7+
# CREATE OR REPLACE EVENT (uses EVENT_ACL for both CREATE and DROP)
8+
# CREATE OR DROP SERVER (uses SUPER_ALC for both CREATE and DROP)
9+
# CREATE OR DROP TRIGGER (uses TRIGGER_ACL for both CREATE and DROP)
10+
SELECT CURRENT_USER;
11+
CURRENT_USER
12+
root@localhost
13+
CREATE DATABASE db1;
14+
GRANT ALL ON db1.* TO mysqltest_1@localhost;
15+
REVOKE DROP ON db1.* FROM mysqltest_1@localhost;
16+
REVOKE ALTER ROUTINE ON db1.* FROM mysqltest_1@localhost;
17+
GRANT DELETE ON mysql.* TO mysqltest_1@localhost;
18+
REVOKE DELETE ON mysql.* FROM mysqltest_1@localhost;
19+
FLUSH PRIVILEGES;
20+
SELECT CURRENT_USER;
21+
CURRENT_USER
22+
mysqltest_1@localhost
23+
CREATE DATABASE db1;
24+
ERROR HY000: Can't create database 'db1'; database exists
25+
CREATE OR REPLACE DATABASE db1;
26+
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'db1'
27+
CREATE OR REPLACE DATABASE db2;
28+
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'db2'
29+
USE db1;
30+
CREATE OR REPLACE TABLE t1(id INT);
31+
ERROR 42000: DROP command denied to user 'mysqltest_1'@'localhost' for table 't1'
32+
SELECT CURRENT_USER;
33+
CURRENT_USER
34+
root@localhost
35+
REVOKE ALL ON db1.* FROM mysqltest_1@localhost;
36+
DROP DATABASE IF EXISTS db2;
37+
Warnings:
38+
Note 1008 Can't drop database 'db2'; database doesn't exist
39+
DROP DATABASE db1;
40+
DROP USER mysqltest_1@localhost;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Grant tests not performed with embedded server
2+
-- source include/not_embedded.inc
3+
4+
--echo #
5+
--echo # Tests for checking permission denied on CREATE OR REPLACE if DROP
6+
--echo # access is revoked
7+
--echo #
8+
9+
--echo # These statements do not need special tests for CREATE OR REPLACE,
10+
--echo # because they do not have separate permissions for create and drop:
11+
--echo # CREATE OR REPLACE EVENT (uses EVENT_ACL for both CREATE and DROP)
12+
--echo # CREATE OR DROP SERVER (uses SUPER_ALC for both CREATE and DROP)
13+
--echo # CREATE OR DROP TRIGGER (uses TRIGGER_ACL for both CREATE and DROP)
14+
15+
SELECT CURRENT_USER;
16+
CREATE DATABASE db1;
17+
GRANT ALL ON db1.* TO mysqltest_1@localhost;
18+
REVOKE DROP ON db1.* FROM mysqltest_1@localhost;
19+
REVOKE ALTER ROUTINE ON db1.* FROM mysqltest_1@localhost;
20+
GRANT DELETE ON mysql.* TO mysqltest_1@localhost;
21+
REVOKE DELETE ON mysql.* FROM mysqltest_1@localhost;
22+
FLUSH PRIVILEGES;
23+
24+
connect (user_a, localhost, mysqltest_1,,);
25+
connection user_a;
26+
SELECT CURRENT_USER;
27+
28+
# mysqltest_1 has CREATE privilege on db1
29+
--error ER_DB_CREATE_EXISTS
30+
CREATE DATABASE db1;
31+
32+
# mysqltest_1 has no DROP privilege on db1
33+
--error ER_DBACCESS_DENIED_ERROR
34+
CREATE OR REPLACE DATABASE db1;
35+
36+
# mysqltest_1 has no any privileges on db2
37+
--error ER_DBACCESS_DENIED_ERROR
38+
CREATE OR REPLACE DATABASE db2;
39+
40+
USE db1;
41+
--error ER_TABLEACCESS_DENIED_ERROR
42+
CREATE OR REPLACE TABLE t1(id INT);
43+
44+
#TODO: add this when "MDEV-5359 CREATE OR REPLACE..." is done
45+
#DELIMITER $;
46+
#--error ER_PROCACCESS_DENIED_ERROR
47+
#CREATE OR REPLACE PROCEDURE proc1 (OUT cnt INT) BEGIN SELECT COUNT(*) INTO cnt FROM t1; END$
48+
#DELIMITER ;$
49+
#
50+
#--error ER_DBACCESS_DENIED_ERROR
51+
#CREATE OR REPLACE FUNCTION lookup RETURNS STRING SONAME "udf_example.so";
52+
#
53+
#--error ER_PROCACCESS_DENIED_ERROR
54+
#CREATE OR REPLACE FUNCTION hello(str char(20)) RETURNS TEXT RETURN CONCAT('Hello, ', str, '!');
55+
#
56+
#--error ER_SPECIFIC_ACCESS_DENIED_ERROR
57+
#CREATE OR REPLACE USER u1@localhost;
58+
#
59+
#--error ER_SPECIFIC_ACCESS_DENIED_ERROR
60+
#CREATE OR REPLACE ROLE developer;
61+
62+
connection default;
63+
SELECT CURRENT_USER;
64+
REVOKE ALL ON db1.* FROM mysqltest_1@localhost;
65+
DROP DATABASE IF EXISTS db2;
66+
DROP DATABASE db1;
67+
DROP USER mysqltest_1@localhost;

sql/sql_parse.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,9 @@ mysql_execute_command(THD *thd)
42984298
}
42994299
}
43004300
#endif
4301-
if (check_access(thd, CREATE_ACL, lex->name.str, NULL, NULL, 1, 0))
4301+
if (check_access(thd, lex->create_info.or_replace() ?
4302+
(CREATE_ACL | DROP_ACL) : CREATE_ACL,
4303+
lex->name.str, NULL, NULL, 1, 0))
43024304
break;
43034305
WSREP_TO_ISOLATION_BEGIN(lex->name.str, NULL, NULL)
43044306
res= mysql_create_db(thd, lex->name.str, lex->create_info, &create_info);

0 commit comments

Comments
 (0)