Who really runs with sysadmin rights on your SQL Server: the people you trust, or the code they run? That question sits at the heart of "Don't Let Your Permissions Be Hijacked!", presented at SQLDay Lite 2026 by Erland Sommarskog, an independent consultant based in Stockholm, a Data Platform MVP and a SQL Server MVP since 2001. The conference was organized by Data Community Poland at the Gdańsk University of Technology on 24-25 September 2026.
Security is easy to postpone until something goes wrong. Erland's session turns it into a set of habits a DBA can adopt tomorrow: small, concrete changes to how maintenance, deployments and scheduled jobs run, so that high privileges are only ever used for the work they are meant for. Every idea comes with a demo script, which makes the talk as practical as it is thorough.
The question behind the session: whose permissions does this code use?
Erland opens with a precise definition. Permission hijacking is when someone with fewer permissions than you gets you to unknowingly run code that uses your permissions for their benefit. The key word is "unknowingly". Nobody asks the DBA to grant anything. The DBA simply does routine work, and code that someone else placed in the database runs along with it, under the DBA's identity.
In SQL Server that code can live in several places: DDL triggers that fire when objects are created or changed, DML triggers that fire on inserts, updates and deletes, stored procedures, functions, and even computed columns. Whoever can create or change those objects can, in principle, attach work to whatever a more privileged user does next.
Erland is realistic about where this matters. On a server with only sysadmins and plain users, there is little room in between. The risk grows on servers with layers: a server DBA with sysadmin rights, an application admin who is db_owner of one database, and developers with rights such as db_ddladmin. The slides also point to two groups that are easy to overlook: application logins with elevated permissions, and the developers whose code gets deployed to the server. His preamble sets the tone with a calm reminder: "When it comes to security, nothing is 'too unlikely'."
Sandboxing the DBA with EXECUTE AS USER = 'dbo'
The first demo looks at everyday index and statistics maintenance. A server DBA rebuilds indexes in an application database. If that database contains a DDL trigger, the trigger fires during the rebuild and runs with the DBA's sysadmin rights, which is far more than index maintenance ever needs.
Erland's answer is the Principle of Least Privilege (PoLP), applied with one statement. Before doing the work, the DBA runs EXECUTE AS USER = 'dbo'. Impersonating a database user sandboxes the session into the current database: it keeps full rights inside that database, but gives up all server-level permissions for as long as the impersonation lasts. Any trigger that fires now runs with database rights only. The demo makes this visible by comparing sys.user_token and sys.login_token before and during impersonation.
The session explains the details that make the technique reliable:
EXECUTE AS USERruns commands as a database user, sandboxed into the current database, with temp tables still available.EXECUTE AS LOGINruns as a server login, sandboxed inside the instance but able to reach other databases.- Impersonation stays in force until
REVERTruns in the same scope. AREVERTinside a trigger or procedure has no effect on anEXECUTE ASin the outer scope, which the demo confirms. - Erland's tip: put
REVERTin its own batch, so that it runs even if an earlier statement fails.
There is also a "small hiccup" worth knowing. On databases restored from another server, EXECUTE AS USER = 'dbo' can fail with error 15517, because the owner SID in sys.databases does not match dbo inside the database. The fix is a routine: set the database owner whenever you restore a database. Erland's own recommendation is that each database should be owned by an SQL login that exists solely to own that database, and his demo creates such a login and disables it.
Maintenance tools that support the habit
Turning this into daily practice is easier than it sounds. Erland points out that IndexOptimize in Ola Hallengren's Maintenance Solution accepts an @ExecuteAsUser parameter, which you set to 'dbo' or another user of your choice. For the maintenance plans in SSMS, which have no such option, the slides suggest keeping those plans to backup and DBCC, and handling index and statistics maintenance either through the application owners or with one task per database, each with its own login that is db_owner in that database only.
The same thinking applies to any database-level DDL a server DBA runs where there is a local db_owner, from creating indexes to adding users and granting permissions. Erland recommends bracketing that DDL with EXECUTE AS USER = 'dbo'. Since the SSMS dialogs do not offer this, the practical route is the Script button in those dialogs: generate the script, wrap it in the impersonation and run it from a query window.
A safer home for developers: the our_ddladmin role
The next layer protects the application admin. A user in db_ddladmin can create DDL triggers, and a DDL trigger can reach the rights of a db_owner who later changes the schema. Erland's view is clear: this role should not have the right to create DDL triggers.
SQL Server does not allow a DENY directly on the fixed role db_ddladmin, so the session shows an elegant workaround. Create your own role, make it a member of db_ddladmin, deny that role the right to manage database DDL triggers, and add the developers to your role instead of to db_ddladmin. From Erland's demo scripts:
CREATE ROLE our_ddladmin
ALTER ROLE db_ddladmin ADD MEMBER our_ddladmin
DENY ALTER ANY DATABASE DDL TRIGGER TO our_ddladmin
ALTER ROLE our_ddladmin ADD MEMBER DevDave
CREATE ROLE our_ddladmin
ALTER ROLE db_ddladmin ADD MEMBER our_ddladmin
DENY ALTER ANY DATABASE DDL TRIGGER TO our_ddladmin
ALTER ROLE our_ddladmin ADD MEMBER DevDave
CREATE ROLE our_ddladmin
ALTER ROLE db_ddladmin ADD MEMBER our_ddladmin
DENY ALTER ANY DATABASE DDL TRIGGER TO our_ddladmin
ALTER ROLE our_ddladmin ADD MEMBER DevDave
The demo then shows the result: the developer can still create tables and stored procedures as before, but the attempt to create a DDL trigger is refused. It is a good example of least privilege that keeps developers productive while closing one specific door. The demo also shows how an admin reviews the database's existing DDL triggers through sys.triggers, a quick check that is worth adding to any regular review.
Sandbox users for DML, procedures and even SELECT
Triggers on tables and code in stored procedures raise the same question for everyday data work. If you only need to protect sysadmin, impersonating dbo again does the job. To combine it with data from outside the database, read that data into temp tables before the impersonation starts.
Protecting db_owner calls for a lower floor. Erland's slide shows a sandbox user without a login that has only the rights the task needs:
CREATE USER SandboxUser WITHOUT LOGIN
GRANT EXECUTE TO SandboxUser
GRANT SELECT, INSERT, UPDATE, DELETE TO SandboxUser
EXECUTE AS USER = 'SandboxUser'
CREATE USER SandboxUser WITHOUT LOGIN
GRANT EXECUTE TO SandboxUser
GRANT SELECT, INSERT, UPDATE, DELETE TO SandboxUser
EXECUTE AS USER = 'SandboxUser'
CREATE USER SandboxUser WITHOUT LOGIN
GRANT EXECUTE TO SandboxUser
GRANT SELECT, INSERT, UPDATE, DELETE TO SandboxUser
EXECUTE AS USER = 'SandboxUser'
The most surprising part of the session concerns plain SELECT statements. A table can have a computed column that calls a user-defined function, and that function runs with the rights of whoever reads the table. Erland credits Baptiste Devigne for pointing out this case. The defenses are straightforward: keep xp_cmdshell disabled and only enable it briefly when it is genuinely needed, keep in mind that OPENQUERY and OPENROWSET can also reach outside the database where they are enabled, and treat ad-hoc SELECTs in someone else's database like any other task, under EXECUTE AS USER.
Agent jobs and deployments under a limited identity
Two more places where high privileges tend to creep in are scheduled jobs and deployment packages.
When an application team asks for a nightly purge procedure to run as a SQL Server Agent job, the job should not run as sysadmin. Erland's first suggestion is friendly: application jobs may fit better in Task Scheduler on the application server, since Agent is meant for management tasks. Where Agent is the answer, the slides list three ways to run a job with limited permissions: set the user on the job step (which uses EXECUTE AS USER), set the job owner (which uses EXECUTE AS LOGIN), or use an Agent proxy, the most general option. In every case the identity should be a dedicated, fictive user rather than a person who might one day leave the company.
Deployment packages get the same treatment. For a plain script, Erland suggests running it under EXECUTE AS USER = 'dbo' WITH NO REVERT, which makes any REVERT in the script fail, or better, under a sandbox user with exactly the rights you intend to give the application team. Running the deployment in QA first reveals unexpected but legitimate actions before production. When a deployment genuinely needs high permissions, or arrives as a DACPAC or MSI install with no control over the login, auditing is the safety net. Of the available options, Erland notes that SQL Server Audit is the only one that also audits being turned off.
Practical takeaways
- Apply the Principle of Least Privilege to your own work as a DBA, not only to other users.
- Impersonate dbo (
EXECUTE AS USER = 'dbo') for index and statistics maintenance and database-level DDL, and putREVERTin its own batch. - Set the database owner after every restore, ideally to a dedicated, disabled login that only owns that database.
- Never add people directly to db_ddladmin. Use a role such as our_ddladmin that is denied ALTER ANY DATABASE DDL TRIGGER.
- Use sandbox users to protect db_owner, and treat ad-hoc SELECTs in an application database the same way.
- Run application jobs under a dedicated user with limited permissions, never as sysadmin.
- Run deployment scripts under a sandbox identity with
WITH NO REVERT, test them in QA, and use SQL Server Audit where that is not possible.
Erland's closing slide adds a thoughtful nuance: watch for the "benevolent attacker", someone who works around a restriction not out of bad intent but simply to get their job done. Locking down permissions is a good decision, and the techniques in this session are what keep it effective.
Trusted access is the foundation of trusted data
Erland's summary makes a point that reaches beyond security: "elevated permission" includes access to the deployment pipeline, and an application team can in practice be equal to db_owner even without access to the server. Whoever can change triggers, procedures and deployment scripts can also change how data is written. Clear ownership of who may change what is therefore as much a data quality question as a security one.
Good data quality governance builds on exactly that: defined owners, controlled changes and checks that confirm data still behaves the way it should after a deployment or a maintenance run. Our guide to data quality governance covers how roles, policies and continuous monitoring fit together, and access discipline like Erland's is the natural layer underneath it.
Thank you, Erland and SQLDay Lite
Many thanks to Erland Sommarskog for a session that turns a complex security topic into clear, runnable steps, and to Data Community Poland for organizing SQLDay Lite 2026 in Gdańsk. The slides and demo scripts are available in the SQLDay Lite 2026 repository, and Erland publishes his slides and scripts on his presentations page. For the full background, read his article Don't Let Your Permissions Be Hijacked!. More about the conference is on the SQLDay Lite website.
Frequently asked questions
What is permission hijacking in SQL Server?
Permission hijacking is when someone with fewer permissions gets a more privileged user to unknowingly run code that uses those permissions for their benefit. In SQL Server that code can hide in DDL and DML triggers, stored procedures, functions and even computed columns, as Erland Sommarskog showed at SQLDay Lite 2026.
How does EXECUTE AS USER = 'dbo' protect a DBA during index maintenance?
It sandboxes the session into the current database. The DBA keeps full rights inside that database but gives up all server-level permissions while the impersonation lasts, so a DDL trigger firing during an index rebuild runs with database rights only. The demo proves it by comparing sys.user_token and sys.login_token.
Why does EXECUTE AS USER = 'dbo' fail with error 15517 after a restore?
Error 15517 appears when the owner SID in sys.databases does not match dbo inside a database restored from another server. The fix is to set the database owner after every restore. Erland recommends a dedicated SQL login that exists only to own that database, created and then disabled.
How can developers keep db_ddladmin rights without creating DDL triggers?
Create your own role, such as our_ddladmin, make it a member of db_ddladmin and deny it ALTER ANY DATABASE DDL TRIGGER, since a DENY directly on the fixed role is not allowed. In the demo, developers in the new role still create tables and procedures, while creating a DDL trigger is refused.
How should SQL Server Agent jobs and deployment scripts run safely?
Under a dedicated identity with limited permissions, never as sysadmin. Agent jobs can use a job step user, the job owner or an Agent proxy. Deployment scripts can run under EXECUTE AS USER = 'dbo' WITH NO REVERT or a sandbox user, with SQL Server Audit as the safety net when high permissions are unavoidable.