Skip to content

Commit 2189376

Browse files
committed
dhcp: clone hardware addresses throughout
I still noticed DHCP requests with incorrect MAC source addresses. Turns out there were still a number of incorrect HardwareAddr usages.
1 parent e2d4de4 commit 2189376

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

cmd/dhcp4/dhcp4.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os"
2929
"os/signal"
3030
"path/filepath"
31+
"slices"
3132
"syscall"
3233
"time"
3334

@@ -97,7 +98,8 @@ func logic() error {
9798
if err != nil {
9899
return err
99100
}
100-
hwaddr := iface.HardwareAddr
101+
// Clone the hardware address as the backing array does not remain valid.
102+
hwaddr := slices.Clone(iface.HardwareAddr)
101103
// The interface may not have been configured by netconfigd yet and might
102104
// still use the old hardware address. We overwrite it with the address that
103105
// netconfigd is going to use to fix this issue without additional

internal/dhcp4/dhcp4.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func (c *Client) ObtainOrRenew() bool {
103103
return
104104
}
105105
if c.hardwareAddr == nil && c.HWAddr != nil {
106-
c.hardwareAddr = c.HWAddr
106+
// Clone the hardware address as the backing array does not remain valid.
107+
c.hardwareAddr = slices.Clone(c.HWAddr)
107108
}
108109
if c.hardwareAddr == nil {
109110
// Defensive slices.Clone because I noticed c.hardwareAddr

internal/dhcp6/dhcp6.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"log"
2121
"net"
22+
"slices"
2223
"strconv"
2324
"time"
2425

@@ -115,9 +116,10 @@ func NewClient(cfg ClientConfig) (*Client, error) {
115116
}
116117
}
117118

118-
hardwareAddr := iface.HardwareAddr
119+
// Clone the hardware address as the backing array does not remain valid.
120+
hardwareAddr := slices.Clone(iface.HardwareAddr)
119121
if cfg.HardwareAddr != nil {
120-
hardwareAddr = cfg.HardwareAddr
122+
hardwareAddr = slices.Clone(cfg.HardwareAddr)
121123
}
122124

123125
var duid *dhcpv6.Duid

0 commit comments

Comments
 (0)