Exploiting SQL Server Date Correlation Optimization: How Tampered Backups Enable Cross‑Database Data Leaks

Comments 0

Share to social media

This article is a follow-up to SQL Server DBaaS Vulnerability: Decrypting System Code & Exfiltrating User Data, in which we saw some vulnerabilities that affected pretty much all DBaaS offerings available in the cloud. Now, we’ll look at another vulnerability that once again affects every major cloud vendor.

In this article, I’ll demonstrate how SQL Server’s own internal optimization mechanisms – specifically those tied to Date Correlation Optimization (DCO) – can be manipulated to carry malicious logic across restore operations.

Back in 2009, I wrote my first two Simple Talk articles all about the feature we’ll explore today. Take a look at them to learn more about DCO, here and here.

The Vulnerability Explained: How Metadata Trust Leads to Exploits

This vulnerability involves restoring a database into a DBaaS and uses a “corrupted” internal view, effectively turning the engine’s own intelligence against itself.

The exploit is elegant in a disturbing way, since there’s no need for xp_cmdshell, CLR, or any of the usual suspects – just a carefully crafted .bak file and SQL Server’s unwavering belief in its own metadata integrity.

The payload hides where no DBA would look – system tables that are supposed to be immutable – and executes under a context that was never meant to be controllable by the user.

This is not a misconfiguration or a permission trick – it’s a design flaw rooted in how the SQL Server engine trusts its persisted metadata, and how that trust persists across the boundary between a user-managed instance and a cloud-managed environment.

Why This Design Flaw is Serious

  • Confidentiality breach across database boundaries – The exploit allows sensitive rows from other databases (application or system) to be read and returned to an attacker who has no direct permissions on those databases. This is not just reading within the attacker’s database; it breaks isolation.

  • Low attacker privileges required – The only privileged action the attacker need is the ability to restore a user-supplied .bak file into a SQL instance (a normal customer operation). No elevated server roles, SQL injection of live code, or OS access are required.

  • Attack executes with engine-level context – The tampered DCO internal view is executed by the optimizer in a privileged engine context (NOEXPAND/internal execution) – bypassing normal cross-database permission checks and effectively executing reads that the tenant account cannot perform directly.

  • Audit and detection blind spot – Reads performed by optimizer-internal queries are not attributed to the user’s batch and are frequently not recorded under SENSITIVE_BATCH_COMPLETED_GROUP or similar audit categories. That makes forensic detection and real-time alerting unreliable or blind to this vector.

  • Persistence and supply-chain style delivery – The malicious logic is persisted in system metadata and survives a standard backup/restore flow. That means an attacker can prepare the payload offline (on a local instance) and deliver it to a managed environment via a legitimate restore operation.

  • Wide blast radius – The exploit can leak data from other application databases and from internal databases, increasing the impact beyond a single tenant schema or table.

What’s the Root Cause of the Vulnerability?

At the heart of the issue is SQL Server’s implicit trust in its own persisted metadata, specifically, the internal objects created by the Date Correlation Optimization (DCO) feature.

When DCO is enabled, SQL Server automatically generates internal, schema-bound indexed views to optimize date correlation queries between related tables.

Those internal views are stored as text definitions inside system metadata tables (not dynamically regenerated each time the database starts). During a backup and restore, that metadata is faithfully preserved and SQL Server assumes it’s safe because the definitions are system-generated.

However, if someone tampers with those definitions offline (for example, by starting the instance in single-user mode, enabling allow updates, and directly modifying sys.sysobjvalues), the malicious view definition is embedded permanently inside the database. When the modified .bak is restored into SQL Server, the engine replays the definitions exactly as-is, without validation or regeneration.

Later, when the optimizer queries those internal DCO views as part of a normal execution plan, it executes the tampered code under engine-level privileges, not the user’s context. This bypasses permission checks and any cross-database subquery inside them can access data outside the attacker’s database.

The final twist: the exfiltration happens through type conversion errors. The malicious predicate forces the optimizer to convert XML (containing sensitive rows) to an integer. The conversion fails, and the raw XML is embedded directly in the resulting error message – an elegant, almost invisible, data leak path.

Because these reads are triggered by optimizer internals, no audit trail is associated with the attacker’s session, creating a stealthy, privilege-bypassing channel that hides in the most trusted layer of SQL Server’s execution pipeline.

The Root Cause in Technical Terms

  • Date Correlation Optimization (DCO) internals:
    When DATE_CORRELATION_OPTIMIZATION is enabled, SQL Server automatically creates hidden, schema-bound indexed views (_MPStats_Sys_*) and supporting indexes to accelerate correlation queries.

  • Persisted view definitions:
    The definition text of these internal views is stored in system metadata (sys.sysobjvalues.imageval).
    This text survives backup/restore operations.

  • Tamperability in local SQL Server:
    On a local SQL Server instance, an attacker with admin control can:
    • Start the instance in single-user mode.
    • Connect via DAC and enable allow updates.
    • Directly overwrite the stored definition of the internal view in sys.sysobjvalues.
    • Insert a malicious predicate that references another database (e.g., SensitiveData or rdsadmin).

  • Trusted restore path:
    When the tampered database is backed up and restored into Amazon RDS, the engine accepts the persisted definition without regenerating it. Thus, the malicious code is preserved in RDS.

  • Optimizer execution context:
    During query compilation/execution, the SQL Server optimizer internally issues queries against these DCO views using NOEXPAND. Because the tampered definition includes a cross-database subquery, the optimizer executes it in the engine’s internal context, bypassing normal cross-database permission checks.

  • Leakage via error messages (XML-to-INT Conversion Error):
    The malicious view is engineered to return XML data (containing the sensitive rows) within a predicate that the optimizer expects to evaluate as an integer (e.g., WHERE 1 = (SELECT ColXML FROM ...)). This forced type conversion failure causes the SQL Server engine to embed the raw XML string directly into the error message, providing the data exfiltration channel. When the optimizer attempts to convert a XML into INT, the engine raises a conversion error. The error text embeds the raw XML, exposing the sensitive data.

  • Audit bypass:
    Since the sensitive read originates from an internal optimizer query (not from the user’s explicit batch), it’s not recorded by SENSITIVE_BATCH_COMPLETED_GROUP. This leaves a critical detection blind spot.

