Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw38G-0007RD-VY for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jun 2021 13:41:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lw38F-0002kA-Q4 for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jun 2021 13:41:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw38F-0002k2-1p for pgsql-bugs@lists.postgresql.org; Wed, 23 Jun 2021 13:41:31 +0000 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lw38B-0007KC-Ow for pgsql-bugs@lists.postgresql.org; Wed, 23 Jun 2021 13:41:29 +0000 Received: by mail-pj1-x102c.google.com with SMTP id m15-20020a17090a5a4fb029016f385ffad0so1441524pji.0 for ; Wed, 23 Jun 2021 06:41:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tekenlight.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=IcKpbNkpFMj867eXppd8aTJ3bI8zTQqEs4WlJsDxDwo=; b=Z8r7b9d/lnnt8+DVeh1sr0omCTo3UldP+8v7r71XmPz4sALbHppW5KK697ajufNRT1 lCc6allb8/3OUmM20jCU/wnwkTvyj+OdfST3NsuKTjgsi2RElr1aBe2cVXaEmc/6ZSbU Pgzm57PWvUhauPgK+w2qnj+dtvn3DD5UJ8yFyBjZ1G0l1/EL3LEO95paQys4seSx11zd 0X1TGN+PcetnHp+IreLLk7WVkmhV8EB90hCE5u8EcxOSA4mEFcCoIZOOtoquUrxj/fQI PKOt+YqZEERE9qm4kM+HehRS6/tM0RDezeJnSxGB84qp1DZWv8wk3b9REEHZNzPAdNBJ gbJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=IcKpbNkpFMj867eXppd8aTJ3bI8zTQqEs4WlJsDxDwo=; b=GBeXjZyaaNCQRFMU9gYYmhzRqQbaQnXGdyBwhKL5FTGSgyOPHocRKEeunel32QqwMn hE466A0X2i3MfcdY4VZYaXVdYe2b9NE+cOQIqVH0IuJvZq+fgsfdOLG+KZRg7ZgatR6t Yw7DEcD8nzkUxiltXDACwyp42MnDf075KW0o6XNckOyCDzdSekcNZQiHAlSu66/98EG5 OhhQn/CGt8SiS2Ri91S0Yu4qQk8Wg9BPiuCaw3JQox1ZddF+K/21UwuSBNLqKURdHMES WhsrFCspsNhjQY2nGeVGx8VTEv3iZPtMB9WUbgqcML/2QIhqu3+wLqBBAH8/8y0186JD iD8g== X-Gm-Message-State: AOAM53275ymowWhLe5AfugEs7ZLnWd6CWc+LclmqitJ92p1MU9ly2BcY WiudcmBbsTepb0P9/p7Vdk+oCmiwAS0Dqw== X-Google-Smtp-Source: ABdhPJyNJdKWQy8U4eFcHQd/VRDRtY6pJek8y/jY2XUa8C9MPjY4yJGfWblJwjNxvFlU7CHZCbXQFw== X-Received: by 2002:a17:902:b409:b029:114:afa6:7f4a with SMTP id x9-20020a170902b409b0290114afa67f4amr52559plr.56.1624455686125; Wed, 23 Jun 2021 06:41:26 -0700 (PDT) Received: from smtpclient.apple ([106.201.36.222]) by smtp.gmail.com with ESMTPSA id a23sm48396pfk.146.2021.06.23.06.41.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Jun 2021 06:41:25 -0700 (PDT) From: Sudheer H R Message-Id: <205CACC4-0EBF-4672-95F4-2E1949371FE7@tekenlight.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_4C8C173B-2624-48DA-A139-602E6DE131C3" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.100.0.2.22\)) Subject: Re: Found a buffer-overflow defect in asynchronous database connection API PQconnectPoll Date: Wed, 23 Jun 2021 19:11:20 +0530 In-Reply-To: <96EB194C-6EF0-4F79-A855-F28CD7DE4F95@tekenlight.com> Cc: pgsql-bugs@lists.postgresql.org To: Tom Lane References: <5372B6D4-8276-42C0-B8FB-BD0918826FC3@tekenlight.com> <2080223.1624454678@sss.pgh.pa.us> <96EB194C-6EF0-4F79-A855-F28CD7DE4F95@tekenlight.com> X-Mailer: Apple Mail (2.3654.100.0.2.22) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_4C8C173B-2624-48DA-A139-602E6DE131C3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Further to this. I downloaded the source from the listing (version 13.3) and build the = targets. With the built version this worked fine. Sudheer > On 23-Jun-2021, at 7:03 PM, Sudheer H R = wrote: >=20 > Thanks for the response >=20 >=20 > I am running this on a MacBook Pro Big Sur OSX 11.4 , the database = server is running in another process. I build the executable and run = them on the command line >=20 > Attached are the source code files and make file (quite simple and = trivial) >=20 > Pasted below is the AddressSanitizer report (which gets produced upon = any buffer overflow, when compiled and linked with -fsanitize=3Daddress) >=20 >=20 > Sudheer >=20 >=20 >=20 > > > >=20 >=20 >=20 > Upon running program ./a >=20 > open_connection_finalize[1] > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D94769=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on = address 0x60700001abd6 at pc 0x0001099618f4 bp 0x7ffee631b910 sp = 0x7ffee631b0d0 > READ of size 71 at 0x60700001abd6 thread T0 > #0 0x1099618f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) > #1 0x1099127ea in dopr+0xe4 (libpq.5.dylib:x86_64+0x1c7ea) > #2 0x1099126e2 in pg_vsnprintf+0x52 (libpq.5.dylib:x86_64+0x1c6e2) > #3 0x10990ae91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91) > #4 0x10990afae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae) > #5 0x10990db64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64) > #6 0x10990daf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #7 0x10990e4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #8 0x1098fc40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #9 0x1098e5a2c in main+0x46c (a:x86_64+0x100003a2c) > #10 0x7fff20563f5c in start+0x0 (libdyld.dylib:x86_64+0x15f5c) >=20 > 0x60700001abd6 is located 0 bytes to the right of 70-byte region = [0x60700001ab90,0x60700001abd6) > allocated by thread T0 here: > #0 0x109990460 in wrap_malloc+0xa0 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x48460) > #1 0x7fff2d8f7396 in _gss_mg_get_error+0x96 (GSS:x86_64+0x9396) > #2 0x7fff2d8f71e6 in gss_display_status+0x176 (GSS:x86_64+0x91e6) > #3 0x10990db4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b) > #4 0x10990daf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #5 0x10990e4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #6 0x1098fc40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #7 0x1098e5a2c in main+0x46c (a:x86_64+0x100003a2c) > #8 0x7fff20563f5c in start+0x0 (libdyld.dylib:x86_64+0x15f5c) >=20 > SUMMARY: AddressSanitizer: heap-buffer-overflow = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) in = wrap_strlen+0x183 > Shadow bytes around the buggy address: > 0x1c0e00003520: fd fd fd fd fd fd fd fd fa fa fa fa fd fd fd fd > 0x1c0e00003530: fd fd fd fd fd fd fa fa fa fa fd fd fd fd fd fd > 0x1c0e00003540: fd fd fd fa fa fa fa fa fd fd fd fd fd fd fd fd > 0x1c0e00003550: fd fa fa fa fa fa fd fd fd fd fd fd fd fd fd fa > 0x1c0e00003560: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fa fa > =3D>0x1c0e00003570: fa fa 00 00 00 00 00 00 00 00[06]fa fa fa fa fa > 0x1c0e00003580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e00003590: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > Shadow byte legend (one shadow byte represents 8 application bytes): > Addressable: 00 > Partially addressable: 01 02 03 04 05 06 07=20 > Heap left redzone: fa > Freed heap region: fd > Stack left redzone: f1 > Stack mid redzone: f2 > Stack right redzone: f3 > Stack after return: f5 > Stack use after scope: f8 > Global redzone: f9 > Global init order: f6 > Poisoned by user: f7 > Container overflow: fc > Array cookie: ac > Intra object redzone: bb > ASan internal: fe > Left alloca redzone: ca > Right alloca redzone: cb > Shadow gap: cc > =3D=3D94769=3D=3DABORTING > Abort >=20 > Upon running program ./b >=20 > =3D=3D94780=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on = address 0x60700001abd6 at pc 0x0001007e08f4 bp 0x7ffeef49c8a0 sp = 0x7ffeef49c060 > READ of size 71 at 0x60700001abd6 thread T0 > #0 0x1007e08f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) > #1 0x1007937ea in dopr+0xe4 (libpq.5.dylib:x86_64+0x1c7ea) > #2 0x1007936e2 in pg_vsnprintf+0x52 (libpq.5.dylib:x86_64+0x1c6e2) > #3 0x10078be91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91) > #4 0x10078bfae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae) > #5 0x10078eb64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64) > #6 0x10078eaf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #7 0x10078f4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #8 0x10077d40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #9 0x10077b0f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1) > #10 0x10077ac61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61) > #11 0x100764a84 in main+0x3a4 (b:x86_64+0x100003a84) > #12 0x7fff20563f5c in start+0x0 (libdyld.dylib:x86_64+0x15f5c) >=20 > 0x60700001abd6 is located 0 bytes to the right of 70-byte region = [0x60700001ab90,0x60700001abd6) > allocated by thread T0 here: > #0 0x10080f460 in wrap_malloc+0xa0 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x48460) > #1 0x7fff2d8f7396 in _gss_mg_get_error+0x96 (GSS:x86_64+0x9396) > #2 0x7fff2d8f71e6 in gss_display_status+0x176 (GSS:x86_64+0x91e6) > #3 0x10078eb4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b) > #4 0x10078eaf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #5 0x10078f4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #6 0x10077d40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #7 0x10077b0f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1) > #8 0x10077ac61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61) > #9 0x100764a84 in main+0x3a4 (b:x86_64+0x100003a84) > #10 0x7fff20563f5c in start+0x0 (libdyld.dylib:x86_64+0x15f5c) >=20 > SUMMARY: AddressSanitizer: heap-buffer-overflow = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) in = wrap_strlen+0x183 > Shadow bytes around the buggy address: > 0x1c0e00003520: fd fd fd fd fd fd fd fd fa fa fa fa fd fd fd fd > 0x1c0e00003530: fd fd fd fd fd fd fa fa fa fa fd fd fd fd fd fd > 0x1c0e00003540: fd fd fd fa fa fa fa fa fd fd fd fd fd fd fd fd > 0x1c0e00003550: fd fa fa fa fa fa fd fd fd fd fd fd fd fd fd fa > 0x1c0e00003560: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fa fa > =3D>0x1c0e00003570: fa fa 00 00 00 00 00 00 00 00[06]fa fa fa fa fa > 0x1c0e00003580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e00003590: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x1c0e000035c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > Shadow byte legend (one shadow byte represents 8 application bytes): > Addressable: 00 > Partially addressable: 01 02 03 04 05 06 07=20 > Heap left redzone: fa > Freed heap region: fd > Stack left redzone: f1 > Stack mid redzone: f2 > Stack right redzone: f3 > Stack after return: f5 > Stack use after scope: f8 > Global redzone: f9 > Global init order: f6 > Poisoned by user: f7 > Container overflow: fc > Array cookie: ac > Intra object redzone: bb > ASan internal: fe > Left alloca redzone: ca > Right alloca redzone: cb > Shadow gap: cc > =3D=3D94780=3D=3DABORTING > Abort >=20 >=20 >=20 >=20 >> On 23-Jun-2021, at 6:54 PM, Tom Lane > wrote: >>=20 >> Sudheer H R > writes: >>> While trying to sanitise the code for heap buffer overflows I = compiled and linked the executable with clang -fsanitize=3D=E2=80=9Caddres= s=E2=80=9D option. The connection library indicates a buffer over flow = in an internal source code of the module. >>=20 >> Hm, interesting. Our code is expecting that gss_display_status() = returns >> a null-terminated string, but this trace suggests that the string is >> not necessarily null-terminated. The documentation I found on the = net >> is unclear on the point, and the code I could find is split as to how >> the string is treated. If it's not supposed to be null-terminated, >> we're hardly the only ones making that mistake. >>=20 >> In any case, you wouldn't get here unless we'd run into some kind of >> problem trying to make a GSS connection. Could you maybe explain the >> conditions you're running this under, and/or print out the failure = message >> it constructs? >>=20 >> regards, tom lane >=20 --Apple-Mail=_4C8C173B-2624-48DA-A139-602E6DE131C3 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Further to this.

I downloaded the source from the listing (version 13.3) and = build the targets.

With the built version this worked fine.

Sudheer



On 23-Jun-2021, at 7:03 PM, Sudheer H R <sudheer.hr@tekenlight.com> wrote:

Thanks for the = response


I am running this on a MacBook Pro Big = Sur OSX 11.4 , the database server is running in another process. I = build the executable and run them on the command line

Attached are the source = code files and make file (quite simple and trivial)

Pasted below is = the AddressSanitizer report (which gets produced upon any buffer = overflow, when compiled and linked with -fsanitize=3Daddress)


Sudheer



<a.c>
<b.c>
<makefile>



Upon = running program ./a

open_connection_finalize[1]
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=3D=3D94769=3D=3DERROR: AddressSanitizer: = heap-buffer-overflow on address 0x60700001abd6 at pc 0x0001099618f4 bp = 0x7ffee631b910 sp 0x7ffee631b0d0
READ of size 71 at = 0x60700001abd6 thread T0
    #0 0x1099618f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3)
  =   #1 0x1099127ea in dopr+0xe4 = (libpq.5.dylib:x86_64+0x1c7ea)
    #2 0x1099126e2 in pg_vsnprintf+0x52 = (libpq.5.dylib:x86_64+0x1c6e2)
    #3 0x10990ae91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91)
    #4 0x10990afae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae)
    #5 0x10990db64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64)
    #6 0x10990daf3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
    #7 0x10990e4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
    #8 0x1098fc40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
    #9 0x1098e5a2c in main+0x46c = (a:x86_64+0x100003a2c)
    #10 0x7fff20563f5c in start+0x0 = (libdyld.dylib:x86_64+0x15f5c)

