Skip to content

Commit 8295bc0

Browse files
bb7133zz-jason
authored andcommitted
planner/core: fix a bug that check update privilege use wrong AsName and DBName (pingcap#9003) (pingcap#10157)
1 parent 33f1f79 commit 8295bc0

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

planner/core/logical_plan_builder.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ func (g *gbyResolver) Leave(inNode ast.Node) (ast.Node, bool) {
12441244

12451245
func tblInfoFromCol(from ast.ResultSetNode, col *expression.Column) *model.TableInfo {
12461246
var tableList []*ast.TableName
1247-
tableList = extractTableList(from, tableList)
1247+
tableList = extractTableList(from, tableList, true)
12481248
for _, field := range tableList {
12491249
if field.Name.L == col.TblName.L {
12501250
return field.TableInfo
@@ -2144,7 +2144,7 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
21442144
}
21452145

21462146
var tableList []*ast.TableName
2147-
tableList = extractTableList(sel.From.TableRefs, tableList)
2147+
tableList = extractTableList(sel.From.TableRefs, tableList, false)
21482148
for _, t := range tableList {
21492149
dbName := t.Schema.L
21502150
if dbName == "" {
@@ -2262,6 +2262,15 @@ func (b *planBuilder) buildUpdateLists(tableList []*ast.TableName, list []*ast.A
22622262
p = np
22632263
newList = append(newList, &expression.Assignment{Col: col, Expr: newExpr})
22642264
}
2265+
for _, assign := range newList {
2266+
col := assign.Col
2267+
2268+
dbName := col.DBName.L
2269+
if dbName == "" {
2270+
dbName = b.ctx.GetSessionVars().CurrentDB
2271+
}
2272+
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.UpdatePriv, dbName, col.OrigTblName.L, "")
2273+
}
22652274
return newList, p, nil
22662275
}
22672276

@@ -2363,7 +2372,7 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) (Plan, error) {
23632372
del.SetSchema(expression.NewSchema())
23642373

23652374
var tableList []*ast.TableName
2366-
tableList = extractTableList(delete.TableRefs.TableRefs, tableList)
2375+
tableList = extractTableList(delete.TableRefs.TableRefs, tableList, true)
23672376

23682377
// Collect visitInfo.
23692378
if delete.Tables != nil {
@@ -2416,14 +2425,16 @@ func (b *planBuilder) buildDelete(delete *ast.DeleteStmt) (Plan, error) {
24162425
}
24172426

24182427
// extractTableList extracts all the TableNames from node.
2419-
func extractTableList(node ast.ResultSetNode, input []*ast.TableName) []*ast.TableName {
2428+
// If asName is true, extract AsName prior to OrigName.
2429+
// Privilege check should use OrigName, while expression may use AsName.
2430+
func extractTableList(node ast.ResultSetNode, input []*ast.TableName, asName bool) []*ast.TableName {
24202431
switch x := node.(type) {
24212432
case *ast.Join:
2422-
input = extractTableList(x.Left, input)
2423-
input = extractTableList(x.Right, input)
2433+
input = extractTableList(x.Left, input, asName)
2434+
input = extractTableList(x.Right, input, asName)
24242435
case *ast.TableSource:
24252436
if s, ok := x.Source.(*ast.TableName); ok {
2426-
if x.AsName.L != "" {
2437+
if x.AsName.L != "" && asName {
24272438
newTableName := *s
24282439
newTableName.Name = x.AsName
24292440
input = append(input, &newTableName)

planner/core/logical_plan_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,13 @@ func (s *testPlanSuite) TestVisitInfo(c *C) {
16011601
{mysql.SelectPriv, "test", "t", ""},
16021602
},
16031603
},
1604+
{
1605+
sql: "update t a1 set a1.a = a1.a + 1",
1606+
ans: []visitInfo{
1607+
{mysql.UpdatePriv, "test", "t", ""},
1608+
{mysql.SelectPriv, "test", "t", ""},
1609+
},
1610+
},
16041611
{
16051612
sql: "select a, sum(e) from t group by a",
16061613
ans: []visitInfo{

session/session_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,24 @@ func (s *testSessionSuite) TestSetGroupConcatMaxLen(c *C) {
23482348
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue, Commentf("err %v", err))
23492349
}
23502350

2351+
func (s *testSessionSuite) TestUpdatePrivilege(c *C) {
2352+
tk := testkit.NewTestKitWithInit(c, s.store)
2353+
2354+
// Fix issue 8911
2355+
tk.MustExec("create database weperk")
2356+
tk.MustExec("use weperk")
2357+
tk.MustExec("create table tb_wehub_server (id int, active_count int, used_count int)")
2358+
tk.MustExec("create user 'weperk'")
2359+
tk.MustExec("grant all privileges on weperk.* to 'weperk'@'%'")
2360+
tk.MustExec("flush privileges;")
2361+
2362+
tk1 := testkit.NewTestKitWithInit(c, s.store)
2363+
c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "weperk", Hostname: "%"},
2364+
[]byte(""), []byte("")), IsTrue)
2365+
tk1.MustExec("use weperk")
2366+
tk1.MustExec("update tb_wehub_server a set a.active_count=a.active_count+1,a.used_count=a.used_count+1 where id=1")
2367+
}
2368+
23512369
func (s *testSessionSuite) TestTxnGoString(c *C) {
23522370
tk := testkit.NewTestKitWithInit(c, s.store)
23532371
tk.MustExec("drop table if exists gostr;")

0 commit comments

Comments
 (0)