-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstrconv_bitsize_mismatch.go
158 lines (134 loc) · 4.21 KB
/
strconv_bitsize_mismatch.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// (c) Copyright 2021 Hewlett Packard Enterprise Development LP
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdk
import (
"fmt"
"go/ast"
"strconv"
"github.com/cosmos/gosec/v2"
)
type bitsizeOverflowCheck struct {
gosec.MetaData
calls gosec.CallList
}
func (bc *bitsizeOverflowCheck) ID() string {
return bc.MetaData.ID
}
func (bc *bitsizeOverflowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
var parseUintVarObj map[*ast.Object]ast.Node
// Given that the code could be splayed over multiple line, we
// examine ctx.PassedValues to check for temporarily stored data.
if retr, ok := ctx.PassedValues[bc.ID()]; !ok {
parseUintVarObj = make(map[*ast.Object]ast.Node)
ctx.PassedValues[bc.ID()] = parseUintVarObj
} else if saved, ok := retr.(map[*ast.Object]ast.Node); ok {
parseUintVarObj = saved
} else {
return nil, fmt.Errorf("ctx.PassedValues[%s] is of type %T, want %T", bc.ID(), retr, parseUintVarObj)
}
// strconv.ParseUint*
// To reduce false positives, detect code that is converted to any of: int16, int32, int64 only.
switch n := node.(type) {
case *ast.AssignStmt:
for _, expr := range n.Rhs {
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
continue
}
if bc.calls.ContainsPkgCallExpr(callExpr, ctx, false) == nil {
continue
}
ident, ok := n.Lhs[0].(*ast.Ident)
if ok && ident.Name != "_" {
parseUintVarObj[ident.Obj] = n
}
}
case *ast.CallExpr:
fn, ok := n.Fun.(*ast.Ident)
if !ok {
return nil, nil
}
switch fn.Name {
default:
return nil, nil
case "int", "int16", "int32", "int64":
ident, ok := n.Args[0].(*ast.Ident)
if !ok {
return nil, nil
}
nFound, ok := parseUintVarObj[ident.Obj]
if !ok {
return nil, nil
}
stmt, ok := nFound.(*ast.AssignStmt)
if !ok {
return nil, nil
}
r0 := stmt.Rhs[0]
call, ok := r0.(*ast.CallExpr)
if !ok {
return nil, nil
}
bitSizeLit, ok := call.Args[2].(*ast.BasicLit)
if !ok {
return nil, nil
}
// Actually strconv parse it.
bitSize, err := strconv.Atoi(bitSizeLit.Value)
if err != nil {
failure := fmt.Sprintf("Invalid bitSize %q parse failure: %v", bitSizeLit.Value, err)
return gosec.NewIssue(ctx, nFound, bc.ID(), failure, bc.Severity, bc.Confidence), nil
}
failed := false
switch {
case fn.Name == "int16" && bitSize >= 16:
failed = true
case fn.Name == "int64" && bitSize >= 64:
failed = true
case fn.Name == "int32" && bitSize >= 32:
failed = true
case fn.Name == "int" && (bitSize == 32 || bitSize >= 64):
failed = true
}
if !failed {
return nil, nil
}
// Otherwise compose the message now.
failure := fmt.Sprintf("Overflow in bitSize of %d for %q", bitSize, fn.Name)
// The value was found, next let's check for the size of:
// strconv.ParseUint(str, base, digits)
// Awesome, we found the conversion to int*
// Next we need to examine what the bitSize was.
return gosec.NewIssue(ctx, nFound, bc.ID(), failure, bc.Severity, bc.Confidence), nil
}
}
return nil, nil
}
// NewStrconvIntBitSizeOverflow returns an error if a constant bitSize is used
// for a cast signed value that was retrieved from strconv.ParseUint.
func NewStrconvIntBitSizeOverflow(id string, config gosec.Config) (rule gosec.Rule, nodes []ast.Node) {
calls := gosec.NewCallList()
calls.Add("strconv", "ParseUint")
bc := &bitsizeOverflowCheck{
MetaData: gosec.MetaData{
ID: id,
Severity: gosec.High,
Confidence: gosec.Medium,
What: "Overflow due to wrong bitsize in strconv.ParseUint yet cast from uint64 to int*",
},
calls: calls,
}
nodes = append(nodes, (*ast.FuncDecl)(nil), (*ast.AssignStmt)(nil), (*ast.CallExpr)(nil))
return bc, nodes
}