Enjoying this article? You may also be interested in:


Step-by-Step Exploit Walkthrough

What follows is a step-by-step demonstration of how this subtle metadata trust violation can turn into a full data exfiltration path, bypassing both isolation and audit mechanisms.

The steps show a repeatable, hands-on reproduction of the vulnerability from a user-supplied backup to observable data exfiltration inside Amazon RDS for SQL Server.

They list the exact sequence we used: preparing a sensitive target database and audit policy, creating a malicious DCO artifact on a local instance, backing up and restoring that database into RDS, and triggering the optimizer to execute the tampered view.

Warning: Execute these steps only in controlled test environments – do not run them against production systems or systems you don’t own.

1. Log in a RDS using SQL Server 2022:

2. Set up audit on sensitive data:

3. Test the audit:

4. Create a database to be restored on RDS in a local SQL Server instance:

5. Restore Sword_of_Omens.bak on the RDS instance:

6. Back on RDS and with Sword_of_Omens already restored, create a low-privilege login:

7. Open a new session and log as AppLogin:

Although this write-up uses Amazon RDS for SQL Server as the worked example, the underlying flaw is platform-agnostic. Any managed service that accepts customer backups and restores them, whether AWS RDS, Google Cloud SQL, Alibaba ApsaraDB, or a private managed offering, inherits that risk.

Final Thoughts

The vulnerability presented here exposes how even mature, enterprise-grade cloud services for SQL Server can contain critical architectural oversights that allow full privilege escalation within managed environments.

While cloud vendors have typically demonstrated strong security practices, the existence of such sophisticated vulnerabilities emphasizes the need for continuous security assessment, relentless vigilance in security architecture design, robust anomaly detection mechanisms, the rigorous testing of safeguards against complex privilege escalation scenarios, and immediate remediation procedures.

I’ve reported the vulnerability to Microsoft, who assessed it as low-severity – meaning they probably won’t work on it in the near future. Until a vendor-level remediation is applied, all SQL Server users must understand the risks of restoring a database from an untrusted source.

FAQs: SQL Server Date Correlation Optimization (DCO) Exploit

1. What is Date Correlation Optimization (DCO) in SQL Server?

DCO is a SQL Server feature that creates hidden, schema‑bound indexed views to speed up date correlation queries between related tables.

2. What’s the core vulnerability discussed here?

A low‑privileged tenant can restore a specially crafted .bak where the internal DCO view definition was tampered, causing the optimizer to execute malicious cross‑database logic during normal queries and leak data via error messages.

3. Who is impacted by this issue?

Any SQL Server environment—especially DBaaS/managed services—that accepts customer backups (e.g., RDS for SQL Server and similar offerings) can be affected.

4. How does the attack work in practice?

The attacker embeds a malicious predicate in the internal DCO view; when the optimizer probes that view (NOEXPAND), it runs the cross‑database subquery under engine‑level context, and sensitive data can surface in conversion error messages.

5. Does this bypass database permissions and auditing?

Yes. The execution happens as an optimizer‑internal query, which can bypass normal cross‑database checks and often won’t appear under typical sensitive batch audit categories.

6. What data could be exposed?

Rows from other user databases and, in some cases, internal/managed databases if referenced by the tampered view.

7. Is this a misconfiguration or a design issue with SQL Server?

It’s a design flaw stemming from SQL Server’s trust in persisted metadata for internal objects (like DCO views) that survive backup/restore.

8. Which versions of SQL Server are affected?

The technique targets how DCO’s internal objects are persisted and trusted; the demo uses SQL Server 2022, but the risk relates to the DCO mechanism and restore path, not one specific version.

9. What are practical mitigations right now?

  • Never restore untrusted backups into shared or managed instances.
  • Disable or restrict DCO where it’s not needed, and rebuild internal stats/objects after restore when feasible.
  • Harden restore workflows (quarantine/validation steps, re‑creation of internal objects, post‑restore integrity checks).
  • Tighten auditing/monitoring to flag unusual optimizer errors (e.g., XML→INT conversion) and unexpected cross‑DB access patterns.

10. How can I detect signs of this attack?

Look for unexpected conversion errors containing XML payloads, optimizer‑internal accesses to hidden DCO views, and unexplained cross‑database reads not tied to a user batch.

Subscribe to the Simple Talk newsletter

Get selected articles, event information, podcasts and other industry content delivered straight to your inbox every two weeks.
Subscribe now

Article tags

Load comments

About the author

Fabiano Amorim

See Profile

Fabiano Amorim is a Data Platform MVP since 2011 that loves to conquer complex, challenging problems—especially ones that others aren’t able to solve. He first became interested in technology when his older brother would bring him to his work meetings at the age of 14. With over a decade of experience, Fabiano is well known in the database community for his performance tuning abilities. When he isn’t working, he loves to read and spend time with his family.

Fabiano Amorim's contributions