Skip to content

deserialization sinks#5508

Merged
hvitved merged 34 commits intomainfrom
unknown repository
Aug 17, 2021
Merged

deserialization sinks#5508
hvitved merged 34 commits intomainfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Mar 23, 2021

Added twenty new sinks and verification if user controls instantiated type in strong type serializer case.

@ghost ghost self-requested a review as a code owner March 23, 2021 21:25
@tamasvajk tamasvajk self-assigned this Mar 25, 2021
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution. This PR is rather large and therefore a bit difficult to review. I didn't yet check all the deserializer callables, weak/strong type deserializers, but I wanted to give you an initial review.

Comment thread csharp/ql/src/semmle/code/csharp/serialization/Deserializers.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserializationUntrustedInput.ql Outdated
@ghost ghost mentioned this pull request Apr 9, 2021
1 task
@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 9, 2021

Ping

@tamasvajk
Copy link
Copy Markdown
Contributor

Ping

Sorry for the delay, I'm a bit behind on code reviews. I'll catch up until mid next week. If not, feel free to ping me again.

Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Copy link
Copy Markdown
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments. Thanks for you contributions.

Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserializationUntrustedInput.ql Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserializationUntrustedInput.ql Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
@ghost ghost requested review from hvitved and tamasvajk April 15, 2021 19:33
Comment thread csharp/ql/src/semmle/code/csharp/serialization/Deserializers.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/serialization/Deserializers.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/serialization/Deserializers.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/serialization/Deserializers.qll Outdated
Comment thread csharp/ql/src/Security Features/CWE-502/UnsafeDeserialization.ql Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
@ghost ghost requested a review from hvitved April 21, 2021 14:26
@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Apr 22, 2021

The QLDoc check fails with

Error: The following CodeQL elements are lacking documentation:
semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll  UnsafeDeserialization::UnsafeDeserialization::LocalSource                 class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ActivityLoadMethod                                         class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::BinaryFormatterDeserializeMethod                           class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::BinaryFormatterUnsafeDeserializeMethod                     class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::BinaryFormatterUnsafeDeserializeMethodResponseMethod       class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::BinaryMessageFormatterReadMethod                           class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::DataContractJsonSerializerReadObjectMethod                 class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::DataContractSerializerReadObjectMethod                     class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::FastJsonClassToObjectMethod                                class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::JavaScriptSerializerClassDeserializeMethod                 class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::JavaScriptSerializerClassDeserializeObjectMethod           class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::JaysonConverterToObjectMethod                              class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::LosFormatterDeserializeMethod                              class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::NetDataContractSerializerDeserializeMethod                 class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::NetDataContractSerializerReadObjectMethod                  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ObjectStateFormatterDeserializeMethod                      class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ProxyObjectDecodeSerializedObjectMethod                    class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ProxyObjectDecodeValueMethod                               class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ResourceReaderConstructor                                  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextCsvSerializerDeserializeFromReaderMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextCsvSerializerDeserializeFromStreamMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextCsvSerializerDeserializeFromStringMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextJsonSerializerDeserializeFromReaderMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextJsonSerializerDeserializeFromStreamMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextJsonSerializerDeserializeFromStringMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextTypeSerializerDeserializeFromReaderMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextTypeSerializerDeserializeFromStreamMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextTypeSerializerDeserializeFromStringMethod  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextXmlSerializerDeserializeFromReaderMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextXmlSerializerDeserializeFromStreamMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::ServiceStackTextXmlSerializerDeserializeFromStringMethod   class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::SoapFormatterDeserializeMethod                             class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XamlReaderLoadAsyncMethod                                  class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XamlReaderLoadMethod                                       class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XamlReaderParseMethod                                      class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XmlMessageFormatterReadMethod                              class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XmlObjectSerializerReadObjectMethod                        class
semmle/code/csharp/serialization/Deserializers.qll              Deserializers::XmlSerializerDeserializeMethod                             class

So either add doc, or make the classes private.

@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 22, 2021

So either add doc, or make the classes private.

If I make for example BinaryFormatterDeserializeMethod private it breaks with ERROR: Could not resolve type BinaryFormatterDeserializeMethod (/csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll:152... which is because of

  /** BinaryFormatter */
  private predicate isBinaryFormatterCall(MethodCall mc, Method m) {
    m = mc.getTarget() and
    (
      not mc.getArgument(0).hasValue() and
      (
        m instanceof BinaryFormatterDeserializeMethod
        or
        m instanceof BinaryFormatterUnsafeDeserializeMethod
        or
        m instanceof BinaryFormatterUnsafeDeserializeMethodResponseMethod
      )
    )
  }

Should I merge UnsafeDeserialization.qll and Deserializers.qll into one?

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Apr 22, 2021

Should I merge UnsafeDeserialization.qll and Deserializers.qll into one?

Then I think it is better to add documentation for the classes.

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented May 5, 2021

Here is a run of the two queries comparing results before and after:

@ghost
Copy link
Copy Markdown
Author

ghost commented May 10, 2021

Here is a run of the two queries comparing results before and after:

* `UnsafeDeserializationUntrustedInput.ql`: https://lgtm.com/query/5618697988968865317/

* `UnsafeDeserialization.ql`: https://lgtm.com/query/4515885059546123954/

Probably the query is more valuable for corporate clients than for open source.

this instanceof ServiceStackTextCsvSerializerClass
or
this instanceof ServiceStackTextXmlSerializerClass
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to complete these lists with:

  • YamlDotNet
  • FsPickler
  • Json.Net
  • SharpSerializerBinary
  • SharpSerializerXml

And maybe LitJSON according to this tweet: https://twitter.com/frycos/status/1391497640425279489

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added support for YamlDotNet (pre 5.x version), FsPickler, Json.Net and SharpSerializer.
I didn't add LitJson because in case of ToObject<T> (string json) the T cannot be user controlled. This leaves only ToObject(string json, Type ConvertType ), but my search didn't return any usages.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review.

@ghost ghost requested a review from hvitved July 12, 2021 12:41
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
Comment thread csharp/ql/src/semmle/code/csharp/security/dataflow/UnsafeDeserialization.qll Outdated
@ghost ghost requested a review from hvitved July 14, 2021 13:08
@ghost
Copy link
Copy Markdown
Author

ghost commented Jul 26, 2021

Ping

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Aug 2, 2021

Please resolve merge conflicts.

@hvitved hvitved added the no-change-note-required This PR does not need a change note label Aug 5, 2021
@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Aug 9, 2021

I will perform an internal test for performance regressions; I'll let you know once it's done.

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Aug 17, 2021

As expected, UnsafeDeserializationUntrustedInput.ql is now slower than before (e.g., it now takes ~600s instead of ~100s on mono/mono). However, I was unable to fix this, and appears to be simply because we are now computing more data flow.

@hvitved hvitved dismissed tamasvajk’s stale review August 17, 2021 09:41

Comments have been addressed.

@hvitved hvitved merged commit 44ff623 into github:main Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# documentation no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants