First Published 21st April 2017, Last Updated 17th June 2019 by Tim Trott

There is a new configuration option in SQL Server 2017 called CLR strict security, it is enabled by default, and can cause problems when using CLR.

CLR uses Code Access Security (CAS) in the .NET Framework, which is no longer supported as a security boundary. A CLR assembly created with PERMISSION_SET = SAFE may be able to access external system resources, call unmanaged code, and acquire sysadmin privileges.

When enabled, it treats SAFE and EXTERNAL_ACCESS assemblies as if they were marked UNSAFE and any assemblies that are not signed will fail to load.

Microsoft recommends that all assemblies be signed by a certificate or asymmetric key with a corresponding login that has been granted UNSAFE ASSEMBLY permission in the master database.

Signing Assemblies

The proper way around this is to insall only signed assemblies. Use these steps to sign your own assemblies, or contact your vendor to obtain updated signed assembleies.

Step 1 - Create a certificate

There are a few ways to do this including the MAKECERT utility, PowerShell or even SQL Server (if you use SQL Server, you will need to BACKUP CERTIFICATE including the private key). In the end, the certificate will be in both places: file system and SQL Server. Once created, you shouldn't ever need to do this again. But it might be a good idea to add the .cer file to the repository.

Step 2 - Password Protect

Use the PVK2PFX utility to combine the .cer and .pvk files into a password-protected .pfx file. Once combined, you shouldn't ever need to do this again.

Step 3 - Signing Assembly

This new .pfx file will be used to sign your assembly DLL using the SignTool utility. This can be automated rather easily by adding it as a Post-Build Event. This only needs to be run if the assembly is ever re-compiled and is useful to have this part automated.

Step 4 - Update Build Process

Update the build process such that the deployment has the following being executed before any CREATE ASSEMBLY statements (even if the assemblies are being created as SAFE):

All 3 steps are executed in [master]

If you need the installation to be compatible for SQL Server versions prior to 2012, then wrap steps 1 - 3 in an IF block based on SQL Server "major" version. The two DROP statements, 8 and 9, should be wrapped in an IF EXISTS (or similar) check so those don't need to check for the version (if those objects weren't created due to running on SQL Server 2008, for example, then they just won't do anything).

CREATE CERTIFICATE [InstallationKey] from a VARBINARY literal of the .cer file.

CREATE LOGIN [tmp] from that certificate.

GRANT that cert-based login the UNSAFE ASSEMBLY permission.

CREATE ASSEMBLY [tSQLtExternalAccessKey] ...;

CREATE ASYMMETRIC KEY [tSQLtExternalAccessKey] ...;

CREATE LOGIN from that asymmetric key.

GRANT that key-based login the UNSAFE ASSEMBLY permission.

DROP LOGIN [tmp];

DROP CERTIFICATE [InstallationKey];

DROP ASSEMBLY [tSQLtExternalAccessKey];

All of this might seem complicated at first, but this is all just a one-time setup.

Disable CLR strict security

The CLR strict security option can be disabled for backward compatibility, but this is not recommended.

Why Did Microsoft Change This?

These changes were made due to the risk of hacking from exploits known as Meltdown and Spectre which are ways of hacking CPUs. Because this is a hardware fault with the CPU itself, the only way to solve this (aside from upgrading to a newer, patched, processor) is to apply software patches.

All versions of SQL Server are affected and Microsoft recommends installing these security updates as a matter of urgency. This should have a negligible-to-minimal performance effect on existing applications, based on Microsoft testing of SQL workloads. However, Microsoft does recommend that you test all updates before you deploy them to a production environment.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.

About the Author

Tim Trott

Tim Trott is a creative photographer, traveller, astronomer and software engineer with a passion for self-growth and a desire for personal challenge.

Hi, I'm Tim Trott. I'm a creative photographer, traveller, astronomer and software engineer with a passion for self-growth and a desire for personal challenge.

This is my website, a place for me to share my experiences, knowledge and photography. I love to help people by writing articles and tutorials about my hobbies that I'm most passionate about. I hope you enjoy reading my articles as much as I enjoy writing them.