TMAegis
More rules for mssql

TMA-MSSQL-6.2

Ensure 'CLR Assembly Permission Set' is set to 'SAFE_ACCESS' for All CLR Assemblies

MediumLevel 1Automatedmssql

Description

Setting CLR Assembly Permission Sets to SAFE_ACCESS will hinder assemblies from accessing external system resources such as files, the network, environment variables, or the registry. Counts user-defined assemblies whose permission set is not SAFE_ACCESS across all online databases. Default: SAFE permission is set by default. Runs one sqlcmd query with its defaults (Windows authentication to the local default instance); set SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD for another instance or SQL authentication. Needs VIEW SERVER STATE / sysadmin-level read access.

How it is checked

Audit method: SQL query

sql_query
sqlcmd -b -h -1 -W -Q "SET NOCOUNT ON; DECLARE @n int = 0, @k int, @db sysname, @s nvarchar(max); DECLARE c CURSOR LOCAL FAST_FORWARD FOR SELECT name FROM sys.databases WHERE state = 0 AND HAS_DBACCESS(name) = 1; OPEN c; FETCH NEXT FROM c INTO @db; WHILE @@FETCH_STATUS = 0 BEGIN SET @s = N'USE ' + QUOTENAME(@db) + N'; SELECT @k = COUNT(*) FROM (SELECT name FROM sys.assemblies WHERE is_user_defined = 1 AND permission_set_desc <> ''SAFE_ACCESS'') AS x;'; EXEC sp_executesql @s, N'@k int OUTPUT', @k = @k OUTPUT; SET @n = @n + @k; FETCH NEXT FROM c INTO @db; END; CLOSE c; DEALLOCATE c; SELECT @n;"
Expected output
0

How it is fixed

Fix method: Guidance or infrastructure patch

USE <database_name>; GO ALTER ASSEMBLY <assembly_name> WITH PERMISSION_SET = SAFE;

Properties

Operating systems
Any
Tags
mssqldatabaseclr
Reference
CIS Microsoft SQL Server 2025 Benchmark v1.0.0 - 6.2 Ensure 'CLR Assembly Permission Set' is set to 'SAFE_ACCESS' for All CLR Assemblies

Check this host

Run this on a machine you are allowed to audit. The scan is read-only.

shell
tmaegis scan --target mssql