Skip to content

Commit 6fe1b96

Browse files
author
Bharathy Satish
committed
WL14073: Disable DROP/RENAME USER for SQL DEFINER users in procedures, functions,
views, triggers and events. DROP/RENAME USER sql should report error if this user account is referenced in any of the stored programs (procedures, functions, triggers, events) and views as a definer account. If user has SET_USER_ID privilege then we allow this operation and report a warning. For stored programs and views for which definer account is missing, then this objects become inactive. To make these object active we need to create missing definer account. CREATE USER operation should report error if user does not have SET_USER_ID privilege, else report a warning. rb#24617
1 parent 9b04f0c commit 6fe1b96

31 files changed

+925
-2
lines changed
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
#
2+
# WL#14073: Disable DROP/RENAME USER for SQL DEFINER users in procedures,
3+
# functions, views, triggers and events.
4+
#
5+
CREATE DATABASE wl14073;
6+
USE wl14073;
7+
CREATE table t1(i int);
8+
CREATE USER normal_user;
9+
GRANT ALL ON *.* TO normal_user;
10+
REVOKE SET_USER_ID ON *.* FROM normal_user;
11+
CREATE USER power_user;
12+
GRANT ALL ON *.* TO power_user;
13+
# case1: check for view with normal_user
14+
connect normal_conn, localhost, normal_user;
15+
CREATE USER u1;
16+
USE wl14073;
17+
CREATE DEFINER = u1 VIEW v1 as SELECT * FROM t1;
18+
# user is referenced in view v1 so drop/rename user should fail
19+
DROP USER u1;
20+
ERROR HY000: Operation DROP USER failed for 'u1'@'%' as it is referenced as a definer account in a view.
21+
RENAME USER u1 to new_u1;
22+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a view.
23+
DROP VIEW v1;
24+
# user is not more referenced in view v1 so rename/drop user should pass
25+
RENAME USER u1 to new_u1;
26+
DROP USER new_u1;
27+
# case2: check for view with power_user
28+
connect power_conn, localhost, power_user;
29+
CREATE USER u1;
30+
USE wl14073;
31+
CREATE DEFINER = u1 VIEW v1 as SELECT * FROM t1;
32+
# user is referenced in view v1 so rename/drop user should pass as user has SET_USER_ID.
33+
RENAME USER u1 to new_u1;
34+
Warnings:
35+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a view.
36+
DROP USER new_u1;
37+
DROP VIEW v1;
38+
# case3: check for event with normal_user
39+
connection normal_conn;
40+
CREATE USER u1;
41+
USE wl14073;
42+
CREATE DEFINER = u1 EVENT ev1 ON SCHEDULE EVERY 5 HOUR DO SELECT 1;
43+
# user is referenced in event ev1 so drop/rename user should fail
44+
DROP USER u1;
45+
ERROR HY000: Operation DROP USER failed for 'u1'@'%' as it is referenced as a definer account in an event.
46+
RENAME USER u1 to new_u1;
47+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in an event.
48+
DROP EVENT ev1;
49+
# user is no more referenced in event ev1 so rename/drop user should pass
50+
RENAME USER u1 to new_u1;
51+
DROP USER new_u1;
52+
# case4: check for event with power_user
53+
connection power_conn;
54+
CREATE USER u1;
55+
USE wl14073;
56+
CREATE DEFINER = u1 EVENT ev1 ON SCHEDULE EVERY 5 HOUR DO SELECT 1;
57+
# user is referenced in event ev1 so rename/drop user should pass
58+
RENAME USER u1 to new_u1;
59+
Warnings:
60+
Warning 4005 User 'u1'@'%' is referenced as a definer account in an event.
61+
DROP USER new_u1;
62+
DROP EVENT ev1;
63+
# case5: check for procedure with normal_user
64+
connection normal_conn;
65+
CREATE USER u1;
66+
USE wl14073;
67+
CREATE DEFINER = u1 PROCEDURE p1() DELETE FROM t1;
68+
# user is referenced in procedure p1, so drop/rename user should fail
69+
DROP USER u1;
70+
ERROR HY000: Operation DROP USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
71+
RENAME USER u1 to new_u1;
72+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
73+
DROP PROCEDURE p1;
74+
# user is no more referenced in procedure p1, so rename/drop user should pass
75+
RENAME USER u1 to new_u1;
76+
DROP USER new_u1;
77+
# case6: check for procedure with power_user
78+
connection power_conn;
79+
CREATE USER u1;
80+
USE wl14073;
81+
CREATE DEFINER = u1 PROCEDURE p1() DELETE FROM t1;
82+
# user is referenced in procedure p1, so rename/drop user should pass
83+
RENAME USER u1 to new_u1;
84+
Warnings:
85+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
86+
DROP USER new_u1;
87+
DROP PROCEDURE p1;
88+
# case7: check for function with normal_user
89+
connection normal_conn;
90+
set GLOBAL log_bin_trust_function_creators=1;
91+
CREATE USER u1;
92+
USE wl14073;
93+
CREATE DEFINER = u1 FUNCTION f1() RETURNS INT RETURN 1;
94+
# user is referenced in function f1, so drop/rename user should fail
95+
DROP USER u1;
96+
ERROR HY000: Operation DROP USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
97+
RENAME USER u1 to new_u1;
98+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
99+
DROP FUNCTION f1;
100+
# user is no more referenced in function f1, so rename/drop user should pass
101+
RENAME USER u1 to new_u1;
102+
DROP USER new_u1;
103+
# case8: check for function with power_user
104+
connection power_conn;
105+
CREATE USER u1;
106+
USE wl14073;
107+
CREATE DEFINER = u1 FUNCTION f1() RETURNS INT RETURN 1;
108+
# user is referenced in function f1, so rename/drop user should pass
109+
RENAME USER u1 to new_u1;
110+
Warnings:
111+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
112+
DROP USER new_u1;
113+
DROP FUNCTION f1;
114+
# case9: check for trigger with normal_user
115+
connection normal_conn;
116+
CREATE USER u1;
117+
USE wl14073;
118+
CREATE DEFINER = u1 TRIGGER trig1 BEFORE INSERT ON t1 FOR EACH ROW DELETE FROM t1;
119+
# user is referenced in trigger trig1, so drop/rename user should fail
120+
DROP USER u1;
121+
ERROR HY000: Operation DROP USER failed for 'u1'@'%' as it is referenced as a definer account in a trigger.
122+
RENAME USER u1 to new_u1;
123+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a trigger.
124+
DROP TRIGGER trig1;
125+
# user is no more referenced in trigger trig1, so rename/drop user should pass
126+
RENAME USER u1 to new_u1;
127+
DROP USER new_u1;
128+
# case10: check for trigger with power_user
129+
connection power_conn;
130+
CREATE USER u1;
131+
USE wl14073;
132+
CREATE DEFINER = u1 TRIGGER trig1 BEFORE INSERT ON t1 FOR EACH ROW DELETE FROM t1;
133+
RENAME USER u1 to new_u1;
134+
Warnings:
135+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a trigger.
136+
DROP USER new_u1;
137+
DROP TRIGGER trig1;
138+
# case11: check CREATE USER for orphaned view for normal user/power user
139+
connection normal_conn;
140+
USE wl14073;
141+
CREATE USER dummy;
142+
CREATE DEFINER = u1 VIEW v1 as SELECT * FROM t1;
143+
Warnings:
144+
Note 1449 The user specified as a definer ('u1'@'%') does not exist
145+
SELECT * FROM v1;
146+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
147+
# try creating missing definer account, should report error as SET_USER_ID privilege is not there
148+
CREATE USER u1;
149+
ERROR HY000: Operation CREATE USER failed for 'u1'@'%' as it is referenced as a definer account in a view.
150+
# try renaming existing user to a matching definer account, it should fail.
151+
RENAME USER dummy to u1;
152+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a view.
153+
connection power_conn;
154+
USE wl14073;
155+
SELECT * FROM v1;
156+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
157+
# try creating missing definer account, should pass as SET_USER_ID privilege is there
158+
CREATE USER u1;
159+
Warnings:
160+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a view.
161+
DROP USER u1;
162+
Warnings:
163+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a view.
164+
# try renaming existing user to a matching definer account, it should pass
165+
RENAME USER dummy to u1;
166+
Warnings:
167+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a view.
168+
DROP VIEW v1;
169+
DROP USER u1;
170+
# case12: check CREATE USER for orphaned event for normal user/power user
171+
connection normal_conn;
172+
USE wl14073;
173+
CREATE USER dummy;
174+
CREATE DEFINER = u1 EVENT ev1 ON SCHEDULE EVERY 5 HOUR DO SELECT 1;
175+
Warnings:
176+
Note 1449 The user specified as a definer ('u1'@'%') does not exist
177+
# try creating missing definer account, should report error as SET_USER_ID privilege is not there
178+
CREATE USER u1;
179+
ERROR HY000: Operation CREATE USER failed for 'u1'@'%' as it is referenced as a definer account in an event.
180+
# try renaming existing user to a matching definer account, it should fail.
181+
RENAME USER dummy to u1;
182+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in an event.
183+
connection power_conn;
184+
USE wl14073;
185+
# try creating missing definer account, should pass as SET_USER_ID privilege is there
186+
CREATE USER u1;
187+
Warnings:
188+
Warning 4005 User 'u1'@'%' is referenced as a definer account in an event.
189+
DROP USER u1;
190+
Warnings:
191+
Warning 4005 User 'u1'@'%' is referenced as a definer account in an event.
192+
# try renaming existing user to a matching definer account, it should pass
193+
RENAME USER dummy to u1;
194+
Warnings:
195+
Warning 4005 User 'u1'@'%' is referenced as a definer account in an event.
196+
DROP EVENT ev1;
197+
DROP USER u1;
198+
# case13: check CREATE USER for orphaned procedure for normal user/power user
199+
connection normal_conn;
200+
USE wl14073;
201+
CREATE USER dummy;
202+
CREATE DEFINER = u1 PROCEDURE p1() DELETE FROM t1;
203+
Warnings:
204+
Note 1449 The user specified as a definer ('u1'@'%') does not exist
205+
CALL p1();
206+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
207+
# try creating missing definer account, should report error as SET_USER_ID privilege is not there
208+
CREATE USER u1;
209+
ERROR HY000: Operation CREATE USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
210+
# try renaming existing user to a matching definer account, it should fail.
211+
RENAME USER dummy to u1;
212+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
213+
connection power_conn;
214+
USE wl14073;
215+
CALL p1();
216+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
217+
# try creating missing definer account, should pass as SET_USER_ID privilege is there
218+
CREATE USER u1;
219+
Warnings:
220+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
221+
DROP USER u1;
222+
Warnings:
223+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
224+
# try renaming existing user to a matching definer account, it should pass
225+
RENAME USER dummy to u1;
226+
Warnings:
227+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
228+
DROP PROCEDURE p1;
229+
DROP USER u1;
230+
# case14: check CREATE USER for orphaned function for normal user/power user
231+
connection normal_conn;
232+
USE wl14073;
233+
CREATE USER dummy;
234+
CREATE DEFINER = u1 FUNCTION f1() RETURNS INT RETURN 1;
235+
Warnings:
236+
Note 1449 The user specified as a definer ('u1'@'%') does not exist
237+
SELECT f1();
238+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
239+
# try creating missing definer account, should report error as SET_USER_ID privilege is not there
240+
CREATE USER u1;
241+
ERROR HY000: Operation CREATE USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
242+
# try renaming existing user to a matching definer account, it should fail.
243+
RENAME USER dummy to u1;
244+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a procedure.
245+
connection power_conn;
246+
USE wl14073;
247+
SELECT f1();
248+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
249+
# try creating missing definer account, should pass as SET_USER_ID privilege is there
250+
CREATE USER u1;
251+
Warnings:
252+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
253+
DROP USER u1;
254+
Warnings:
255+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
256+
# try renaming existing user to a matching definer account, it should pass
257+
RENAME USER dummy to u1;
258+
Warnings:
259+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a procedure.
260+
DROP FUNCTION f1;
261+
DROP USER u1;
262+
# case15: check CREATE USER for orphaned trigger for normal user/power user
263+
connection normal_conn;
264+
USE wl14073;
265+
CREATE USER dummy;
266+
CREATE DEFINER = u1 TRIGGER trig1 BEFORE INSERT ON t1 FOR EACH ROW DELETE FROM t1;
267+
Warnings:
268+
Note 1449 The user specified as a definer ('u1'@'%') does not exist
269+
INSERT INTO t1 VALUES (10);
270+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
271+
# try creating missing definer account, should report error as SET_USER_ID privilege is not there
272+
CREATE USER u1;
273+
ERROR HY000: Operation CREATE USER failed for 'u1'@'%' as it is referenced as a definer account in a trigger.
274+
# try renaming existing user to a matching definer account, it should fail.
275+
RENAME USER dummy to u1;
276+
ERROR HY000: Operation RENAME USER failed for 'u1'@'%' as it is referenced as a definer account in a trigger.
277+
connection power_conn;
278+
USE wl14073;
279+
INSERT INTO t1 VALUES (10);
280+
ERROR HY000: The user specified as a definer ('u1'@'%') does not exist
281+
# try creating missing definer account, should pass as SET_USER_ID privilege is there
282+
CREATE USER u1;
283+
Warnings:
284+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a trigger.
285+
DROP USER u1;
286+
Warnings:
287+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a trigger.
288+
# try renaming existing user to a matching definer account, it should pass
289+
RENAME USER dummy to u1;
290+
Warnings:
291+
Warning 4005 User 'u1'@'%' is referenced as a definer account in a trigger.
292+
DROP TRIGGER trig1;
293+
DROP USER u1;
294+
connection default;
295+
disconnect normal_conn;
296+
disconnect power_conn;
297+
DROP USER normal_user, power_user;
298+
# case16: check for user name case sensitivity
299+
CREATE USER ABC;
300+
CREATE USER ABc;
301+
USE wl14073;
302+
CREATE DEFINER = ABC VIEW v2 as SELECT * FROM t1;
303+
# should pass without any warnings
304+
DROP USER ABc;
305+
# should pass with warnings
306+
DROP USER ABC;
307+
Warnings:
308+
Warning 4005 User 'ABC'@'%' is referenced as a definer account in a view.
309+
# case17: check for host name
310+
CREATE USER u1@192.129.12.11;
311+
CREATE USER 'u1'@'%.com';
312+
CREATE USER 'u1'@'abc.com';
313+
USE wl14073;
314+
CREATE DEFINER = u1@192.129.12.11 VIEW v3 as SELECT * FROM t1;
315+
CREATE DEFINER = 'u1'@'%.com' VIEW v4 as SELECT * FROM t1;
316+
# check that host name is not case sensitive
317+
CREATE DEFINER = 'u1'@'AbC.com' VIEW v5 as SELECT * FROM t1;
318+
# should not match with any definer account names
319+
DROP USER 'u1'@'192.129.12.%';
320+
ERROR HY000: Operation DROP USER failed for 'u1'@'192.129.12.%'
321+
DROP USER 'u1'@'%';
322+
ERROR HY000: Operation DROP USER failed for 'u1'@'%'
323+
# should pass with warnings
324+
DROP USER u1@192.129.12.11;
325+
Warnings:
326+
Warning 4005 User 'u1'@'192.129.12.11' is referenced as a definer account in a view.
327+
# should pass with warnings
328+
DROP USER 'u1'@'%.com';
329+
Warnings:
330+
Warning 4005 User 'u1'@'%.com' is referenced as a definer account in a view.
331+
# should pass with warnings even when hostname is specified with different case
332+
DROP USER 'u1'@'ABC.COM';
333+
Warnings:
334+
Warning 4005 User 'u1'@'abc.com' is referenced as a definer account in a view.
335+
DROP DATABASE wl14073;

