Stairway to SQLCLR Level 4: Security (EXTERNAL and UNSAFE Assemblies)

  • Solomon Rutzky

    SSCoach

    Points: 15964

    Comments posted to this topic are about the item Stairway to SQLCLR Level 4: Security (EXTERNAL and UNSAFE Assemblies)

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • copling

    Say Hey Kid

    Points: 696

    Mr. Solomon, I really enjoyed your article in regards to External and Unsafe security framework for SQLCLR. I've really been interested in utilizing the power that this piece of technology possess. I do however, one question: Is it possible for a SQLCLR to be built with an external dll? For example, let's say I have a dll that receives data and creates an excel file. Can I reference that dll from within my SQLCLR and pass in the necessary parameters to it's methods and to build that file? I would assume if so, the security for your SQLCLR would have to run under EXTERNAL or UNSAFE. Again, thanks for the great article.

  • Solomon Rutzky

    SSCoach

    Points: 15964

    copling (9/10/2014)


    Mr. Solomon, I really enjoyed your article in regards to External and Unsafe security framework for SQLCLR. I've really been interested in utilizing the power that this piece of technology possess. I do however, one question: Is it possible for a SQLCLR to be built with an external dll? For example, let's say I have a dll that receives data and creates an excel file. Can I reference that dll from within my SQLCLR and pass in the necessary parameters to it's methods and to build that file? I would assume if so, the security for your SQLCLR would have to run under EXTERNAL or UNSAFE. Again, thanks for the great article.

    Hi there. Thanks and I am glad that you liked the article :-).

    Regarding the external DLL, I have not tried that specifically. I believe you will need to load it into SQL Server with a PERMISSION_SET of UNSAFE (but you should try EXTERNAL_ACCESS first, just in case it works). In your project, you would add the reference and "using" statement. That DLL will be able to save the Excel file to the file system.

    If you are going to do the preferred method of the Asymmetric Key and Login in order to be able to set the 3rd party Assembly to UNSAFE (or even EXTERNAL_ACCESS), you will need to sign it with the same private key (i.e. pfx file) that are using to sign the Assembly that has the SqlProcedure.

    Take care,

    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • copling

    Say Hey Kid

    Points: 696

    Thanks a bunch. I greatly appreciate the advice. By the way, you did a great presentation at this past SQL Saturday in Raleigh. Good stuff. Take care.

  • Solomon Rutzky

    SSCoach

    Points: 15964

    copling (9/10/2014)


    Thanks a bunch. I greatly appreciate the advice. By the way, you did a great presentation at this past SQL Saturday in Raleigh. Good stuff. Take care.

    No problem. If you need further help with that particular issue, post a question to the CLR Integration and Programming Forum here on SQL Server Central.

    And thank you for that compliment regarding my presentation and thanks for attending it. Nobody filled out the eval forms (or if they did then I did not get them) so I left that presentation thinking that I had not done all that well since it was the first time I was giving it and some of the slides needed to be finished and some demos needed to be polished up a bit. So I am glad to hear that I did get through to the audience :-).

    Take care,

    Solomon..

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • David.Poole

    SSC Guru

    Points: 75108

    I had a lot of fun reading the drafts of these articles and they both opened my eyes and dispelled quite a few misconceptions.

    I wish I had known everything that was in these articles a long time ago as I would have pushed the use of SQLCLR much harder.

    The stuff about Host Protection Attributes, shared memory and the implications of External access reassured me greatly. My worries that SQLCLR is a great big server destabilising security leak has largely been dispelled. Obviously it is possible to write something stupid and highly inefficient but that is just as possible in T-SQL as it is in C#/VB. In fact, using the geographic data types and methods can throw frightening errors but I haven't seen it bring down a server yet.

  • Solomon Rutzky

    SSCoach

    Points: 15964

    David.Poole (9/12/2014)


    I had a lot of fun reading the drafts of these articles and they both opened my eyes and dispelled quite a few misconceptions.

    I wish I had known everything that was in these articles a long time ago as I would have pushed the use of SQLCLR much harder.

    The stuff about Host Protection Attributes, shared memory and the implications of External access reassured me greatly. My worries that SQLCLR is a great big server destabilising security leak has largely been dispelled. Obviously it is possible to write something stupid and highly inefficient but that is just as possible in T-SQL as it is in C#/VB. In fact, using the geographic data types and methods can throw frightening errors but I haven't seen it bring down a server yet.

    Hi David. Thank you for the compliment and for sharing what you got out of these two "Security" articles. I feel it is important to make sure that people have accurate information and understanding to base their decisions and designs on. If people don't want to use SQLCLR then that is certainly fine, but it should be due to valid reasons instead of commonly propagated misinformation. But in terms of extending the capabilities of SQL Server, SQLCLR is far better than Extended Stored Procedures and the sp_OA* OLE Automation procedures. SQLCLR is certainly more secure than those other choices, including SQLCMD. The mere ability to do Impersonation allows for a more consistent approach to Windows security for the sys admins; they don't need to create exceptions for the Log On account of the SQL Server service.

    But just to be perfectly clear about the wording here: outside of the performance concern (which as you pointed out, is just as easy to do poorly in pure T-SQL), it certainly is possible to do stuff in EXTERNAL_ACCESS and UNSAFE that can destabilize the system or allow for security-related issues. The point is that the DBA has a good degree of control:

    • enabling "CLR Integration" does not automatically open up the system to all types of functionality: a SAFE assembly cannot reach outside of SQL Server. This allows for doing string functions (e.g. Regular Expressions, etc), math functions, and other types of operations without also allowing network and file system access. The OLE Automation procedures (i.e. sp_OA*), on the other hand, do not make such a distinction.
    • the DBA can limit (to a degree) what can be done if reaching outside of SQL Server: EXTERNAL_ACCESS allows file system access, network access, and reading from the Registry without allowing untrusted / unmanaged / non-verified code or writing to the Registry or executing any random DOS / Windows program.

    And thank you again for reviewing the articles. Your time and help is greatly appreciated, and the articles have definitely benefited from your suggestions :-).

    Take care,

    Solomon...

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • SQLIndia

    Ten Centuries

    Points: 1073

    Hi

    I know it's been long time for this post... but I have across with an issue during "CLR Assembly with Asymmetric Key". I have searched  and could not found any solution around it apart from few suggestions on possible resolutions which relate to permissions.

    Not sure what is missing. Need help,

    Creating asymmetric key...

    create asymmetric key BNZi_SqlClr_Key 
    from executable file = '\\RemoteServer\CLRServerAssemblies\Filename.dll'
    go

    Creating assembly...

    Create assembly [MicrosoftApplicationBlocksData]
    authorization [dbo]
    from '\\RemoteServer\CLRServerAssemblies\Microsoft.ApplicationBlocks.Data.dll'
    with permission_set = unsafe
    go

    On  creation of Assembly following ERROR appeared...

    Msg 6501, Level 16, State 7, Line 5CREATE ASSEMBLY failed because it could not open the physical file'\\RemoteServer\CLRServerAssemblies\Microsoft.ApplicationBlocks.Data.dll': 5(Access is denied.).

     

    Details:-

    • Clr enabled
    • Disable 'CLR Strict Security' option
    • Trustworthy ON for database
    • Login create from asymmetric key
    • DB user created for login
    • Grant unsafe assembly to Login
    • Remote folder 'CLRServerAssemblies' have full control  permissions to Everyone.
    • Windows 2012 R2 + SQL 2017

    Thanks in Advance

    SqlIndia

  • Solomon Rutzky

    SSCoach

    Points: 15964

    Hello SQLIndia.

     

    First, why have you disabled "CLR strict security" and enabled "TRUSTWORTHY" if you have signed assemblies? Using signed assemblies means that you don't need to do either of those things. And in fact, there is really no reason to ever enable TRUSTWORTHY (please see:  PLEASE, Please, please Stop Using Impersonation, TRUSTWORTHY, and Cross-DB Ownership Chaining).

     

    Second, you are getting an "Access denied" error, which is a filesystem permissions issue, which is not specifically a SQLCLR security issue. Looking at the documentation for CREATE ASSEMBLY , it states (emphasis added):

    The Windows login of the user that executes CREATE ASSEMBLY must have read permission on the share and the files being loaded in the statement.

    The documentation for CREATE ASYMMETRIC KEY doesn't say anything useful about permissions, but the documentation for CREATE CERTIFICATE (which I assume is similar in approach to file system access) states:

    The file is accessed in the security context of the SQL Server service account.

     

    So, it's possible that either:

    1. the SQL Server service account has access to that folder and file, which is why CREATE ASYMMETRIC KEY works, yet your login does not have access to that folder or file, which is why CREATE ASSEMBLY fails,

      OR

    2. this is a kerberos double-hop issue since the file is on a share (i.e. remote system), in which case CREATE ASYMMETRIC KEY works because the SQL Server service account logs in directly to the server running SQL Server, but CREATE ASSEMBLY fails because you log on indirectly to that server (through a SQL client tool) and thus your credentials are not trusted by that remote server.

     

    For #1, check permissions on the file. For testing, make sure that the folder and file assign read and execute to "Everyone". If this works, it's a simple permissions issue.

    For #2, copy the file to the server running SQL Server (so that it is a local file) and see if that works. If it does, then you need to enable delegation, which takes some setup to do, so you might consider using BinaryFormatter (an open source utility I wrote) to turn the DLL into its string representation so that you can include it inline in the CREATE ASSEMBLY statement and skip the file system entirely (which is the better way to go in the first place). AND, if this is a Microsoft supplied DLL, then it is likely that they signed it with a certificate, in which case you can import the certificate instead of the asymmetric key. I am almost done with a blog post in which I will share a script that provides the CREATE CERTIFICATE statement with the binary literal representation of the certificate used to sign most (if not all) .NET Framework 4.x libraries, which might work for you as well. That should be published next week, hopefully.

     

    Take care, Solomon..

     

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply