public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: resolver: A few fixes for cygwin_query()
Date: Wed, 19 Jan 2022 07:55:37 +0000 (GMT)	[thread overview]
Message-ID: <20220119075537.EBEF2385780E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=12bf36f72b7e6019a3adb60d86e65262f43a9311

commit 12bf36f72b7e6019a3adb60d86e65262f43a9311
Author: Anton Lavrentiev via Cygwin-patches <cygwin-patches@cygwin.com>
Date:   Tue Jan 18 16:34:34 2022 -0500

    Cygwin: resolver: A few fixes for cygwin_query()
    
    - Make sure the answer buffer is properly cleared so there is no trailing
    garbage when the response does not fit entirely in;
    - Make sure an internal decomp failure gets reported correctly (w/return code -1);
    - Make sure that the buffer is not overrun when filling out the header.

Diff:
---
 winsup/cygwin/libc/minires-os-if.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/winsup/cygwin/libc/minires-os-if.c b/winsup/cygwin/libc/minires-os-if.c
index bb6786f6c..c4183db9c 100644
--- a/winsup/cygwin/libc/minires-os-if.c
+++ b/winsup/cygwin/libc/minires-os-if.c
@@ -193,6 +193,8 @@ static int cygwin_query(res_state statp, const char * DomName, int Class, int Ty
   dnptrs[0] = AnsPtr;
   dnptrs[1] = NULL;
 
+  memset(AnsPtr, 0, AnsLength);
+
   if (Class != ns_c_in) {
     errno = ENOSYS;
     statp->res_h_errno = NETDB_INTERNAL;
@@ -214,7 +216,7 @@ static int cygwin_query(res_state statp, const char * DomName, int Class, int Ty
     switch (res) {
     case ERROR_INVALID_NAME:
       errno = EINVAL;
-      statp->res_h_errno = NETDB_INTERNAL;;
+      statp->res_h_errno = NETDB_INTERNAL;
       break;
     case ERROR_TIMEOUT:
       statp->res_h_errno = TRY_AGAIN;
@@ -259,8 +261,9 @@ static int cygwin_query(res_state statp, const char * DomName, int Class, int Ty
       /* No question. Adopt the first name as the name in the question */
       if ((len = dn_comp(rr->pName, ptr, AnsLength - 4,
 			 dnptrs, &dnptrs[DIM(dnptrs) - 1])) < 0) {
-	ptr = NULL;
-	break;
+	statp->res_h_errno = NETDB_INTERNAL;  /* dn_comp sets errno */
+	len = -1;
+	goto done;
       }
       ptr += len;
       PUTSHORT(Type, ptr);
@@ -289,11 +292,13 @@ static int cygwin_query(res_state statp, const char * DomName, int Class, int Ty
 
   len = ptr - AnsPtr;
 done:
-  ptr = AnsPtr;
-  PUTSHORT(0, ptr); /* Id */
-  PUTSHORT((QR << 8) + RA + RD, ptr);
-  for (section = 0; section < DIM(counts); section++) {
-    PUTSHORT(counts[section], ptr);
+  if (HFIXEDSZ <= AnsLength) {
+    ptr = AnsPtr;
+    PUTSHORT(0, ptr); /* Id */
+    PUTSHORT((QR << 8) + RA + RD, ptr);
+    for (section = 0; section < DIM(counts); section++) {
+      PUTSHORT(counts[section], ptr);
+    }
   }
   return len;
 }


                 reply	other threads:[~2022-01-19  7:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220119075537.EBEF2385780E@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).