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 wrong result of SHOW GRANTS (#19704) #19834

Merged
merged 2 commits into from
Sep 7, 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
18 changes: 18 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ func (s *testSuite5) TestIssue18878(c *C) {
tk.MustQuery("show grants for `show_grants`@`127.0.%`")
}

func (s *testSuite5) TestIssue17794(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("CREATE USER 'root'@'8.8.%'")
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
c.Assert(se.Auth(&auth.UserIdentity{Username: "root", Hostname: "9.9.9.9", AuthHostname: "%"}, nil, nil), IsTrue)
tk.Se = se

tk1 := testkit.NewTestKit(c, s.store)
se1, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
c.Assert(se1.Auth(&auth.UserIdentity{Username: "root", Hostname: "8.8.8.8", AuthHostname: "8.8.%"}, nil, nil), IsTrue)
tk1.Se = se1

tk.MustQuery("show grants").Check(testkit.Rows("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION"))
tk1.MustQuery("show grants").Check(testkit.Rows("GRANT USAGE ON *.* TO 'root'@'8.8.%'"))
}

func (s *testSuite5) TestIssue3641(c *C) {
tk := testkit.NewTestKit(c, s.store)
_, err := tk.Exec("show tables;")
Expand Down
12 changes: 8 additions & 4 deletions privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ func (record *baseRecord) match(user, host string) bool {
record.hostMatch(host))
}

func (record *baseRecord) fullyMatch(user, host string) bool {
return record.User == user && record.Host == host
}

func (record *dbRecord) match(user, host, db string) bool {
return record.baseRecord.match(user, host) &&
patternMatch(strings.ToUpper(db), record.dbPatChars, record.dbPatTypes)
Expand Down Expand Up @@ -1007,7 +1011,7 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
// Check whether user exists.
if userList, ok := p.UserMap[user]; ok {
for _, record := range userList {
if host == record.Host || record.hostMatch(host) {
if record.fullyMatch(user, host) {
userExists = true
break
}
Expand All @@ -1018,7 +1022,7 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
}
var g string
for _, record := range p.User {
if record.baseRecord.match(user, host) {
if record.fullyMatch(user, host) {
hasGlobalGrant = true
if (record.Privileges & mysql.GrantPriv) > 0 {
hasGrantOptionPriv = true
Expand Down Expand Up @@ -1067,7 +1071,7 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
// Show db scope grants.
dbPrivTable := make(map[string]mysql.PrivilegeType)
for _, record := range p.DB {
if record.baseRecord.match(user, host) {
if record.fullyMatch(user, host) {
if _, ok := dbPrivTable[record.DB]; ok {
if (record.Privileges & mysql.GrantPriv) > 0 {
hasGrantOptionPriv = true
Expand Down Expand Up @@ -1124,7 +1128,7 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
tablePrivTable := make(map[string]mysql.PrivilegeType)
for _, record := range p.TablesPriv {
recordKey := record.DB + "." + record.TableName
if record.baseRecord.match(user, host) {
if user == record.User && host == record.Host {
if _, ok := dbPrivTable[record.DB]; ok {
if (record.TablePriv & mysql.GrantPriv) > 0 {
hasGrantOptionPriv = true
Expand Down