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: privilege check USE command #8418

Merged
merged 7 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions executor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
ErrCantChangeTxCharacteristics = terror.ClassExecutor.New(mysql.ErrCantChangeTxCharacteristics, mysql.MySQLErrName[mysql.ErrCantChangeTxCharacteristics])
ErrPsManyParam = terror.ClassExecutor.New(mysql.ErrPsManyParam, mysql.MySQLErrName[mysql.ErrPsManyParam])
ErrAdminCheckTable = terror.ClassExecutor.New(mysql.ErrAdminCheckTable, mysql.MySQLErrName[mysql.ErrAdminCheckTable])
ErrDBaccessDenied = terror.ClassExecutor.New(mysql.ErrDBaccessDenied, mysql.MySQLErrName[mysql.ErrDBaccessDenied])
)

func init() {
Expand All @@ -57,6 +58,7 @@ func init() {
mysql.ErrCantChangeTxCharacteristics: mysql.ErrCantChangeTxCharacteristics,
mysql.ErrPsManyParam: mysql.ErrPsManyParam,
mysql.ErrAdminCheckTable: mysql.ErrAdminCheckTable,
mysql.ErrDBaccessDenied: mysql.ErrDBaccessDenied,
}
terror.ErrClassToMySQLCodes[terror.ClassExecutor] = tableMySQLErrCodes
}
18 changes: 18 additions & 0 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -83,8 +84,25 @@ func (e *SimpleExec) Next(ctx context.Context, chk *chunk.Chunk) (err error) {
return errors.Trace(err)
}

func (e *SimpleExec) dbAccessDenied(dbname string) error {
user := e.ctx.GetSessionVars().User
u := user.Username
h := user.Hostname
if len(user.AuthUsername) > 0 && len(user.AuthHostname) > 0 {
u = user.AuthUsername
h = user.AuthHostname
}
return ErrDBaccessDenied.GenWithStackByArgs(u, h, dbname)
}

func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
dbname := model.NewCIStr(s.DBName)

checker := privilege.GetPrivilegeManager(e.ctx)
if checker != nil && e.ctx.GetSessionVars().User != nil && !checker.DBIsVisible(fmt.Sprint(dbname)) {
return e.dbAccessDenied(dbname.O)
}

dbinfo, exists := e.is.SchemaByName(dbname)
if !exists {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbname)
Expand Down
25 changes: 25 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ func (s *testPrivilegeSuite) TestCheckAuthenticate(c *C) {
c.Assert(se.Auth(&auth.UserIdentity{Username: "u4", Hostname: "localhost"}, nil, nil), IsFalse)
}

func (s *testPrivilegeSuite) TestUseDb(c *C) {

se := newSession(c, s.store, s.dbName)
// high privileged user
mustExec(c, se, "CREATE USER 'usesuper'")
mustExec(c, se, "CREATE USER 'usenobody'")
mustExec(c, se, "GRANT ALL ON *.* TO 'usesuper'")
mustExec(c, se, "FLUSH PRIVILEGES")
c.Assert(se.Auth(&auth.UserIdentity{Username: "usesuper", Hostname: "localhost", AuthUsername: "usesuper", AuthHostname: "%"}, nil, nil), IsTrue)
mustExec(c, se, "use mysql")
// low privileged user
c.Assert(se.Auth(&auth.UserIdentity{Username: "usenobody", Hostname: "localhost", AuthUsername: "usenobody", AuthHostname: "%"}, nil, nil), IsTrue)
_, err := se.Execute(context.Background(), "use mysql")
c.Assert(err, NotNil)

// try again after privilege granted
c.Assert(se.Auth(&auth.UserIdentity{Username: "usesuper", Hostname: "localhost", AuthUsername: "usesuper", AuthHostname: "%"}, nil, nil), IsTrue)
mustExec(c, se, "GRANT SELECT ON mysql.* TO 'usenobody'")
mustExec(c, se, "FLUSH PRIVILEGES")
c.Assert(se.Auth(&auth.UserIdentity{Username: "usenobody", Hostname: "localhost", AuthUsername: "usenobody", AuthHostname: "%"}, nil, nil), IsTrue)
_, err = se.Execute(context.Background(), "use mysql")
c.Assert(err, IsNil)

}

func (s *testPrivilegeSuite) TestInformationSchema(c *C) {

// This test tests no privilege check for INFORMATION_SCHEMA database.
Expand Down
12 changes: 8 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,34 +619,36 @@ func runTestShowProcessList(c *C) {
func runTestAuth(c *C) {
runTests(c, nil, func(dbt *DBTest) {
dbt.mustExec(`CREATE USER 'authtest'@'%' IDENTIFIED BY '123';`)
dbt.mustExec(`GRANT ALL on test.* to 'authtest'`)
dbt.mustExec(`FLUSH PRIVILEGES;`)
})
runTests(c, func(config *mysql.Config) {
config.User = "authtest"
config.Passwd = "123"
}, func(dbt *DBTest) {
dbt.mustExec(`USE mysql;`)
dbt.mustExec(`USE information_schema;`)
})

db, err := sql.Open("mysql", getDSN(func(config *mysql.Config) {
config.User = "authtest"
config.Passwd = "456"
}))
c.Assert(err, IsNil)
_, err = db.Query("USE mysql;")
_, err = db.Query("USE information_schema;")
c.Assert(err, NotNil, Commentf("Wrong password should be failed"))
db.Close()

// Test login use IP that not exists in mysql.user.
runTests(c, nil, func(dbt *DBTest) {
dbt.mustExec(`CREATE USER 'authtest2'@'localhost' IDENTIFIED BY '123';`)
dbt.mustExec(`GRANT ALL on test.* to 'authtest2'@'localhost'`)
dbt.mustExec(`FLUSH PRIVILEGES;`)
})
runTests(c, func(config *mysql.Config) {
config.User = "authtest2"
config.Passwd = "123"
}, func(dbt *DBTest) {
dbt.mustExec(`USE mysql;`)
dbt.mustExec(`USE information_schema;`)
morgo marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down Expand Up @@ -683,7 +685,9 @@ func runTestIssue3680(c *C) {
func runTestIssue3682(c *C) {
runTests(c, nil, func(dbt *DBTest) {
dbt.mustExec(`CREATE USER 'issue3682'@'%' IDENTIFIED BY '123';`)
dbt.mustExec(`FLUSH PRIVILEGES;`)
dbt.mustExec(`GRANT ALL on test.* to 'issue3682'`)
dbt.mustExec(`GRANT ALL on mysql.* to 'issue3682'`)
dbt.mustExec(`FLUSH PRIVILEGES`)
})
runTests(c, func(config *mysql.Config) {
config.User = "issue3682"
Expand Down