Skip to content

Commit 7fa27b6

Browse files
BinaryFormatter risks (#4508)
* BinaryFormatter risks * BinaryFormatter risks * BinaryFormatter risks * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * work * Update xml/System.Runtime.Serialization/SerializationBinder.xml Co-authored-by: Genevieve Warren <[email protected]> * work * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 7ea253d commit 7fa27b6

File tree

9 files changed

+42
-13
lines changed

9 files changed

+42
-13
lines changed

includes/binaryformatter.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> [!WARNING]
2+
> `BinaryFormatter` is insecure and can't be made secure. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).

xml/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
<remarks>
5454
<format type="text/markdown"><![CDATA[
5555
56-
## Remarks
56+
## Remarks
57+
58+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
59+
5760
The <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> and <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> classes implement the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter> interface to support remote procedure calls (RPCs), and the <xref:System.Runtime.Serialization.IFormatter> interface (inherited by the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter>) to support serialization of a graph of objects. The <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> class also supports RPCs with <xref:System.Runtime.Serialization.Formatters.ISoapMessage> objects, without using the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter> functionality.
5861
5962
During RPCs, the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter> interface allows the specification of two separate object graphs: the graph of objects to serialize, and an additional graph that contains an array of header objects that convey information about the remote function call (for example, transaction ID or a method signature).
@@ -65,7 +68,7 @@
6568
The serialization procedure for a method response is identical to that of a method call, except the first object of the object graph must support the <xref:System.Runtime.Remoting.Messaging.IMethodReturnMessage> interface. To deserialize a method response, use the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse%2A> method. To save time, details about the caller object are not sent to the remote object during the method call. These details are instead obtained from the original method call, which is passed to the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse%2A> method in the <xref:System.Runtime.Remoting.Messaging.IMethodCallMessage> parameter. The first object in the graph returned by the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.DeserializeMethodResponse%2A> method supports the <xref:System.Runtime.Remoting.Messaging.IMethodReturnMessage> interface.
6669
6770
> [!IMPORTANT]
68-
> Using binary serialization to deserialize untrusted data can lead to security risks. For more information, see [Data Validation](https://www.owasp.org/index.php/Data_Validation).
71+
> Using binary serialization to deserialize untrusted data can lead to security risks. For more information, see [Data Validation](https://www.owasp.org/index.php/Data_Validation) and the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
6972
7073
## Unpaired Surrogates
7174
Any unpaired surrogate characters are lost in binary serialization. For example, the following string contains a high surrogate Unicode character `(\ud800`) in between the two `Test` words:
@@ -289,11 +292,15 @@
289292
<ReturnType>System.Runtime.Serialization.SerializationBinder</ReturnType>
290293
</ReturnValue>
291294
<Docs>
292-
<summary>Gets or sets an object of type <see cref="T:System.Runtime.Serialization.SerializationBinder" /> that controls the binding of a serialized object to a type.</summary>
295+
<summary>(Insecure) Gets or sets an object of type <see cref="T:System.Runtime.Serialization.SerializationBinder" /> that controls the binding of a serialized object to a type.</summary>
293296
<value>The serialization binder to use with this formatter.</value>
294297
<remarks>
295298
<format type="text/markdown"><![CDATA[
296-
299+
300+
## BinaryFormatter is insecure
301+
302+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
303+
297304
## Examples
298305
[!code-cpp[SerializationBinder Example#1](~/samples/snippets/cpp/VS_Snippets_Remoting/SerializationBinder Example/CPP/serializationbinder.cpp#1)]
299306
[!code-csharp[SerializationBinder Example#1](~/samples/snippets/csharp/VS_Snippets_Remoting/SerializationBinder Example/CS/serializationbinder.cs#1)]

xml/System.Runtime.Serialization.Formatters.Soap/SoapFormatter.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
## Remarks
3131
3232
> [!NOTE]
33-
> Beginning with the .NET Framework 2.0, this class is obsolete. Use <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> instead.
33+
> Beginning with the .NET Framework 2.0, this class is obsolete.
34+
35+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
3436
3537
The <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> and <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> classes implement the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter> interface to support remote procedure calls (RPCs), and the <xref:System.Runtime.Serialization.IFormatter> interface (inherited by the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter>) to support serialization of a graph of objects. The <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> class also supports RPCs with <xref:System.Runtime.Serialization.Formatters.ISoapMessage> objects, without using the <xref:System.Runtime.Remoting.Messaging.IRemotingFormatter> functionality.
3638

xml/System.Runtime.Serialization.Formatters/TypeFilterLevel.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@
4040
<remarks>
4141
<format type="text/markdown"><![CDATA[
4242
43-
## Remarks
44-
.NET Framework remoting provides two levels of automatic deserialization, Low and Full. The Low deserialization level helps protect against deserialization attacks by deserializing only the types associated with the most basic remoting functionality. The Full deserialization level supports automatic deserialization of all types that remoting supports in all situations. For a list of the .NET Framework remoting types that Low and Full support, see [Automatic Deserialization in .NET Framework Remoting](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/5dxse167(v=vs.100)).
43+
## Remarks
44+
45+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
46+
47+
.NET Framework remoting provides two levels of automatic deserialization, Low and Full. The Full deserialization level supports automatic deserialization of all types that remoting supports in all situations. For a list of the .NET Framework remoting types that Low and Full support, see [Automatic Deserialization in .NET Framework Remoting](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/5dxse167(v=vs.100)).
4548
4649
You can set the members of this enumeration programmatically or by using an application configuration file. For examples, see [Automatic Deserialization in .NET Framework Remoting](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/5dxse167(v=vs.100)).
4750
4851
> [!CAUTION]
49-
> Do not assume that controlling deserialization is the only security your application requires. In distributed applications, even a high degree of control over serialization might not prevent malicious clients from intercepting the communication and using it in some way, even if that is merely showing data to others. Therefore, although the Low deserialization level provides some protection against certain types of attack based upon automatic deserialization, you must still evaluate whether to use authentication and encryption to help protect the confidentiality of your data.
52+
> Do not assume that controlling deserialization is the only security your application requires. In distributed applications, even a high degree of control over serialization might not prevent malicious clients from intercepting the communication and using it in some way, even if that is merely showing data to others.
5053
5154
]]></format>
5255
</remarks>

xml/System.Runtime.Serialization/NetDataContractSerializer.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
<summary>Serializes and deserializes an instance of a type into XML stream or document using the supplied .NET Framework types. This class cannot be inherited.</summary>
2424
<remarks>
2525
<format type="text/markdown"><![CDATA[
26-
26+
27+
## Security
28+
29+
<xref:System.Runtime.Serialization.NetDataContractSerializer> is insecure. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
30+
2731
## Remarks
2832
The <xref:System.Runtime.Serialization.NetDataContractSerializer> differs from the <xref:System.Runtime.Serialization.DataContractSerializer> in one important way: the <xref:System.Runtime.Serialization.NetDataContractSerializer> includes CLR type information in the serialized XML, whereas the <xref:System.Runtime.Serialization.DataContractSerializer> does not. Therefore, the <xref:System.Runtime.Serialization.NetDataContractSerializer> can be used only if both the serializing and deserializing ends share the same CLR types.
2933

xml/System.Runtime.Serialization/SerializationBinder.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@
4444
<summary>Allows users to control class loading and mandate what class to load.</summary>
4545
<remarks>
4646
<format type="text/markdown"><![CDATA[
47-
47+
48+
## Insecure serializers
49+
50+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
51+
4852
## Remarks
4953
During serialization, a formatter transmits the information required to create an instance of an object of the correct type and version. This information generally includes the full type name and assembly name of the object. The assembly name includes the name, version, and strong name (see [Strong-Named Assemblies](/dotnet/standard/assembly/strong-named)) hash of the assembly. By default, deserialization uses this information to create an instance of an identical object (with the exception of any assembly loading restricted by the security policy). Some users need to control which class to load, either because the class has moved between assemblies or a different version of the class is required on the server and client.
5054
51-
<xref:System.Runtime.Serialization.SerializationBinder> can also be used for security. There might be some security exploits when you are trying to deserialize some data from an untrusted source. The binder gives you an opportunity to inspect what types are being loaded in your application domain. You can then either maintain list of denied types or a list of allowed types and restrict which types are being loaded and instantiated. In addition you should be mindful of what information is being put out on the wire, you may want to secure (use transport or message security) when sending type names or other data on the wire.
55+
<xref:System.Runtime.Serialization.SerializationBinder> can not be used for security. There might be some security exploits when you are trying to deserialize some data from an untrusted source. The binder gives you an opportunity to inspect what types are being loaded in your application domain, but this doesn't guarantee an exploit is not possible. For more information see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide). You can then either maintain list of denied types or a list of allowed types and restrict which types are being loaded and instantiated. In addition, be mindful of what information is being put out on the wire. You may want to use transport or message security when sending type names or other data on the network.
5256
5357
> [!WARNING]
5458
> Only use <xref:System.Runtime.Serialization.SerializationBinder> if you are completely sure of what information is being serialized. Malicious types can cause unexpected behavior.

xml/System.Web.UI/LosFormatter.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
<remarks>
2121
<format type="text/markdown"><![CDATA[
2222
23-
## Remarks
23+
## Remarks
24+
25+
> [!WARNING]
26+
> `LosFormatter` is insecure and can't be made secure. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
27+
2428
The limited object serialization (LOS) formatter is designed for highly compact ASCII format serialization. This class supports serializing any object graph, but is optimized for those containing strings, arrays, and hash tables. It offers second order optimization for many of the .NET Framework primitive types.
2529
2630
This is a private format and remains consistent only for the lifetime of a Web request.

xml/System.Web.UI/ObjectStateFormatter.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<remarks>
3030
<format type="text/markdown"><![CDATA[
3131
32-
## Remarks
32+
## Remarks
33+
34+
[!INCLUDE[binaryformatter](~/includes/binaryformatter.md)]
35+
3336
The <xref:System.Web.UI.ObjectStateFormatter> class serializes and deserializes object state graphs in a compact format.
3437
3538
<xref:System.Web.UI.ObjectStateFormatter> is used by the <xref:System.Web.UI.PageStatePersister> class and classes that derive from it to serialize view state and control state. It is also used by the <xref:System.Web.UI.LosFormatter> class to provide object state graph formatting for various parts of the ASP.NET infrastructure.

0 commit comments

Comments
 (0)