0003-resolved-do-not-allocate-packets-with-minimum-size.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. From 88795538726a5bbfd9efc13d441cb05e1d7fc139 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
  3. Date: Tue, 27 Jun 2017 14:20:00 -0400
  4. Subject: [PATCH] resolved: do not allocate packets with minimum size
  5. dns_packet_new() is sometimes called with mtu == 0, and in that case we should
  6. allocate more than the absolute minimum (which is the dns packet header size),
  7. otherwise we have to resize immediately again after appending the first data to
  8. the packet.
  9. This partially reverts the previous commit.
  10. [Upstream commit: https://github.com/systemd/systemd/commit/88795538726a5bbfd9efc13d441cb05e1d7fc139]
  11. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  12. ---
  13. src/resolve/resolved-dns-packet.c | 12 +++++++++++-
  14. 1 file changed, 11 insertions(+), 1 deletion(-)
  15. diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c
  16. index 821b66e266..d1f0f760a4 100644
  17. --- a/src/resolve/resolved-dns-packet.c
  18. +++ b/src/resolve/resolved-dns-packet.c
  19. @@ -28,6 +28,9 @@
  20. #define EDNS0_OPT_DO (1<<15)
  21. +#define DNS_PACKET_SIZE_START 512
  22. +assert_cc(DNS_PACKET_SIZE_START > UDP_PACKET_HEADER_SIZE)
  23. +
  24. typedef struct DnsPacketRewinder {
  25. DnsPacket *packet;
  26. size_t saved_rindex;
  27. @@ -47,7 +50,14 @@ int dns_packet_new(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
  28. assert(ret);
  29. - a = MAX(mtu, DNS_PACKET_HEADER_SIZE);
  30. + /* When dns_packet_new() is called with mtu == 0, allocate more than the
  31. + * absolute minimum (which is the dns packet header size), to avoid
  32. + * resizing immediately again after appending the first data to the packet.
  33. + */
  34. + if (mtu < UDP_PACKET_HEADER_SIZE)
  35. + a = DNS_PACKET_SIZE_START;
  36. + else
  37. + a = MAX(mtu, DNS_PACKET_HEADER_SIZE);
  38. /* round up to next page size */
  39. a = PAGE_ALIGN(ALIGN(sizeof(DnsPacket)) + a) - ALIGN(sizeof(DnsPacket));