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 DBIsVisible privilege check (#14190) #14386

Merged
merged 4 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
columnPrivMask = computePrivMask(mysql.AllColumnPrivs)
)

const globalDBVisible = mysql.CreatePriv | mysql.SelectPriv | mysql.InsertPriv | mysql.UpdatePriv | mysql.DeletePriv | mysql.ShowDBPriv | mysql.DropPriv | mysql.AlterPriv | mysql.IndexPriv | mysql.CreateViewPriv | mysql.ShowViewPriv | mysql.GrantPriv

func computePrivMask(privs []mysql.PrivilegeType) mysql.PrivilegeType {
var mask mysql.PrivilegeType
for _, p := range privs {
Expand Down Expand Up @@ -848,7 +850,7 @@ func (p *MySQLPrivilege) RequestVerification(activeRoles []*auth.RoleIdentity, u
// DBIsVisible checks whether the user can see the db.
func (p *MySQLPrivilege) DBIsVisible(user, host, db string) bool {
if record := p.matchUser(user, host); record != nil {
if record.Privileges != 0 {
if record.Privileges&globalDBVisible > 0 {
return true
}
}
Expand Down
52 changes: 52 additions & 0 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,55 @@ func checkUserRecord(x, y []privileges.UserRecord, c *C) {
c.Assert(x[i].Host, Equals, y[i].Host)
}
}

func (s *testCacheSuite) TestDBIsVisible(c *C) {
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
defer se.Close()
mustExec(c, se, "create database visdb")
p := privileges.MySQLPrivilege{}
err = p.LoadAll(se)
c.Assert(err, IsNil)

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Create_role_priv, Super_priv) VALUES ("%", "testvisdb", "Y", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible := p.DBIsVisible("testvisdb", "%", "visdb")
c.Assert(isVisible, IsFalse)
mustExec(c, se, "TRUNCATE TABLE mysql.user")

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Select_priv) VALUES ("%", "testvisdb2", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible = p.DBIsVisible("testvisdb2", "%", "visdb")
c.Assert(isVisible, IsTrue)
mustExec(c, se, "TRUNCATE TABLE mysql.user")

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Create_priv) VALUES ("%", "testvisdb3", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible = p.DBIsVisible("testvisdb3", "%", "visdb")
c.Assert(isVisible, IsTrue)
mustExec(c, se, "TRUNCATE TABLE mysql.user")

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Insert_priv) VALUES ("%", "testvisdb4", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible = p.DBIsVisible("testvisdb4", "%", "visdb")
c.Assert(isVisible, IsTrue)
mustExec(c, se, "TRUNCATE TABLE mysql.user")

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Update_priv) VALUES ("%", "testvisdb5", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible = p.DBIsVisible("testvisdb5", "%", "visdb")
c.Assert(isVisible, IsTrue)
mustExec(c, se, "TRUNCATE TABLE mysql.user")

mustExec(c, se, `INSERT INTO mysql.user (Host, User, Create_view_priv) VALUES ("%", "testvisdb6", "Y")`)
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
isVisible = p.DBIsVisible("testvisdb6", "%", "visdb")
c.Assert(isVisible, IsTrue)
mustExec(c, se, "TRUNCATE TABLE mysql.user")
}