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 1lw2CR-0005e6-Gm for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jun 2021 12:41:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lw2Bu-0008R3-3z for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jun 2021 12:41:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw2Bt-0008Qv-O8 for pgsql-bugs@lists.postgresql.org; Wed, 23 Jun 2021 12:41:13 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lw2Bm-0007n8-Bn for pgsql-bugs@lists.postgresql.org; Wed, 23 Jun 2021 12:41:13 +0000 Received: by mail-pj1-x1032.google.com with SMTP id x21-20020a17090aa395b029016e25313bfcso1316385pjp.2 for ; Wed, 23 Jun 2021 05:41:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tekenlight.com; s=google; h=from:mime-version:subject:date:references:to:in-reply-to:message-id; bh=wNoPdRu476beUlxpcngNAXUH9rxLmmazUrw2AYXHYHs=; b=Yp/Dcr/6QfiQLRCB+12HO+FquO3xY96v1CJyZ9+R2of35Nax5quUJezz12Z7HeVDSw kvPzJ+VA4UKg63MhTM16rfAZMEoCTfimHK919nUA6yZIRPhUnOcMxlim5B3+9y1qQi8i PEBeODJxpUnsG7onXKgqr36QKYjvQxfsRbu2Wbl39WGguz5cC8WI9/QWuovHDZ48KssA AUQ9XPsHQK2NZNzsyQvLG2SsnDoWZTAQDy2tJw0W8qGtUUPOGKu6K2902uM9sC2FLzhZ pd9HzDsCXu96xJvW13vEZvJmBRU/kODYmrGaJMXs4vInOJ2vQjcUOPPUKrqLwb3+wkwS UQTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:subject:date:references:to :in-reply-to:message-id; bh=wNoPdRu476beUlxpcngNAXUH9rxLmmazUrw2AYXHYHs=; b=ajnBmhzqJAG5r9q53QmCJL3jsrwN7xEJ3OL3c3sm2GurXsYcQPslmOrR3jvvoGa5Fg dlSSoWiTDULsp5wzEeNv5hZC6RCg39Pp2pteS1F45A08YXA7GvGmovteC0ml4fZAfoGD bzKgYoRNVqYqyuh4QS6+qGX5JKDUik641iLD2FDwI8LcfmqTN+OfadMcKvsL1I25lQyF REgYC7vaDP6h7aJb5yGCxTwkp5ri8bRCo/IsL7IWHu2xL2RqAP3g9QSlGtT4Kg191+2o +v7jlBZGU3fgsdtEVhRkKRKT0cr/ViDQpLVZ/RAuKVcLg+uaHIOOrCGGBmmv24BsgKRx ltsg== X-Gm-Message-State: AOAM531fmljCZrWu7iFVYgM+CBPVOT6ZJ8yr1sD6/zGbbDT2szj+Qw5B NYhfeIOaKScjbftJHIRDqGbhkc+Mp+cL2w== X-Google-Smtp-Source: ABdhPJxcHm6SyZ7CDHeO4g3Pk/V11mNtcnz6mG3/rD6lNtZx7k+J1FmOTdOo8opdJFf9hmNPDdQ91Q== X-Received: by 2002:a17:90a:4a8f:: with SMTP id f15mr9552025pjh.76.1624452063142; Wed, 23 Jun 2021 05:41:03 -0700 (PDT) Received: from smtpclient.apple ([106.201.36.222]) by smtp.gmail.com with ESMTPSA id f2sm2518101pfe.23.2021.06.23.05.41.01 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Jun 2021 05:41:02 -0700 (PDT) From: Sudheer H R Content-Type: multipart/alternative; boundary="Apple-Mail=_816E7788-E44F-4398-AE44-5B4979D8C35C" 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 18:10:57 +0530 References: <5372B6D4-8276-42C0-B8FB-BD0918826FC3@tekenlight.com> To: pgsql-bugs@lists.postgresql.org In-Reply-To: <5372B6D4-8276-42C0-B8FB-BD0918826FC3@tekenlight.com> Message-Id: 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=_816E7788-E44F-4398-AE44-5B4979D8C35C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Further to this. I tried a synchronous mode of connection, with the code below. Even that produces the error report as below; Sudheer Source code #include #include #include #include "libpq-fe.h" int main() { PGconn *p =3D NULL; const char* keywords[] =3D { "host", "dbname", "user", "password", = NULL }; const char* values[] =3D { "localhost", "AAA", "gen", "GEN", NULL }; p =3D PQconnectdbParams(keywords, values, 0); if (p =3D=3D NULL) { printf("COULD NOT ALLOCATE MEMORY\n"); exit(1); } else if (PQstatus(p) =3D=3D CONNECTION_BAD) { printf("COULD NOT CONNECT\n"); PQfinish(p); p =3D NULL; } return 0; } makefile all: a b b.o: b.c cc -c -fsanitize=3D"address" b.c -I/opt/local/include/postgresql13 a.o: a.c cc -c -fsanitize=3D"address" a.c -I/opt/local/include/postgresql13 a: a.o cc -lpq -L/opt/local/lib/postgresql13/ -fsanitize=3D"address" -o a = a.o =20 b: b.o cc -lpq -L/opt/local/lib/postgresql13/ -fsanitize=3D"address" -o b = b.o clean:=20 rm -f a a.o b b.o REPORT: =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=3D62688=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on = address 0x6070000173d6 at pc 0x0001049d68f4 bp 0x7ffeeb2a38a0 sp = 0x7ffeeb2a3060 READ of size 71 at 0x6070000173d6 thread T0 #0 0x1049d68f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) #1 0x10498a7ea in dopr+0xe4 (libpq.5.dylib:x86_64+0x1c7ea) #2 0x10498a6e2 in pg_vsnprintf+0x52 (libpq.5.dylib:x86_64+0x1c6e2) #3 0x104982e91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91) #4 0x104982fae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae) #5 0x104985b64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64) #6 0x104985af3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) #7 0x1049864fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) #8 0x10497440d in PQconnectPoll+0xac9 (libpq.5.dylib:x86_64+0x640d) #9 0x1049720f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1) #10 0x104971c61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61) #11 0x10495da84 in main+0x3a4 (b:x86_64+0x100003a84) #12 0x7fff20563f5c in start+0x0 (libdyld.dylib:x86_64+0x15f5c) 0x6070000173d6 is located 0 bytes to the right of 70-byte region = [0x607000017390,0x6070000173d6) allocated by thread T0 here: #0 0x104a05460 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 0x104985b4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b) #4 0x104985af3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) #5 0x1049864fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) #6 0x10497440d in PQconnectPoll+0xac9 (libpq.5.dylib:x86_64+0x640d) #7 0x1049720f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1) #8 0x104971c61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61) #9 0x10495da84 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: 0x1c0e00002e20: fd fd fd fd fd fd fd fd fa fa fa fa fd fd fd fd 0x1c0e00002e30: fd fd fd fd fd fd fa fa fa fa fd fd fd fd fd fd 0x1c0e00002e40: fd fd fd fa fa fa fa fa fd fd fd fd fd fd fd fd 0x1c0e00002e50: fd fa fa fa fa fa fd fd fd fd fd fd fd fd fd fa 0x1c0e00002e60: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fa fa =3D>0x1c0e00002e70: fa fa 00 00 00 00 00 00 00 00[06]fa fa fa fa fa 0x1c0e00002e80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x1c0e00002e90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x1c0e00002ea0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x1c0e00002eb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x1c0e00002ec0: 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=3D62688=3D=3DABORTING Abort > On 23-Jun-2021, at 5:43 PM, Sudheer H R = wrote: >=20 > Hello, >=20 > I am trying to integrate the client library libpq with a server that = is developed based on libev model on a Macbook PRO - maxOS Big Sur OSX = version 11.4 >=20 > 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. >=20 > In order to isolate the problem, I tried writing a separate simple = program as listed below, which also gives the same error report upon = running. >=20 >=20 > Request you to please provide any guidance in this regard. >=20 >=20 > I am using the Mac ports based installation of PostgreSQL version 13.2 = (or 3) >=20 >=20 > Any help in this regard is much appreciated=20 >=20 > Regards, > Sudheer >=20 >=20 >=20 > Source code: >=20 >=20 > #include > #include "libpq-fe.h" >=20 >=20 > int main() > { > PGconn *p =3D NULL; > const char* keywords[] =3D { "host", "dbname", "user", "password", = NULL }; > const char* values[] =3D { "localhost", "AAA", "gen", "GEN", NULL = }; >=20 > p =3D PQconnectStartParams(keywords, values, 0); >=20 > if (p =3D=3D NULL) { > printf("COULD NOT ALLOCATE MEMORY\n"); > exit(1); > } > else if (PQstatus(p) =3D=3D CONNECTION_BAD) { > printf("COULD NOT CONNECT\n"); > PQfinish(p); > p =3D NULL; > } >=20 > PostgresPollingStatusType ps; >=20 > ps =3D PQconnectPoll(p); > while (ps !=3D PGRES_POLLING_FAILED && ps !=3D PGRES_POLLING_OK) { > sleep(1); > ps =3D PQconnectPoll(p); > printf("open_connection_finalize[%d]\n", ps); > } >=20 > printf("DONE\n"); >=20 > return 0; > } >=20 >=20 >=20 >=20 >=20 > BUFFER OVERFLOW SANITIZER REPORT >=20 >=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=3D62203=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on = address 0x60700001abd6 at pc 0x0001098118f4 bp 0x7ffee646d910 sp = 0x7ffee646d0d0 > READ of size 71 at 0x60700001abd6 thread T0 > #0 0x1098118f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3) > #1 0x1097c37ea in dopr+0xe4 (libpq.5.dylib:x86_64+0x1c7ea) > #2 0x1097c36e2 in pg_vsnprintf+0x52 (libpq.5.dylib:x86_64+0x1c6e2) > #3 0x1097bbe91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91) > #4 0x1097bbfae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae) > #5 0x1097beb64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64) > #6 0x1097beaf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #7 0x1097bf4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #8 0x1097ad40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #9 0x109793a2c 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 0x109840460 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 0x1097beb4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b) > #4 0x1097beaf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3) > #5 0x1097bf4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe) > #6 0x1097ad40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d) > #7 0x109793a2c 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=3D62203=3D=3DABORTING > Abort >=20 >=20 --Apple-Mail=_816E7788-E44F-4398-AE44-5B4979D8C35C Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Further to this.

