|
| 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 | +} |
0 commit comments