0x60700001abd6 is located 0 bytes to the right of 70-byte = region [0x60700001ab90,0x60700001abd6)
allocated by thread T0 here:
  =   #0 0x109990460 in wrap_malloc+0xa0 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x48460)
  =   #1 0x7fff2d8f7396 in _gss_mg_get_error+0x96 = (GSS:x86_64+0x9396)
    #2 0x7fff2d8f71e6 in gss_display_status+0x176 = (GSS:x86_64+0x91e6)
    #3 0x10990db4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b)
    #4 0x10990daf3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
    #5 0x10990e4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
    #6 0x1098fc40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
    #7 0x1098e5a2c in main+0x46c = (a:x86_64+0x100003a2c)
    #8 0x7fff20563f5c in start+0x0 = (libdyld.dylib:x86_64+0x15f5c)

SUMMARY: = AddressSanitizer: heap-buffer-overflow = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) in = wrap_strlen+0x183
Shadow bytes around the buggy address:
  = 0x1c0e00003520: fd fd = fd = fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd
  = 0x1c0e00003530: fd fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd
  = 0x1c0e00003540: fd fd = fd = fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd
  = 0x1c0e00003550: fd fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa
  = 0x1c0e00003560: fa fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa = fa
=3D>0x1c0e00003570: fa = fa 00 00 = 00 00 00 00 00 00[06]fa fa = fa = fa = fa
  = 0x1c0e00003580: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e00003590: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e000035a0: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e000035b0: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e000035c0: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
Shadow = byte legend (one shadow byte represents 8 application = bytes):
  = Addressable:           00
  = Partially addressable: 01 02 03 04 05 06 07 
  = Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          = f9
  = Global init order:       f6
  Poisoned by user:        = f7
  = Container overflow:      fc
  Array cookie:            = ac
  = Intra object redzone:    bb
  ASan internal:           = fe
  = Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:            =   cc
=3D=3D94769=3D=3DABORTING
Abort

Upon = running program ./b

=3D=3D94780=3D=3DERROR: AddressSanitizer: = heap-buffer-overflow on address 0x60700001abd6 at pc 0x0001007e08f4 bp = 0x7ffeef49c8a0 sp 0x7ffeef49c060
READ of size 71 at 0x60700001abd6 thread = T0
    #0 0x1007e08f3 in = wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3)
    #1 0x1007937ea in dopr+0xe4 = (libpq.5.dylib:x86_64+0x1c7ea)
  =   #2 0x1007936e2 in pg_vsnprintf+0x52 = (libpq.5.dylib:x86_64+0x1c6e2)
  =   #3 0x10078be91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91)
  =   #4 0x10078bfae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae)
  =   #5 0x10078eb64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64)
  =   #6 0x10078eaf3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
  =   #7 0x10078f4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
  =   #8 0x10077d40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
  =   #9 0x10077b0f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1)
  =   #10 0x10077ac61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61)
  =   #11 0x100764a84 in main+0x3a4 = (b:x86_64+0x100003a84)
  =   #12 0x7fff20563f5c in start+0x0 = (libdyld.dylib:x86_64+0x15f5c)