I tried a synchronous mode of connection, with the code = below.

Even = that produces the error report as below;

Sudheer


Source code

#include = <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libpq-fe.h"


int main()
{
    PGconn *p =3D NULL;
    const char* keywords[] =3D { = "host", "dbname", "user", "password", NULL };
  =   const = char* = values[] =3D { "localhost", = "AAA", = "gen", = "GEN", = NULL = };

  =   p =3D PQconnectdbParams(keywords, values, 0);

    if (p =3D=3D= NULL) = {
        printf("COULD = NOT ALLOCATE MEMORY\n");
  =       exit(1);
    }
    else = if (PQstatus(p) =3D=3D CONNECTION_BAD) = {
        printf("COULD = NOT CONNECT\n");
  =       PQfinish(p);
        p =3D NULL;
    }


    return = 0;
}


makefile

all: a b

b.o: = b.c
  =   cc -c -fsanitize=3D"address" b.c = -I/opt/local/include/postgresql13

a.o: = a.c
  =   cc -c -fsanitize=3D"address" a.c = -I/opt/local/include/postgresql13

a: = a.o
  =   cc -lpq -L/opt/local/lib/postgresql13/ -fsanitize=3D"address" -o = a a.o

    

b: = b.o
  =   cc -lpq -L/opt/local/lib/postgresql13/ -fsanitize=3D"address" -o = b b.o

