ping: check invalid ip header

This commit is contained in:
世界
2025-08-25 00:16:56 +08:00
parent 58f331b49e
commit fe4e54bb0d

View File

@@ -159,6 +159,9 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
}
if !c.destination.Is6() {
ipHdr := header.IPv4(buffer.Bytes())
if !ipHdr.IsValid(buffer.Len()) {
return E.New("invalid IPv4 header received")
}
ipHdr.SetDestinationAddr(c.source.Load())
ipHdr.SetChecksum(0)
ipHdr.SetChecksum(^ipHdr.CalculateChecksum())
@@ -168,6 +171,9 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
c.logger.TraceContext(c.ctx, "read icmpv4 echo reply from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr())
} else {
ipHdr := header.IPv6(buffer.Bytes())
if !ipHdr.IsValid(buffer.Len()) {
return E.New("invalid IPv6 header received")
}
ipHdr.SetDestinationAddr(c.source.Load())
icmpHdr := header.ICMPv6(ipHdr.Payload())
icmpHdr.SetChecksum(0)