安全地使用SuppressUnmanagedCodeSecurity

时间:2011-07-09 14:22:06

标签: c# .net pinvoke unmanaged managed

我目前正在为非托管dll创建托管包装器。 Point是包装器对非托管dll进行TON调用,但本身只输出很少的方法。 从我做的研究中,这应该是安全的,但我想确保我得到正确的。基本上这就是我的做法。

[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 disposeManagedResources)
    {
        SomeAPI.SomeFunc();
    }
}

我添加的每个受保护或公共的方法都应该获得[SecurityPermission(SecurityAction.Demand,UnmanagedCode = true)]属性。我的意思是每一个都避免意外的代码路径导致SomeAPI调用。

现在添加到内部或私有的Wrapper的任何方法都是“安全的”。这个假设是否正确?

很抱歉,如果我不清楚的话。我正在编写包装器,因此它不会重新格式化硬盘驱动器或类似的东西。包装器将在其自己的托管dll中显示(以及其他内容)。因为对包装器的一次调用可能导致对非托管dll的100次调用,所以我不希望CLR的性能开销检查所有这些调用 - 因此使用SuppressUnmanagedCodeSecurity。文档提到“非常谨慎地使用此属性。不正确的使用可能会产生安全漏洞。”,这就是我要问的问题,使用上述方法再次“安全”。

0 个答案:

没有答案