clean: 
  =   rm -f a a.o b b.o


REPORT:



=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=3D62688=3D=3DERROR: AddressSanitizer: = heap-buffer-overflow on address 0x6070000173d6 at pc 0x0001049d68f4 bp = 0x7ffeeb2a38a0 sp 0x7ffeeb2a3060
READ of size 71 at = 0x6070000173d6 thread T0
    #0 0x1049d68f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3)
  =   #1 0x10498a7ea in dopr+0xe4 = (libpq.5.dylib:x86_64+0x1c7ea)
    #2 0x10498a6e2 in pg_vsnprintf+0x52 = (libpq.5.dylib:x86_64+0x1c6e2)
    #3 0x104982e91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91)
    #4 0x104982fae in appendPQExpBuffer+0xc4 = (libpq.5.dylib:x86_64+0x14fae)
    #5 0x104985b64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64)
    #6 0x104985af3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
    #7 0x1049864fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
    #8 0x10497440d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
    #9 0x1049720f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1)
    #10 0x104971c61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61)
    #11 0x10495da84 in main+0x3a4 = (b:x86_64+0x100003a84)
    #12 0x7fff20563f5c in start+0x0 = (libdyld.dylib:x86_64+0x15f5c)

0x6070000173d6 is located 0 bytes to the right of 70-byte = region [0x607000017390,0x6070000173d6)
allocated by thread T0 here:
  =   #0 0x104a05460 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 0x104985b4b in pg_GSS_error_int+0x42 = (libpq.5.dylib:x86_64+0x17b4b)
    #4 0x104985af3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
    #5 0x1049864fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
    #6 0x10497440d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
    #7 0x1049720f1 in connectDBComplete+0x11f = (libpq.5.dylib:x86_64+0x40f1)
    #8 0x104971c61 in PQconnectdbParams+0x23 = (libpq.5.dylib:x86_64+0x3c61)
    #9 0x10495da84 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:
  = 0x1c0e00002e20: fd fd = fd = fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd
  = 0x1c0e00002e30: fd fd = fd = fd = fd = fd = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd
  = 0x1c0e00002e40: fd fd = fd = fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd
  = 0x1c0e00002e50: fd fa = fa = fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa
  = 0x1c0e00002e60: fa fa = fa = fa = fd = fd = fd = fd = fd = fd = fd = fd = fd = fd = fa = fa