mysql-test/r/events_bugs.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,14 @@ SELECT COUNT(*) > 0 AS "Expect 1" FROM events_test.event_log;
609609
Expect 1
610610
1
611611
connection default;
612-
DROP USER evtest1@localhost;
613612
Sleep 4 seconds
614613
SELECT COUNT(*) INTO @row_cnt FROM events_test.event_log;
615614
Sleep 4 seconds
616615
SELECT COUNT(*) > @row_cnt AS "Expect 0" FROM events_test.event_log;
617616
Expect 0
618617
0
619618
DROP EVENT events_test.ev_sched_1823;
619+
DROP USER evtest1@localhost;
620620
DROP TABLE events_test.event_log;
621621
SET GLOBAL event_scheduler = OFF;
622622
SET @@session.autocommit = @save_session_autocommit;

mysql-test/r/events_grant.result

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,7 @@ def events_test three_event ev_test@localhost SQL SELECT 123 RECURRING NULL 20 S
119119
def events_test two_event ev_test@localhost SQL SELECT 123 RECURRING NULL 20 SECOND ENABLED NOT PRESERVE two event
120120
def events_test2 five_event root@localhost SQL SELECT 42 RECURRING NULL 20 SECOND ENABLED NOT PRESERVE
121121
DROP USER ev_test@localhost;
122+
Warnings:
123+
Warning 4005 User 'ev_test'@'localhost' is referenced as a definer account in an event.
122124
DROP DATABASE events_test2;
123125
DROP DATABASE events_test;

mysql-test/r/grant.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,8 @@ SELECT fn2();
16311631
fn2()
16321632
2
16331633
DROP USER 'userbug33464'@'localhost';
1634+
Warnings:
1635+
Warning 4005 User 'userbug33464'@'localhost' is referenced as a definer account in a procedure.
16341636
DROP FUNCTION fn1;
16351637
DROP FUNCTION fn2;
16361638
DROP PROCEDURE sp3;
@@ -2811,6 +2813,8 @@ VIEW_DEFINITION
28112813

28122814
# Cleanup
28132815
DROP USER mysqluser1@localhost, MySQLuser1@localhost;
2816+
Warnings:
2817+
Warning 4005 User 'mysqluser1'@'localhost' is referenced as a definer account in a view.
28142818
DROP DATABASE mysqltest_1;
28152819
#
28162820
# Test prepared statements and REVOKE between PREPARE and EXECUTE

0 commit comments

Comments
 (0)