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: forbid user to drop important system table #7471

Merged
merged 8 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
20 changes: 20 additions & 0 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -154,8 +155,19 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error {
return errors.Trace(err)
}

var errForbidDrop = errors.New("Drop mysql tables is forbidden")

func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
dbName := model.NewCIStr(s.Name)

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if dbName.L == "mysql" {
if !privileges.SkipWithGrant {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we need to consider the privileges.SkipWithGrant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know it's a rare case, but how about that we really want to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need a "backdoor".

return errors.Trace(errForbidDrop)
}
}

err := domain.GetDomain(e.ctx).DDL().DropSchema(e.ctx, dbName)
if infoschema.ErrDatabaseNotExists.Equal(err) {
if s.IfExists {
Expand Down Expand Up @@ -198,6 +210,14 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
return errors.Trace(err)
}

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if tn.Schema.L == "mysql" {
if !privileges.SkipWithGrant {
return errors.Trace(errForbidDrop)
}
}

if config.CheckTableBeforeDrop {
log.Warnf("admin check table `%s`.`%s` before drop.", fullti.Schema.O, fullti.Name.O)
sql := fmt.Sprintf("admin check table `%s`.`%s`", fullti.Schema.O, fullti.Name.O)
Expand Down
6 changes: 6 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func (s *testSuite) TestCreateDropDatabase(c *C) {
c.Assert(err.Error(), Equals, plan.ErrNoDB.Error())
_, err = tk.Exec("select * from t;")
c.Assert(err.Error(), Equals, plan.ErrNoDB.Error())

_, err = tk.Exec("drop database mysql")
c.Assert(err, NotNil)
}

func (s *testSuite) TestCreateDropTable(c *C) {
Expand All @@ -137,6 +140,9 @@ func (s *testSuite) TestCreateDropTable(c *C) {
tk.MustExec("drop table if exists drop_test")
tk.MustExec("create table drop_test (a int)")
tk.MustExec("drop table drop_test")

_, err := tk.Exec("drop table mysql.user")
c.Assert(err, NotNil)
}

func (s *testSuite) TestCreateDropIndex(c *C) {
Expand Down
6 changes: 6 additions & 0 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ func (s *testCacheSuite) TestAbnormalMySQLTable(c *C) {
c.Assert(err, IsNil)
defer se.Close()

saveSkipWithGrant := privileges.SkipWithGrant
defer func() {
privileges.SkipWithGrant = saveSkipWithGrant
}()
privileges.SkipWithGrant = true

// Simulate the case mysql.user is synchronized from MySQL.
mustExec(c, se, "DROP TABLE mysql.user;")
mustExec(c, se, "USE mysql;")
Expand Down