Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rfBdT-00G2MG-S1 for pgsql-odbc@arkaria.postgresql.org; Wed, 28 Feb 2024 04:33:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rfBdS-007nuJ-3H for pgsql-odbc@arkaria.postgresql.org; Wed, 28 Feb 2024 04:33:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rfBdR-007nuB-Pm for pgsql-odbc@lists.postgresql.org; Wed, 28 Feb 2024 04:33:38 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rfBdO-001eXv-PA for pgsql-odbc@lists.postgresql.org; Wed, 28 Feb 2024 04:33:37 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-512fd840142so3815645e87.2 for ; Tue, 27 Feb 2024 20:33:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709094814; x=1709699614; darn=lists.postgresql.org; h=in-reply-to:cc:from:content-language:references:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=nZ3hzX8ow1Z4iIGzBD18w1zOhsDeT7sG2/G9056WjuQ=; b=nQIzIjzqaXP/eeP+Dls9/GxABx3lY1akRRXjPxCmTnbGQzNDYPteACoLqsbB6K6+6m tseFg2SSwWvkO9hpSOxUYmt43o/gT8eWCzf9GyoDlkoxXQ+umAFhjigMuW0rw13RrF90 3K8OnUzuXmx/wn8CgOUKwX4AS4Adf8WNg8Z8HQOCNPrp9RC01IYLajduEizSqhW+WiIE RtczDW7nlc744ZPks5PgxOG0ka8tsjUYQp7pcy18gCtV9vKUbWnhyLykdhgHKGgPSUMq aMYD6eUY8XkTthKF3Af1eYB0jPp9UbIWi+R0nBFYITf97k8QdkekiDoJuhlfZdbChOiP /Vyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709094814; x=1709699614; h=in-reply-to:cc:from:content-language:references:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=nZ3hzX8ow1Z4iIGzBD18w1zOhsDeT7sG2/G9056WjuQ=; b=luU1W8imy+Gy4oVJmUyM2/bvvEFZ3rEFTKi/Iz3Lr/Taw2HbX5K9WXWaGXv1eHCcOJ q9nKh67DfJ82pfMy8EozcgDWfjA6FyyUOu8PWs6JlWlzJwFfXsujid9G10oKx2DeUCi1 10xB+Ey7Z81NhVm/10RtDE68xIMaNc9ceUgFZBObiRD+E84NoFwCWg6B3h/oecvOs6x8 RO7EhCiBt9LwdN3WD/Cj+O31pA8xDhJb3Msc61Q/hr23Rh+2yE9JNZ8/v8qnQM2NiC/P 2S7ftc5adF7CsqYSFr4pUAJu1R26QZYjV24Ufy371kF2lGHUfjl/rflQviRidAH+/KaD l2Iw== X-Gm-Message-State: AOJu0YzUoaQr/8LeFyPo+11k6YR/z2eQw5J01YgJi/BhMhfPIO6TVD57 SjUQgDo4EwSaMPHtWEekMQelblOusRfJz7IhlhTHZui9LYOEvoPH X-Google-Smtp-Source: AGHT+IEVJSp+9j92k/NkBqDLctAbgA7Tkz4xmyZ9N0hKly696Wok2SuY+n7RUGPQ8ZqJr+q2BVi7oA== X-Received: by 2002:a05:6512:15a3:b0:513:1a02:7304 with SMTP id bp35-20020a05651215a300b005131a027304mr552831lfb.54.1709094813249; Tue, 27 Feb 2024 20:33:33 -0800 (PST) Received: from [172.16.145.34] (isp-nts.automiq.ru. [92.243.127.242]) by smtp.gmail.com with ESMTPSA id m12-20020ac2428c000000b00512e3924539sm1405803lfh.309.2024.02.27.20.33.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 27 Feb 2024 20:33:32 -0800 (PST) Content-Type: multipart/alternative; boundary="------------GkW2C0inWqcuk0bljGAjM0Qh" Message-ID: <9dc06227-b554-4dd7-ad4e-2ffbe8490d08@gmail.com> Date: Wed, 28 Feb 2024 11:33:31 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: psqlodbc crashes while collecting diagnostic records with SQLGetDiagRecW To: Dave Cramer References: <112f6883-34e4-4372-8bda-d04f45ee31bf@gmail.com> <1ad9d955-445f-4eef-a4cd-83071beba366@gmail.com> Content-Language: ru, en-US From: Andrey Sukhanov Cc: pgsql-odbc@lists.postgresql.org In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------GkW2C0inWqcuk0bljGAjM0Qh Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Dave, I think it should solve this problem. Thank you for your help. It seems that there won't be a problem with use of unsigned values, since all values that could be negative are handled. I'm not sure if "stapos" should be short.   Product of 2byte*2byte variables could require 4 bytes to hold the value, if it's possible for "error->recsize" and "RecNumber" to hold big enough values at the same time. Maybe make "stapos" signed/unsigned int to make sure it won't happen? Regards, Andrey 27.02.2024 21:21, Dave Cramer wrote: > Hi Andrey, > > Thoughts use unsigned word for lengths to avoid overflow by davecramer > · Pull Request #3 · postgresql-interfaces/psqlodbc (github.com) > > > Dave Cramer > www.postgres.rocks > > > On Tue, 27 Feb 2024 at 01:07, Andrey Sukhanov wrote: > > Hi Dave, > > Thank you for your time. > The problem is caused by signed integer overflow, which leads to > attempt to read from unallocated memory. > > I have downloaded psqlodbc 16 and debug symbols from > https://www.postgresql.org/ftp/odbc/versions/. > Source code from for psqlodbc 16 from > https://git.postgresql.org/git/psqlodbc.git. > I compiled the attached program with msvc 14 and msvc 16 compilers > (same results). Program should be compiled with defined "UNICODE" > to use functions (SQLGetDiagRecW etc.) that accept wchar. > > On windows 10 attached program crashes inside ER_ReturnError line > environ.c:249. Where it tries to memcpy from the buffer "msg" with > offset "stapos". > > > "msg" is a valid pointer to "pgerror->__error_msg". "pgerror" also > have field "pgerror->errorsize" with value "32690". > Offset "stapos" is calculated as "stapos = (RecNumber - 1) * > error->recsize;" > Here "error" is "pgerror", "recsize" = 99, "RecNumber" = 332. > "stapos" should be assigned the value of 32769  but it has type > SWORD (short int) (INT16_MAX is 32767). > Expression"if (stapos > msglen)" (here "msglen" = 32690) evaluates > to "false", because of signed integer overflow. It should've been > "true" instead, and function would've returned SQL_NO_DATA_FOUND. > Callstack: >      vcruntime140.dll!memcpy() line 438 >      psqlodbc35w.dll!ER_ReturnError(PG_ErrorInfo * pgerror, short > RecNumber, unsigned char * szSqlState, long * pfNativeError, > unsigned char * szErrorMsg, short cbErrorMsgMax, short * > pcbErrorMsg, unsigned short) line 250 >      psqlodbc35w.dll!PGAPI_StmtError(void * hstmt, short > RecNumber, unsigned char * szSqlState, long * pfNativeError, > unsigned char * szErrorMsg, short cbErrorMsgMax, short * > pcbErrorMsg, unsigned short) line 1626 >      psqlodbc35w.dll!PGAPI_GetDiagRec(short) line 57 >      psqlodbc35w.dll!SQLGetDiagRecW(short fHandleType, void * > handle, short iRecord, wchar_t * szSqlState, long * pfNativeError, > wchar_t * szErrorMsg, short cbErrorMsgMax, short * pcbErrorMsg) > line 234 >      odbc32.dll!SQLGetDiagRecW() >      odbc.pg.exe!CollectDiagRecords(short hndlType, void * hndl) > line 30 >      odbc.pg.exe!main(int argc, char * * argv) line 117 > > That means, that the problem is caused by signed integer overflow, > which in turn causes memcpy to read from unallocated memory. > The easiest way to reproduce this problem is to try increasing the > number of "raise notice" messages in for loop. If their number > gets past certain point, no diagnostic records from SQLGetDiagRec > are being returned at all. Then I've decreased the number of these > messages until they started appearing again. In my case at this > number I am able to reproduce the problem. With further decrease > of this number problem doesn't appear anymore. > > I can't fix this problem myself, because I'm not familiar with the > project, and I don't know how to properly test psqlodbc on > different platforms. > > Regards, > Andrey > > > 27.02.2024 3:11, Dave Cramer wrote: >> Hi Andrey, >> >> I've tried on PostgreSQL 11 and 15 with no luck to replicate this >> problem. >> >> Is it possible for you to debug it and send more information? >> >> Dave Cramer >> www.postgres.rocks >> >> >> On Thu, 22 Feb 2024 at 01:29, Andrey Sukhanov >> wrote: >> >> Dear pgsql-odbc developers, >> >> Windows 10,  psqlodbc 16 (psqlodbc35w.dll), postgresql 11. >> Getting certain amount of diagnostic records with >> SQLGetDiagRecW crashes >> the application with memory access violation. >> >> Steps to reproduce: >> 1. Create procedure: >> CREATE OR REPLACE PROCEDURE crashme() >> LANGUAGE plpgsql >> AS $$ >> BEGIN >> FOR i IN 1..841 LOOP >>          RAISE NOTICE 'msgmsgmsgmsg (%)', i; >> END LOOP; >> END; $$; >> >> 2. Use example code in attachments. >> 3. Application crashes with memory access violation after >> calling >> SQLGetDiagRecW function, with iRecord = 332. >> Application doesn't crash if number of iterations in >> procedure's for >> loop is changed. >> Expected outcome: SQLGetDiagRecW would return SQL_NO_DATA >> when there's >> no more diagnostic records. >> >> Regards, >> Andrey >> --------------GkW2C0inWqcuk0bljGAjM0Qh Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Hi Dave,

