Skip to content

Commit 7c02e87

Browse files
committed
MDEV-26081 set role crashes when a hostname cannot be resolved
host can be NULL
1 parent ffe744e commit 7c02e87

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

mysql-test/r/skip_name_resolve.result

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ SET @@LOCAL.skip_name_resolve=0;
3939
ERROR HY000: Variable 'skip_name_resolve' is a read only variable
4040
SET @@GLOBAL.skip_name_resolve=0;
4141
ERROR HY000: Variable 'skip_name_resolve' is a read only variable
42-
End of 5.1 tests
42+
#
43+
# End of 5.1 tests
44+
#
45+
#
46+
# MDEV-26081 set role crashes when a hostname cannot be resolved
47+
#
48+
create user u1@`%`;
49+
create role r1;
50+
create role r2;
51+
grant r2 to r1;
52+
grant r1 to u1@`%`;
53+
connect u1,127.0.0.1,u1,,,$MASTER_MYPORT;
54+
set role r2;
55+
ERROR OP000: User `u1`@`%` has not been granted role `r2`
56+
disconnect u1;
57+
connection default;
58+
drop user u1@`%`;
59+
drop role r1, r2;
60+
#
61+
# End of 10.2 tests
62+
#

mysql-test/suite/roles/set_default_role_for.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set default role role_a for user_a@localhost;
1414
set default role invalid_role for user_a@localhost;
1515
ERROR OP000: Invalid role specification `invalid_role`
1616
set default role role_b for user_a@localhost;
17-
ERROR OP000: User `user_a@localhost` has not been granted role `role_b`
17+
ERROR OP000: User `root`@`localhost` has not been granted role `role_b`
1818
set default role role_b for user_b@localhost;
1919
show grants;
2020
Grants for user_a@localhost
@@ -37,7 +37,7 @@ user host default_role
3737
user_a localhost role_a
3838
user_b localhost role_b
3939
set default role role_b for current_user;
40-
ERROR OP000: User `user_a@localhost` has not been granted role `role_b`
40+
ERROR OP000: User `user_a`@`localhost` has not been granted role `role_b`
4141
show grants;
4242
Grants for user_b@localhost
4343
GRANT role_b TO 'user_b'@'localhost'

mysql-test/suite/roles/set_default_role_invalid.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CREATE USER b;
4848
CREATE ROLE r1;
4949
CREATE ROLE r2;
5050
SET DEFAULT ROLE r1 FOR a;
51-
ERROR OP000: User `a@%` has not been granted role `r1`
51+
ERROR OP000: User `root`@`localhost` has not been granted role `r1`
5252
GRANT r1 TO b;
5353
GRANT r2 TO b;
5454
SET DEFAULT ROLE r1 FOR b;
@@ -100,7 +100,7 @@ GRANT USAGE ON *.* TO 'b'@'%'
100100
GRANT SELECT, UPDATE ON `mysql`.* TO 'b'@'%'
101101
SET DEFAULT ROLE r2 FOR 'b'@'%'
102102
SET DEFAULT ROLE r1 FOR a;
103-
ERROR OP000: User `a@%` has not been granted role `r1`
103+
ERROR OP000: User `b`@`%` has not been granted role `r1`
104104
SET DEFAULT ROLE invalid_role;
105105
ERROR OP000: Invalid role specification `invalid_role`
106106
SET DEFAULT ROLE invalid_role FOR a;
@@ -117,7 +117,7 @@ SET DEFAULT ROLE None;
117117
# Change user b (session 3: role granted to user a)
118118
SET DEFAULT ROLE r1 FOR a;
119119
SET DEFAULT ROLE r2 FOR a;
120-
ERROR OP000: User `a@%` has not been granted role `r2`
120+
ERROR OP000: User `b`@`%` has not been granted role `r2`
121121
SET DEFAULT ROLE invalid_role;
122122
ERROR OP000: Invalid role specification `invalid_role`
123123
SET DEFAULT ROLE invalid_role FOR a;

mysql-test/suite/roles/set_role-recursive.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Grants for test_user@localhost
6666
GRANT USAGE ON *.* TO 'test_user'@'localhost'
6767
GRANT test_role1 TO 'test_user'@'localhost'
6868
set role test_role2;
69-
ERROR OP000: User `test_user@localhost` has not been granted role `test_role2`
69+
ERROR OP000: User `test_user`@`localhost` has not been granted role `test_role2`
7070
select current_user(), current_role();
7171
current_user() current_role()
7272
test_user@localhost NULL

mysql-test/t/skip_name_resolve.test

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,28 @@ SET @@LOCAL.skip_name_resolve=0;
5050
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
5151
SET @@GLOBAL.skip_name_resolve=0;
5252

53-
--echo End of 5.1 tests
53+
--echo #
54+
--echo # End of 5.1 tests
55+
--echo #
56+
57+
--echo #
58+
--echo # MDEV-26081 set role crashes when a hostname cannot be resolved
59+
--echo #
60+
61+
create user u1@`%`;
62+
create role r1;
63+
create role r2;
64+
grant r2 to r1;
65+
grant r1 to u1@`%`;
66+
67+
connect u1,127.0.0.1,u1,,,$MASTER_MYPORT;
68+
error ER_INVALID_ROLE;
69+
set role r2;
70+
disconnect u1;
71+
connection default;
72+
drop user u1@`%`;
73+
drop role r1, r2;
74+
75+
--echo #
76+
--echo # End of 10.2 tests
77+
--echo #

sql/sql_acl.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,6 @@ static int check_user_can_set_role(THD *thd, const char *user, const char *host,
27322732
my_error(ER_INVALID_ROLE, MYF(0), rolename);
27332733
break;
27342734
case 1:
2735-
StringBuffer<1024> c_usr;
27362735
LEX_CSTRING role_lex;
27372736
/* First, check if current user can see mysql database. */
27382737
bool read_access= !check_access(thd, SELECT_ACL, "mysql", NULL, NULL, 1, 1);
@@ -2753,11 +2752,9 @@ static int check_user_can_set_role(THD *thd, const char *user, const char *host,
27532752
NULL) == -1))
27542753
{
27552754
/* Role is not granted but current user can see the role */
2756-
c_usr.append(user, strlen(user));
2757-
c_usr.append('@');
2758-
c_usr.append(host, strlen(host));
2759-
my_printf_error(ER_INVALID_ROLE, "User %`s has not been granted role %`s",
2760-
MYF(0), c_usr.c_ptr(), rolename);
2755+
my_printf_error(ER_INVALID_ROLE, "User %`s@%`s has not been granted role %`s",
2756+
MYF(0), thd->security_ctx->priv_user,
2757+
thd->security_ctx->priv_host, rolename);
27612758
}
27622759
else
27632760
{

0 commit comments

Comments
 (0)