FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

16 cryptography models libraries and queries migration by bdrodes · Pull Request #17 · microsoft/codeql · GitHub

forked from github/codeql

16 cryptography models libraries and queries migration - #17

Closed
Ben Rodes (bdrodes) wants to merge 9 commits into
mainfrom
16-cryptography-models-libraries-and-queries-migration
Closed

16 cryptography models libraries and queries migration#17
Ben Rodes (bdrodes) wants to merge 9 commits into
mainfrom
16-cryptography-models-libraries-and-queries-migration

Conversation

Copy link
Copy Markdown

Migration of C/C++ crypto models, inventory queries and example alerts.

Ben Rodes (bdrodes) linked an issue Sep 12, 2023 that may be closed by this pull request

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

A couple of comments on the design (and a couple of ad-hoc way-too-detailed comments when I spotted something).

I'm not sure what kind of review process you have on this repo, so please don't let any of this block your PR. I just wanted to highlight a subset of the things I'd like to see fixed before we get this into the official CodeQL repository 😄.

Comment on lines +11 to +22
string unknownAlgorithm() { result = "UNKNOWN" }

string getHashType() { result = "HASH" }
string getSymmetricEncryptionType() { result = "SYMMETRIC_ENCRYPTION" }
string getAsymmetricEncryptionType() { result = "ASYMMETRIC_ENCRYPTION" }
string getKeyDerivationType() { result = "KEY_DERIVATION" }
string getCipherBlockModeType() { result = "BLOCK_MODE" }
string getSymmetricPaddingType() { result = "SYMMETRIC_PADDING" }
string getAsymmetricPaddingType() { result = "ASYMMETRIC_PADDING" }
string getEllipticCurveType() { result = "ELLIPTIC_CURVE" }
string getSignatureType() { result = "SIGNATURE" }
string getKeyExchangeType() { result = "KEY_EXCHANGE" }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is there a reason this has to be modelled using strings?

Here's a suggestion for how I'd probably structure this to not rely on hardcoded strings:

newtype TCryptoAlgorithmName =
  TUnknownAlgorithm() or
  THash() or
  TSymmetricEncryption() or
  TAsymmetricEncryption() or
  TKeyDerivation() or
  TCipherBlockMode() or
  TSymmetricPadding() or
  TAsymmetricPadding() or
  TEllipticCurve() or
  TSignature() or
  TKeyExchange()

class CryptoAlgorithmName extends TCryptoAlgorithmName {
  string toString() { none() } // overridden in subclasses

  predicate isUnknown() { none() }

  predicate isAsymmetric() { none() }
}

class UnknownAlgorithm extends CryptoAlgorithmName, TUnknownAlgorithm {
  override predicate isUnknown() { any() }
}

class Hash extends CryptoAlgorithmName, THash { }

class SymmetricEncryption extends CryptoAlgorithmName, TSymmetricEncryption { }

class AsymmetricEncryption extends CryptoAlgorithmName, TAsymmetricEncryption {
  override predicate isAsymmetric() { any() }
}

class KeyDerivation extends CryptoAlgorithmName, TKeyDerivation { }

class CipherBlockMode extends CryptoAlgorithmName, TCipherBlockMode { }

class SymmetricPadding extends CryptoAlgorithmName, TSymmetricPadding { }

class AsymmetricPadding extends CryptoAlgorithmName, TAsymmetricPadding { }

class EllipticCurve extends CryptoAlgorithmName, TEllipticCurve {
  override predicate isAsymmetric() { any() }
}

class Signature extends CryptoAlgorithmName, TSignature {
  override predicate isAsymmetric() { any() }
}

class KeyExchange extends CryptoAlgorithmName, TKeyExchange {
  override predicate isAsymmetric() { any() }
}

With potentially more predicates on the CryptoAlgorithmName class. This avoids continiously having to enumerate a bunch of cases in each of the predicates further down in this file.

string getKeyExchangeType() { result = "KEY_EXCHANGE" }

string getAsymmetricType(){
result in [getAsymmetricEncryptionType(), getSignatureType(), getKeyExchangeType(), getEllipticCurveType()]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

For instance, this would just be:

CryptoAlgorithmName getAsymmetricType() { result.isAsymmetric() }

* A cryptographic artifact is a DataFlow::Node associated with some
* operation, algorithm, or any other aspect of cryptography.
*/
abstract class CryptographicArtifact extends Expr {}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The comment specifies that an artifact is a DataFlow::Node, but this class is extending Expr?

I haven't looked in detail of all the implementations of this abstract class, but since this abstract class is so general it may be bad to couple if so tightly with either Expr or DataFlow::Nodes? If so, maybe it makes sense to apply the same design as I proposed in CryptoAlgorithmNames.qll? That is, an IPA type that defines the "universe" of possible values, and then define CryptographicArtifact as an abstract class that extends this type?

Comment on lines +54 to +58
exists(Expr cur, Expr next |
(cur = node1.asExpr() or cur = node1.asIndirectArgument()) and
(next = node2.asExpr() or next = node2.asIndirectArgument() or next = node2.asDefiningArgument())
|
exists(Call c | knownPassthoughCall(c, cur, next)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't see why next should ever be an indirect argument. Indirect arguments always go into a function, but it looks like next is always either an outgoing argument or a return value?

Comment on lines +122 to +124
node.asVariable().hasName("argv")
or
node.asIndirectVariable().hasName("argv")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hmm... I don't think this will work since asVariable and asIndirectVariable is about global variables. You probably want to replace these with node.asExpr().(VariableAccess).getTarget().hasName("argv") (and similarly for the indirect case).

}


abstract class AlgorithmSinkArgument extends Expr {

Mathias Vorreiter Pedersen (MathiasVP) Sep 14, 2023
edited
Loading

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If you ever want to distinguish between a function in these two scenarios:

void runAlgorithm(unsigned algorithm_type);

and

void runAlgorithm(unsigned* pointer_to_algorithm_type);

then you most likely want to extend DataFlow::Node instead.


/**
* A hard-coded mapping of known algorithm aliases in OpenSSL.
* This was derived by applying the same kind of logic foun din `customAliases` to the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
* This was derived by applying the same kind of logic foun din `customAliases` to the
* This was derived by applying the same kind of logic found in `customAliases` to the

* `normalized` is the normalized name of the algorithm (e.g., "AES128" for "aes-128-cbc")
* `algType` is the type of algorithm (e.g., "SYMMETRIC_ENCRYPTION")
*/
predicate knownOpenSSLAlgorithm(string name, int nid, string normalized, string algType)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

🤯

* OpenSSL functions as defined in the OpenSSL docs
* https://www.openssl.org/docs/manmaster/man3/
*/
predicate openSSLAPIFuncName(string name){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

🤯

Changing metadata to under python namespace

Copy link
Copy Markdown
Author

Merged from Josh Brown (@ropwareJB)'s version of this branch that was also merged upstream. #19

Ben Rodes (bdrodes) pushed a commit that referenced this pull request Jan 9, 2025
feat(reusable-workflow-models): Reusable workflow MaD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cryptography Models, Libraries and Queries Migration

3 participants


Back | FazBrowse Home | New Git URL