Forum OpenACS Q&A: Bugtraq: Buffer overflow in PostgreSQL

From: Sir Mordred The Traitor mordred at s-mail.com
Date: Mon Aug 19, 2002  08:40:28 AM US/Pacific
Subject: @(#) Mordred Labs advisory 0x0001: Buffer overflow in 
PostgreSQL


// @(#) Mordred Labs Advisory 0x0001

Release data: 19/08/02
Name: Buffer overflow in PostgreSQL
Versions affected: <= 7.2
Risk: average

--[ Description:
PostgreSQL is an advanced object-relational database 
management system
that supports an extended subset of the SQL standard, including
transactions,
foreign keys, subqueries, triggers, user-defined types and 
functions.

There exists a stack based buffer overflow in cash_words() 
function, that
potentially allows an attacker to execute malicious code.

--[ How to reproduce:
psql> select cash_words('-
700000000000000000000000000000');
pgReadData() -- backend closed the channel unexpectedly.
	.... ....
The connection to the server was lost...

--[ Solution:
Upgrade to version 7.2.1.
and a response to this posting:
From: Florian Weimer 
Date: Mon Aug 19, 2002  10:30:52 AM US/Pacific
To: Sir Mordred The Traitor 
Subject: Re: @(#) Mordred Labs advisory 0x0001: Buffer overflow 
in PostgreSQL


Sir Mordred The Traitor  writes:

--[ How to reproduce:
psql> select cash_words('-
700000000000000000000000000000');
pgReadData() -- backend closed the channel unexpectedly.
	.... ....
The connection to the server was lost...

--[ Solution:
Upgrade to version 7.2.1.

PostgreSQL 7.2.1 has a buffer overflow bug in the date parser 
(which
is invoked each time a string is converted to a datetime object).  
If
a frontend does not perform proper date checking and rejects 
overlong
date strings, a buffer is overwritten by parser.  The string has to
pass some checks of the parser, so it is not immediately 
obvious that
this can be exploited.  Denial of service is possible, though,
especially if the frontend does not automatically reestablish the
database connection. (All connections are affected, not just the 
one
that is issueing the query.)

To my knowledge, the PostgreSQL developers do not think this 
warrants
an additional 7.2.x release.  They expect that users do not trust 
the
PostgreSQL parsers and write input validation checks.  That 
gives me
the creeps---how can I trust a database which manipulates 
complex
in-memory and on-disk data structures to keep my data, if its
developers say I shouldn't rely on a simple thing they wrote, 
such as
a date parser?

A different problem: "select cash_out(2);".  Known for ages, no fix 
in
sight (seems to be a design problem which is not easy to 
resolve).

*sigh*

-- 
Florian Weimer 	                  Weimer@CERT.Uni-Stuttgart.DE
University of Stuttgart           http://CERT.Uni-Stuttgart.DE/people/
fw/
RUS-CERT                          fax +49-711-685-5898


Collapse
Posted by Jon Griffin on
Ahh yes those wonderful PG developers.

What a bunch of arrogant f'ng aholes.

Of course this warrants a new release. It IS a security flaw.

And while they are at it, they can eliminate the 16 parameter "bug" and let friggin identifiers be longer than 32 characters.

End of rant.

Collapse
Posted by Dan Wickstrom on
Last thing I saw on the pg list, they were changing the number of args to 32, and they were changing the length of identifiers to 128.
Collapse
Posted by Jon Griffin on
I hope so, but they need to make export work since you need to initdb to change.
Collapse
Posted by Jade Rubick on
Jon, I love that you reply to each and every security related
posting. It makes me feel more secure knowing security-minded
folk are at work here.

Incidentally, I think Bugtraq is pretty much a must-subscribe to
list. Subscribe to it in digest format, and skim it (there is too to
read it all), and it'll make you a paranoid bastard like Jon 😊

Collapse
Posted by Jon Griffin on
I don't really respond to everyone ;), but this is stupidity (if it is true they don't consider that a hole).
Collapse
Posted by Don Baccus on
Well, Oracle isn't making a special release to fix the fact that one can reliably crash the entire Oracle database server (not just one connection as is the case here) on Solaris if someone connects with a Linux client that uses the OCI.

They're just saying "don't do that".

PG 7.3 will be in beta in about two weeks and final about four weeks later.  It looks like the PG team will have a release out that fixes the problem much earlier than Oracle will have one which fixes theirs.

Note that the PG developers aren't saying they're not fixing it, merely that they're not going through a full release cycle to get out a new PG 7.2 release.

What does this mean in practice?  Instead of waiting a week or two for a PG 7.2 release with the fix  you'll have to wait two or three weeks for a PG 7.3 beta and about 6-7 weeks for PG 7.3 final.

Collapse
Posted by Jon Griffin on
I still don't buy the excuse. Stallman created patch and they should be able to use that. Granted it may not affect most people but it is still a security flaw and needs to be addressed now, not on MS bugfix time.

Even though I understand their thinking (and obviously don't agree) it is at the least very bad for PR. Explain to your next client that you are trying to convince to leave IIS/SQLServer,MySQL or even Oracle, that PG's developers determined they don't want to fix buffer overflows, and if you get DOS'd or cracked it is your problem. Security problems need to be fixed rapidly, this isn't another feature being added.

I certainly don't hold Oracle (unbreakable?) as the standard for security.

Collapse
Posted by Marc Spitzer on
I think there was some discussion about this on pg hackers.  If I remember correctly, it was stated that yes there is a buffer over flow and there was no way to exploit it.  You could not inject malicious code.  It was more of a dos then an exploit.
From: Sir Mordred The Traitor mordred@s-mail.com
Date: Tue Aug 20, 2002  08:01:34 AM US/Pacific
To: bugtraq@securityfocus.com
Subject: @(#)Mordred Labs advisory 0x0004: Multiple buffer 
overflows in PostgreSQL.



//@(#) Mordred Labs advisory 0x0004

Release data: 20/08/02
Name: Two buffer overflows in PostgreSQL
Versions affected: all versions
Conditions: multibyte support
Risk: average

--[ Description:

I guess all of you already hear about the PostgreSQL.
If not, try to visit
http://www.postgresql.org/idocs/index.php?preface.html#INTRO-
WHATIS.

There are two buffer overflows in src/backend/utils/adt/
oracle_compat.c.
1) lpad(text, integer, text) function
2) rpad(text, integer, text) function

--[ Details:

The code for this functions is
src/backend/utils/adt/oracle_compat.c::lpad() and
src/backend/utils/adt/oracle_compat.c::rpad() respectively.
The code suffers from a buffer overflow (of course).

--[ How to reproduce:

shell> pgsql template1 postgres
template1=# select version();
                          version
-----------------------------------------------------------
 PostgreSQL 7.2 on i686-pc-linux-gnu, compiled by GCC 2.96
(1 row)

template1=# create database my_db with encoding='UNICODE';
CREATE DATABASE
template1# c my_db
You are now connected to database my_db.

my_db=# select lpad('xxxxx',1431655765,'yyyyyyyyyyyyyyyy');
pqReadData() -- backend closed the channel unexpectedly.
        This probably means the backend terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!#

The same for rpad() function.

The vulnerable encodings are: EUC_JP, EUC_CN, EUC_KR, 
EUC_TW, UNICODE,
MULE_INTERNAL.

--[ Solution

Secure coding of web applications, input validation checks...etc...
Collapse
Posted by Jade Rubick on
Let me know if you'd rather not receive this:
From: Sir Mordred The Traitor 
Date: Tue Aug 20, 2002  07:28:49 AM US/Pacific
To: bugtraq@securityfocus.com
Subject: @(#)Mordred Labs advisory 0x0003: Buffer overflow in 
PostgreSQL


//@(#)Mordred Labs advisory 0x0003

Release data: 20/08/02
Name: Buffer overflow in PostgreSQL
Versions affected: all versions
Risk: high

--[ Description:

...PostgreSQL is a sophisticated Object-Relational DBMS,
supporting almost all SQL constructs, including subselects,
transactions, and user-defined types and functions. It is the
most advanced open-source database available 
anywhere...blah...blah...
For more info check out this link:
http://www.postgresql.org/idocs/index.php?preface.html#INTRO-
WHATIS

There exists a heap buffer overflow in a repeat(text, integer) 
function,
which
allows an attacker to execute malicious code.

--[ Details:

Upon invoking a repeat() function, a
src/backend/utils/adt/oracle_compat.c::repeat() function
will gets called which suffers from a buffer overflow.

--[ How to reproduce:

psql> select repeat('xxx',1431655765);
pqReadData() -- backend closed the channel unexpectedly.
        This probably means the backend terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

--[ Solution

Do you still running postgresql? ...Can't believe that...
If so, execute the following command as a root: "killall -9 
postmaster",
and wait until the patch will be available.
Collapse
Posted by Jade Rubick on
From: Florian Weimer 
Date: Tue Aug 20, 2002  01:43:23 PM US/Pacific
To: Sir Mordred The Traitor 
Subject: Re: @(#)Mordred Labs advisory 0x0003: Buffer overflow 
in PostgreSQL


Sir Mordred The Traitor  writes:

--[ Solution

Do you still running postgresql? ...Can't believe that...
If so, execute the following command as a root: "killall -9 
postmaster",
and wait until the patch will be available.

There's no need for such drastic action.  Executing

DROP FUNCTION "repeat" (text, integer);

as the PostgreSQL superuser (usually "postgres") is sufficient in 
this
case.  Most installations won't ever need this procedure anyway.

By the way: This bug is very similar to the xdr_array/calloc/new[] 
bug
(see e.g. http://cert.uni-stuttgart.de/advisories/calloc.php).

-- 
Florian Weimer 	                  Weimer@CERT.Uni-Stuttgart.DE
University of Stuttgart           http://CERT.Uni-Stuttgart.DE/people/
fw/
RUS-CERT                          fax +49-711-685-5898
Collapse
Posted by Don Baccus on
If you follow his advice:
<blockquote><i>
Do you still running postgresql? ...Can't believe that...
If so, execute the following command as a root: "killall -9
postmaster"</i></blockquote>
you will in all likeliness corrupt your database.  Never kill -KILL your postmaster.
<p>As far as the problems he's been asked to post the bugs to the group so they can fix them before he posts them to bugtraq.  He say he'll cooperate.
<p>As to the actual risk to your systems, I do hope that no one here exposes their database to the outside world on the internet without enabling password protection for the database????
<p>When I run PG on the same server as AOLserver I don't even start postmaster with "-i".
<p>Whether or not these bugs can be turned into exploits is debatable, however if anyone exposes their RDBMS to the outside world without password protection their stupidity isn't debatable at all.
<p>These will all be fixed in PG 7.3 for sure.
Collapse
Posted by Lamar Owen on
As a PG developer, let me answer these issues briefly.
<p>
1.)    A 7.2.2 is being prepared as I write this.  7.2.2 has the
majority of these issues addressed.
<p>
2.)    Sir Mordred did not notify the PG community prior to posting
to bugtraq.
<p>
3.)    Jon, Larry Wall created patch, rms didn't. 😊
<p>
4.)    Exploiting the datetime parser overrun will be exceedingly
difficult.  It IS being fixed, however.
<p>
5.)    The DoS attacks against cash_words and cash_out require SQL
command line access, as does the attack against repeat.
<p>
6.)    Don't overblow the issue.  It is being addressed and will be
addressed in an expeditious manner.
<p>
7.3 will be in beta on schedule.  7.2.2 won't have any changes that
require initdb, though.  7.3 beta is slated for Sep 1. 7.2.2 will
probably be released either over the weekend or early next week.
<p>
So much for the arrogant PG developers... 😊
Collapse
Posted by Jon Griffin on
You are right of course, a memory lapse as I knew that ;)
Collapse
Posted by Jon Griffin on
Also, IRT arrogance, I am extremely sensitive to security issues even if they are "hard" to exploit. It is all in perception and this is the first I heard of a 7.2.2 release (although I don't hang out at PG) and that makes me happy.

When 7.3 comes out will it require an initdb? The reason I ask is that pg_dump is really unreliable and if it is true that the parameters and identifiers will be lengthened (and logging improved), then it will require an easy way to dump/restore. Coming from Oracle land I never had this problem, but I tried to dump a 7.2.1 db and when I reimported it no matter what I tried it failed.

Collapse
Posted by Jonathan Ellis on
if 7.3 follows the typical pg pattern, it will require a dump/import.

it's true that PG's dump still gets things wrong but I've never seen it hose things so badly that it couldn't be loaded w/ manual editing.  It took half a dozen tries to get everything straightened out when I upgraded from 7.1.3 to 7.2.1 but it got there eventually. :)

Collapse
Posted by Lamar Owen on
Oh, one other point.  If you just absolutely MUST have fixed code
RIGHT NOW, do a cvs checkout on REL7_2_STABLE.
<p>
That would involve using the -r option to checkout: cvs co -r
REL7_2_STABLE pgsql (set cvsroot properly first, of course).
Instructions for doing that are in the PostgreSQL docs under 'cvs
access'.
<p>
Understand that in the next few days REL7_2_STABLE will CHANGE in
various ways as the 7.2.2 release is hammered out.
Collapse
Posted by Lamar Owen on
7.3 will require a dump/initdb/restore cycle from 7.2.x, and you
will need to use the 7.3 pg_dump to dump your 7.2.x database.  Don't
shoot the messenger -- I don't like it any better than anyone else.
Collapse
Posted by Lamar Owen on
Barring any bad build problems, expect 7.2.2 to be released
tomorrow.  RPMs will follow in a day or two -- a prerelease source
RPM is on ftp.postgresql.org now.
Collapse
Posted by Lamar Owen on
Announced August 23.