-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsystem-time.ql
49 lines (46 loc) · 1.91 KB
/
system-time.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @name Calling the system time
* @description Calling the system time is non-deterministic and could cause issues in consensus-critical code.
* @kind problem
* @problem.severity warning
* @id crypto-com/cosmos-sdk-codeql/system-time
* @tags correctness
*/
import go
predicate isIrrelevantPackage(string packageName) {
packageName = "cli" or packageName = "main" or packageName = "testutil"
}
from CallExpr call
where
call.getTarget().getQualifiedName() = "time.Now" and
// string formatting is usually a false positive (used in logging etc.),
// so it's usually ok to ignore.
// but there could be false negatives (if the string formatting output is written to a consensus state)
not (
call.getParent().(CallExpr).getTarget().getQualifiedName() =
"github.com/cosmos/cosmos-sdk/types.FormatTimeBytes" or
call.getParent*().(CallExpr).getTarget().getQualifiedName().matches("fmt.Sprintf")
) and
not call.getEnclosingFunction().getName().matches("Test%") and
call.getLocation().getFile().getBaseName() != "test_helpers.go" and
not isIrrelevantPackage(call.getLocation().getFile().getPackageName()) and
// ignore cases where time is fed into telemetry calls
not exists(DataFlow::CallNode telemetryCall |
telemetryCall
.getExpr()
.(CallExpr)
.getTarget()
.getQualifiedName()
.matches("github.com/cosmos/cosmos-sdk/telemetry%")
|
DataFlow::localFlow(DataFlow::exprNode(call), telemetryCall.getAnArgument())
) and
// explicit comment explaining why it is safe
// TODO: extract to a common predicate or a class
not exists(CommentGroup c |
call.getLocation().getStartLine() - 1 = c.getLocation().getStartLine() or
call.getEnclosingFunction().getLocation().getStartLine() - 1 = c.getLocation().getStartLine()
|
c.getAComment().getText().matches("%SAFE:%")
)
select call, "Calling the system time may be a possible source of non-determinism"