X-Original-To: pgsql-odbc-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A069B9FB3C2 for ; Fri, 18 Aug 2006 11:13:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 55781-08 for ; Fri, 18 Aug 2006 11:13:41 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from samwel.tk (bsamwel.xs4all.nl [82.92.179.183]) by postgresql.org (Postfix) with ESMTP id 20E549FB38A for ; Fri, 18 Aug 2006 11:13:41 -0300 (ADT) Received: from localhost ([127.0.0.1]) by samwel.tk with esmtp (Exim 4.62) (envelope-from ) id 1GE56Q-0000C5-6P for pgsql-odbc@postgresql.org; Fri, 18 Aug 2006 16:13:38 +0200 Message-ID: <44E5CB09.1040005@samwel.tk> Date: Fri, 18 Aug 2006 16:13:29 +0200 From: Bart Samwel User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: pgsql-odbc@postgresql.org Subject: Buffer overrun in handle_notice_message() Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: bart@samwel.tk X-SA-Exim-Scanned: No (on samwel.tk); SAEximRunCond expanded to false X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.135 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200608/63 X-Sequence-Number: 8027 Hi all, I'd like to report a buffer overrun in handle_notice_message(). Analysis: when I run a query >4096 characters that has a 'C' at a specific location (at a specific, small offset before a multiple of 4096), then the stack is trashed and the driver crashes. The code fragment: for (;;) { truncated = SOCK_get_string(sock, msgbuffer, sizeof(msgbuffer)); if (!msgbuffer[0]) break; mylog("%s: 'N' - %s\n", comment, msgbuffer); qlog("NOTICE from backend during %s: '%s'\n", comment, msgbuffer); switch (msgbuffer[0]) { case 'S': strncat(msgbuf, msgbuffer + 1, buflen); strncat(msgbuf, ": ", buflen); buflen -= (strlen(msgbuffer) + 1); break; case 'M': strncat(msgbuf, msgbuffer + 1, buflen); msg_truncated = truncated; break; case 'C': if (sqlstate && !sqlstate[0] && strcmp(msgbuffer + 1, "00000")) strcpy(sqlstate, msgbuffer + 1); break; } } The problem is that PostgreSQL replies to the query with a "LOG"-type message, quoting the _complete_ query. This loop retrieves the message in chunks of 4096 bytes (i.e., sizeof(msgbuffer)) and normally just outputs things to mylog and qlog, _except_ if the first character of a chunk just happens to be 'S', 'M' or 'C'. In the 'S' or 'M' cases, nothing bad happens, but in the 'C' case, the strcpy(sqlstate,msgbuffer+1) copies 4 kbytes of data into the 8-byte sqlstate buffer. The way I understand it, the switch on msgbuffer[0] should only happen on the first iteration, not on subsequent iterations. After I changed the loop like that, my application ran without any problems. Oh, in case you're wondering: protocol is 7.4+, PostgreSQL server version is 8.1.4. Cheers, Bart