]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: put size limit in DnsAnswer size to UINT16_MAX
authorLennart Poettering <lennart@poettering.net>
Tue, 3 Nov 2020 17:31:03 +0000 (18:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 3 Nov 2020 19:35:04 +0000 (20:35 +0100)
The three answer sections can only carry up to UINT16_MAX entries, hence
put a hard upper limit on how far DnsAnswer can grow. The three count
fields in the DNS packet header are 16 bit only, hence the limit.

If code actually tries to add more than 64K RRs it will get ENOSPC with
this new checking.

And similar to DnsQuestion.

src/resolve/resolved-dns-answer.c
src/resolve/resolved-dns-question.c

index d6fba7ead0e79b0268067aab086dd52e6cd208d5..74d185fbd63f43b11943cc19b6e644559506646c 100644 (file)
@@ -11,6 +11,9 @@
 DnsAnswer *dns_answer_new(size_t n) {
         DnsAnswer *a;
 
+        if (n > UINT16_MAX) /* We can only place 64K RRs in an answer at max */
+                n = UINT16_MAX;
+
         a = malloc0(offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * n);
         if (!a)
                 return NULL;
@@ -624,12 +627,16 @@ int dns_answer_reserve(DnsAnswer **a, size_t n_free) {
                         return -EBUSY;
 
                 ns = (*a)->n_rrs + n_free;
+                if (ns > UINT16_MAX) /* Maximum number of RRs we can stick into a DNS packet section */
+                        ns = UINT16_MAX;
 
                 if ((*a)->n_allocated >= ns)
                         return 0;
 
                 /* Allocate more than we need */
                 ns *= 2;
+                if (ns > UINT16_MAX)
+                        ns = UINT16_MAX;
 
                 n = realloc(*a, offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * ns);
                 if (!n)
index 62833efa0e1f787b7b1083dfd479aac90caa3c54..809965a8454c2bbacec5eac6f707fa1ae1ce4715 100644 (file)
@@ -8,7 +8,8 @@
 DnsQuestion *dns_question_new(size_t n) {
         DnsQuestion *q;
 
-        assert(n > 0);
+        if (n > UINT16_MAX) /* We can only place 64K key in an question section at max */
+                n = UINT16_MAX;
 
         q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
         if (!q)