Skip to content

Commit

Permalink
resolved: packet - ensure there is space for IP+UDP headers
Browse files Browse the repository at this point in the history
Currently we only make sure our links can handle the size of the payload witohut
taking the headers into account.
  • Loading branch information
teg committed Jul 14, 2015
1 parent ff89f8b commit a016660
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/resolve/resolved-dns-packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ int dns_packet_new(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {

assert(ret);

if (mtu <= 0)
if (mtu <= UDP_PACKET_HEADER_SIZE)
a = DNS_PACKET_SIZE_START;
else
a = mtu;
a = mtu - UDP_PACKET_HEADER_SIZE;

if (a < DNS_PACKET_HEADER_SIZE)
a = DNS_PACKET_HEADER_SIZE;
Expand Down
3 changes: 3 additions & 0 deletions src/resolve/resolved-dns-packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <netinet/udp.h>
#include <netinet/ip.h>

#include "macro.h"
#include "sparse-endian.h"
Expand Down Expand Up @@ -53,6 +55,7 @@ struct DnsPacketHeader {
};

#define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
#define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))

/* The various DNS protocols deviate in how large a packet can grow,
but the TCP transport has a 16bit size field, hence that appears to
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/resolved-dns-scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int dns_scope_emit(DnsScope *s, DnsPacket *p) {
if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
return -EMSGSIZE;

if (p->size > mtu)
if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
return -EMSGSIZE;

if (family == AF_INET)
Expand Down

0 comments on commit a016660

Please sign in to comment.