Skip to content

Commit 7d4c949

Browse files
geofffranksameowlia
authored andcommitted
Convert PortAllocator's ints to uint32 to avoid int overflows when picking ports
1 parent 240c742 commit 7d4c949

File tree

10 files changed

+73
-73
lines changed

10 files changed

+73
-73
lines changed

src/code.cloudfoundry.org/garden-external-networker/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Config struct {
1111
CniConfigDir string `json:"cni_config_dir"`
1212
BindMountDir string `json:"bind_mount_dir"`
1313
StateFilePath string `json:"state_file"`
14-
StartPort int `json:"start_port"`
15-
TotalPorts int `json:"total_ports"`
14+
StartPort uint32 `json:"start_port"`
15+
TotalPorts uint32 `json:"total_ports"`
1616
LogPrefix string `json:"log_prefix"`
1717
SearchDomains []string `json:"search_domains"`
1818
IPTablesLockFile string `json:"iptables_lock_file"`

src/code.cloudfoundry.org/garden-external-networker/config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ var _ = Describe("Config", func() {
5050
Expect(c.CniConfigDir).To(Equal("bar"))
5151
Expect(c.BindMountDir).To(Equal("baz"))
5252
Expect(c.StateFilePath).To(Equal("some/path"))
53-
Expect(c.StartPort).To(Equal(1234))
54-
Expect(c.TotalPorts).To(Equal(56))
53+
Expect(c.StartPort).To(Equal(uint32(1234)))
54+
Expect(c.TotalPorts).To(Equal(uint32(56)))
5555
Expect(c.LogPrefix).To(Equal("prefix"))
5656
Expect(c.SearchDomains).Should(ConsistOf("pivotal.io", "foo.bar", "baz.me"))
5757
Expect(c.IPTablesLockFile).To(Equal("some-lock-file"))

src/code.cloudfoundry.org/garden-external-networker/fakes/portAllocator.go

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/code.cloudfoundry.org/garden-external-networker/fakes/tracker.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/code.cloudfoundry.org/garden-external-networker/manager/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type mounter interface {
3131

3232
//go:generate counterfeiter -o ../fakes/portAllocator.go --fake-name PortAllocator . portAllocator
3333
type portAllocator interface {
34-
AllocatePort(handle string, port int) (int, error)
34+
AllocatePort(handle string, port uint32) (uint32, error)
3535
ReleaseAllPorts(handle string) error
3636
}
3737

@@ -82,11 +82,11 @@ func (m *Manager) Up(containerHandle string, inputs UpInputs) (*UpOutputs, error
8282
mappedPorts := []garden.PortMapping{}
8383
for i := range inputs.NetIn {
8484
if inputs.NetIn[i].HostPort == 0 {
85-
hostPort, err := m.PortAllocator.AllocatePort(containerHandle, int(inputs.NetIn[i].HostPort))
85+
hostPort, err := m.PortAllocator.AllocatePort(containerHandle, inputs.NetIn[i].HostPort)
8686
if err != nil {
8787
return nil, fmt.Errorf("allocating port: %s", err)
8888
}
89-
inputs.NetIn[i].HostPort = uint32(hostPort)
89+
inputs.NetIn[i].HostPort = hostPort
9090
}
9191

9292
mappedPorts = append(mappedPorts, garden.PortMapping{

src/code.cloudfoundry.org/garden-external-networker/manager/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ var _ = Describe("Manager", func() {
276276
Expect(portAllocator.AllocatePortCallCount()).To(Equal(1))
277277
handle, port := portAllocator.AllocatePortArgsForCall(0)
278278
Expect(handle).To(Equal("some-container-handle"))
279-
Expect(port).To(Equal(0))
279+
Expect(port).To(Equal(uint32(0)))
280280

281281
Expect(cniController.UpCallCount()).To(Equal(1))
282282
_, handle, _, legacyNetConf := cniController.UpArgsForCall(0)

src/code.cloudfoundry.org/garden-external-networker/port_allocator/pool.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
var ErrorPortPoolExhausted = errors.New("port pool exhausted")
99

1010
type Pool struct {
11-
AcquiredPorts map[int]string
11+
AcquiredPorts map[uint32]string
1212
}
1313

1414
func (p *Pool) MarshalJSON() ([]byte, error) {
1515
var jsonData struct {
16-
AcquiredPorts map[string][]int `json:"acquired_ports"`
16+
AcquiredPorts map[string][]uint32 `json:"acquired_ports"`
1717
}
18-
jsonData.AcquiredPorts = make(map[string][]int)
18+
jsonData.AcquiredPorts = make(map[string][]uint32)
1919

2020
for port, handle := range p.AcquiredPorts {
2121
jsonData.AcquiredPorts[handle] = append(jsonData.AcquiredPorts[handle], port)
@@ -25,14 +25,14 @@ func (p *Pool) MarshalJSON() ([]byte, error) {
2525

2626
func (p *Pool) UnmarshalJSON(bytes []byte) error {
2727
var jsonData struct {
28-
AcquiredPorts map[string][]int `json:"acquired_ports"`
28+
AcquiredPorts map[string][]uint32 `json:"acquired_ports"`
2929
}
3030
err := json.Unmarshal(bytes, &jsonData)
3131
if err != nil {
3232
return err
3333
}
3434

35-
p.AcquiredPorts = make(map[int]string)
35+
p.AcquiredPorts = make(map[uint32]string)
3636
for handle, ports := range jsonData.AcquiredPorts {
3737
for _, port := range ports {
3838
p.AcquiredPorts[port] = handle
@@ -42,27 +42,27 @@ func (p *Pool) UnmarshalJSON(bytes []byte) error {
4242
}
4343

4444
type Tracker struct {
45-
StartPort int
46-
Capacity int
45+
StartPort uint32
46+
Capacity uint32
4747
}
4848

49-
func (t *Tracker) InRange(port int) bool {
49+
func (t *Tracker) InRange(port uint32) bool {
5050
return port >= t.StartPort && port < t.StartPort+t.Capacity
5151
}
5252

53-
func (t *Tracker) AcquireOne(pool *Pool, handler string) (int, error) {
53+
func (t *Tracker) AcquireOne(pool *Pool, handler string) (uint32, error) {
5454
if pool.AcquiredPorts == nil {
55-
pool.AcquiredPorts = make(map[int]string)
55+
pool.AcquiredPorts = make(map[uint32]string)
5656
}
5757

58-
for i := 0; i < t.Capacity; i++ {
58+
for i := uint32(0); i < t.Capacity; i++ {
5959
candidatePort := t.StartPort + i
6060
if !contains(pool.AcquiredPorts, candidatePort) {
6161
pool.AcquiredPorts[candidatePort] = handler
6262
return candidatePort, nil
6363
}
6464
}
65-
return -1, ErrorPortPoolExhausted
65+
return 0, ErrorPortPoolExhausted
6666
}
6767

6868
func (t *Tracker) ReleaseAll(pool *Pool, handle string) error {
@@ -74,7 +74,7 @@ func (t *Tracker) ReleaseAll(pool *Pool, handle string) error {
7474
return nil
7575
}
7676

77-
func contains(list map[int]string, candidate int) bool {
77+
func contains(list map[uint32]string, candidate uint32) bool {
7878
_, ok := list[candidate]
7979
return ok
8080
}

0 commit comments

Comments
 (0)