Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×

PostgreSQL 8.1.4 Released to Plug Injection Hole 162

alurkar writes to tell us that PostgreSQL released version 8.1.4 today in order to combat a security flaw allowing a SQL injection attack. From the article: "The vulnerability affects PostgreSQL servers exposed to untrusted input, such as input coming from Web forms, in conjunction with multi-byte encodings like (Shift-JIS (SJIS), 8-bit Unicode Transformation Format (UTF-8), 16-bit Unicode Transformation Format (UTF-16), and BIG5. In particular, Berkus says that applications using 'ad-hoc methods to "escape" strings going into the database, such as regexes, or PHP3's addslashes() and magic_quotes' are particularly unsafe. 'Since these bypass database-specific code for safe handling of strings, many such applications will need to be re-written to become secure.'"
This discussion has been archived. No new comments can be posted.

PostgreSQL 8.1.4 Released to Plug Injection Hole

Comments Filter:
  • by ByTor-2112 ( 313205 ) on Tuesday May 23, 2006 @09:50PM (#15391065)
    Most of the PHP apps I've ever had the (mis)pleasure to peruse make liberal use of this type of "escaping" rather than calling the provided "escape_string" functions. That never made any sense to me, but the practice appears to be quite common.
  • Re:This is why... (Score:5, Informative)

    by jrockway ( 229604 ) * <jon-nospam@jrock.us> on Tuesday May 23, 2006 @09:54PM (#15391080) Homepage Journal
    It especially bugs me because it's easier to Do Things Right. The DBI manpage for perl doesn't even mention the sloppy way that nearly everyone uses... but they do it anyway! In nearly every database application / script I look at, people do things like $dbh->execute("SELECT * FROM foo WHERE bar=$bar AND baz=$baz") after "escaping" $bar and $baz. No, no, no!

    It's much easier to prepare a query handle and then execute it as needed:

    $sth = $dbh->prepare("SELECT a,b,c FROM foo WHERE bar=? and baz=?")
    $sth->execute($bar, $baz);


    Not only is it more efficient (if you're going to use the same query twice), it's secure by default. Let the database programmers handle the Hard Stuff (parsing) so that you can concentrate on your application.

    Speaking of which, is there a way to do this in PHP? I've never seen a PHP script that did anything like this (which is probably why bugtraq is 99% php SQL injection holes).
  • by Bogtha ( 906264 ) on Tuesday May 23, 2006 @09:57PM (#15391090)

    Mismatches between different character encodings seem to have been responsible for vast swathes of security vulnerabilities over the past few years. The sooner everybody moves to programming languages and software that use Unicode natively, the more secure we will all be.

    Unfortunately, the languages receiving the most attention for web development have abysmal Unicode support. PHP and Ruby haven't a clue, although the next version of PHP is supposed to be much better in this respect. Python developers can at least handle things fairly well, although it's still a bit of a pain in the neck.

    This vulnerability is probably going to cause quite a few problems for people, as it's a client issue that will probably need whatever adapter you use to be updated. Here is the user guide to the vulnerability for PostgreSQL [postgresql.org]. psycopg should be fixed shortly [initd.org].

  • Re:This is why... (Score:3, Informative)

    by onlyjoking ( 536550 ) on Tuesday May 23, 2006 @10:12PM (#15391147)
    Speaking of which, is there a way to do this in PHP?

    PHP5's mysqli extension enables you to use prepared queries.

  • by chibi.nowheregirl ( 974380 ) on Tuesday May 23, 2006 @10:15PM (#15391154) Homepage
    As long as you set the right multibyte string encoding in PHP via the multibyte string functions [php.net] (specifically, the mb_internal_encoding [php.net] function), the parser will catch the invalid multibyte sequence and fix it.

    Move along, folks. No need to panic.
  • Use placeholders! (Score:5, Informative)

    by mortonda ( 5175 ) on Tuesday May 23, 2006 @10:26PM (#15391198)
    This is why I gripe and complain anytime I see someone doing sql calls without using placeholder routines, such as perl's DBI or PEAR::DB for php. From the technical doc [postgresql.org] that someone posted above:
    If your code is doing escaping "by hand", for instance by doubling quotes and backslashes, you really need to fix it to use the library routines instead. If you're avoiding the need for escaping at all, by sending variable strings as out-of-line parameters, then you've saved yourself a whole lot of trouble and can stop worrying.
    Start using a proper placeholder syntax and variable substitution for parameters when it comes to untrusted data. It solves a lot of problems.
  • by edwdig ( 47888 ) on Tuesday May 23, 2006 @10:37PM (#15391255)
    Unicode isn't a character encoding, it's a character set. According to this unicode faq [cam.ac.uk], there are 13 different encodings for Unicode. Switching to Unicode doesn't help the problem of character encodings.
  • Re:This is why... (Score:5, Informative)

    by Dwonis ( 52652 ) * on Tuesday May 23, 2006 @10:47PM (#15391297)
    Speaking of which, is there a way to do this in PHP? I've never seen a PHP script that did anything like this (which is probably why bugtraq is 99% php SQL injection holes).

    Most people probably aren't aware of it, but several years ago, I wrote a few short scripts for PHP 4 [dlitz.net] that specifically address this problem. Currently-supported database backends are MySQL and anything that DBX supports, but it wouldn't take much to adapt it to PostgreSQL.

    It basically lets you write code like this:

    require_once "mysqlext.php";
    $link = mysql_connect(...);
    $results = mysql_execute($link, "SELECT a,b,c FROM foo WHERE bar=? and baz=?", array($bar, $baz));

    It doesn't have the performance benefits that real prepared statements have, but I still find it handy for typical PHP4 database work.

    The code is released under the MIT license, so feel free to use it.

  • Re:This is why... (Score:5, Informative)

    by Slashcrunch ( 626325 ) on Tuesday May 23, 2006 @11:15PM (#15391405) Homepage
    For PHP, Zend_Db has a way of doing this which is very similar to the way you do it in Perl and Java. It's quite nice. There are other ways of doing this as well :) // get a Zend_Db_Adapter (basically a DB connection)
    $db = getConnection(); // the sql with a placeholder for a parameter called 'id'
    $sql = 'select * from Foo where id = :id'; // anyparameters are defined in the array. in this case, just 'id'
    $params = array('id' => $id); // send the query
    $result = $db->query($sql, $params);
  • by RuneB ( 170521 ) on Tuesday May 23, 2006 @11:19PM (#15391420)
    PostgreSQL ignored invalid UTF-8 sequences, meaning a ' character at the end of a incomplete sequence could cause only one ' to be seen by the parser when escaped.
    See http://www.postgresql.org/docs/techdocs.50 [postgresql.org] for the details.
  • Re:This is why... (Score:3, Informative)

    by OnyxRaven ( 9906 ) on Tuesday May 23, 2006 @11:44PM (#15391509) Homepage
    PEAR::DB supports almost the exact same method.

    $data = array('one',2);

    (short)
    $result = $db->query('select * from table where foo=? and bar=?',$data);

    (prepare)
    $stmt = $db->prepare('select * from table where foo=? and bar=?');
    $result = $db->execute($stmt,$data);

    Works with mysql, pgsql, mssql... etc etc. MDB2 is the new version of this library which uses much the same syntax. Uses database-specific escaping/quoting automatically.

    http://pear.php.net/manual/en/package.database.php [php.net]
  • Re:This is why... (Score:2, Informative)

    by a.d.trick ( 894813 ) on Wednesday May 24, 2006 @12:50AM (#15391760) Homepage
    Yes, but properly escaping everything is at least as important. Whitelisting and blacklisting can't be used in a lot of situations (for example text fields) without causing problems. The easiest way to do that is to use an existing library that handles most of that for you. The more you have automated, the less room there is for human error. Unfortunatly, PHP coders tend to trive in reinventing the wheel.
  • by Jac_no_k ( 5957 ) <jsuzuki@spamcop.net> on Wednesday May 24, 2006 @02:44AM (#15392161) Homepage Journal

    I recently switched from coding for single company to joining a consulting firm. I'm shocked at how sloppy the commands sent to the database are. It drives me nuts and makes me want to fix all the code... but since I'm low man on the totem pole, my concerns are replied generally with lame excuses like "emulate the coding style of the original author", "we don't get paid much, so it's okay to be sloppy", or "we have a deadline to meet". And no, I can't find a new gig.

    Some of the sites I've worked with are vulnerable to this type of injection attack. From my perspective, this is widespread, a bit scary, and should be nice little eye openner as sites get hacked. This may actually be a blessing as I could start pointing what happens with sloppy code and not being given enough time clean everything up.

  • by ultranova ( 717540 ) on Wednesday May 24, 2006 @05:21AM (#15392577)

    Loads of fun that worthless whore is.

    If a whore is "loads of fun", then she's not worthless, since she can propably get a good price once the word spreads. Just because you are trolling is no excuse to be illogical.

    Now let's see if someone mods me Insightfull or Informative...

UNIX is hot. It's more than hot. It's steaming. It's quicksilver lightning with a laserbeam kicker. -- Michael Jay Tucker

Working...