You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✨ [safecast] Introduced utilities to perform casting safely (#511)
<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description
protect against
[CWE-190](https://cwe.mitre.org/data/definitions/190.html)
### Test Coverage
<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->
- [x] This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [ ] Additional tests are not required for this change (e.g.
documentation update).
returnfileList, fileCounter.Load(), totalSizeOnDisk.Load(), fmt.Errorf("%w: more than %v B of disk space was used while unzipping %v (%v B used already)", commonerrors.ErrTooLarge, limits.GetMaxTotalSize(), source, totalSizeOnDisk.Load())
367
368
}
368
-
iffilecount:=fileCounter.Load(); limits.Apply() &&filecount<=math.MaxInt64&&int64(filecount) >limits.GetMaxFileCount() {//nolint:gosec // if filecount of uint64 is greater than the max value of int64 then it must be greater than GetMaxFileCount as that is an int64
returnfileList, filecount, totalSizeOnDisk.Load(), fmt.Errorf("%w: more than %v files were created while unzipping %v (%v files created already)", commonerrors.ErrTooLarge, limits.GetMaxFileCount(), source, filecount)
err=fmt.Errorf("%w: could not convert uptime '%v' to duration as it exceeds the upper limit for time.Duration", commonerrors.ErrOutOfRange, _uptime)
115
116
return
116
117
}
117
-
uptime=time.Duration(_uptime) *time.Second//nolint:gosec // we have verified the value of _uptime is whithin the upper limit for time.Duration in the above check
err=fmt.Errorf("%w: could not convert uptime '%v' to duration as it exceeds the upper limit for time.Duration", commonerrors.ErrOutOfRange, _bootime)
129
130
return
130
131
}
131
-
bootime=time.Unix(int64(_bootime), 0)//nolint:gosec // we have verified the value of _bootime is whithin the upper limit for time.Duration in the above check
p, err:=process.NewProcessWithContext(ctx, int32(pid))//nolint:gosec // Max PID is 2^22 which is within int32 range https://stackoverflow.com/a/6294196
returnreflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
23
+
returnreflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
24
24
Elem().
25
25
Interface()
26
26
}
@@ -31,7 +31,7 @@ func SetStructureField(field reflect.Value, value interface{}) {
31
31
if!field.IsValid() {
32
32
return
33
33
}
34
-
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
34
+
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
retry.Attempts(uint(retryPolicy.RetryMax)),//nolint:gosec // in normal use this will have had Validate() called which enforces that the minimum number of RetryMax is 0 so it won't overflow
the purpose of this utilities is to perform safe number conversion in go similarly to [go-safecast](https://github.com/ccoVeille/go-safecast) from which they are inspired from.
4
+
It should help tackling gosec [G115 rule](https://github.com/securego/gosec/pull/1149)
5
+
6
+
G115: Potential overflow when converting between integer types.
7
+
8
+
and [CWE-190](https://cwe.mitre.org/data/definitions/190.html)
9
+
10
+
11
+
infinite loop
12
+
access to wrong resource by id
13
+
grant access to someone who exhausted their quota
14
+
15
+
Contrary to `go-safecast` no error is returned when attempting casting and the MAX or MIN value of the type is returned instead if the value is beyond the allowed window.
16
+
For instance, `toInt8(255)-> 127` and `toInt8(-255)-> -128`
0 commit comments