Skip to content

Commit

Permalink
executor: privilege check USE command (#8418)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored and zz-jason committed Nov 27, 2018
1 parent 654964a commit 73c9173
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
17 changes: 17 additions & 0 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,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 @@ -323,6 +323,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;`)
})
}

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

0 comments on commit 73c9173

Please sign in to comment.