dhcp4d: ensure that SetHostname operates on the correct lease (#64)

Previously SetHostname could operate on an expired lease, or even on a
lease for a different hwaddr, if the lease for the correct hwaddr
expired and the same lease ID was given away to someone else.

That's though mostly a theoretical concern, given the actual usage of
SetHostname and the time scales involved.
This commit is contained in:
Robert Obryk 2020-11-23 09:32:42 +01:00 committed by GitHub
parent 7f135438b8
commit 0507d93b3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -279,7 +279,10 @@ func newSrv(permDir string) (*srv, error) {
http.Error(w, "missing hostname parameter", http.StatusBadRequest)
return
}
handler.SetHostname(hwaddr, hostname)
if err := handler.SetHostname(hwaddr, hostname); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.Redirect(w, r, "/", http.StatusFound)
})

View File

@ -18,6 +18,7 @@ package dhcp4d
import (
"bytes"
"encoding/hex"
"fmt"
"log"
"math/rand"
"net"
@ -137,14 +138,18 @@ func (h *Handler) callLeasesLocked(lease *Lease) {
h.Leases(leases, lease)
}
func (h *Handler) SetHostname(hwaddr, hostname string) {
func (h *Handler) SetHostname(hwaddr, hostname string) error {
h.leasesMu.Lock()
defer h.leasesMu.Unlock()
leaseNum := h.leasesHW[hwaddr]
lease := h.leasesIP[leaseNum]
if lease.HardwareAddr != hwaddr || lease.Expired(h.timeNow()) {
return fmt.Errorf("hwaddr %v does not have a valid lease", hwaddr)
}
lease.Hostname = hostname
lease.HostnameOverride = hostname
h.callLeasesLocked(lease)
return nil
}
func (h *Handler) findLease() int {