mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alexey Kodanev <aleksei.kodanev@bell-sw.com>
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] dns: check length field in tcp response message
Date: Wed, 22 Mar 2023 16:48:00 +0300	[thread overview]
Message-ID: <4c0cc3b7-7b2b-f9c3-6a1a-6bb44c28d71d@bell-sw.com> (raw)
In-Reply-To: <20230322131651.GW4163@brightrain.aerifal.cx>

Hi Rich,
On 22.03.2023 16:16, Rich Felker wrote:
> On Wed, Mar 22, 2023 at 03:29:16PM +0300, Alexey Kodanev wrote:
>> The received length field in the message may be greater than the
>> size of the 'answer' buffer in which the message resides. Currently,
>> ABUF_SIZE is 768. And if we get a larger 'alen', it will result
>> in an out-of-bounds reading during parsing, because 'alen' will
>> be passed to __dns_parse() later:
>>
>>     __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
>>
>> To fix this, limit 'alen' to the size of the received buffer.
>> ---
>>  src/network/res_msend.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/src/network/res_msend.c b/src/network/res_msend.c
>> index fef7e3a2..291853de 100644
>> --- a/src/network/res_msend.c
>> +++ b/src/network/res_msend.c
>> @@ -297,6 +297,7 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
>>  			int rcode = answers[i][3] & 15;
>>  			if (rcode != 0 && rcode != 3)
>>  				goto out;
>> +			if (alen > asize) alen = asize;
>>  
>>  			/* Storing the length here commits the accepted answer.
>>  			 * Immediately close TCP socket so as not to consume
>> -- 
>> 2.25.1
> 
> This is incorrect. It breaks res_send, whose contract is to return the
> full answer length even if it did not fit, so that the caller can
> retry with the appropriate size.
> 
> Instead, name_from_dns just needs to clamp the value before passing it
> to __dns_parse.
> 

OK, I see, something like this or better with sizeof *abuf?

diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c
index 5f6867cb..65b3e8fb 100644
--- a/src/network/lookup_name.c
+++ b/src/network/lookup_name.c
@@ -175,6 +175,7 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static
                if (alens[i] < 4 || (abuf[i][3] & 15) == 2) return EAI_AGAIN;
                if ((abuf[i][3] & 15) == 3) return 0;
                if ((abuf[i][3] & 15) != 0) return EAI_FAIL;
+               if (alens[i] > ABUF_SIZE) alens[i] = ABUF_SIZE;
        }

        for (i=nq-1; i>=0; i--) {

  reply	other threads:[~2023-03-22 13:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22 12:29 Alexey Kodanev
2023-03-22 13:16 ` Rich Felker
2023-03-22 13:48   ` Alexey Kodanev [this message]
2023-03-22 14:07     ` Rich Felker
2023-05-08 16:25   ` Alexey Izbyshev
2024-02-29 10:49     ` Alexey Izbyshev
2024-02-29 15:36       ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c0cc3b7-7b2b-f9c3-6a1a-6bb44c28d71d@bell-sw.com \
    --to=aleksei.kodanev@bell-sw.com \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).