Skip to content

Commit d916614

Browse files
authored
privilege: fix create temporary tables privileges related bugs (#60313)
close #29280, close #29281
1 parent 446952c commit d916614

File tree

8 files changed

+42
-6
lines changed

8 files changed

+42
-6
lines changed

pkg/executor/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func (e *ShowExec) fetchShowTables(ctx context.Context) error {
629629
for _, v := range showInfos {
630630
// Test with mysql.AllPrivMask means any privilege would be OK.
631631
// TODO: Should consider column privileges, which also make a table visible.
632-
if checker != nil && !checker.RequestVerification(activeRoles, e.DBName.O, v.Name.O, "", mysql.AllPrivMask) {
632+
if checker != nil && !checker.RequestVerification(activeRoles, e.DBName.O, v.Name.O, "", mysql.AllPrivMask&(^mysql.CreateTMPTablePriv)) {
633633
continue
634634
} else if fieldFilter != "" && v.Name.L != fieldFilter {
635635
continue

pkg/parser/mysql/privs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ const (
257257
CreateRolePriv
258258
// DropRolePriv is the privilege to drop a role.
259259
DropRolePriv
260-
// CreateTMPTablePriv is the privilege to create a temporary table.
260+
// CreateTMPTablePriv is the privilege to create a local temporary table.
261261
CreateTMPTablePriv
262262
// LockTablesPriv is the privilege to lock tables.
263263
LockTablesPriv

pkg/planner/core/logical_plans_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ func TestVisitInfo(t *testing.T) {
14061406
{
14071407
sql: `show create table test.ttt`,
14081408
ans: []visitInfo{
1409-
{mysql.AllPrivMask, "test", "ttt", "", nil, false, nil, false},
1409+
{mysql.AllPrivMask & (^mysql.CreateTMPTablePriv), "test", "ttt", "", nil, false, nil, false},
14101410
},
14111411
},
14121412
{

pkg/planner/core/planbuilder.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3439,6 +3439,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (base.P
34393439
}.Init(b.ctx)
34403440
isView := false
34413441
isSequence := false
3442+
isTempTableLocal := false
34423443
// It depends on ShowPredicateExtractor now
34433444
buildPattern := true
34443445

@@ -3456,6 +3457,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (base.P
34563457
if table, err := b.is.TableByName(ctx, show.Table.Schema, show.Table.Name); err == nil {
34573458
isView = table.Meta().IsView()
34583459
isSequence = table.Meta().IsSequence()
3460+
isTempTableLocal = table.Meta().TempTableType == model.TempTableLocal
34593461
}
34603462
user := b.ctx.GetSessionVars().User
34613463
if isView {
@@ -3467,7 +3469,11 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (base.P
34673469
if user != nil {
34683470
err = plannererrors.ErrTableaccessDenied.GenWithStackByArgs("SHOW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
34693471
}
3470-
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask, show.Table.Schema.L, show.Table.Name.L, "", err)
3472+
if isTempTableLocal {
3473+
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask, show.Table.Schema.L, show.Table.Name.L, "", err)
3474+
} else {
3475+
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask&(^mysql.CreateTMPTablePriv), show.Table.Schema.L, show.Table.Name.L, "", err)
3476+
}
34713477
}
34723478
case ast.ShowConfig:
34733479
privErr := plannererrors.ErrSpecificAccessDenied.GenWithStackByArgs("CONFIG")

pkg/privilege/privileges/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
tablePrivMask = computePrivMask(mysql.AllTablePrivs)
5757
)
5858

59-
const globalDBVisible = mysql.CreatePriv | mysql.SelectPriv | mysql.InsertPriv | mysql.UpdatePriv | mysql.DeletePriv | mysql.ShowDBPriv | mysql.DropPriv | mysql.AlterPriv | mysql.IndexPriv | mysql.CreateViewPriv | mysql.ShowViewPriv | mysql.GrantPriv | mysql.TriggerPriv | mysql.ReferencesPriv | mysql.ExecutePriv
59+
const globalDBVisible = mysql.CreatePriv | mysql.SelectPriv | mysql.InsertPriv | mysql.UpdatePriv | mysql.DeletePriv | mysql.ShowDBPriv | mysql.DropPriv | mysql.AlterPriv | mysql.IndexPriv | mysql.CreateViewPriv | mysql.ShowViewPriv | mysql.GrantPriv | mysql.TriggerPriv | mysql.ReferencesPriv | mysql.ExecutePriv | mysql.CreateTMPTablePriv
6060

6161
const (
6262
sqlLoadRoleGraph = "SELECT HIGH_PRIORITY FROM_USER, FROM_HOST, TO_USER, TO_HOST FROM mysql.role_edges"

pkg/privilege/privileges/privileges_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,9 @@ func TestShowCreateTable(t *testing.T) {
722722
store := createStoreAndPrepareDB(t)
723723

724724
tk := testkit.NewTestKit(t, store)
725-
tk.MustExec(`CREATE USER tsct1, tsct2`)
725+
tk.MustExec(`CREATE USER tsct1, tsct2, tsct3`)
726726
tk.MustExec(`GRANT select ON mysql.* to tsct2`)
727+
tk.MustExec(`GRANT create temporary tables on mysql.* to tsct3`)
727728

728729
// should fail
729730
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "tsct1", Hostname: "localhost", AuthUsername: "tsct1", AuthHostname: "%"}, nil, nil, nil))
@@ -733,6 +734,12 @@ func TestShowCreateTable(t *testing.T) {
733734
// should pass
734735
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "tsct2", Hostname: "localhost", AuthUsername: "tsct2", AuthHostname: "%"}, nil, nil, nil))
735736
tk.MustExec(`SHOW CREATE TABLE mysql.user`)
737+
738+
// should fail
739+
// https://github.com/pingcap/tidb/issues/29281
740+
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "tsct3", Hostname: "localhost", AuthUsername: "tsct3", AuthHostname: "%"}, nil, nil, nil))
741+
err = tk.ExecToErr(`SHOW CREATE TABLE mysql.user`)
742+
require.True(t, terror.ErrorEqual(err, plannererrors.ErrTableaccessDenied))
736743
}
737744

738745
func TestAnalyzeTable(t *testing.T) {

tests/integrationtest/r/privilege/privileges.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,13 @@ Error 1227 (42000): Access denied; you need (at least one of) the CREATE USER pr
722722
alter user test account unlock;
723723
Error 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
724724
alter user u59677 identified by 'abcde';
725+
drop database if exists tmpdb;
726+
create database tmpdb;
727+
drop user test_user;
728+
create user test_user;
729+
grant create temporary tables on tmpdb.* to test_user;
730+
show databases;
731+
Database
732+
INFORMATION_SCHEMA
733+
tmpdb
734+
create temporary table tmpdb.tmp(id int);

tests/integrationtest/t/privilege/privileges.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,16 @@ alter user test account unlock;
977977
alter user u59677 identified by 'abcde';
978978
disconnect u59677;
979979
connection default;
980+
981+
# TestIssue29280
982+
drop database if exists tmpdb;
983+
create database tmpdb;
984+
drop user test_user;
985+
create user test_user;
986+
grant create temporary tables on tmpdb.* to test_user;
987+
connect (test_user, localhost, test_user,,);
988+
show databases;
989+
create temporary table tmpdb.tmp(id int);
990+
disconnect test_user;
991+
connection default;
992+

0 commit comments

Comments
 (0)