Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address integer overflows when downcasting for G115 #145

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ func (r *securityGroupRule) Ports() []PortRange {
for _, portRangeStr := range portRangeStrs {
ports := strings.Split(portRangeStr, "-")
if len(ports) == 1 {
port, err := strconv.Atoi(strings.TrimSpace(ports[0]))
port, err := strconv.ParseUint(strings.TrimSpace(ports[0]), 10, 16)
if err != nil {
continue
}
// #nosec G115 - ParseUint above validates the int range here
portRanges = append(portRanges, PortRange{Start: uint16(port), End: uint16(port)})
} else if len(ports) == 2 {
startPort, err := strconv.Atoi(strings.TrimSpace(ports[0]))
startPort, err := strconv.ParseUint(strings.TrimSpace(ports[0]), 10, 16)
if err != nil {
continue
}
endPort, err := strconv.Atoi(strings.TrimSpace(ports[1]))
endPort, err := strconv.ParseUint(strings.TrimSpace(ports[1]), 10, 16)
if err != nil {
continue
}
// #nosec G115 - ParseUint above validates the int range here
portRanges = append(portRanges, PortRange{Start: uint16(startPort), End: uint16(endPort)})
}
}
Expand All @@ -84,7 +86,9 @@ func (r *securityGroupRule) Ports() []PortRange {

func (r *securityGroupRule) ICMPInfo() *ICMPInfo {
return &ICMPInfo{
// #nosec G115 - CAPI validates these as -1 to 255, and we make use of -1 as a short-hand for "all" types/codes (255)
Type: garden.ICMPType(r.rule.Type),
// #nosec G115 - CAPI validates these as -1 to 255, and we make use of -1 as a short-hand for "all" types/codes (255)
Code: garden.ICMPCode(r.rule.Code),
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/code.cloudfoundry.org/silk/controller/leaser/cidrpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ func NewCIDRPool(subnetRange string, subnetMask int) *CIDRPool {
}
cidrMask, _ := ipCIDR.Mask.Size()

if cidrMask > 32 || cidrMask < 0 {
panic(fmt.Errorf("subnet range's CIDR mask must be between [0-32]"))
}
if subnetMask > 32 || subnetMask < 0 {
panic(fmt.Errorf("subnet mask must be between [0-32]"))
}

return &CIDRPool{
blockPool: generateBlockPool(ipCIDR.IP, uint(cidrMask), uint(subnetMask)),
// #nosec - G115 - we check valid values above for IPv4 subnet masks
blockPool: generateBlockPool(ipCIDR.IP, uint(cidrMask), uint(subnetMask)),
// #nosec - G115 - we check valid values above for IPv4 subnet masks
singlePool: generateSingleIPPool(ipCIDR.IP, uint(subnetMask)),
}
}
Expand Down