Skip to content

Swift: Queries for CWE-311 (originally CWE-200) #10061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 1, 2022

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Aug 16, 2022

The idea is to have two queries, one for "Cleartext storage of sensitive information in a local database" and one for "Cleartext transmission of sensitive information". Both will use the same heuristic framework for determining what is sensitive information, but each will have different taint sinks. Right now there are no queries, just test cases covering the following:

  • storage using the Core Data database library
  • storage using the Realm database library
  • transmission as part of a URL
  • transmission using NWConnection.send

At this stage I would appreciate a review of these tests, with a particular focus on whether I've modelled Core Data and Realm in a reasonably realistic way (albeit with a lot of setup + result handling code missing as its long winded and should be irrelevant to the queries). I'm also open to other suggestions, including other sinks that might be worth considering.

@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Swift labels Aug 16, 2022
@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 25, 2022

I've just pushed a load of progress.

  • SensitiveExprs.qll library, which detects likely sensitive / private data heuristically (based on the proven CPP implementation; though we'll probably want to refine things a bit more for the Swift eco-system when we have more data).
  • written the queries.
  • .qhelp for the queries.

There are some refinements I still want to make (to deal with some potential false positives; and spot certain types of sensitive data more reliably), but I'd rather do these things as follow-up. This PR is already quite big and I believe complete enough to be reviewed and merged now.

@geoffw0 geoffw0 added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Aug 25, 2022
@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 25, 2022

The merge with main has caused some non-trivial problems in the taint flow. I'll have to fix them next week.

I've requested a docs review for the two .qhelp files.

@mchammer01
Copy link
Contributor

@geoffw0 👋🏻 - just wondering if this is ready for editorial review. I am confused by the presence of the ready-for-doc-review label in this draft PR. Thanks for clarifying 🙏🏻 🙂

@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 30, 2022

just wondering if this is ready for editorial review.

The docs are finished and ready for review. Something needs fixing with the query itself running on latest main, which I need to look into, but the fix won't affect docs.

@mchammer01
Copy link
Contributor

Great, thanks for clarifying @geoffw0

mchammer01
mchammer01 previously approved these changes Aug 30, 2022
Copy link
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

@geoffw0 - this LGTM ✨
I've made minor comments on punctuation. You can ignore any you don't agree with, but it would be great to add the full stops to the list items in the references.
Also, perhaps I misunderstood but I thought these two queries related to CWE 311 🤔
The previews failed so I wasn't able to preview the qhelp files. Also, do we need to add something to the changelog?

* @precision medium
* @id swift/cleartext-storage-database
* @tags security
* external/cwe/cwe-312
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be CWE 311?

Suggested change
* external/cwe/cwe-312
* external/cwe/cwe-311

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, CWE-312 is a child of CWE-311. The relevant part of the hierarchy looks something like this:

CWE-693: Protection Mechanism Failure
 |-- ...
 \-- CWE-311: Missing Encryption of Sensitive Data
      |-- CWE-312: Cleartext Storage of Sensitive Information
      \-- CWE-319: Cleartext Transmission of Sensitive Information

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying 🙇🏻‍♀️

@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 30, 2022

Thanks for the review @mchammer01 !

@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 30, 2022

The previews failed so I wasn't able to preview the qhelp files.

Where is this?

Also, do we need to add something to the changelog?

Swift isn't released to the public yet, so we're not writing change notes until then.

@mchammer01
Copy link
Contributor

Ah, the previews ran correctly now (see QHelp Preview Checks check above, which has a green tick). When I reviewed the PR, it had failed.

@geoffw0 geoffw0 marked this pull request as ready for review August 30, 2022 17:13
@geoffw0 geoffw0 requested a review from a team as a code owner August 30, 2022 17:13
@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 30, 2022

The merge issues are fixed, the query is now working again. And ready to review.

Some of the results are less than perfect, that will be improved by one or more follow-up PRs at some stage. Right now I just want to get the essentials in.

MathiasVP
MathiasVP previously approved these changes Aug 30, 2022
Copy link
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

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

A couple of questions. LGTM otherwise!

@geoffw0
Copy link
Contributor Author

geoffw0 commented Sep 1, 2022

All comments addressed (thanks for the review and help).

Copy link
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +87 to +98
override predicate allowImplicitRead(DataFlow::Node node, DataFlow::ContentSet c) {
// flow out from fields of a `RealmSwiftObject` at the sink, for example in `obj.var = tainted; sink(obj)`.
isSink(node) and
exists(ClassDecl cd |
c.getAReadContent().(DataFlow::Content::FieldContent).getField() = cd.getAMember() and
cd.getType().(NominalType).getABaseType*().getName() = "RealmSwiftObject"
)
or
// any default implicit reads
super.allowImplicitRead(node, c)
}
}
Copy link
Contributor

@MathiasVP MathiasVP Sep 1, 2022

Choose a reason for hiding this comment

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

Fantastic! I'm very happy to see that we could get rid of all the additional taint steps 😍. I just opened #10254 which will hopefully make it possible to get a base type declaration without going the types themselves. But we can always do that change as a follow-up to avoid an unnecessary merge from main.

@MathiasVP MathiasVP merged commit 96752f0 into github:main Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation no-change-note-required This PR does not need a change note ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants