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

executor: add privilege check for show bindings #14443

Merged
merged 3 commits into from
Feb 3, 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
17 changes: 17 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/auth"
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -682,3 +683,19 @@ func (s *testSuite) TestOutdatedInfoSchema(c *C) {
tk.MustExec("truncate table mysql.bind_info")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")
}

func (s *testSuite) TestPrivileges(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")
c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue)
rows := tk.MustQuery("show global bindings").Rows()
c.Assert(len(rows), Equals, 1)
tk.MustExec("create user test@'%'")
c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "test", Hostname: "%"}, nil, nil), IsTrue)
rows = tk.MustQuery("show global bindings").Rows()
c.Assert(len(rows), Equals, 0)
}
49 changes: 49 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,39 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
return nil
}

// visibleChecker checks if a stmt is visible for a certain user.
type visibleChecker struct {
defaultDB string
ctx sessionctx.Context
is infoschema.InfoSchema
manager privilege.Manager
ok bool
}

func (v *visibleChecker) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch x := in.(type) {
case *ast.TableName:
schema := x.Schema.L
if schema == "" {
schema = v.defaultDB
}
if !v.is.TableExists(model.NewCIStr(schema), x.Name) {
v.ok = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about setting v.ok to true if table does not exist in the information schema?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v.ok=v.is.TableExists(model.NewCIStr(schema), x.Name)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eurekaka Why? If the table does not exist anymore, why should we show it?
@crazycs520 No, ok may change from fasle to true then, which should not happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the table is re-created, then the binding would be shown then? would it confuse users since they don't touch bindings actually but the results change?

return in, true
}
activeRoles := v.ctx.GetSessionVars().ActiveRoles
if v.manager != nil && !v.manager.RequestVerification(activeRoles, schema, x.Name.L, "", mysql.SelectPriv) {
v.ok = false
}
return in, true
}
return in, false
}

func (v *visibleChecker) Leave(in ast.Node) (out ast.Node, ok bool) {
return in, true
}

func (e *ShowExec) fetchShowBind() error {
var bindRecords []*bindinfo.BindRecord
if !e.GlobalScope {
Expand All @@ -204,8 +237,24 @@ func (e *ShowExec) fetchShowBind() error {
} else {
bindRecords = domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord()
}
parser := parser.New()
for _, bindData := range bindRecords {
for _, hint := range bindData.Bindings {
stmt, err := parser.ParseOneStmt(hint.BindSQL, hint.Charset, hint.Collation)
if err != nil {
return err
}
checker := visibleChecker{
defaultDB: bindData.Db,
ctx: e.ctx,
is: e.is,
manager: privilege.GetPrivilegeManager(e.ctx),
ok: true,
}
stmt.Accept(&checker)
if !checker.ok {
continue
}
e.appendRow([]interface{}{
bindData.OriginalSQL,
hint.BindSQL,
Expand Down