Skip to content

Commit

Permalink
*: fix nil pointer panic of some operations when skip-grant-table is …
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Nov 4, 2021
1 parent 37560e5 commit 64319c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,13 @@ const (
// NotifyUpdatePrivilege updates privilege key in etcd, TiDB client that watches
// the key will get notification.
func (do *Domain) NotifyUpdatePrivilege() error {
// If skip-grant-table is configured, do not flush privileges.
// Because LoadPrivilegeLoop does not run and the privilege Handle is nil,
// the call to do.PrivilegeHandle().Update would panic.
if config.GetGlobalConfig().Security.SkipGrantTable {
return nil
}

if do.etcdClient != nil {
row := do.etcdClient.KV
_, err := row.Put(context.Background(), privilegeKey, "")
Expand Down
6 changes: 0 additions & 6 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -1497,12 +1497,6 @@ func (e *SimpleExec) executeFlush(s *ast.FlushStmt) error {
return errors.New("FLUSH TABLES WITH READ LOCK is not supported. Please use @@tidb_snapshot")
}
case ast.FlushPrivileges:
// If skip-grant-table is configured, do not flush privileges.
// Because LoadPrivilegeLoop does not run and the privilege Handle is nil,
// Call dom.PrivilegeHandle().Update would panic.
if config.GetGlobalConfig().Security.SkipGrantTable {
return nil
}
dom := domain.GetDomain(e.ctx)
return dom.NotifyUpdatePrivilege()
case ast.FlushTiDBPlugin:
Expand Down
42 changes: 42 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"testing"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -2928,3 +2929,44 @@ func TestIssue28675(t *testing.T) {
require.Equal(t, 1, len(tk.MustQuery("desc test.v").Rows()))
require.Equal(t, 1, len(tk.MustQuery("explain test.v").Rows()))
}

func TestSkipGrantTable(t *testing.T) {
save := config.GetGlobalConfig()
config.UpdateGlobal(func(c *config.Config) { c.Security.SkipGrantTable = true })
defer config.StoreGlobalConfig(save)

store, clean := newStore(t)
defer clean()

// Issue 29317
tk := testkit.NewTestKit(t, store)
tk.MustExec(`CREATE USER 'test1'@'%';`)
tk.MustExec(`GRANT BACKUP_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTORE_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RELOAD ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SHUTDOWN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SYSTEM_VARIABLES_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTRICTED_VARIABLES_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTRICTED_STATUS_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTRICTED_CONNECTION_ADMIN, CONNECTION_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTRICTED_USER_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT RESTRICTED_TABLES_ADMIN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT PROCESS ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SHUTDOWN ON *.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SELECT, INSERT, UPDATE, DELETE ON mysql.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SELECT ON information_schema.* TO 'test1'@'%';`)
tk.MustExec(`GRANT SELECT ON performance_schema.* TO 'test1'@'%';`)
tk.MustExec(`GRANT ALL PRIVILEGES ON *.* TO root;`)
tk.MustExec(`revoke SHUTDOWN on *.* from root;`)
tk.MustExec(`revoke CONFIG on *.* from root;`)

tk.MustExec(`CREATE USER 'test2'@'%' IDENTIFIED BY '12345';`)
tk.MustExec(`GRANT PROCESS, CONFIG ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT SHOW DATABASES ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT DASHBOARD_CLIENT ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT SYSTEM_VARIABLES_ADMIN ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT RESTRICTED_VARIABLES_ADMIN ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT RESTRICTED_STATUS_ADMIN ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT RESTRICTED_TABLES_ADMIN ON *.* TO 'test2'@'%';`)
tk.MustExec(`GRANT RESTRICTED_USER_ADMIN ON *.* TO 'test2'@'%';`)
}

0 comments on commit 64319c0

Please sign in to comment.