pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feedBuffer overrun in handle_notice_message()
2+ messages / 2 participants
[nested] [flat]
* Buffer overrun in handle_notice_message()
@ 2006-08-18 14:13 Bart Samwel <bart@samwel.tk>
0 siblings, 1 reply; 2+ messages in thread
From: Bart Samwel @ 2006-08-18 14:13 UTC (permalink / raw)
To: pgsql-odbc
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Buffer overrun in handle_notice_message()
@ 2006-08-18 21:30 Hiroshi Inoue <inoue@tpf.co.jp>
parent: Bart Samwel <bart@samwel.tk>
0 siblings, 0 replies; 2+ messages in thread
From: Hiroshi Inoue @ 2006-08-18 21:30 UTC (permalink / raw)
To: Bart Samwel <bart@samwel.tk>; +Cc: pgsql-odbc
Bart Samwel wrote:
> 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;
> }
> }
Hi Bart,
Hmm, ISTM I should place the following code at the end of the above loop
not after the above code..
while (truncated)
truncated = SOCK_get_string(sock, msgbuffer,
sizeof(msgbuffer));
Actually I do so In handle_error_message().
I would fix it ASAP.
Thanks.
regards,
Hiroshi Inoue
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2006-08-18 21:30 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-08-18 14:13 Buffer overrun in handle_notice_message() Bart Samwel <bart@samwel.tk>
2006-08-18 21:30 ` Hiroshi Inoue <inoue@tpf.co.jp>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox