Skip to content

Commit d54f4f4

Browse files
committed
add SEM configuration document
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
1 parent 678cead commit d54f4f4

File tree

5 files changed

+191
-4
lines changed

5 files changed

+191
-4
lines changed

TOC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@
763763
- [TiDB Password Management](/password-management.md)
764764
- [Role-Based Access Control](/role-based-access-control.md)
765765
- [Certificate-Based Authentication](/certificate-authentication.md)
766+
- [Security Enhanced Mode](/security-enhanced-mode.md)
766767
- SQL
767768
- SQL Language Structure and Syntax
768769
- Attributes

basic-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ You can try out TiDB features on [TiDB Playground](https://play.tidbcloud.com/?u
209209
| [Password management](/password-management.md) | Y | Y | Y | Y | Y | N | N | N | N | N |
210210
| [MySQL compatible `GRANT` system](/privilege-management.md) | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
211211
| [Dynamic Privileges](/privilege-management.md#dynamic-privileges) | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
212-
| [Security Enhanced Mode](/system-variables.md#tidb_enable_enhanced_security) | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
212+
| [Security Enhanced Mode](/security-enhanced-mode.md) | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
213213
| [Redacted Log Files](/log-redaction.md) | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y |
214214

215215
## Data import and export

security-enhanced-mode.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# TiDB Security Enhanced Mode (SEM)
2+
3+
## Overview and Purpose
4+
5+
Security Enhanced Mode (SEM) provides a mandatory access control layer that operates on top of TiDB's standard privilege system. Its primary purpose is to limit the capabilities of all users, including the `root` user.
6+
7+
This feature is especially critical in Database-as-a-Service (DBaaS) environments. Service providers can offer tenants `root` access to their databases—ensuring compatibility with applications—while simultaneously preventing them from executing commands that could compromise the underlying cluster's security, stability, or data isolation.
8+
9+
You can enable SEM in two ways: a default mode with a predefined set of restrictions, or a custom mode that uses a configuration file for a highly detailed security policy.
10+
11+
## Enabling and Configuring SEM
12+
13+
You enable SEM by setting `security.enable-sem = true` in your TiDB server's configuration file (`tidb.toml`). The specific behavior of SEM depends on whether you also provide a configuration file.
14+
15+
You can verify which mode is active by checking the `tidb_enable_enhanced_security` system variable.
16+
17+
```sql
18+
SHOW VARIABLES LIKE 'tidb_enable_enhanced_security';
19+
```
20+
21+
### Mode 1: Default Restrictions
22+
23+
This mode provides a baseline set of security enhancements that primarily reduce the broad power of the `SUPER` privilege, replacing it with fine-grained privileges.
24+
25+
* Activation: Set `enable-sem = true` in `tidb.toml` without setting the `sem-config` path.
26+
* System Variable: `tidb_enable_enhanced_security` will be `ON`.
27+
28+
In this mode, TiDB enforces the following restrictions:
29+
30+
| Restricted Action | Required Privilege for Exemption |
31+
| :------------------------------------------------------------------------------------------------------------ | :------------------------------- |
32+
| Writing data to system tables in the `mysql` schema and viewing sensitive columns in `information_schema` tables. | `RESTRICTED_TABLES_ADMIN` |
33+
| Viewing sensitive variables in `SHOW STATUS`. | `RESTRICTED_STATUS_ADMIN` |
34+
| Viewing and setting sensitive system variables. | `RESTRICTED_VARIABLES_ADMIN` |
35+
| Dropping or modifying a user account that holds the `RESTRICTED_USER_ADMIN` privilege. | `RESTRICTED_USER_ADMIN` |
36+
37+
### Mode 2: Custom Restrictions via Configuration File
38+
39+
This mode enables a fully customizable security policy defined in a JSON file. It offers granular control over tables, variables, privileges, and SQL commands.
40+
41+
* Activation: Set both `enable-sem = true` and `sem-config = '/path/to/your/sem-policy.json'` in `tidb.toml`.
42+
* System Variable: `tidb_enable_enhanced_security` will be `CONFIG`.
43+
44+
You must restart your TiDB cluster for any configuration changes to take effect.
45+
46+
## Custom Policy Feature Reference (Mode 2)
47+
48+
The following sections detail the features available when using a custom configuration file (Mode 2).
49+
50+
### Restricting Access to Tables and Databases
51+
52+
This feature prevents access to specified databases or individual tables.
53+
54+
* Configuration:
55+
* `restricted_databases`: An array of database names. All tables within these databases become inaccessible.
56+
* `restricted_tables`: An array of objects specifying a `schema` and `name`. The optional `"hidden": true` flag makes the table invisible.
57+
* Exemption Privilege: `RESTRICTED_TABLES_ADMIN`
58+
* Configuration Example:
59+
```json
60+
{
61+
"version": "1", "tidb_version": "9.0.0",
62+
"restricted_databases": ["mysql"],
63+
"restricted_tables": [{"schema": "information_schema", "name": "columns", "hidden": true}]
64+
}
65+
```
66+
As a restricted user (e.g., `root`):
67+
```
68+
mysql> select * from information_schema.columns;
69+
ERROR 1142 (42000): SELECT command denied to user 'root'@'%' for table 'columns'
70+
mysql> use mysql;
71+
ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mysql'
72+
```
73+
74+
### Restricting System Variables
75+
76+
This feature controls interaction with system variables by hiding them, making them read-only, or masking their values.
77+
78+
* Configuration: The `restricted_variables` key contains an array of variable objects with a control flag:
79+
* `"hidden": true`: The variable is inaccessible.
80+
* `"readonly": true`: The variable can be read but not modified.
81+
* `"value": "string"`: Overrides the variable's return value. Note: This option is only supported for local read-only variables.
82+
* Exemption Privilege: `RESTRICTED_VARIABLES_ADMIN`
83+
* Configuration Example:
84+
```json
85+
{
86+
"version": "1", "tidb_version": "9.0.0",
87+
"restricted_variables": [
88+
{"name": "tidb_config", "hidden": true},
89+
{"name": "hostname", "hidden": false, "value": "testhostname"}
90+
]
91+
}
92+
```
93+
As a restricted user (e.g., `root`):
94+
```
95+
mysql> SELECT @@tidb_config;
96+
ERROR 1227 (42000): Access denied; you need (at least one of) the RESTRICTED_VARIABLES_ADMIN privilege(s) for this operation
97+
mysql> SELECT @@hostname;
98+
+--------------+
99+
| @@hostname |
100+
+--------------+
101+
| testhostname |
102+
+--------------+
103+
1 row in set (0.00 sec)
104+
```
105+
106+
### Restricting Privileges and User Management
107+
108+
This feature prevents powerful privileges from being granted and protects administrative accounts from being altered or dropped.
109+
110+
* Configuration: The `restricted_privileges` key contains an array of privilege names. Once listed, a privilege cannot be granted. Listing `RESTRICTED_USER_ADMIN` itself protects users who hold that privilege.
111+
* Exemption Privilege: `RESTRICTED_PRIV_ADMIN`
112+
* Configuration Example:
113+
```json
114+
{
115+
"version": "1", "tidb_version": "9.0.0",
116+
"restricted_privileges": ["FILE"]
117+
}
118+
```
119+
As a restricted user (e.g., `root`):
120+
```
121+
mysql> GRANT FILE ON *.* TO 'some_user'@'%';
122+
ERROR 1227 (42000): Access denied; you need (at least one of) the RESTRICTED_PRIV_ADMIN privilege(s) for this operation
123+
-- Assuming 'sem_admin' has the RESTRICTED_USER_ADMIN privilege, attempt to drop the user
124+
mysql> DROP USER 'sem_admin'@'%';
125+
ERROR 1227 (42000): Access denied; you need (at least one of) the RESTRICTED_USER_ADMIN privilege(s) for this operation
126+
```
127+
128+
### Restricting Status Variables
129+
130+
This feature filters sensitive operational data from the output of `SHOW STATUS`.
131+
132+
* Configuration:
133+
* `restricted_status_variables`: An array of status variable names to hide from `SHOW STATUS`.
134+
* Exemption Privilege: `RESTRICTED_STATUS_ADMIN`
135+
* Configuration Example:
136+
```json
137+
{
138+
"version": "1", "tidb_version": "9.0.0",
139+
"restricted_status_variables": ["tidb_gc_leader_desc"]
140+
}
141+
```
142+
As a restricted user (e.g., `root`):
143+
```
144+
mysql> SHOW STATUS LIKE 'tidb_gc_leader_desc';
145+
Empty set (0.01 sec)
146+
```
147+
148+
### Restricting SQL Commands
149+
150+
This feature blocks the execution of specific SQL statements or entire classes of commands.
151+
152+
* Configuration:
153+
* `restricted_sql`: An object containing two arrays:
154+
* `sql`: A list of specific SQL commands to block (e.g., `BACKUP`, `RESTORE`).
155+
* `rule`: A list of predefined rule names that block specific classes of statements. Supported rules are:
156+
* `time_to_live`: Blocks DDL statements related to Table TTL.
157+
* `alter_table_attributes`: Blocks the `ALTER TABLE ... ATTRIBUTES="..."` statement.
158+
* `import_with_external_id`: Blocks `IMPORT INTO` statements that use an S3 `EXTERNAL_ID`.
159+
* `select_into_file`: Blocks `SELECT ... INTO OUTFILE` statements.
160+
* `import_from_local`: Blocks `LOAD DATA LOCAL INFILE` and `IMPORT INTO` from a local file path.
161+
* Exemption Privilege: `RESTRICTED_SQL_ADMIN`
162+
* Configuration Example:
163+
```json
164+
{
165+
"version": "1", "tidb_version": "9.0.0",
166+
"restricted_sql": {
167+
"rule": ["time_to_live"],
168+
"sql": ["BACKUP"]
169+
}
170+
}
171+
```
172+
As a restricted user (e.g., `root`):
173+
```
174+
mysql> BACKUP DATABASE `test` TO 's3://bucket/backup';
175+
ERROR 8132 (HY000): Feature 'BACKUP DATABASE `test` TO 's3://bucket/backup'' is not supported when security enhanced mode is enabled
176+
mysql> CREATE TABLE test.t1 (id INT, created_at TIMESTAMP) TTL = `created_at` + INTERVAL 1 DAY;
177+
ERROR 8132 (HY000): Feature 'CREATE TABLE test.t1 (id INT, created_at TIMESTAMP) TTL = `created_at` + INTERVAL 1 DAY' is not supported when security enhanced mode is enabled
178+
```

system-variables.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,12 +2062,13 @@ mysql> SELECT job_info FROM mysql.analyze_jobs ORDER BY end_time DESC LIMIT 1;
20622062
20632063
- Scope: NONE
20642064
- Applies to hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value): No
2065-
- Type: Boolean
2065+
- Type: String
20662066
20672067
<CustomContent platform="tidb">
20682068
20692069
- Default value: `OFF`
2070-
- This variable indicates whether the TiDB server you are connected to has the Security Enhanced Mode (SEM) enabled. To change its value, you need to modify the value of `enable-sem` in your TiDB server configuration file and restart the TiDB server.
2070+
- Possible values: `OFF`, `ON`, `CONFIG`
2071+
- This variable indicates whether the TiDB server you are connected to has the Security Enhanced Mode (SEM) enabled. To change its value, you need to modify the value of `enable-sem` and `sem-config` in your TiDB server [configuration file](/tidb-configuration-file.md) and restart the TiDB server.
20712072
20722073
</CustomContent>
20732074
@@ -2083,6 +2084,7 @@ mysql> SELECT job_info FROM mysql.analyze_jobs ORDER BY end_time DESC LIMIT 1;
20832084
- `RESTRICTED_STATUS_ADMIN`: The ability to see sensitive variables in the command `SHOW STATUS`.
20842085
- `RESTRICTED_VARIABLES_ADMIN`: The ability to see and set sensitive variables in `SHOW [GLOBAL] VARIABLES` and `SET`.
20852086
- `RESTRICTED_USER_ADMIN`: The ability to prevent other users from making changes or dropping a user account.
2087+
- For detailed configuration, see [Security Enhanced Mode documentation](/security-enhanced-mode.md).
20862088
20872089
### tidb_enable_exchange_partition
20882090

tidb-configuration-file.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,16 @@ Configuration items related to security.
385385

386386
### `enable-sem`
387387

388-
- Enables the Security Enhanced Mode (SEM).
388+
- Enables the [Security Enhanced Mode (SEM)](/security-enhanced-mode.md).
389389
- Default value: `false`
390390
- The status of SEM is available via the system variable [`tidb_enable_enhanced_security`](/system-variables.md#tidb_enable_enhanced_security).
391391

392+
### `sem-config`
393+
394+
- Specifies the path to the JSON configuration file for custom [Security Enhanced Mode (SEM)](/security-enhanced-mode.md) restrictions.
395+
- Default value: `""`
396+
- The status of SEM is available via the system variable [`tidb_enable_enhanced_security`](/system-variables.md#tidb_enable_enhanced_security).
397+
392398
### `ssl-ca`
393399

394400
- The file path of the trusted CA certificate in the PEM format.

0 commit comments

Comments
 (0)