I think it should solve this problem. Thank you for your help.

It seems that there won't be a problem with use of unsigned values, since all values that could be negative are handled.
I'm not sure if "stapos" should be short.   Product of 2byte*2byte variables could require 4 bytes to hold the value, 
if it's possible for "error->recsize" and "RecNumber" to hold big enough values at the same time.
Maybe make "stapos" signed/unsigned int to make sure it won't happen?

Regards,
Andrey


27.02.2024 21:21, Dave Cramer wrote:

On Tue, 27 Feb 2024 at 01:07, Andrey Sukhanov <siwenter@gmail.com> wrote:
Hi Dave,

Thank you for your time.
The problem is caused by signed integer overflow, which leads to attempt to read from unallocated memory.

I have downloaded psqlodbc 16 and debug symbols from https://www.postgresql.org/ftp/odbc/versions/.
Source code from for psqlodbc 16 from https://git.postgresql.org/git/psqlodbc.git.
I compiled the attached program with msvc 14 and msvc 16 compilers (same results). Program should be compiled with defined "UNICODE" to use functions (SQLGetDiagRecW etc.) that accept wchar.

On windows 10 attached program crashes inside ER_ReturnError line environ.c:249. Where it tries to memcpy from the buffer "msg" with offset "stapos".


"msg" is a valid pointer to "pgerror->__error_msg". "pgerror" also have field "pgerror->errorsize" with value "32690".
Offset "stapos" is calculated as "stapos = (RecNumber - 1) * error->recsize;" 
Here "error" is "pgerror", "recsize" = 99, "RecNumber" = 332.
"stapos" should be assigned the value of 32769  but it has type SWORD (short int) (INT16_MAX is 32767).
Expression"if (stapos > msglen)" (here "msglen" = 32690) evaluates to "false", because of signed integer overflow.  It should've been "true" instead, and function would've returned SQL_NO_DATA_FOUND.
Callstack:
     vcruntime140.dll!memcpy() line 438
     psqlodbc35w.dll!ER_ReturnError(PG_ErrorInfo * pgerror, short RecNumber, unsigned char * szSqlState, long * pfNativeError, unsigned char * szErrorMsg, short cbErrorMsgMax, short * pcbErrorMsg, unsigned short) line 250
     psqlodbc35w.dll!PGAPI_StmtError(void * hstmt, short RecNumber, unsigned char * szSqlState, long * pfNativeError, unsigned char * szErrorMsg, short cbErrorMsgMax, short * pcbErrorMsg, unsigned short) line 1626
     psqlodbc35w.dll!PGAPI_GetDiagRec(short) line 57
     psqlodbc35w.dll!SQLGetDiagRecW(short fHandleType, void * handle, short iRecord, wchar_t * szSqlState, long * pfNativeError, wchar_t * szErrorMsg, short cbErrorMsgMax, short * pcbErrorMsg) line 234
     odbc32.dll!SQLGetDiagRecW()
     odbc.pg.exe!CollectDiagRecords(short hndlType, void * hndl) line 30
     odbc.pg.exe!main(int argc, char * * argv) line 117

