Skip to content

Commit 9eac551

Browse files
committed
rules/sdk: add pass to reject time.Now()
time.Now() uses local clocks that unfortunately when used in distributed consensus introduce lots of skew and can be exploited in vulnerabilities. Instead there is consensus aware clock whose timestamp is embedded in the current Block. This pass rejects usages as per https://forum.cosmos.network/t/cosmos-sdk-vulnerability-retrospective-security-advisory-jackfruit-october-12-2021/5349 Updates #1
1 parent eff6662 commit 9eac551

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

rules/rulelist.go

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func Generate(filters ...RuleFilter) RuleList {
117117
{"G703", "Errors that don't result in rollback", sdk.NewErrorNotPropagated},
118118
{"G704", "Strconv invalid bitSize and cast", sdk.NewStrconvIntBitSizeOverflow},
119119
{"G705", "Iterating over maps undeterministically", sdk.NewMapRangingCheck},
120+
{"G706", "Non-consensus aware time.Now() usage", sdk.NewTimeNowRefusal},
120121
}
121122

122123
ruleMap := make(map[string]RuleDefinition)

rules/rules_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ var _ = Describe("gosec rules", func() {
9595
runner("G705", testutils.SampleCodeMapRangingNonDeterministic)
9696
})
9797

98+
It("should detect non-consensus aware time.Now() usage", func() {
99+
runner("G706", testutils.SampleCodeTimeNowNonCensusAware)
100+
})
101+
98102
It("should detect DoS vulnerability via decompression bomb", func() {
99103
runner("G110", testutils.SampleCodeG110)
100104
})

rules/sdk/time_now.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// (c) Copyright 2021 Hewlett Packard Enterprise Development LP
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sdk
16+
17+
import (
18+
"errors"
19+
"go/ast"
20+
21+
"github.com/cosmos/gosec/v2"
22+
)
23+
24+
// This static analyzer discourages the use of time.Now() as it was discovered that
25+
// its usage caused local non-determinism as reported and detailed at
26+
// https://forum.cosmos.network/t/cosmos-sdk-vulnerability-retrospective-security-advisory-jackfruit-october-12-2021/5349
27+
28+
type timeNowCheck struct {
29+
gosec.MetaData
30+
calls gosec.CallList
31+
}
32+
33+
func (tmc *timeNowCheck) ID() string { return tmc.MetaData.ID }
34+
35+
func (tmc *timeNowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
36+
// We want to catch all function invocations as well as assignments of any of the form:
37+
// .Value = time.Now().*
38+
// fn := time.Now
39+
callExpr, ok := node.(*ast.CallExpr)
40+
if !ok {
41+
return nil, nil
42+
}
43+
44+
sel, ok := callExpr.Fun.(*ast.SelectorExpr)
45+
if !ok {
46+
return nil, nil
47+
}
48+
49+
if sel.Sel.Name != "Now" {
50+
return nil, nil
51+
}
52+
53+
switch x := sel.X.(type) {
54+
case *ast.Ident:
55+
if x.Name != "time" {
56+
return nil, nil
57+
}
58+
59+
case *ast.SelectorExpr:
60+
if x.Sel.Name != "time" {
61+
return nil, nil
62+
}
63+
}
64+
65+
// By this point issue the error.
66+
return nil, errors.New("time.Now() is non-deterministic for distributed consensus, you should use the current Block's timestamp")
67+
}
68+
69+
func NewTimeNowRefusal(id string, config gosec.Config) (rule gosec.Rule, nodes []ast.Node) {
70+
calls := gosec.NewCallList()
71+
72+
tnc := &timeNowCheck{
73+
MetaData: gosec.MetaData{
74+
ID: id,
75+
Severity: gosec.High,
76+
Confidence: gosec.High,
77+
What: "Non-determinism from using non-consensus aware time.Now()",
78+
},
79+
calls: calls,
80+
}
81+
82+
nodes = append(nodes, (*ast.CallExpr)(nil))
83+
return tnc, nodes
84+
}

testutils/source.go

+19
Original file line numberDiff line numberDiff line change
@@ -2528,4 +2528,23 @@ func noop(keys []string) []string {return keys}
25282528
`}, 13, gosec.NewConfig(),
25292529
},
25302530
}
2531+
2532+
SampleCodeTimeNowNonCensusAware = []CodeSample{
2533+
{
2534+
[]string{`
2535+
package main
2536+
2537+
func main() {
2538+
_ = time.Now()
2539+
}`}, 3, gosec.NewConfig(),
2540+
},
2541+
{
2542+
[]string{`
2543+
package main
2544+
2545+
func main() {
2546+
_ = time.Now().Unix()
2547+
}`}, 3, gosec.NewConfig(),
2548+
},
2549+
}
25312550
)

0 commit comments

Comments
 (0)