0x60700001abd6 is located 0 bytes to the right = of 70-byte region [0x60700001ab90,0x60700001abd6)
allocated by thread T0 = here:
    #0 0x10080f460 in = wrap_malloc+0xa0 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x48460)
    #1 0x7fff2d8f7396 in _gss_mg_get_error+0x96 = (GSS:x86_64+0x9396)
  =   #2 0x7fff2d8f71e6 in gss_display_status+0x176 = (GSS:x86_64+0x91e6)
  =   #3 0x10078eb4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b)
  =   #4 0x10078eaf3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
  =   #5 0x10078f4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
  =   #6 0x10077d40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
  =   #7 0x10077b0f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1)
  =   #8 0x10077ac61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61)
  =   #9 0x100764a84 in main+0x3a4 = (b:x86_64+0x100003a84)
  =   #10 0x7fff20563f5c in start+0x0 = (libdyld.dylib:x86_64+0x15f5c)

SUMMARY: = AddressSanitizer: heap-buffer-overflow = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) in = wrap_strlen+0x183
Shadow = bytes around the buggy address:
  = 0x1c0e00003520: fd fd = fd = fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd
  0x1c0e00003530: fd = fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd
  0x1c0e00003540: fd = fd = fd = fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd
  0x1c0e00003550: fd = fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa
  0x1c0e00003560: fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa = fa
=3D>0x1c0e00003570: fa = fa 00 00 = 00 00 00 00 00 00[06]fa fa = fa = fa = fa
  0x1c0e00003580: fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  0x1c0e00003590: fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  0x1c0e000035a0: fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  0x1c0e000035b0: fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  0x1c0e000035c0: fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
Shadow byte legend (one shadow byte represents 8 application = bytes):
  Addressable:       =     00
  = Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  = Freed heap region:       fd
  = Stack left redzone:      f1
  = Stack mid redzone:       f2
  = Stack right redzone:     f3
  = Stack after return:      f5
  = Stack use after scope:   f8
  = Global redzone:          f9
  = Global init order:       f6
  = Poisoned by user:        f7
  = Container overflow:      fc
  = Array cookie:            ac
  = Intra object redzone:    bb
  = ASan internal:           fe
  = Left alloca redzone:     ca
  = Right alloca redzone:    cb
  = Shadow gap:              = cc
=3D=3D94780=3D=3DABORTING
Abort




On 23-Jun-2021, at 6:54 PM, Tom = Lane <tgl@sss.pgh.pa.us> wrote:

Sudheer H R <sudheer.hr@tekenlight.com> writes:
While trying to sanitise = the code for heap buffer overflows I compiled and linked the executable = with clang -fsanitize=3D=E2=80=9Caddress=E2=80=9D option. The connection = library indicates a buffer over flow in an internal source code of the = module.

Hm, interesting. =  Our code is expecting that gss_display_status() returns
a null-terminated string, but this trace suggests that the = string is
not necessarily null-terminated.  The = documentation I found on the net
is unclear on the point, = and the code I could find is split as to how
the string is = treated.  If it's not supposed to be null-terminated,
we're hardly the only ones making that mistake.

In any case, you wouldn't get here unless we'd = run into some kind of
problem trying to make a GSS = connection.  Could you maybe explain the
conditions = you're running this under, and/or print out the failure message
it constructs?

regards, = tom lane


= --Apple-Mail=_4C8C173B-2624-48DA-A139-602E6DE131C3--