Permalink
Browse files

Make dhcpv4 handler more robust in the face of malformed packets

Summary: The third party lib we use for dhcpv4 is not the best... apply some defensive coding.

Reviewed By: martelli

Differential Revision: D6347856

fbshipit-source-id: f7588aa95ba5ef16da1ee23c31bb4984ad66cd69
  • Loading branch information...
pallotron authored and facebook-github-bot committed Nov 16, 2017
1 parent ba01cee commit b4d600cacbf7663efba5f902c2b956366e596b14
Showing with 17 additions and 6 deletions.
  1. +7 −5 glog_logger.go
  2. +10 −1 lib/handler.go
View
@@ -46,11 +46,13 @@ func (l glogLogger) Log(msg dhcplb.LogMessage) error {
if msg.Packet != nil {
if msg.Version == 4 {
packet := dhcp4.Packet(msg.Packet)
t := dhcp4.MessageType(
packet.ParseOptions()[dhcp4.OptionDHCPMessageType][0])
sample["type"] = t.String()
sample["xid"] = fmt.Sprintf("%#06x", packet.XId())
sample["giaddr"] = packet.GIAddr().String()
opts := packet.ParseOptions()
if len(opts) > 0 {
t := dhcp4.MessageType(opts[dhcp4.OptionDHCPMessageType][0])
sample["type"] = t.String()
sample["xid"] = fmt.Sprintf("%#06x", packet.XId())
sample["giaddr"] = packet.GIAddr().String()
}
sample["client_mac"] = packet.CHAddr().String()
} else if msg.Version == 6 {
packet := dhcplb.Packet6(msg.Packet)
View
@@ -12,6 +12,7 @@ package dhcplb
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"github.com/golang/glog"
"github.com/krolaw/dhcp4"
@@ -208,7 +209,15 @@ func handleRawPacketV4(logger loggerHelper, config *Config, buffer []byte, peer
message.Peer = peer
message.ClientID = packet.CHAddr()
message.Mac = packet.CHAddr()
t := dhcp4.MessageType(packet.ParseOptions()[dhcp4.OptionDHCPMessageType][0])
opts := packet.ParseOptions()
if len(opts) == 0 {
msg := fmt.Sprintf("Failed to parse options for packet coming from peer %s.", peer.String())
glog.Errorf(msg)
logger.LogErr(start, nil, packet, peer, ErrParse, errors.New(msg))
return
}
t := dhcp4.MessageType(opts[dhcp4.OptionDHCPMessageType][0])
packet.SetHops(packet.Hops() + 1)
server, err := selectDestinationServer(config, &message)

0 comments on commit b4d600c

Please sign in to comment.