开发者

Safely use SuppressUnmanagedCodeSecurity

开发者 https://www.devze.com 2023-03-19 06:05 出处:网络
I\'m currently creating a managed wrapper to an unmanaged dll. Point is the wrapper does a TON of calls to the unmanaged dll but exports very few methods itself.

I'm currently creating a managed wrapper to an unmanaged dll. Point is the wrapper does a TON of calls to the unmanaged dll but exports very few methods itself. From the research I did this should be safe but I want to make sure I get this correctly. Essentially this is how I'm doing it.

[SuppressUnmanagedCodeSecurity()]
internal static class SomeAPI
{
    [DllImport("base.dll"]
    internal static extern bool Somefunc();
    [...] Other internal DllImports
}

public class Wrapper : IDisposable
{
    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    public Wrapper()
    {
        SomeAPI.SomeFunc();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    protected override void Dispose(bool disposeMa开发者_如何学PythonnagedResources)
    {
        SomeAPI.SomeFunc();
    }
}

Every method I add that is protected or public should get the [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] attribute. And I mean every to avoid an accidental code path that leads to a SomeAPI call.

Now any method added to Wrapper that is internal or private is "safe". Is that assumption correct?

Sorry if I was unclear. I am writing the wrapper so it won't reformat the hard drive or something like that. The wrapper will be exposed in its own managed dll (along with other things). Because one call to the wrapper could result in 100's of calls to the unmanaged dll I don't want the performance overhead of the CLR checking all those calls - thus the use of SuppressUnmanagedCodeSecurity. The docs mention "Use this attribute with extreme care. Incorrect use can create security weaknesses.", and thats what I'm asking, am I "safe" again with the above methodology.

0

精彩评论

暂无评论...
验证码 换一张
取 消