@@ -182,57 +182,65 @@ module HardcodedKeys {
182182 FormattingSanitizer ( ) { exists ( Formatting:: StringFormatCall s | s .getAResult ( ) = this ) }
183183 }
184184
185+ private string getRandIntFunctionName ( ) {
186+ result =
187+ [
188+ "ExpFloat64" , "Float32" , "Float64" , "Int" , "Int31" , "Int31n" , "Int63" , "Int63n" , "Intn" ,
189+ "NormFloat64" , "Uint32" , "Uint64"
190+ ]
191+ }
192+
193+ private DataFlow:: CallNode getARandIntCall ( ) {
194+ result .getTarget ( ) .hasQualifiedName ( "math/rand" , getRandIntFunctionName ( ) ) or
195+ result .getTarget ( ) .( Method ) .hasQualifiedName ( "math/rand" , "Rand" , getRandIntFunctionName ( ) ) or
196+ result .getTarget ( ) .hasQualifiedName ( "crypto/rand" , "Int" )
197+ }
198+
199+ private DataFlow:: CallNode getARandReadCall ( ) {
200+ result .getTarget ( ) .hasQualifiedName ( "crypto/rand" , "Read" )
201+ }
202+
185203 /**
186204 * Mark any taint arising from a read on a tainted slice with a random index as a
187205 * sanitizer for all instances of the taint
188206 */
189207 private class RandSliceSanitizer extends Sanitizer {
190208 RandSliceSanitizer ( ) {
191- // Sanitize flows like this:
192- // func GenerateCryptoString(n int) (string, error) {
193- // const chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
194- // ret := make([]byte, n)
195- // for i := range ret {
196- // num, err := crand.Int(crand.Reader, big.NewInt(int64(len(chars))))
197- // if err != nil {
198- // return "", err
199- // }
200- // ret[i] = chars[num.Int64()]
201- // }
202- // return string(ret), nil
203- // }
204- exists (
205- DataFlow:: CallNode randint , string name , DataFlow:: ElementReadNode r , DataFlow:: Node index
209+ exists ( DataFlow:: Node randomValue , DataFlow:: Node index |
210+ // Sanitize flows like this:
211+ // func GenerateCryptoString(n int) (string, error) {
212+ // const chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
213+ // ret := make([]byte, n)
214+ // for i := range ret {
215+ // num, err := crand.Int(crand.Reader, big.NewInt(int64(len(chars))))
216+ // if err != nil {
217+ // return "", err
218+ // }
219+ // ret[i] = chars[num.Int64()]
220+ // }
221+ // return string(ret), nil
222+ // }
223+ randomValue = getARandIntCall ( ) .getAResult ( )
224+ or
225+ // Sanitize flows like :
226+ // func GenerateRandomString(size int) string {
227+ // var bytes = make([]byte, size)
228+ // rand.Read(bytes)
229+ // for i, x := range bytes {
230+ // bytes[i] = characters[x%byte(len(characters))]
231+ // }
232+ // return string(bytes)
233+ // }
234+ randomValue =
235+ any ( DataFlow:: PostUpdateNode pun |
236+ pun .getPreUpdateNode ( ) = getARandReadCall ( ) .getArgument ( 0 )
237+ )
206238 |
239+ TaintTracking:: localTaint ( randomValue , index ) and
207240 (
208- randint .getTarget ( ) .hasQualifiedName ( "math/rand" , name ) or
209- randint .getTarget ( ) .( Method ) .hasQualifiedName ( "math/rand" , "Rand" , name ) or
210- randint .getTarget ( ) .hasQualifiedName ( "crypto/rand" , "Int" )
211- ) and
212- name =
213- [
214- "ExpFloat64" , "Float32" , "Float64" , "Int" , "Int31" , "Int31n" , "Int63" , "Int63n" , "Intn" ,
215- "NormFloat64" , "Uint32" , "Uint64"
216- ] and
217- TaintTracking:: localTaint ( randint .getAResult ( ) , index ) and
218- r .reads ( this , index )
219- )
220- or
221- // Sanitize flows like :
222- // func GenerateRandomString(size int) string {
223- // var bytes = make([]byte, size)
224- // rand.Read(bytes)
225- // for i, x := range bytes {
226- // bytes[i] = characters[x%byte(len(characters))]
227- // }
228- // return string(bytes)
229- // }
230- exists ( DataFlow:: CallNode randread , DataFlow:: Node rand |
231- randread .getTarget ( ) .hasQualifiedName ( "crypto/rand" , "Read" ) and
232- TaintTracking:: localTaint ( any ( DataFlow:: PostUpdateNode pun |
233- pun .getPreUpdateNode ( ) = randread .getArgument ( 0 )
234- ) , rand ) and
235- this .( DataFlow:: ElementReadNode ) .reads ( _, rand )
241+ this .( DataFlow:: ElementReadNode ) .reads ( _, randomValue ) or
242+ any ( DataFlow:: ElementReadNode r ) .reads ( this , index )
243+ )
236244 )
237245 }
238246 }
@@ -250,7 +258,7 @@ module HardcodedKeys {
250258 }
251259
252260 /*
253- * This is code is used to model taint flow through a binary operation such as a
261+ * Models taint flow through a binary operation such as a
254262 * modulo `%` operation or an addition `+` operation
255263 */
256264
@@ -282,8 +290,6 @@ module HardcodedKeys {
282290
283291 override predicate isSanitizer ( DataFlow:: Node sanitizer ) { sanitizer instanceof Sanitizer }
284292
285- // override predicate isAdditionalTaintStep(DataFlow::Node prev, DataFlow::Node succ) {
286- // }
287293 override predicate isSanitizerGuard ( DataFlow:: BarrierGuard guard ) {
288294 guard instanceof SanitizerGuard
289295 }
0 commit comments