Skip to content

Commit 43df639

Browse files
authored
Merge pull request #6862 from Rick-Anderson/patch-1
Binary formatter not secure
2 parents 074ff1b + 70a5671 commit 43df639

File tree

10 files changed

+49
-71
lines changed

10 files changed

+49
-71
lines changed

docs/code-quality/ca2300.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2300: Do not use insecure deserializer BinaryFormatter"
3-
ms.date: 04/05/2019
3+
ms.date: 07/15/2020
44
ms.topic: reference
55
author: dotpaul
66
ms.author: paulming
@@ -30,24 +30,17 @@ A <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayPr
3030

3131
[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
3232

33-
This rule finds <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayProperty=nameWithType> deserialization method calls or references. If you want to deserialize only when the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property is set to restrict types, disable this rule and enable rules [CA2301](ca2301.md) and [CA2302](ca2302.md) instead.
33+
This rule finds <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayProperty=nameWithType> deserialization method calls or references. If you want to deserialize only when the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property is set to restrict types, disable this rule and enable rules [CA2301](ca2301.md) and [CA2302](ca2302.md) instead. Limiting which types can be deserialized can help mitigate against known remote code execution attacks, but your deserialization will still be vulnerable to denial of service attacks.
34+
35+
[!INCLUDE[binaryformatter](includes/binaryformatter.md)]
3436

3537
## How to fix violations
3638

37-
- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include:
38-
- <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType>
39-
- <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType>
40-
- <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list.
41-
- <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType>
42-
- Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder.
43-
- Protocol Buffers
44-
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
45-
- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter>, set the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception to stop deserialization.
46-
- If you restrict deserialized types, you may want to disable this rule and enable rules [CA2301](ca2301.md) and [CA2302](ca2302.md). Rules [CA2301](ca2301.md) and [CA2302](ca2302.md) help to ensure that the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property is always set before deserializing.
39+
[!INCLUDE[fix-binaryformatter](includes/fix-binaryformatter-serializationbinder.md)]
4740

4841
## When to suppress warnings
4942

50-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
43+
[!INCLUDE[cannot-secure-binaryformatter](includes/cannot-secure-binaryformatter.md)]
5144

5245
## Pseudo-code examples
5346

docs/code-quality/ca2301.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder"
3-
ms.date: 04/05/2019
3+
ms.date: 07/15/2020
44
ms.topic: reference
55
author: dotpaul
66
ms.author: paulming
@@ -28,27 +28,24 @@ A <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayPr
2828

2929
By default, this rule analyzes the entire codebase, but this is [configurable](#configurability).
3030

31+
> [!WARNING]
32+
> Restricting types with a SerializationBinder can't prevent all attacks. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
33+
3134
## Rule description
3235

3336
[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
3437

3538
This rule finds <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayProperty=nameWithType> deserialization method calls or references, when <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> doesn't have its <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> set. If you want to disallow any deserialization with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> regardless of the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property, disable this rule and [CA2302](ca2302.md), and enable rule [CA2300](ca2300.md).
3639

40+
3741
## How to fix violations
3842

39-
- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include:
40-
- <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType>
41-
- <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType>
42-
- <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list.
43-
- <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType>
44-
- Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder.
45-
- Protocol Buffers
46-
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
47-
- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter>, set the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception to stop deserialization.
43+
44+
[!INCLUDE[fix-binaryformatter-serializationbinder](includes/fix-binaryformatter-serializationbinder.md)]
4845

4946
## When to suppress warnings
5047

51-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
48+
`BinaryFormatter` is insecure and can't be made secure.
5249

5350
## Configurability
5451

docs/code-quality/ca2302.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize"
3-
ms.date: 04/05/2019
3+
ms.date: 07/15/2020
44
ms.topic: reference
55
author: dotpaul
66
ms.author: paulming
@@ -30,28 +30,23 @@ This rule is similar to [CA2301](ca2301.md), but analysis can't determine if the
3030

3131
By default, this rule analyzes the entire codebase, but this is [configurable](#configurability).
3232

33+
> [!WARNING]
34+
> Restricting types with a SerializationBinder can't prevent all attacks. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
35+
3336
## Rule description
3437

3538
[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
3639

3740
This rule finds <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayProperty=nameWithType> deserialization method calls or references when the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> might be null. If you want to disallow any deserialization with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> regardless of the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property, disable this rule and [CA2301](ca2301.md), and enable rule [CA2300](ca2300.md).
3841

42+
3943
## How to fix violations
4044

41-
- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include:
42-
- <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType>
43-
- <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType>
44-
- <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list.
45-
- <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType>
46-
- Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder.
47-
- Protocol Buffers
48-
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
49-
- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter>, set the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception to stop deserialization.
50-
- Ensure that all code paths have the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property set.
45+
[!INCLUDE[fix-binaryformatter-serializationbinder](includes/fix-binaryformatter-serializationbinder.md)]
5146

5247
## When to suppress warnings
5348

54-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
49+
[!INCLUDE[cannot-secure-binaryformatter](includes/cannot-secure-binaryformatter.md)]
5550

5651
## Configurability
5752

docs/code-quality/ca2305.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ A <xref:System.Web.UI.LosFormatter?displayProperty=nameWithType> deserialization
3232

3333
This rule finds <xref:System.Web.UI.LosFormatter?displayProperty=nameWithType> deserialization method calls or references.
3434

35+
`LosFormatter` is insecure and can't be made secure. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
36+
3537
## How to fix violations
3638

37-
[!INCLUDE[insecure-deserializers-fixes-for-always-insecure-deserializers](includes/insecure-deserializers-fixes-for-always-insecure-deserializers-md.md)]
39+
- Use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. For more information see the [Preferred alternatives](/dotnet/standard/serialization/binaryformatter-security-guide#preferred-alternatives).
40+
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
3841

3942
## When to suppress warnings
4043

41-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
44+
`LosFormatter` is insecure and can't be made secure.
4245

4346
## Pseudo-code examples
4447

docs/code-quality/ca2310.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,17 @@ A <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=n
3030

3131
[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
3232

33-
This rule finds <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=nameWithType> deserialization method calls or references. If you want to deserialize only when the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property is set to restrict types, disable this rule and enable rules [CA2311](ca2311.md) and [CA2312](ca2312.md) instead.
33+
This rule finds <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=nameWithType> deserialization method calls or references. If you want to deserialize only when the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property is set to restrict types, disable this rule and enable rules [CA2311](ca2311.md) and [CA2312](ca2312.md) instead. Limiting which types can be deserialized can help mitigate against known remote code execution attacks, but your deserialization will still be vulnerable to denial of service attacks.
34+
35+
`NetDataContractSerializer` is insecure and can't be made secure. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
3436

3537
## How to fix violations
3638

37-
- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include:
38-
- <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType>
39-
- <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType>
40-
- <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list.
41-
- <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType>
42-
- Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder.
43-
- Protocol Buffers
44-
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
45-
- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.NetDataContractSerializer>, set the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception to stop deserialization.
46-
- If you restrict deserialized types, you may want to disable this rule and enable rules [CA2311](ca2311.md) and [CA2312](ca2312.md). Rules [CA2311](ca2311.md) and [CA2312](ca2312.md) help to ensure that the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property is always set before deserializing.
39+
[!INCLUDE[fix-binaryformatter-serializationbinder](includes/fix-binaryformatter-serializationbinder.md)]
4740

4841
## When to suppress warnings
4942

50-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
43+
`NetDataContractSerializer` is insecure and can't be made secure.
5144

5245
## Pseudo-code examples
5346

docs/code-quality/ca2311.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,23 @@ A <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=n
2828

2929
By default, this rule analyzes the entire codebase, but this is [configurable](#configurability).
3030

31+
> [!WARNING]
32+
> Restricting types with a SerializationBinder can't prevent all attacks. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide).
33+
3134
## Rule description
3235

3336
[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
3437

3538
This rule finds <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=nameWithType> deserialization method calls or references, when <xref:System.Runtime.Serialization.NetDataContractSerializer> doesn't have its <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> set. If you want to disallow any deserialization with <xref:System.Runtime.Serialization.NetDataContractSerializer> regardless of the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property, disable this rule and [CA2312](ca2312.md), and enable rule [CA2310](ca2310.md).
3639

40+
3741
## How to fix violations
3842

39-
- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include:
40-
- <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType>
41-
- <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType>
42-
- <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list.
43-
- <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType>
44-
- Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder.
45-
- Protocol Buffers
46-
- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations.
47-
- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.NetDataContractSerializer>, set the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception to stop deserialization.
43+
[!INCLUDE[fix-binaryformatter-serializationbinder](includes/fix-binaryformatter-serializationbinder.md)]
4844

4945
## When to suppress warnings
5046

51-
[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)]
47+
`NetDataContractSerializer` is insecure and can't be made secure.
5248

5349
## Configurability
5450

0 commit comments

Comments
 (0)