Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privilege: fix privilege check of CREATE ROLE and DROP ROLE (#13940) #14088

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,11 @@ func (e *SimpleExec) executeCreateUser(ctx context.Context, s *ast.CreateUserStm
}
activeRoles := e.ctx.GetSessionVars().ActiveRoles
if !checker.RequestVerification(activeRoles, mysql.SystemDB, mysql.UserTable, "", mysql.InsertPriv) {
if s.IsCreateRole && !checker.RequestVerification(activeRoles, "", "", "", mysql.CreateRolePriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("CREATE ROLE")
if s.IsCreateRole {
if !checker.RequestVerification(activeRoles, "", "", "", mysql.CreateRolePriv) &&
!checker.RequestVerification(activeRoles, "", "", "", mysql.CreateUserPriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("CREATE ROLE or CREATE USER")
}
}
if !s.IsCreateRole && !checker.RequestVerification(activeRoles, "", "", "", mysql.CreateUserPriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("CREATE User")
Expand All @@ -661,7 +664,10 @@ func (e *SimpleExec) executeCreateUser(ctx context.Context, s *ast.CreateUserStm
}
if exists {
if !s.IfNotExists {
return errors.New("Duplicate user")
if s.IsCreateRole {
return ErrCannotUser.GenWithStackByArgs("CREATE ROLE", spec.User.String())
}
return ErrCannotUser.GenWithStackByArgs("CREATE USER", spec.User.String())
}
continue
}
Expand Down Expand Up @@ -819,8 +825,11 @@ func (e *SimpleExec) executeDropUser(s *ast.DropUserStmt) error {
}
activeRoles := e.ctx.GetSessionVars().ActiveRoles
if !checker.RequestVerification(activeRoles, mysql.SystemDB, mysql.UserTable, "", mysql.DeletePriv) {
if s.IsDropRole && !checker.RequestVerification(activeRoles, "", "", "", mysql.DropRolePriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("DROP ROLE")
if s.IsDropRole {
if !checker.RequestVerification(activeRoles, "", "", "", mysql.DropRolePriv) &&
!checker.RequestVerification(activeRoles, "", "", "", mysql.CreateUserPriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("DROP ROLE or CREATE USER")
}
}
if !s.IsDropRole && !checker.RequestVerification(activeRoles, "", "", "", mysql.CreateUserPriv) {
return core.ErrSpecificAccessDenied.GenWithStackByArgs("CREATE USER")
Expand Down
48 changes: 48 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,54 @@ func (s *testSuite3) TestDo(c *C) {
tk.MustQuery("select @a").Check(testkit.Rows("1"))
}

func (s *testSuite3) TestCreateRole(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user testCreateRole;")
tk.MustExec("grant CREATE USER on *.* to testCreateRole;")
se, err := session.CreateSession4Test(s.store)
c.Check(err, IsNil)
defer se.Close()
c.Assert(se.Auth(&auth.UserIdentity{Username: "testCreateRole", Hostname: "localhost"}, nil, nil), IsTrue)

ctx := context.Background()
_, err = se.Execute(ctx, `create role test_create_role;`)
c.Assert(err, IsNil)
tk.MustExec("revoke CREATE USER on *.* from testCreateRole;")
tk.MustExec("drop role test_create_role;")
tk.MustExec("grant CREATE ROLE on *.* to testCreateRole;")
_, err = se.Execute(ctx, `create role test_create_role;`)
c.Assert(err, IsNil)
tk.MustExec("drop role test_create_role;")
_, err = se.Execute(ctx, `create user test_create_role;`)
c.Assert(err, NotNil)
tk.MustExec("drop user testCreateRole;")
}

func (s *testSuite3) TestDropRole(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user testCreateRole;")
tk.MustExec("create user test_create_role;")
tk.MustExec("grant CREATE USER on *.* to testCreateRole;")
se, err := session.CreateSession4Test(s.store)
c.Check(err, IsNil)
defer se.Close()
c.Assert(se.Auth(&auth.UserIdentity{Username: "testCreateRole", Hostname: "localhost"}, nil, nil), IsTrue)

ctx := context.Background()
_, err = se.Execute(ctx, `drop role test_create_role;`)
c.Assert(err, IsNil)
tk.MustExec("revoke CREATE USER on *.* from testCreateRole;")
tk.MustExec("create role test_create_role;")
tk.MustExec("grant DROP ROLE on *.* to testCreateRole;")
_, err = se.Execute(ctx, `drop role test_create_role;`)
c.Assert(err, IsNil)
tk.MustExec("create user test_create_role;")
_, err = se.Execute(ctx, `drop user test_create_role;`)
c.Assert(err, NotNil)
tk.MustExec("drop user testCreateRole;")
tk.MustExec("drop user test_create_role;")
}

func (s *testSuite3) TestTransaction(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("begin")
Expand Down