Microsoft Executes Code Against Managed Instances That Doesn’t Follow Basic Security Practices

I’m the kind of guy who logs all of the errors that occur within SQL Server, so it was easy to notice when some unusual code that we didn’t write was throwing errors. The code was executed under a client app name of “DMVCollector” which sounds kind of sinister if you ask me.

The Code

IF OBJECT_ID('tempdb.dbo.#memory_optimized_tables_internal_attributes ') IS NOT NULL
    DROP TABLE #memory_optimized_tables_internal_attributes

-- Create the table with the proper schema
SELECT TOP 0
    newid() as logical_database_guid,
    db_name() as logical_database_name,
    db_name() as db_name,
    object_id,
    xtp_object_id,
    type,
    type_desc
INTO
    #memory_optimized_tables_internal_attributes
FROM
    sys.memory_optimized_tables_internal_attributes

-- This is a DB-scoped DMV. Iterate over all user databases (id > 4) and collect the data.
DECLARE @name sysname;
DECLARE @logical_database_guid UNIQUEIDENTIFIER;
DECLARE @logical_database_name sysname;
DECLARE @database_id int;
DECLARE @SQLString nvarchar(max);
DECLARE DbCursor CURSOR LOCAL FORWARD_ONLY
FOR
SELECT
    PS.logical_database_id [logical_database_guid], D.name [name], D.database_id [database_id], DB_NAME(D.database_id) [logical_database_name]
FROM
    sys.databases D join sys.dm_hadr_fabric_partition_states PS ON PS.database_name = D.name

OPEN DbCursor
FETCH NEXT FROM DbCursor INTO @logical_database_guid, @name, @database_id, @logical_database_name

WHILE @@fetch_status = 0
BEGIN
    SET @SQLString = N'INSERT INTO #memory_optimized_tables_internal_attributes
        SELECT '''
        + convert(nvarchar(100), @logical_database_guid) + ''', '''
        + QUOTENAME(@logical_database_name, '''') + ', '
        + QUOTENAME(@name, '''') + ',
        object_id,
        xtp_object_id,
        type,
        type_desc
    FROM '
        + QUOTENAME(@name) + '.sys.memory_optimized_tables_internal_attributes'

    BEGIN TRY
        EXEC (@SQLString)
    END TRY
    BEGIN CATCH
        -- Swallow the exception.
    END CATCH

    -- Move to the next database.
    FETCH NEXT FROM DbCursor INTO @logical_database_guid, @name, @database_id, @logical_database_name
END

CLOSE DbCursor
DEALLOCATE DbCursor

A Quick Code Review

  1. The code comment says “Iterate over all user databases (id > 4) and collect the data.” but that wasn’t implemented.
  2. I’m not familiar with sys.dm_hadr_fabric_partition_states, but the join clause PS.database_name = D.name typically will compare a name to a GUID. Perhaps PS.database_name = D.physical_database_name is what was intended.
  3. It feels odd to return both sys.databases.name and DB_NAME(database_id), but to be fair, maybe there are instances where those can be different.
  4. The dynamic SQL query throws an error due to this part: ''' + QUOTENAME(@logical_database_name, '''') + ', '. A single quote is added to the beginning and quotes are added around the name, so the result is ,''msdb',
  5. The only reason the above error occurs is that the code clumsily tries to build its own protection against sql injection and special characters with EXEC() instead of sp_executesql with input parameters. Can we please move past 1997 coding practices?
  6. Instead of swallowing the error, maybe get some feedback that it occurred?

Final Thoughts

I understand that going into a cloud platform means giving up full control and that cloud vendors will always be snooping, but I would appreciate it if their snooping code followed basic security standards and actually did what was intended instead of throwing errors. Can Microsoft please do better?