That means, that the problem is caused by signed integer overflow, which in turn causes memcpy to read from unallocated memory.
The easiest way to reproduce this problem is to try increasing the number of "raise notice" messages in for loop. If their number gets past certain point, no diagnostic records from SQLGetDiagRec are being returned at all. Then I've decreased the number of these messages until they started appearing again. In my case at this number I am able to reproduce the problem. With further decrease of this number problem doesn't appear anymore.

I can't fix this problem myself, because I'm not familiar with the project, and I don't know how to properly test psqlodbc on different platforms.

Regards,
Andrey


27.02.2024 3:11, Dave Cramer wrote:
Hi Andrey,

I've tried on PostgreSQL 11 and 15 with no luck to replicate this problem.

Is it possible for you to debug it and send more information?

Dave Cramer


On Thu, 22 Feb 2024 at 01:29, Andrey Sukhanov <siwenter@gmail.com> wrote:
Dear pgsql-odbc developers,

Windows 10,  psqlodbc 16 (psqlodbc35w.dll), postgresql 11.
Getting certain amount of diagnostic records with SQLGetDiagRecW crashes
the application with memory access violation.

Steps to reproduce:
1. Create procedure:
CREATE OR REPLACE PROCEDURE crashme()
LANGUAGE plpgsql
AS $$
BEGIN
FOR i IN 1..841 LOOP
         RAISE NOTICE 'msgmsgmsgmsg (%)', i;
END LOOP;
END; $$;

2. Use example code in attachments.
3. Application crashes with memory access violation after calling
SQLGetDiagRecW function, with iRecord = 332.
Application doesn't crash if number of iterations in  procedure's for
loop is changed.
Expected outcome: SQLGetDiagRecW would return SQL_NO_DATA when there's
no more diagnostic records.

Regards,
Andrey
--------------GkW2C0inWqcuk0bljGAjM0Qh--