Skip to content

Commit 763de4f

Browse files
authored
Merge pull request github#6425 from raulgarciamsft/insecureRandom_potential_fix
C#: Adding Membership.GeneratePassword() as a bad source of random data
2 parents 1eb804a + d97525e commit 763de4f

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Membership.GeneratePassword()` has been added as a bad source of random data.

csharp/ql/src/Security Features/InsecureRandomness.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ string GeneratePassword()
1515
password = "mypassword" + BitConverter.ToInt32(randomBytes);
1616
}
1717

18-
// GOOD: Password is generated using a cryptographically secure RNG
18+
// BAD: Membership.GeneratePassword generates a password with a bias
1919
password = Membership.GeneratePassword(12, 3);
2020

2121
return password;

csharp/ql/src/Security Features/InsecureRandomness.ql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ module Random {
5959
this.getExpr() =
6060
any(MethodCall mc |
6161
mc.getQualifier().getType().(RefType).hasQualifiedName("System", "Random")
62+
or
63+
// by using `% 87` on a `byte`, `System.Web.Security.Membership.GeneratePassword` has a bias
64+
mc.getQualifier()
65+
.getType()
66+
.(RefType)
67+
.hasQualifiedName("System.Web.Security", "Membership") and
68+
mc.getTarget().hasName("GeneratePassword")
6269
)
6370
}
6471
}

csharp/ql/test/query-tests/Security Features/CWE-338/InsecureRandomness.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ public static string InsecureRandomStringFromIndexer(int length)
7373
}
7474
return result;
7575
}
76+
77+
public static string BiasPasswordGeneration()
78+
{
79+
// BAD: Membership.GeneratePassword generates a password with a bias
80+
string password = System.Web.Security.Membership.GeneratePassword(12, 3);
81+
return password;
82+
}
83+
84+
}
85+
86+
namespace System.Web.Security
87+
{
88+
public static class Membership
89+
{
90+
public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
91+
{
92+
return "stub";
93+
}
94+
95+
}
7696
}

csharp/ql/test/query-tests/Security Features/CWE-338/InsecureRandomness.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ nodes
2929
| InsecureRandomness.cs:62:16:62:32 | call to method ToString : String | semmle.label | call to method ToString : String |
3030
| InsecureRandomness.cs:72:31:72:39 | call to method Next : Int32 | semmle.label | call to method Next : Int32 |
3131
| InsecureRandomness.cs:74:16:74:21 | access to local variable result : String | semmle.label | access to local variable result : String |
32+
| InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | semmle.label | call to method GeneratePassword |
3233
#select
3334
| InsecureRandomness.cs:12:27:12:50 | call to method InsecureRandomString | InsecureRandomness.cs:28:29:28:43 | call to method Next : Int32 | InsecureRandomness.cs:12:27:12:50 | call to method InsecureRandomString | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:28:29:28:43 | call to method Next | call to method Next |
3435
| InsecureRandomness.cs:13:20:13:56 | call to method InsecureRandomStringFromSelection | InsecureRandomness.cs:60:31:60:39 | call to method Next : Int32 | InsecureRandomness.cs:13:20:13:56 | call to method InsecureRandomStringFromSelection | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:60:31:60:39 | call to method Next | call to method Next |
3536
| InsecureRandomness.cs:14:20:14:54 | call to method InsecureRandomStringFromIndexer | InsecureRandomness.cs:72:31:72:39 | call to method Next : Int32 | InsecureRandomness.cs:14:20:14:54 | call to method InsecureRandomStringFromIndexer | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:72:31:72:39 | call to method Next | call to method Next |
37+
| InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | call to method GeneratePassword |

0 commit comments

Comments
 (0)