Skip to content

Commit

Permalink
executor,infoschema: check privilege for 'show processlist' (#7858)
Browse files Browse the repository at this point in the history
"show processlist" requires the PROCESS privilege.
Otherwise, the user can see only his own threads.
  • Loading branch information
tiancaiamao authored and shenli committed Oct 10, 2018
1 parent ead685b commit 38f2fe0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/ranger"
Expand Down Expand Up @@ -78,6 +79,7 @@ func (s *testExecSuite) TestShowProcessList(c *C) {
}
sctx := mock.NewContext()
sctx.SetSessionManager(sm)
sctx.GetSessionVars().User = &auth.UserIdentity{Username: "test"}

// Compose executor.
e := &ShowExec{
Expand Down
14 changes: 14 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ func (e *ShowExec) fetchShowProcessList() error {
return nil
}

loginUser := e.ctx.GetSessionVars().User
var hasProcessPriv bool
if pm := privilege.GetPrivilegeManager(e.ctx); pm != nil {
if pm.RequestVerification("", "", "", mysql.ProcessPriv) {
hasProcessPriv = true
}
}

pl := sm.ShowProcessList()
for _, pi := range pl {
var info string
Expand All @@ -197,6 +205,12 @@ func (e *ShowExec) fetchShowProcessList() error {
info = fmt.Sprintf("%.100v", pi.Info)
}

// If you have the PROCESS privilege, you can see all threads.
// Otherwise, you can see only your own threads.
if !hasProcessPriv && pi.User != loginUser.Username {
continue
}

e.appendRow([]interface{}{
pi.ID,
pi.User,
Expand Down
14 changes: 14 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,23 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
return nil
}

loginUser := ctx.GetSessionVars().User
var hasProcessPriv bool
if pm := privilege.GetPrivilegeManager(ctx); pm != nil {
if pm.RequestVerification("", "", "", mysql.ProcessPriv) {
hasProcessPriv = true
}
}

var records [][]types.Datum
pl := sm.ShowProcessList()
for _, pi := range pl {
// If you have the PROCESS privilege, you can see all threads.
// Otherwise, you can see only your own threads.
if !hasProcessPriv && pi.User != loginUser.Username {
continue
}

var t uint64
if len(pi.Info) != 0 {
t = uint64(time.Since(pi.Time) / time.Second)
Expand Down

0 comments on commit 38f2fe0

Please sign in to comment.