-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinteger.go
284 lines (242 loc) · 7.3 KB
/
integer.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// (c) Copyright 2016 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 (
"go/ast"
"go/types"
"strconv"
"strings"
"github.com/cosmos/gosec/v2"
)
// originally copied and simplified from the rules/integer_overflow.go
type integerOverflowCheck struct {
gosec.MetaData
}
func (i *integerOverflowCheck) ID() string {
return i.MetaData.ID
}
func hasAnyPrefix(src string, prefixes ...string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(src, prefix) {
return true
}
}
return false
}
// To catch integer type conversion, check if we ever
// call functions `uintX(y)` or `intX(y)` for any X and y,
// where y is not an int literal.
// TODO: restrict it to just the possible bit-sizes for X (unspecified, 8, 16, 32, 64)
// TODO: check if y's bit-size is greater than X
func (i *integerOverflowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
// ignore if it's protobuf
fileName := ctx.FileSet.File(node.Pos()).Name()
if strings.HasSuffix(fileName, ".pb.go") {
return nil, nil
}
switch n := node.(type) {
case *ast.CallExpr:
fun, ok := n.Fun.(*ast.Ident)
if !ok {
return nil, nil
}
if len(n.Args) == 0 {
return nil, nil
}
arg := n.Args[0]
argT := ctx.Info.TypeOf(arg)
if argT == nil {
// TODO: Perhaps log and investigate this case more.
return nil, nil
}
fnType := ctx.Info.TypeOf(fun)
if fnType == nil {
// TODO: Perhaps log and investigate this case more.
return nil, nil
}
argType := argT.Underlying()
destType := fnType.Underlying()
intCast := hasAnyPrefix(destType.String(), "int", "uint")
if !intCast {
return nil, nil
}
// Detect intX(y) and uintX(y) for any X, where y is not an int literal.
// n.Args[0] is of type ast.Expr. It's the arg to the type conversion.
// If the expression string is a constant integer, then ignore.
// TODO: check that the constant will actually fit and wont overflow?
exprString := types.ExprString(arg)
intLiteral, err := strconv.Atoi(exprString)
if err == nil {
// TODO: probably use ParseInt and check if it fits in the target.
_ = intLiteral
return nil, nil
}
switch arg := arg.(type) {
case *ast.CallExpr:
// len() returns an int that is always >= 0, so it will fit in a uint, uint64, or int64.
argFun, ok := arg.Fun.(*ast.Ident)
if !ok || argFun.Name != "len" {
break
}
// Please see the rules for determining if *int*(len(...)) can overflow
// as per: https://github.com/cosmos/gosec/issues/54
lenCanOverflow := canLenOverflow64
if is32Bit {
lenCanOverflow = canLenOverflow32
}
if lenCanOverflow(fun.Name) {
return gosec.NewIssue(ctx, n, i.ID(), i.What, i.Severity, i.Confidence), nil
}
return nil, nil
}
// If the argument is being cast to its underlying type, there's no risk.
if argType == destType {
return nil, nil
}
// Check if both are uint* values.
argIsUint := hasAnyPrefix(argType.String(), "uint")
if argIsUint && !canBothUintsOverflow(argType.String(), destType.String()) {
return nil, nil
}
// Check if both are int* values.
argIsInt := hasAnyPrefix(argType.String(), "int")
if argIsInt && !canBothIntToIntOverflow(argType.String(), destType.String()) {
return nil, nil
}
// ALl other cases should be flagged.
return gosec.NewIssue(ctx, n, i.ID(), i.What, i.Severity, i.Confidence), nil
}
return nil, nil
}
// NewIntegerCast detects if there is potential Integer OverFlow
func NewIntegerCast(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
return &integerOverflowCheck{
MetaData: gosec.MetaData{
ID: id,
Severity: gosec.High,
Confidence: gosec.Medium,
What: "Potential integer overflow by integer type conversion",
},
}, []ast.Node{(*ast.FuncDecl)(nil), (*ast.AssignStmt)(nil), (*ast.CallExpr)(nil)}
}
// Please see the rules at https://github.com/cosmos/gosec/issues/54
func canLenOverflow64(destKind string) bool {
switch destKind {
case "int8", "uint8", "int16", "uint16":
return true
case "uint64":
// uint64([0, maxInt64])
return false
case "uint32":
// uint32([0, maxInt64])
return true
case "uint":
// uint => uint64 => uint64([0, maxInt64])
return false
case "int64":
// int64([0, maxInt64])
return false
case "int32":
// int32([0, maxInt64])
return true
case "int":
// int64([0, maxInt64])
return false
default:
return true
}
}
const s = 1
const is32Bit = (^uint(s-1))>>32 == 0 // #nosec
// Please see the rules at https://github.com/cosmos/gosec/issues/54
func canLenOverflow32(destKind string) bool {
switch destKind {
case "int8", "uint8", "int16", "uint16":
return true
case "uint64":
// uint64([0, maxInt32])
return false
case "uint32":
// uint32([0, maxInt32])
return false
case "uint":
// uint => uint32 => uint32([0, maxInt32])
return false
case "int64":
// int64([0, maxInt32])
return false
case "int32":
// int32([0, maxInt32])
return false
case "int":
// int => int32 => int32([0, maxInt32])
return false
default:
return true
}
}
func canBothUintsOverflow(srcKind, destKind string) bool {
bothUints := hasAnyPrefix(srcKind, "uint") && hasAnyPrefix(destKind, "uint")
if !bothUints {
return true
}
if destKind == "uint" {
// Only in 32-bit is uint equal to uint32 hence can it overflow if src is uint64.
return srcKind == "uint64" && is32Bit
}
if destKind == "uint64" {
// Casting any uint type to uint64 cannot overflow.
return false
}
if destKind == "uint32" {
// Only uint64 or uint (when in 64-bits) can overflow when being cast to uint32.
return srcKind == "uint64" || (srcKind == "uint" && !is32Bit)
}
if destKind == "uint16" {
// Everything except "uint8" and "uint16" can overflow when cast to uint16.
return srcKind == "uint64" || srcKind == "uint32" || srcKind == "uint"
}
if destKind == "uint8" {
// Everything that isn't "uint8" will overflow when cast to uint8.
return srcKind != "uint8"
}
return true
}
func canBothIntToIntOverflow(srcKind, destKind string) bool {
bothInts := hasAnyPrefix(srcKind, "int") && hasAnyPrefix(destKind, "int")
if !bothInts {
return true
}
if destKind == "int" {
// Only in 32-bit is int equal to int32 hence can it overflow if src is int64.
return srcKind == "int64" && is32Bit
}
if destKind == "int64" {
// Casting any int type to int64 cannot overflow.
return false
}
if destKind == "int32" {
// Only int64 or int (when in 64-bits) can overflow when being cast to int32.
return srcKind == "int64" || (srcKind == "int" && !is32Bit)
}
if destKind == "int16" {
// Everything except "int8" and "int16" can overflow when cast to int16.
return srcKind == "int64" || srcKind == "int32" || srcKind == "int"
}
if destKind == "int8" {
// Everything that isn't "int8" will overflow when cast to int8.
return srcKind != "int8"
}
return true
}