=3D>0x1c0e00002e70: fa = fa 00 00 = 00 00 00 00 00 00[06]fa fa = fa = fa = fa
  = 0x1c0e00002e80: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e00002e90: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e00002ea0: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e00002eb0: fa fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa = fa
  = 0x1c0e00002ec0: 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=3D62688=3D=3DABORTING
Abort



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

Hello,

I am trying to integrate = the client library libpq with a server that is developed based on libev = model on a Macbook PRO - maxOS Big Sur OSX version 11.4

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.

In = order to isolate the problem, I tried writing a separate simple program = as listed below, which also gives the same error report upon = running.


Request you to please provide any = guidance in this regard.


I am using the Mac ports = based installation of PostgreSQL version 13.2 (or 3)


Any help in this regard is much appreciated 

Regards,
Sudheer



Source code:


#include <unistd.h>
#include "libpq-fe.h"


int main()
{
  =   PGconn *p =3D NULL;
  =   const char* keywords[] =3D = { "host", "dbname", "user", "password", NULL = ;};
    const char* values[] =3D = { "localhost", "AAA", "gen", "GEN", NULL };<= br class=3D"">
    p =3D = PQconnectStartParams(keywords, values, 0);

    if (p =3D=3D NULL) {
        printf("COULD NOT ALLOCATE = MEMORY\n");
        exit(1);
    }
  =   else if (PQstatus(p) =3D=3D CONNECTION_BAD) {
        printf("COULD NOT = CONNECT\n");
      =   PQfinish(p);
      =   p =3D NULL;
    }

    PostgresPollingStatusType = ps;

    ps =3D = PQconnectPoll(p);
    while (ps !=3D = PGRES_POLLING_FAILED && ps !=3D PGRES_POLLING_OK) {
        sleep(1);
 =       ps =3D PQconnectPoll(p);
  =       printf("open_connection_finalize[%d]\n", = ps);
    }

    printf("DONE\n");

    return 0;
}





BUFFER = OVERFLOW SANITIZER REPORT


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=3D62203=3D=3DERROR: AddressSanitizer: = heap-buffer-overflow on address 0x60700001abd6 at pc 0x0001098118f4 = bp 0x7ffee646d910 sp 0x7ffee646d0d0
READ of size 71 = at 0x60700001abd6 thread T0
    #0 = 0x1098118f3 in wrap_strlen+0x183 = (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x198f3)
  =   #1 0x1097c37ea in dopr+0xe4 = (libpq.5.dylib:x86_64+0x1c7ea)
    #2 = 0x1097c36e2 in pg_vsnprintf+0x52 (libpq.5.dylib:x86_64+0x1c6e2)
    #3 0x1097bbe91 in appendPQExpBufferVA+0x3e = (libpq.5.dylib:x86_64+0x14e91)
    #4 = 0x1097bbfae in appendPQExpBuffer+0xc4 (libpq.5.dylib:x86_64+0x14fae)
    #5 0x1097beb64 in pg_GSS_error_int+0x5b = (libpq.5.dylib:x86_64+0x17b64)
    #6 = 0x1097beaf3 in pg_GSS_error+0x66 (libpq.5.dylib:x86_64+0x17af3)
    #7 0x1097bf4fe in pqsecure_open_gss+0x334 = (libpq.5.dylib:x86_64+0x184fe)
    #8 = 0x1097ad40d in PQconnectPoll+0xac9 (libpq.5.dylib:x86_64+0x640d)
    #9 0x109793a2c 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 0x109840460 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 0x1097beb4b in = pg_GSS_error_int+0x42 (libpq.5.dylib:x86_64+0x17b4b)
  =   #4 0x1097beaf3 in pg_GSS_error+0x66 = (libpq.5.dylib:x86_64+0x17af3)
    #5 = 0x1097bf4fe in pqsecure_open_gss+0x334 (libpq.5.dylib:x86_64+0x184fe)
    #6 0x1097ad40d in PQconnectPoll+0xac9 = (libpq.5.dylib:x86_64+0x640d)
    #7 = 0x109793a2c 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&nbs= p;fd fd fd fd fa fa fa fa fd = fd fd fd
  0x1c0e00003530: fd fd fd fd&nbs= p;fd fd fa fa fa fa fd fd fd = fd fd fd
  0x1c0e00003540: fd fd fd fa&nbs= p;fa fa fa fa fd fd fd fd fd = fd fd fd
  0x1c0e00003550: fd fa fa fa&nbs= p;fa fa fd fd fd fd fd fd fd = fd fd fa
  0x1c0e00003560: fa fa fa fa&nbs= p;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&nbs= p;fa fa fa fa fa fa fa fa fa = fa fa fa
  0x1c0e00003590: fa fa fa fa&nbs= p;fa fa fa fa fa fa fa fa fa = fa fa fa
  0x1c0e000035a0: fa fa fa fa&nbs= p;fa fa fa fa fa fa fa fa fa = fa fa fa
  0x1c0e000035b0: fa fa fa fa&nbs= p;fa fa fa fa fa fa fa fa fa = fa fa fa
  0x1c0e000035c0: fa fa fa fa&nbs= p;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=3D62203=3D=3DABORTING
Abort



= --Apple-Mail=_816E7788-E44F-4398-AE44-5B4979D8C35C--