Skip to content
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

New Rule S6432: Counter Mode initialization vectors should not be reused #5754

Open
pedro-oliveira-sonarsource opened this issue Jun 20, 2022 · 6 comments
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: Security Related to Vulnerability and Security Hotspot rules Sprint: MMF-3716 crypto rules https://sonarsource.atlassian.net/browse/MMF-3716 Type: New Rule Implementation for a rule that HAS been specified.
Projects

Comments

@pedro-oliveira-sonarsource
Copy link

pedro-oliveira-sonarsource commented Jun 20, 2022

What
The rule detects "weak" initialization vectors when used with GCM and CCM block cipher mode of operation.
The detection logic is similar to what is implemented for rule S3329. It supports System.Security.Cryptography.

How
The rule raises on System.Security.Cryptography.AesGcm#Encrypt or System.Security.Cryptography.AesCcm#Encrypt when it matches the following criteria:

  • first argument, nonce is created from a string literal and converted to a byte array through the System.Text Encoding.GetBytes function.
var key = Encoding.UTF8.GetBytes("0123456789123456");
var plaintext = Encoding.UTF8.GetBytes("plaintext");
var ciphertext = new byte[plaintext.Length];
var tag = new byte[AesGcm.TagByteSizes.MaxSize];

var nonce = Encoding.UTF8.GetBytes("7cVgr5cbdCZV"); // Secondary location: The initialization vector is a static value.

using var aes = new AesGcm(key);

aes.Encrypt(nonce, plaintext, ciphertext, tag); // Noncompliant

Code examples

https://github.com/SonarSource/security-expected-issues/pull/717

Message and highlighting

See SonarSource/rspec#1060

@pierre-loup-tristant-sonarsource pierre-loup-tristant-sonarsource added Type: New Feature This hasn't been here before. Area: C# C# rules related issues. Area: Security Related to Vulnerability and Security Hotspot rules labels Jun 20, 2022
@github-actions github-actions bot added this to New Feature in Backlog Jun 20, 2022
@pavel-mikula-sonarsource
Copy link
Contributor

MMF-2826

@andrei-epure-sonarsource
Copy link
Contributor

This looks like a symbolic execution rule.

@andrei-epure-sonarsource andrei-epure-sonarsource added the Area: CFG/SE CFG and SE related issues. label Feb 23, 2023
@andrei-epure-sonarsource
Copy link
Contributor

Before taking a decision about Symbolic Execution vs usage of trackers, let's first look into what it would take to use trackers.

@martin-strecker-sonarsource suggestion:

  • Look for invocations to Encrypt and investigate what expression was passed in as nonce argument:
    • local: look for assigments to it
    • field: look for initializer

@pavel-mikula-sonarsource
Copy link
Contributor

Trackers are tracking a state of an individual object ("is this property set to 'true' or 'false'"), they don't track another value propagation through the code.

ConstantValueFinder has very limited value tracking capabilities, but it cannot answer questions we have.

This sounds like a SE rule. A simple one.

@andrei-epure-sonarsource
Copy link
Contributor

andrei-epure-sonarsource commented Feb 24, 2023

Context: this is very similar to S2053 which follows the data flow for the same Encoding.GetBytes method.

S2053 has been implemented in PR#3829 and can be found in InitializationVectorShouldBeRandom.cs. The follow-up FP fixes might be relevant, too.

I imagine this rule is even simpler than S2053 because it only cares about the ByteArray (whereas S2053 also checks the salt size).

@martin-strecker-sonarsource
Copy link
Contributor

If we want to avoid SE and be more precise with respect to fields and properties as sources of the nonce we could do this instead:

In symbol start:

  • Track creations of byte[] and the like (this includes e.g. utf-8 strings) that is purely made of constant values. Store any symbols this is assigned to (fields, properties, and locals)
  • And also track invocations of Encrypt and any symbols passed as an argument to the nonce parameter.

In symbol end action:

  • see if there is an overlap of symbols

This would cover fields/properties as stores for the nonce as well as locals. It would not cover any mutation that happens between creation and usage.

Furthermore:
The requirement of the cipher value is to come from an external source:

The nonce associated with this message, which should be a unique value for every operation with the same key.

So any nonce that gets passed in as a parameter should be okay.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: New Rule Implementation for a rule that HAS been specified. and removed Type: New Feature This hasn't been here before. labels Aug 15, 2023
@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource changed the title Rule S6432: Counter Mode initialization vectors should not be reused New Rule S6432: Counter Mode initialization vectors should not be reused Nov 17, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource added the Sprint: MMF-3716 crypto rules https://sonarsource.atlassian.net/browse/MMF-3716 label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: Security Related to Vulnerability and Security Hotspot rules Sprint: MMF-3716 crypto rules https://sonarsource.atlassian.net/browse/MMF-3716 Type: New Rule Implementation for a rule that HAS been specified.
Projects
No open projects
Backlog
  
New Feature
Development

No branches or pull requests

6 participants