-
Notifications
You must be signed in to change notification settings - Fork 229
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
Create rule S4347: 'SecureRandom' seeds should not be predictable (Part 1) #9300
Conversation
96ddd49
to
d4ef712
Compare
3068250
to
c856b6f
Compare
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
|
||
private static ProgramState ProcessArrayElementReference(ProgramState state, IArrayElementReferenceOperationWrapper arrayElementReference) => | ||
arrayElementReference.IsAssignmentTarget() || arrayElementReference.IsCompoundAssignmentTarget() | ||
? state.SetSymbolConstraint(arrayElementReference.ArrayReference.TrackedSymbol(state), CryptographicSeedConstraint.Unpredictable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, if the array access is on the left side of the assignment, I set it to unpredictable.
I could look into the right side of the assignment and see if it has constant value, but I am not sure if there is much value in this case, so I opted for the FN.
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
c856b6f
to
52683e5
Compare
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
&& state[value]?.HasConstraint(CryptographicSeedConstraint.Predictable) is true; | ||
} | ||
|
||
private static ProgramState ProcessSecureRandomSetSeed(ProgramState state, IInvocationOperationWrapper invocation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This processes SecureRandom.SetSeed(long/byte[])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments you left on the PR helped a lot. Thanks for that.
What do you think about including them in the code?
2a3576c
to
ebe9b96
Compare
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
486700e
to
453957b
Compare
Quality Gate passed for 'Sonar .NET Java Plugin'Issues Measures |
Quality Gate passed for 'SonarAnalyzer for .NET'Issues Measures |
@costin-zaharia-sonarsource if you look at the spec: ...it also mentions GUID handling. We could also implement only the following:
What do you think? |
<p>When using <code>SecureRandom</code>, it is important not to use predictable seeds. This class is used to generate cryptographically strong random | ||
numbers. Using a predictable seed will make its output predictable as well, which counteracts the use case of <code>SecureRandom</code>.</p> | ||
<h2>Why is this an issue?</h2> | ||
<p><code>java.security.SecureRandom</code> is often used to generate random values for cryptographic algorithms. When a random number generator is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, java.security.SecureRandom
?
It was probably meant to be Org.BouncyCastle.Security.SecureRandom
but you can also check with appsec squad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see also the documentation link
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent catch, thanks!
Left a comment on RSPEC PR.
...src/SonarAnalyzer.CSharp/SymbolicExecution/Roslyn/SecureRandomSeedsShouldNotBePredictable.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Please check though since I've added a few minor comments.
? state.SetOperationConstraint(invocation, CryptographicSeedConstraint.Predictable) | ||
: null; | ||
|
||
private ProgramState ProcessStringToBytes(ProgramState state, IInvocationOperationWrapper invocation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be static
&& state[value]?.HasConstraint(CryptographicSeedConstraint.Predictable) is true; | ||
} | ||
|
||
private static ProgramState ProcessSecureRandomSetSeed(ProgramState state, IInvocationOperationWrapper invocation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments you left on the PR helped a lot. Thanks for that.
What do you think about including them in the code?
private static ProgramState ProcessSecureRandomSetSeed(ProgramState state, IInvocationOperationWrapper invocation) | ||
{ | ||
if (invocation.TargetMethod.Name == "SetSeed" | ||
&& invocation.TargetMethod.ContainingType.DerivesFrom(KnownType.Org_BouncyCastle_Security_SecureRandom) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the indentation is off here.
analyzers/tests/SonarAnalyzer.Test/Rules/SecureRandomSeedsShouldNotBePredictableTest.cs
Show resolved
Hide resolved
@@ -0,0 +1,262 @@ | |||
using System; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very well tested, maybe too well since the symbolic execution engine has it's own tests.
I did not see any tests with inheritance. Would it be worth adding some? SecureRandom
is not sealed, although most of the methods we check are static so I'm not sure if it's actually useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a test on part 3, as in part1+2 I do not call new
on SecureRandom, only GetInstance
, which is a static method that always returns SecureRandom
.
Since most of the suggestions are not breaking, I will merge this and apply the suggestions on the part#2 PR. |
Part of #8992
Implementing Scenario 1