~ubuntu-branches/ubuntu/vivid/postgresql-8.4/vivid

« back to all changes in this revision

Viewing changes to src/interfaces/libpq/fe-secure.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2012-02-25 11:17:15 UTC
  • mfrom: (13.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120225111715-b7tf22u4p8ok7hd9
Tags: 8.4.11-1
* Urgency medium due to security fixes.
* New upstream bug fix/security release:
  - Require execute permission on the trigger function for "CREATE
    TRIGGER".
    This missing check could allow another user to execute a trigger
    function with forged input data, by installing it on a table he
    owns. This is only of significance for trigger functions marked
    SECURITY DEFINER, since otherwise trigger functions run as the
    table owner anyway. (CVE-2012-0866)
  - Remove arbitrary limitation on length of common name in SSL
    certificates.
    Both libpq and the server truncated the common name extracted from
    an SSL certificate at 32 bytes. Normally this would cause nothing
    worse than an unexpected verification failure, but there are some
    rather-implausible scenarios in which it might allow one
    certificate holder to impersonate another. The victim would have to
    have a common name exactly 32 bytes long, and the attacker would
    have to persuade a trusted CA to issue a certificate in which the
    common name has that string as a prefix. Impersonating a server
    would also require some additional exploit to redirect client
    connections. (CVE-2012-0867)
  - Convert newlines to spaces in names written in pg_dump comments.
    pg_dump was incautious about sanitizing object names that are
    emitted within SQL comments in its output script. A name containing
    a newline would at least render the script syntactically incorrect.
    Maliciously crafted object names could present a SQL injection risk
    when the script is reloaded. (CVE-2012-0868)
  - Fix btree index corruption from insertions concurrent with
    vacuuming.
    An index page split caused by an insertion could sometimes cause a
    concurrently-running "VACUUM" to miss removing index entries that
    it should remove. After the corresponding table rows are removed,
    the dangling index entries would cause errors (such as "could not
    read block N in file ...") or worse, silently wrong query results
    after unrelated rows are re-inserted at the now-free table
    locations. This bug has been present since release 8.2, but occurs
    so infrequently that it was not diagnosed until now. If you have
    reason to suspect that it has happened in your database, reindexing
    the affected index will fix things.
  - Update per-column permissions, not only per-table permissions, when
    changing table owner.
    Failure to do this meant that any previously granted column
    permissions were still shown as having been granted by the old
    owner. This meant that neither the new owner nor a superuser could
    revoke the now-untraceable-to-table-owner permissions.
  - Allow non-existent values for some settings in "ALTER USER/DATABASE
    SET".
    Allow default_text_search_config, default_tablespace, and
    temp_tablespaces to be set to names that are not known. This is
    because they might be known in another database where the setting
    is intended to be used, or for the tablespace cases because the
    tablespace might not be created yet. The same issue was previously
    recognized for search_path, and these settings now act like that
    one.
  - Avoid crashing when we have problems deleting table files
    post-commit.
    Dropping a table should lead to deleting the underlying disk files
    only after the transaction commits. In event of failure then (for
    instance, because of wrong file permissions) the code is supposed
    to just emit a warning message and go on, since it's too late to
    abort the transaction. This logic got broken as of release 8.4,
    causing such situations to result in a PANIC and an unrestartable
    database.
  - Track the OID counter correctly during WAL replay, even when it
    wraps around.
    Previously the OID counter would remain stuck at a high value until
    the system exited replay mode. The practical consequences of that
    are usually nil, but there are scenarios wherein a standby server
    that's been promoted to master might take a long time to advance
    the OID counter to a reasonable value once values are needed.
  - Fix regular expression back-references with - attached.
    Rather than enforcing an exact string match, the code would
    effectively accept any string that satisfies the pattern
    sub-expression referenced by the back-reference symbol.
    A similar problem still afflicts back-references that are embedded
    in a larger quantified expression, rather than being the immediate
    subject of the quantifier. This will be addressed in a future
    PostgreSQL release.
  - Fix recently-introduced memory leak in processing of inet/cidr
    values.
  - Fix dangling pointer after "CREATE TABLE AS"/"SELECT INTO" in a
    SQL-language function.
    In most cases this only led to an assertion failure in
    assert-enabled builds, but worse consequences seem possible.
  - Fix I/O-conversion-related memory leaks in plpgsql.
  - Improve pg_dump's handling of inherited table columns.
    pg_dump mishandled situations where a child column has a different
    default expression than its parent column. If the default is
    textually identical to the parent's default, but not actually the
    same (for instance, because of schema search path differences) it
    would not be recognized as different, so that after dump and
    restore the child would be allowed to inherit the parent's default.
    Child columns that are NOT NULL where their parent is not could
    also be restored subtly incorrectly.
  - Fix pg_restore's direct-to-database mode for INSERT-style table
    data.
    Direct-to-database restores from archive files made with
    "--inserts" or "--column-inserts" options fail when using
    pg_restore from a release dated September or December 2011, as a
    result of an oversight in a fix for another problem. The archive
    file itself is not at fault, and text-mode output is okay.
  - Allow AT option in ecpg DEALLOCATE statements.
    The infrastructure to support this has been there for awhile, but
    through an oversight there was still an error check rejecting the
    case.
  - Fix error in "contrib/intarray"'s int[] & int[] operator.
    If the smallest integer the two input arrays have in common is 1,
    and there are smaller values in either array, then 1 would be
    incorrectly omitted from the result.
  - Fix error detection in "contrib/pgcrypto"'s encrypt_iv() and
    decrypt_iv().
    These functions failed to report certain types of invalid-input
    errors, and would instead return random garbage values for
    incorrect input.
  - Fix one-byte buffer overrun in "contrib/test_parser".
    The code would try to read one more byte than it should, which
    would crash in corner cases. Since "contrib/test_parser" is only
    example code, this is not a security issue in itself, but bad
    example code is still bad.
  - Use __sync_lock_test_and_set() for spinlocks on ARM, if available.
    This function replaces our previous use of the SWPB instruction,
    which is deprecated and not available on ARMv6 and later. Reports
    suggest that the old code doesn't fail in an obvious way on recent
    ARM boards, but simply doesn't interlock concurrent accesses,
    leading to bizarre failures in multiprocess operation.
  - Use "-fexcess-precision=standard" option when building with gcc
    versions that accept it.
    This prevents assorted scenarios wherein recent versions of gcc
    will produce creative results.
  - Allow use of threaded Python on FreeBSD.
    Our configure script previously believed that this combination
    wouldn't work; but FreeBSD fixed the problem, so remove that error
    check.
* Drop 04-armel-tas.patch, applied upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
675
675
static bool
676
676
verify_peer_name_matches_certificate(PGconn *conn)
677
677
{
 
678
        char       *peer_cn;
 
679
        int                     r;
 
680
        int                     len;
 
681
        bool            result;
 
682
 
678
683
        /*
679
684
         * If told not to verify the peer name, don't do it. Return true indicating
680
685
         * that the verification was successful.
682
687
        if (strcmp(conn->sslmode, "verify-full") != 0)
683
688
                return true;
684
689
 
 
690
        /*
 
691
         * Extract the common name from the certificate.
 
692
         *
 
693
         * XXX: Should support alternate names here
 
694
         */
 
695
        /* First find out the name's length and allocate a buffer for it. */
 
696
        len = X509_NAME_get_text_by_NID(X509_get_subject_name(conn->peer),
 
697
                                                                        NID_commonName, NULL, 0);
 
698
        if (len == -1)
 
699
        {
 
700
                printfPQExpBuffer(&conn->errorMessage,
 
701
                                                  libpq_gettext("could not get server common name from server certificate\n"));
 
702
                return false;
 
703
        }
 
704
        peer_cn = malloc(len + 1);
 
705
        if (peer_cn == NULL)
 
706
        {
 
707
                printfPQExpBuffer(&conn->errorMessage,
 
708
                                                  libpq_gettext("out of memory\n"));
 
709
                return false;
 
710
        }
 
711
 
 
712
        r = X509_NAME_get_text_by_NID(X509_get_subject_name(conn->peer),
 
713
                                                                  NID_commonName, peer_cn, len + 1);
 
714
        if (r != len)
 
715
        {
 
716
                /* Got different length than on the first call. Shouldn't happen. */
 
717
                printfPQExpBuffer(&conn->errorMessage,
 
718
                                                  libpq_gettext("could not get server common name from server certificate\n"));
 
719
                free(peer_cn);
 
720
                return false;
 
721
        }
 
722
        peer_cn[len] = '\0';
 
723
 
 
724
        /*
 
725
         * Reject embedded NULLs in certificate common name to prevent attacks
 
726
         * like CVE-2009-4034.
 
727
         */
 
728
        if (len != strlen(peer_cn))
 
729
        {
 
730
                printfPQExpBuffer(&conn->errorMessage,
 
731
                                                  libpq_gettext("SSL certificate's common name contains embedded null\n"));
 
732
                free(peer_cn);
 
733
                return false;
 
734
        }
 
735
 
 
736
        /*
 
737
         * We got the peer's common name. Now compare it against the originally
 
738
         * given hostname.
 
739
         */
685
740
        if (!(conn->pghost && conn->pghost[0] != '\0'))
686
741
        {
687
742
                printfPQExpBuffer(&conn->errorMessage,
688
743
                                                  libpq_gettext("host name must be specified for a verified SSL connection\n"));
689
 
                return false;
 
744
                result = false;
690
745
        }
691
746
        else
692
747
        {
693
 
                /*
694
 
                 * Connect by hostname.
695
 
                 *
696
 
                 * XXX: Should support alternate names here
697
 
                 */
698
 
                if (pg_strcasecmp(conn->peer_cn, conn->pghost) == 0)
 
748
                if (pg_strcasecmp(peer_cn, conn->pghost) == 0)
699
749
                        /* Exact name match */
700
 
                        return true;
701
 
                else if (wildcard_certificate_match(conn->peer_cn, conn->pghost))
 
750
                        result = true;
 
751
                else if (wildcard_certificate_match(peer_cn, conn->pghost))
702
752
                        /* Matched wildcard certificate */
703
 
                        return true;
 
753
                        result = true;
704
754
                else
705
755
                {
706
756
                        printfPQExpBuffer(&conn->errorMessage,
707
757
                                                          libpq_gettext("server common name \"%s\" does not match host name \"%s\"\n"),
708
 
                                                          conn->peer_cn, conn->pghost);
709
 
                        return false;
 
758
                                                          peer_cn, conn->pghost);
 
759
                        result = false;
710
760
                }
711
761
        }
 
762
 
 
763
        free(peer_cn);
 
764
        return result;
712
765
}
713
766
 
714
767
/*
1337
1390
         * SSL_CTX_set_verify() if root.crt exists.
1338
1391
         */
1339
1392
 
1340
 
        /* pull out server distinguished and common names */
 
1393
        /* get server certificate */
1341
1394
        conn->peer = SSL_get_peer_certificate(conn->ssl);
1342
1395
        if (conn->peer == NULL)
1343
1396
        {
1351
1404
                return PGRES_POLLING_FAILED;
1352
1405
        }
1353
1406
 
1354
 
        X509_NAME_oneline(X509_get_subject_name(conn->peer),
1355
 
                                          conn->peer_dn, sizeof(conn->peer_dn));
1356
 
        conn->peer_dn[sizeof(conn->peer_dn) - 1] = '\0';
1357
 
 
1358
 
        r = X509_NAME_get_text_by_NID(X509_get_subject_name(conn->peer),
1359
 
                                                          NID_commonName, conn->peer_cn, SM_USER);
1360
 
        conn->peer_cn[SM_USER] = '\0'; /* buffer is SM_USER+1 chars! */
1361
 
        if (r == -1)
1362
 
        {
1363
 
                /* Unable to get the CN, set it to blank so it can't be used */
1364
 
                conn->peer_cn[0] = '\0';
1365
 
        }
1366
 
        else
1367
 
        {
1368
 
                /*
1369
 
                 * Reject embedded NULLs in certificate common name to prevent attacks like
1370
 
                 * CVE-2009-4034.
1371
 
                 */
1372
 
                if (r != strlen(conn->peer_cn))
1373
 
                {
1374
 
                        printfPQExpBuffer(&conn->errorMessage,
1375
 
                                                          libpq_gettext("SSL certificate's common name contains embedded null\n"));
1376
 
                        close_SSL(conn);
1377
 
                        return PGRES_POLLING_FAILED;
1378
 
                }
1379
 
        }
1380
 
 
1381
1407
        if (!verify_peer_name_matches_certificate(conn))
1382
1408
        {
1383
1409
                close_SSL(conn);