Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 安全使用SuppressUnmanagedCodeSecurity_C#_.net_Pinvoke_Unmanaged_Managed - Fatal编程技术网

C# 安全使用SuppressUnmanagedCodeSecurity

C# 安全使用SuppressUnmanagedCodeSecurity,c#,.net,pinvoke,unmanaged,managed,C#,.net,Pinvoke,Unmanaged,Managed,我当前正在为非托管dll创建托管包装。关键是包装器对非托管dll进行了大量调用,但本身只导出很少的方法。 根据我做的研究,这应该是安全的,但我想确保我得到了正确的答案。基本上,我就是这样做的 [SuppressUnmanagedCodeSecurity()] internal static class SomeAPI { [DllImport("base.dll"] internal static extern bool Somefunc(); [...] Other i

我当前正在为非托管dll创建托管包装。关键是包装器对非托管dll进行了大量调用,但本身只导出很少的方法。 根据我做的研究,这应该是安全的,但我想确保我得到了正确的答案。基本上,我就是这样做的

[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调用

现在,任何添加到包装器中的内部或私有方法都是“安全的”。这个假设正确吗


对不起,我不清楚。我正在编写包装器,这样它就不会重新格式化硬盘或类似的东西。包装器将在其自己的托管dll中公开(以及其他内容)。因为对包装器的一次调用可能会导致对非托管dll的100次调用,所以我不希望CLR检查所有这些调用的性能开销—因此使用SuppressUnmanagedCodeSecurity。文档中提到“非常小心地使用此属性。不正确的使用可能会造成安全漏洞”。这就是我要问的问题,使用上述方法,我是否再次“安全”了。

只有在检查该函数是否损坏堆或重新格式化硬盘驱动器后,它才是“安全”的。不安全的是,此属性阻止CLR检查调用堆栈上是否存在不具有非托管代码执行权限的托管方法,并间接地调用包装器。您通常只在尝试创建沙箱时才担心这一点。