Skip to content

Commit 3835845

Browse files
tiancaiamaozz-jason
authored andcommitted
*: kill one's own connection doesn't require SUPER privilege (#6954) (#7003)
1 parent c28a7a7 commit 3835845

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

executor/executor_pkg_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ type mockSessionManager struct {
4141
}
4242

4343
// ShowProcessList implements the SessionManager.ShowProcessList interface.
44-
func (msm *mockSessionManager) ShowProcessList() []util.ProcessInfo {
45-
return msm.PS
44+
func (msm *mockSessionManager) ShowProcessList() map[uint64]util.ProcessInfo {
45+
ret := make(map[uint64]util.ProcessInfo)
46+
for _, item := range msm.PS {
47+
ret[item.ID] = item
48+
}
49+
return ret
4650
}
4751

4852
// Kill implements the SessionManager.Kill interface.

executor/show_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@ type mockSessionManager struct {
386386
}
387387

388388
// ShowProcessList implements the SessionManager.ShowProcessList interface.
389-
func (msm *mockSessionManager) ShowProcessList() []util.ProcessInfo {
390-
return []util.ProcessInfo{msm.ShowProcess()}
389+
func (msm *mockSessionManager) ShowProcessList() map[uint64]util.ProcessInfo {
390+
ps := msm.ShowProcess()
391+
return map[uint64]util.ProcessInfo{ps.ID: ps}
391392
}
392393

393394
// Kill implements the SessionManager.Kill interface.

plan/planbuilder.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,21 @@ func (b *planBuilder) buildSimple(node ast.StmtNode) Plan {
773773
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreateUserPriv, "", "", "")
774774
case *ast.GrantStmt:
775775
b.visitInfo = collectVisitInfoFromGrantStmt(b.visitInfo, raw)
776-
case *ast.SetPwdStmt, *ast.RevokeStmt, *ast.KillStmt:
776+
case *ast.SetPwdStmt, *ast.RevokeStmt:
777777
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SuperPriv, "", "", "")
778+
case *ast.KillStmt:
779+
// If you have the SUPER privilege, you can kill all threads and statements.
780+
// Otherwise, you can kill only your own threads and statements.
781+
sm := b.ctx.GetSessionManager()
782+
if sm != nil {
783+
processList := sm.ShowProcessList()
784+
if pi, ok := processList[raw.ConnectionID]; ok {
785+
loginUser := b.ctx.GetSessionVars().User
786+
if pi.User != loginUser.Username {
787+
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SuperPriv, "", "", "")
788+
}
789+
}
790+
}
778791
}
779792
return p
780793
}

server/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,15 @@ func (s *Server) onConn(c net.Conn) {
314314
}
315315

316316
// ShowProcessList implements the SessionManager interface.
317-
func (s *Server) ShowProcessList() []util.ProcessInfo {
318-
var rs []util.ProcessInfo
317+
func (s *Server) ShowProcessList() map[uint64]util.ProcessInfo {
319318
s.rwlock.RLock()
319+
rs := make(map[uint64]util.ProcessInfo, len(s.clients))
320320
for _, client := range s.clients {
321321
if atomic.LoadInt32(&client.status) == connStatusWaitShutdown {
322322
continue
323323
}
324-
rs = append(rs, client.ctx.ShowProcess())
324+
pi := client.ctx.ShowProcess()
325+
rs[pi.ID] = pi
325326
}
326327
s.rwlock.RUnlock()
327328
return rs

util/processinfo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ProcessInfo struct {
3333
// SessionManager is an interface for session manage. Show processlist and
3434
// kill statement rely on this interface.
3535
type SessionManager interface {
36-
ShowProcessList() []ProcessInfo
36+
// ShowProcessList returns map[connectionID]ProcessInfo.
37+
ShowProcessList() map[uint64]ProcessInfo
3738
Kill(connectionID uint64, query bool)
3839
}

0 commit comments

Comments
 (0)