Skip to content

Commit 463c304

Browse files
authored
cni: prevent NPE if no interface has sandbox field set
When we iterate over the interfaces returned from CNI setup, we filter for one with the `Sandbox` field set. Ensure that if none of the interfaces has that field set that we still return an available interface.
1 parent 9a112af commit 463c304

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ IMPROVEMENTS:
77
BUG FIXES:
88
* core: Fixed a bug where ACLToken and ACLPolicy changes were ignored by the event stream [[GH-9595](https://github.com/hashicorp/nomad/issues/9595)]
99
* core: Fixed a bug to honor HCL2 variables set by environment variables or variable files [[GH-9592](https://github.com/hashicorp/nomad/issues/9592)] [[GH-9623](https://github.com/hashicorp/nomad/issues/9623)]
10+
* cni: Fixed a bug where plugins that do not set the interface sandbox value could crash the Nomad client. [[GH-9648](https://github.com/hashicorp/nomad/issues/9648)]
1011

1112
## 1.0.0 (December 8, 2020)
1213

client/allocrunner/networking_cni.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,20 @@ func (c *cniNetworkConfigurator) Setup(ctx context.Context, alloc *structs.Alloc
114114
netStatus := new(structs.AllocNetworkStatus)
115115

116116
if len(res.Interfaces) > 0 {
117-
iface, name := func(r *cni.CNIResult) (*cni.Config, string) {
118-
for i := range r.Interfaces {
119-
if r.Interfaces[i].Sandbox != "" {
120-
return r.Interfaces[i], i
121-
}
117+
// find an interface with Sandbox set, or any one of them if no
118+
// interface has it set
119+
var iface *cni.Config
120+
var name string
121+
for name, iface = range res.Interfaces {
122+
if iface != nil && iface.Sandbox != "" {
123+
break
122124
}
123-
return nil, ""
124-
}(res)
125+
}
126+
if iface == nil {
127+
// this should never happen but this value is coming from external
128+
// plugins so we should guard against it
129+
return nil, fmt.Errorf("failed to configure network: no valid interface")
130+
}
125131

126132
netStatus.InterfaceName = name
127133
if len(iface.IPConfigs) > 0 {

0 commit comments

Comments
 (0)