Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XPy1C-0005F1-L8 for pgsql-odbc@arkaria.postgresql.org; Fri, 05 Sep 2014 18:13:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XPy1C-0003JG-5Y for pgsql-odbc@arkaria.postgresql.org; Fri, 05 Sep 2014 18:13:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XPy1B-0003J9-C5 for pgsql-odbc@postgresql.org; Fri, 05 Sep 2014 18:13:53 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XPy16-0003ZC-Hq for pgsql-odbc@postgresql.org; Fri, 05 Sep 2014 18:13:52 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 4FAB428DFD for ; Fri, 5 Sep 2014 11:13:43 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (EX13-CAS-003.vmware.com [10.113.191.53]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 475AD90203 for ; Fri, 5 Sep 2014 11:13:43 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-006.vmware.com (10.113.191.26) with Microsoft SMTP Server (TLS) id 15.0.775.38; Fri, 5 Sep 2014 11:13:28 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Fri, 5 Sep 2014 11:13:28 -0700 Message-ID: <5409FD3F.40403@vmware.com> Date: Fri, 5 Sep 2014 21:13:19 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.0 MIME-Version: 1.0 To: "pgsql-odbc@postgresql.org" Subject: Buffer overflows in SQLGetData corner cases Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -8.6 (--------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org If a PostgreSQL timestamp column is fetched using SQLGetData, into string buffer (SQL_C_CHAR), and the output buffer's length is exactly 20 bytes, and the timestamp value has a year larger than 10000 or smaller than 0, the output buffer will be overflown. The core of the problem is this, in copy_and_convert_field(): > case PG_TYPE_ABSTIME: > case PG_TYPE_DATETIME: > case PG_TYPE_TIMESTAMP_NO_TMZONE: > case PG_TYPE_TIMESTAMP: > len = 19; > if (cbValueMax > len) > { > /* sprintf(rgbValueBindRow, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", > std_time.y, std_time.m, std_time.d, std_time.hh, std_time.mm, std_time.ss); */ > stime2timestamp(&std_time, rgbValueBindRow, FALSE, > PG_VERSION_GE(conn, 7.2) ? (int) cbValueMax - len - 2 : 0); > len = strlen(rgbValueBindRow); > } > break; It checks if the output buffer is at least 20 bytes wide, and bails out if it isn't. But 20 bytes isn't enough for some timestamp values that might come from a PostgreSQL server, e.g: postgres=# select length('1011-02-15 15:49:18 BC'::timestamp::text); length -------- 22 (1 row) A better approach to this would be to pass the max length to stime2timestamp, and let it truncate it. stime2timestamp uses sprintf, which is easy to change to snprintf. As a bonus, the SQL standard behavior is to truncate the string anyway, rather than refuse to return anything if the whole value doesn't fit. While looking at this, I noticed that the SQL_C_WCHAR conversion code doesn't add a NULL-terminator to the string, if the output buffer's size is not divisible by two. Now, that's an even more obscure corner case, but I think we should make sure that the returned string is always null-terminated, even if the buffer length is a bit strange. I've pushed fixes for these bugs to the git repository. - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc