Overview
SQL Server security has layers: authentication (who you are: logins), authorization (what you may do: users, roles, permissions), data protection (encryption, masking, row filtering) and auditing. Least privilege and separation of duties are the guiding rules.
Core concepts
- Logins exist at the instance level (SQL logins, Windows/AD logins, or Microsoft Entra ID); users exist inside a database and map to logins. Contained database users authenticate at the database, so they move with it.
- Roles: fixed server roles (
sysadmin,securityadmin…), fixed database roles (db_datareader,db_owner…) and your own roles. Grant to roles, add users to roles. - Permissions are
GRANT,DENY(wins over grant) andREVOKE, on server, database, schema or object. Schemas are the unit for granting a set of objects (pg-security-roles compares). - Ownership chaining lets a stored procedure expose data to a caller without granting table permissions, when owners match; combined with
EXECUTE ASit is a controlled escalation tool. - Protection of data:
- TDE encrypts data files, log and backups at rest, transparently. It does not protect data in memory or in transit.
- Always Encrypted encrypts selected columns; keys stay with the client driver so the server (and DBA) never see plaintext; secure enclaves (2019+) allow richer queries.
- Row-Level Security (
CREATE SECURITY POLICYwith an inline predicate function) filters rows by caller. - Dynamic Data Masking hides values in query results for non-privileged users; it is a convenience, not a security boundary.
- TLS for connections (
Encrypt=Truedefault in modern drivers; avoidTrustServerCertificate=Truein production).
- SQL Server Audit and Extended Events record security-relevant actions.
Worked example
Executed on SQL Server 2022: a login mapped to a user with only read access to Orders.
CREATE LOGIN app_login WITH PASSWORD = '<strong password>', CHECK_POLICY = ON;
CREATE USER app_user FOR LOGIN app_login;
CREATE ROLE report_reader;
GRANT SELECT ON dbo.Orders TO report_reader;
ALTER ROLE report_reader ADD MEMBER app_user;
EXECUTE AS USER = 'app_user';
SELECT COUNT(*) FROM dbo.Orders; -- 500000
SELECT TOP 1 * FROM dbo.Ev; -- error 229: The SELECT permission was denied on the object 'Ev'
REVERT;Transparent Data Encryption, also executed:
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>';
CREATE CERTIFICATE TdeCert WITH SUBJECT = 'kb TDE';
USE kb;
CREATE DATABASE ENCRYPTION KEY WITH ALGORITHM = AES_256 ENCRYPTION BY SERVER CERTIFICATE TdeCert;
ALTER DATABASE kb SET ENCRYPTION ON;
SELECT DB_NAME(database_id), encryption_state_desc, key_algorithm, key_length FROM sys.dm_database_encryption_keys;
-- tempdb ENCRYPTED AES 256
-- kb ENCRYPTED AES 256Note that tempdb is encrypted too as soon as any database on the instance uses TDE.
Pitfalls and production notes
- Back up the certificate and private key (
BACKUP CERTIFICATE ... WITH PRIVATE KEY) immediately; without them an encrypted backup cannot be restored (sqlserver-backup-recovery). - Never use
saorsysadminfor applications; disable or renamesaand use strong, rotated secrets from a secret manager. - Passwords in this note are placeholders; do not commit real credentials.
- Avoid
db_ownerfor application accounts; grant execute on procedures or specific tables. TRUSTWORTHY ONplus a privileged owner is a known escalation path; leave it off.- Patch regularly; SQL injection is prevented by parameterised queries, not by permissions alone.
Practice questions
- Which protects against a stolen backup file: TDE, Always Encrypted or both? What do they not protect against?
- Why is dynamic data masking not enough for PII?
- How do ownership chains reduce the permissions you must grant?
References
- Microsoft Learn, Securing SQL Server; Transparent data encryption; Always Encrypted
Related
sqlserver-backup-recovery · pg-security-roles · stored-routines