为什么不安全的代码上下文 [与指针操作相关] 是 C# 中的安全风险?
Why unsafe code context [related to pointer operation] is a security risk in C#?
我需要调用 C 库函数,该函数将指针作为来自一个 C# 程序的参数。在 C# 中,我们需要使用 #unsafe 关键字来保护指针操作。 Microsoft C# documents 说使用指针会导致稳定性和安全问题。
In the common language runtime (CLR), unsafe code is referred to as
unverifiable code. Unsafe code in C# is not necessarily dangerous; it
is just code whose safety cannot be verified by the CLR. The CLR will
therefore only execute unsafe code if it is in a fully trusted
assembly. If you use unsafe code, it is your responsibility to ensure
that your code does not introduce security risks or pointer errors.
文档说
Using unsafe code introduces security and stability risks.
我是 C# 语言的新手。之前和在UNIX环境下只用过C语言。我知道不正确的指针有稳定性问题。它可能会使 program/OS 崩溃或导致意外结果。
这些是我的问题:
1)我不明白指针操作如何导致安全问题。我唯一能想到的就是调用一个包含恶意代码的函数指针,但是this Stack overflow link说调用函数指针不需要不安全的上下文(如果我的错误请纠正我理解是错误的)。那么禁用指针操作将如何提高安全性?
2)我们可以从C#程序调用C函数,这些C函数可能包含恶意代码。 CLR 能否检测到此安全漏洞?
滥用指针的安全风险的一个例子是use after free. One example of this issue is a recently discovered security issue within Chrome。
我不太熟悉 C#,但我猜 CLR 不 "understand" C 代码而是直接执行它。因此,我认为 CLR 不能 analyze/detect 处理您要使用的 C 代码。
我需要调用 C 库函数,该函数将指针作为来自一个 C# 程序的参数。在 C# 中,我们需要使用 #unsafe 关键字来保护指针操作。 Microsoft C# documents 说使用指针会导致稳定性和安全问题。
In the common language runtime (CLR), unsafe code is referred to as unverifiable code. Unsafe code in C# is not necessarily dangerous; it is just code whose safety cannot be verified by the CLR. The CLR will therefore only execute unsafe code if it is in a fully trusted assembly. If you use unsafe code, it is your responsibility to ensure that your code does not introduce security risks or pointer errors.
文档说
Using unsafe code introduces security and stability risks.
我是 C# 语言的新手。之前和在UNIX环境下只用过C语言。我知道不正确的指针有稳定性问题。它可能会使 program/OS 崩溃或导致意外结果。
这些是我的问题:
1)我不明白指针操作如何导致安全问题。我唯一能想到的就是调用一个包含恶意代码的函数指针,但是this Stack overflow link说调用函数指针不需要不安全的上下文(如果我的错误请纠正我理解是错误的)。那么禁用指针操作将如何提高安全性?
2)我们可以从C#程序调用C函数,这些C函数可能包含恶意代码。 CLR 能否检测到此安全漏洞?
滥用指针的安全风险的一个例子是use after free. One example of this issue is a recently discovered security issue within Chrome。
我不太熟悉 C#,但我猜 CLR 不 "understand" C 代码而是直接执行它。因此,我认为 CLR 不能 analyze/detect 处理您要使用的 C 代码。