| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"log/slog" |
| 6 |
"net" |
| 7 |
"net/netip" |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/slackhq/nebula/cert" |
| 12 |
"github.com/slackhq/nebula/test" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
func TestControl_GetHostInfoByVpnIp(t *testing.T) { |
| 18 |
//TODO: CERT-V2 with multiple certificate versions we have a problem with this test |
| 19 |
// Some certs versions have different characteristics and each version implements their own Copy() func |
| 20 |
// which means this is not a good place to test for exposing memory |
| 21 |
l := test.NewLogger() |
| 22 |
// Special care must be taken to re-use all objects provided to the hostmap and certificate in the expectedInfo object |
| 23 |
// To properly ensure we are not exposing core memory to the caller |
| 24 |
hm := newHostMap(l) |
| 25 |
hm.preferredRanges.Store(&[]netip.Prefix{}) |
| 26 |
|
| 27 |
remote1 := netip.MustParseAddrPort("0.0.0.100:4444") |
| 28 |
remote2 := netip.MustParseAddrPort("[1:2:3:4:5:6:7:8]:4444") |
| 29 |
|
| 30 |
ipNet := net.IPNet{ |
| 31 |
IP: remote1.Addr().AsSlice(), |
| 32 |
Mask: net.IPMask{255, 255, 255, 0}, |
| 33 |
} |
| 34 |
|
| 35 |
ipNet2 := net.IPNet{ |
| 36 |
IP: remote2.Addr().AsSlice(), |
| 37 |
Mask: net.IPMask{255, 255, 255, 0}, |
| 38 |
} |
| 39 |
|
| 40 |
remotes := NewRemoteList([]netip.Addr{netip.IPv4Unspecified()}, nil) |
| 41 |
remotes.unlockedPrependV4(netip.IPv4Unspecified(), netAddrToProtoV4AddrPort(remote1.Addr(), remote1.Port())) |
| 42 |
remotes.unlockedPrependV6(netip.IPv4Unspecified(), netAddrToProtoV6AddrPort(remote2.Addr(), remote2.Port())) |
| 43 |
|
| 44 |
vpnIp, ok := netip.AddrFromSlice(ipNet.IP) |
| 45 |
assert.True(t, ok) |
| 46 |
|
| 47 |
crt := &dummyCert{} |
| 48 |
hi := &HostInfo{ |
| 49 |
remotes: remotes, |
| 50 |
ConnectionState: &ConnectionState{ |
| 51 |
peerCert: &cert.CachedCertificate{Certificate: crt}, |
| 52 |
}, |
| 53 |
remoteIndexId: 200, |
| 54 |
localIndexId: 201, |
| 55 |
vpnAddrs: []netip.Addr{vpnIp}, |
| 56 |
relayState: RelayState{ |
| 57 |
relays: nil, |
| 58 |
relayForByAddr: map[netip.Addr]*Relay{}, |
| 59 |
relayForByIdx: map[uint32]*Relay{}, |
| 60 |
}, |
| 61 |
} |
| 62 |
hi.remote.Store(&remote1) |
| 63 |
hm.unlockedAddHostInfo(hi, &Interface{}) |
| 64 |
|
| 65 |
vpnIp2, ok := netip.AddrFromSlice(ipNet2.IP) |
| 66 |
assert.True(t, ok) |
| 67 |
|
| 68 |
hi2 := &HostInfo{ |
| 69 |
remotes: remotes, |
| 70 |
ConnectionState: &ConnectionState{ |
| 71 |
peerCert: nil, |
| 72 |
}, |
| 73 |
remoteIndexId: 200, |
| 74 |
localIndexId: 201, |
| 75 |
vpnAddrs: []netip.Addr{vpnIp2}, |
| 76 |
relayState: RelayState{ |
| 77 |
relays: nil, |
| 78 |
relayForByAddr: map[netip.Addr]*Relay{}, |
| 79 |
relayForByIdx: map[uint32]*Relay{}, |
| 80 |
}, |
| 81 |
} |
| 82 |
hi2.remote.Store(&remote1) |
| 83 |
hm.unlockedAddHostInfo(hi2, &Interface{}) |
| 84 |
|
| 85 |
c := Control{ |
| 86 |
state: StateReady, |
| 87 |
f: &Interface{ |
| 88 |
hostMap: hm, |
| 89 |
}, |
| 90 |
l: test.NewLogger(), |
| 91 |
} |
| 92 |
|
| 93 |
thi := c.GetHostInfoByVpnAddr(vpnIp, false) |
| 94 |
|
| 95 |
expectedInfo := ControlHostInfo{ |
| 96 |
VpnAddrs: []netip.Addr{vpnIp}, |
| 97 |
LocalIndex: 201, |
| 98 |
RemoteIndex: 200, |
| 99 |
RemoteAddrs: []netip.AddrPort{remote2, remote1}, |
| 100 |
Cert: crt.Copy(), |
| 101 |
MessageCounter: 0, |
| 102 |
CurrentRemote: remote1, |
| 103 |
CurrentRelaysToMe: []netip.Addr{}, |
| 104 |
CurrentRelaysThroughMe: []netip.Addr{}, |
| 105 |
} |
| 106 |
|
| 107 |
// Make sure we don't have any unexpected fields |
| 108 |
assertFields(t, []string{"VpnAddrs", "LocalIndex", "RemoteIndex", "RemoteAddrs", "Cert", "MessageCounter", "CurrentRemote", "CurrentRelaysToMe", "CurrentRelaysThroughMe"}, thi) |
| 109 |
assert.Equal(t, &expectedInfo, thi) |
| 110 |
test.AssertDeepCopyEqual(t, &expectedInfo, thi) |
| 111 |
|
| 112 |
// Make sure we don't panic if the host info doesn't have a cert yet |
| 113 |
assert.NotPanics(t, func() { |
| 114 |
thi = c.GetHostInfoByVpnAddr(vpnIp2, false) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func assertFields(t *testing.T, expected []string, actualStruct any) { |
| 119 |
val := reflect.ValueOf(actualStruct).Elem() |
| 120 |
fields := make([]string, val.NumField()) |
| 121 |
for i := 0; i < val.NumField(); i++ { |
| 122 |
fields[i] = val.Type().Field(i).Name |
| 123 |
} |
| 124 |
|
| 125 |
assert.Equal(t, expected, fields) |
| 126 |
} |
| 127 |
|
| 128 |
// alwaysAllowV4/V6 are check funcs that accept every entry (including nil pointers), |
| 129 |
// letting us inject a nil *V4AddrPort/*V6AddrPort into a RemoteList's reported cache |
| 130 |
// the same way a malformed proto message off the wire could. |
| 131 |
func alwaysAllowV4(netip.Addr, *V4AddrPort) bool { return true } |
| 132 |
func alwaysAllowV6(netip.Addr, *V6AddrPort) bool { return true } |
| 133 |
|
| 134 |
// TestGetRelays_SkipsNilRelayAddrs proves GetRelays tolerates nil entries in the |
| 135 |
// RelayVpnAddrs proto slice (which protoAddrToNetAddr would nil-deref on) and still |
| 136 |
// returns the valid relays, including the legacy OldRelayVpnAddrs. |
| 137 |
func TestGetRelays_SkipsNilRelayAddrs(t *testing.T) { |
| 138 |
good := netip.MustParseAddr("10.0.0.9") |
| 139 |
|
| 140 |
d := &NebulaMetaDetails{ |
| 141 |
OldRelayVpnAddrs: []uint32{0x0a000001}, // 10.0.0.1 |
| 142 |
RelayVpnAddrs: []*Addr{ |
| 143 |
nil, |
| 144 |
netAddrToProtoAddr(good), |
| 145 |
nil, |
| 146 |
}, |
| 147 |
} |
| 148 |
|
| 149 |
var relays []netip.Addr |
| 150 |
require.NotPanics(t, func() { relays = d.GetRelays() }) |
| 151 |
|
| 152 |
assert.Equal(t, []netip.Addr{ |
| 153 |
netip.MustParseAddr("10.0.0.1"), |
| 154 |
good, |
| 155 |
}, relays) |
| 156 |
} |
| 157 |
|
| 158 |
// TestGetRelays_AllNil ensures an all-nil RelayVpnAddrs slice yields no relays and no panic. |
| 159 |
func TestGetRelays_AllNil(t *testing.T) { |
| 160 |
d := &NebulaMetaDetails{RelayVpnAddrs: []*Addr{nil, nil}} |
| 161 |
var relays []netip.Addr |
| 162 |
require.NotPanics(t, func() { relays = d.GetRelays() }) |
| 163 |
assert.Empty(t, relays) |
| 164 |
} |
| 165 |
|
| 166 |
// TestRemoteList_CopyCache_SkipsNilReported proves CopyCache skips nil reported |
| 167 |
// pointers (v4 and v6) instead of nil-dereferencing them in protoV*AddrPortToNetAddrPort. |
| 168 |
func TestRemoteList_CopyCache_SkipsNilReported(t *testing.T) { |
| 169 |
owner := netip.MustParseAddr("10.0.0.1") |
| 170 |
rl := NewRemoteList([]netip.Addr{owner}, nil) |
| 171 |
|
| 172 |
rl.unlockedSetV4(owner, owner, []*V4AddrPort{ |
| 173 |
nil, |
| 174 |
newIp4AndPortFromString("1.2.3.4:5"), |
| 175 |
nil, |
| 176 |
}, alwaysAllowV4) |
| 177 |
|
| 178 |
rl.unlockedSetV6(owner, owner, []*V6AddrPort{ |
| 179 |
nil, |
| 180 |
newIp6AndPortFromString("[1::1]:6"), |
| 181 |
nil, |
| 182 |
}, alwaysAllowV6) |
| 183 |
|
| 184 |
var cm *CacheMap |
| 185 |
require.NotPanics(t, func() { cm = rl.CopyCache() }) |
| 186 |
|
| 187 |
c := (*cm)[owner.String()] |
| 188 |
require.NotNil(t, c) |
| 189 |
assert.ElementsMatch(t, []netip.AddrPort{ |
| 190 |
netip.MustParseAddrPort("1.2.3.4:5"), |
| 191 |
netip.MustParseAddrPort("[1::1]:6"), |
| 192 |
}, c.Reported) |
| 193 |
} |
| 194 |
|
| 195 |
// TestRemoteList_Rebuild_SkipsNilReported drives unlockedCollect (via Rebuild) with |
| 196 |
// nil reported entries and confirms only the valid addresses survive, with no panic. |
| 197 |
func TestRemoteList_Rebuild_SkipsNilReported(t *testing.T) { |
| 198 |
owner := netip.MustParseAddr("10.0.0.1") |
| 199 |
rl := NewRemoteList([]netip.Addr{owner}, nil) |
| 200 |
|
| 201 |
rl.unlockedSetV4(owner, owner, []*V4AddrPort{ |
| 202 |
nil, |
| 203 |
newIp4AndPortFromString("1.2.3.4:5"), |
| 204 |
}, alwaysAllowV4) |
| 205 |
rl.unlockedSetV6(owner, owner, []*V6AddrPort{ |
| 206 |
newIp6AndPortFromString("[1::1]:6"), |
| 207 |
nil, |
| 208 |
}, alwaysAllowV6) |
| 209 |
|
| 210 |
require.NotPanics(t, func() { rl.Rebuild([]netip.Prefix{}) }) |
| 211 |
|
| 212 |
assert.ElementsMatch(t, []netip.AddrPort{ |
| 213 |
netip.MustParseAddrPort("1.2.3.4:5"), |
| 214 |
netip.MustParseAddrPort("[1::1]:6"), |
| 215 |
}, rl.addrs) |
| 216 |
} |
| 217 |
|
| 218 |
// newRelayControl marshals a NebulaControl the way it arrives on the wire so we can feed |
| 219 |
// it through HandleControlMsg's unmarshal + validate path. |
| 220 |
func newRelayControl(t *testing.T, typ NebulaControl_MessageType, from, to *Addr) []byte { |
| 221 |
t.Helper() |
| 222 |
msg := &NebulaControl{ |
| 223 |
Type: typ, |
| 224 |
RelayFromAddr: from, |
| 225 |
RelayToAddr: to, |
| 226 |
} |
| 227 |
b, err := msg.Marshal() |
| 228 |
require.NoError(t, err) |
| 229 |
return b |
| 230 |
} |
| 231 |
|
| 232 |
// TestRelayManager_HandleControlMsg_NilRelayAddrs verifies the validation block added to |
| 233 |
// HandleControlMsg: CreateRelay{Request,Response} carrying a nil RelayFromAddr or |
| 234 |
// RelayToAddr are dropped with a debug log rather than nil-dereferencing downstream. |
| 235 |
func TestRelayManager_HandleControlMsg_NilRelayAddrs(t *testing.T) { |
| 236 |
good := netAddrToProtoAddr(netip.MustParseAddr("10.0.0.9")) |
| 237 |
|
| 238 |
cases := []struct { |
| 239 |
name string |
| 240 |
typ NebulaControl_MessageType |
| 241 |
from *Addr |
| 242 |
to *Addr |
| 243 |
wantLog string // debug substring expected, "" == expect no drop log |
| 244 |
}{ |
| 245 |
{"request nil from", NebulaControl_CreateRelayRequest, nil, good, "nil RelayFromAddr"}, |
| 246 |
{"request nil to", NebulaControl_CreateRelayRequest, good, nil, "nil RelayToAddr"}, |
| 247 |
{"request both nil", NebulaControl_CreateRelayRequest, nil, nil, "nil RelayFromAddr"}, |
| 248 |
{"response nil from", NebulaControl_CreateRelayResponse, nil, good, "nil RelayFromAddr"}, |
| 249 |
{"response nil to", NebulaControl_CreateRelayResponse, good, nil, "nil RelayToAddr"}, |
| 250 |
// A non-relay control type is not subject to the relay-addr validation and must |
| 251 |
// pass through it untouched (the final switch simply no-ops on it). |
| 252 |
{"unrelated type nil addrs", NebulaControl_None, nil, nil, ""}, |
| 253 |
} |
| 254 |
|
| 255 |
for _, tc := range cases { |
| 256 |
t.Run(tc.name, func(t *testing.T) { |
| 257 |
var buf bytes.Buffer |
| 258 |
l := test.NewLoggerWithOutputAndLevel(&buf, slog.LevelDebug) |
| 259 |
rm := &relayManager{l: l, hostmap: newHostMap(l)} |
| 260 |
rm.useRelays.Store(true) |
| 261 |
|
| 262 |
f := &Interface{l: l} |
| 263 |
h := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("10.0.0.2")}, localIndexId: 1} |
| 264 |
|
| 265 |
d := newRelayControl(t, tc.typ, tc.from, tc.to) |
| 266 |
|
| 267 |
require.NotPanics(t, func() { rm.HandleControlMsg(h, d, f) }) |
| 268 |
|
| 269 |
if tc.wantLog == "" { |
| 270 |
assert.NotContains(t, buf.String(), "nil Relay") |
| 271 |
} else { |
| 272 |
assert.Contains(t, buf.String(), tc.wantLog) |
| 273 |
} |
| 274 |
}) |
| 275 |
} |
| 276 |
} |