1. 05 Oct, 2015 9 commits
    • Stephen Frost's avatar
      Add regression tests for INSERT/UPDATE+RETURNING · be400cd2
      Stephen Frost authored
      This adds regressions tests which are specific to INSERT+RETURNING and
      UPDATE+RETURNING to ensure that the SELECT policies are added as
      WithCheckOptions (and should therefore throw an error when the policy is
      violated).
      
      Per suggestion from Andres.
      
      Back-patch to 9.5 as the prior commit was.
      be400cd2
    • Noah Misch's avatar
      Prevent stack overflow in query-type functions. · 5976097c
      Noah Misch authored
      The tsquery, ltxtquery and query_int data types have a common ancestor.
      Having acquired check_stack_depth() calls independently, each was
      missing at least one call.  Back-patch to 9.0 (all supported versions).
      5976097c
    • Noah Misch's avatar
      Prevent stack overflow in container-type functions. · 30cb1288
      Noah Misch authored
      A range type can name another range type as its subtype, and a record
      type can bear a column of another record type.  Consequently, functions
      like range_cmp() and record_recv() are recursive.  Functions at risk
      include operator family members and referents of pg_type regproc
      columns.  Treat as recursive any such function that looks up and calls
      the same-purpose function for a record column type or the range subtype.
      Back-patch to 9.0 (all supported versions).
      
      An array type's element type is never itself an array type, so array
      functions are unaffected.  Recursion depth proportional to array
      dimensionality, found in array_dim_to_jsonb(), is fine thanks to MAXDIM.
      30cb1288
    • Noah Misch's avatar
      Prevent stack overflow in json-related functions. · 08fa47c4
      Noah Misch authored
      Sufficiently-deep recursion heretofore elicited a SIGSEGV.  If an
      application constructs PostgreSQL json or jsonb values from arbitrary
      user input, application users could have exploited this to terminate all
      active database connections.  That applies to 9.3, where the json parser
      adopted recursive descent, and later versions.  Only row_to_json() and
      array_to_json() were at risk in 9.2, both in a non-security capacity.
      Back-patch to 9.2, where the json type was introduced.
      
      Oskari Saarenmaa, reviewed by Michael Paquier.
      
      Security: CVE-2015-5289
      08fa47c4
    • Noah Misch's avatar
      pgcrypto: Detect and report too-short crypt() salts. · 1d812c8b
      Noah Misch authored
      Certain short salts crashed the backend or disclosed a few bytes of
      backend memory.  For existing salt-induced error conditions, emit a
      message saying as much.  Back-patch to 9.0 (all supported versions).
      
      Josh Kupershmidt
      
      Security: CVE-2015-5288
      1d812c8b
    • Stephen Frost's avatar
      Apply SELECT policies in INSERT/UPDATE+RETURNING · 2ca9d544
      Stephen Frost authored
      Similar to 7d8db3e8, given that INSERT+RETURNING requires SELECT rights
      on the table, apply the SELECT policies as WCOs to the tuples being
      inserted.  Apply the same logic to UPDATE+RETURNING.
      
      Back-patch to 9.5 where RLS was added.
      2ca9d544
    • Stephen Frost's avatar
      Do not write out WCOs in Query · 4158cc37
      Stephen Frost authored
      The WithCheckOptions list in Query are only populated during rewrite and
      do not need to be written out or read in as part of a Query structure.
      
      Further, move WithCheckOptions to the bottom and add comments to clarify
      that it is only populated during rewrite.
      
      Back-patch to 9.5 with a catversion bump, as we are still in alpha.
      4158cc37
    • Andres Freund's avatar
      Re-Align *_freeze_max_age reloption limits with corresponding GUC limits. · 2596d705
      Andres Freund authored
      In 020235a5 I lowered the autovacuum_*freeze_max_age minimums to
      allow for easier testing of wraparounds. I did not touch the
      corresponding per-table limits. While those don't matter for the purpose
      of wraparound, it seems more consistent to lower them as well.
      
      It's noteworthy that the previous reloption lower limit for
      autovacuum_multixact_freeze_max_age was too high by one magnitude, even
      before 020235a5.
      
      Discussion: 26377.1443105453@sss.pgh.pa.us
      Backpatch: back to 9.0 (in parts), like the prior patch
      2596d705
    • Stephen Frost's avatar
      ALTER TABLE .. FORCE ROW LEVEL SECURITY · 088c8336
      Stephen Frost authored
      To allow users to force RLS to always be applied, even for table owners,
      add ALTER TABLE .. FORCE ROW LEVEL SECURITY.
      
      row_security=off overrides FORCE ROW LEVEL SECURITY, to ensure pg_dump
      output is complete (by default).
      
      Also add SECURITY_NOFORCE_RLS context to avoid data corruption when
      ALTER TABLE .. FORCE ROW SECURITY is being used. The
      SECURITY_NOFORCE_RLS security context is used only during referential
      integrity checks and is only considered in check_enable_rls() after we
      have already checked that the current user is the owner of the relation
      (which should always be the case during referential integrity checks).
      
      Back-patch to 9.5 where RLS was added.
      088c8336
  2. 04 Oct, 2015 11 commits
    • Tom Lane's avatar
    • Tom Lane's avatar
      Improve contrib/pg_stat_statements' handling of garbage collection failure. · 8bbe4cbd
      Tom Lane authored
      If we can't read the query texts file (whether because out-of-memory, or
      for some other reason), give up and reset the file to empty, discarding all
      stored query texts, though not the statistics per se.  We used to leave
      things alone and hope for better luck next time, but the problem is that
      the file is only going to get bigger and even harder to slurp into memory.
      Better to do something that will get us out of trouble.
      
      Likewise reset the file to empty for any other failure within gc_qtexts().
      The previous behavior after a write error was to discard query texts but
      not do anything to truncate the file, which is just weird.
      
      Also, increase the maximum supported file size from MaxAllocSize to
      MaxAllocHugeSize; this makes it more likely we'll be able to do a garbage
      collection successfully.
      
      Also, fix recalculation of mean_query_len within entry_dealloc() to match
      the calculation in gc_qtexts().  The previous coding overlooked the
      possibility of dropped texts (query_len == -1) and would underestimate the
      mean of the remaining entries in such cases, thus possibly causing excess
      garbage collection cycles.
      
      In passing, add some errdetail to the log entry that complains about
      insufficient memory to read the query texts file, which after all was
      Jim Nasby's original complaint.
      
      Back-patch to 9.4 where the current handling of query texts was
      introduced.
      
      Peter Geoghegan, rather editorialized upon by me
      8bbe4cbd
    • Andres Freund's avatar
      Fix hstore_plpython test when python3 is used. · 86b1e678
      Andres Freund authored
      Due to b67aaf21 / CREATE EXTENSION ... CASCADE the test output
      contains the extension name in yet another place. Since that's variable
      depending on the python version...
      
      Add yet another name mangling stanza to regress-python3-mangle.mk.
      
      Author: Petr Jelinek
      86b1e678
    • Tom Lane's avatar
      Further twiddling of nodeHash.c hashtable sizing calculation. · f2fc98fb
      Tom Lane authored
      On reflection, the submitted patch didn't really work to prevent the
      request size from exceeding MaxAllocSize, because of the fact that we'd
      happily round nbuckets up to the next power of 2 after we'd limited it to
      max_pointers.  The simplest way to enforce the limit correctly is to
      round max_pointers down to a power of 2 when it isn't one already.
      
      (Note that the constraint to INT_MAX / 2, if it were doing anything useful
      at all, is properly applied after that.)
      f2fc98fb
    • Tom Lane's avatar
      Fix some issues in new hashtable size calculations in nodeHash.c. · a31e64d0
      Tom Lane authored
      Limit the size of the hashtable pointer array to not more than
      MaxAllocSize, per reports from Kouhei Kaigai and others of "invalid memory
      alloc request size" failures.  There was discussion of allowing the array
      to get larger than that by using the "huge" palloc API, but so far no proof
      that that is actually a good idea, and at this point in the 9.5 cycle major
      changes from old behavior don't seem like the way to go.
      
      Fix a rather serious secondary bug in the new code, which was that it
      didn't ensure nbuckets remained a power of 2 when recomputing it for the
      multiple-batch case.
      
      Clean up sloppy division of labor between ExecHashIncreaseNumBuckets and
      its sole call site.
      a31e64d0
    • Andrew Dunstan's avatar
      Disallow invalid path elements in jsonb_set · 1edd4ec8
      Andrew Dunstan authored
      Null path elements and, where the object is an array, invalid integer
      elements now cause an error.
      
      Incorrect behaviour noted by Thom Brown, patch from Dmitry Dolgov.
      
      Backpatch to 9.5 where jsonb_set was introduced
      1edd4ec8
    • Peter Eisentraut's avatar
    • Tom Lane's avatar
      Update 9.5 release notes through today. · cf007a4b
      Tom Lane authored
      cf007a4b
    • Tom Lane's avatar
    • Noah Misch's avatar
      Document that row_security is a boolean GUC. · f78ae374
      Noah Misch authored
      Oversight in commit 537bd178.
      Back-patch to 9.5, like that commit.
      f78ae374
    • Noah Misch's avatar
      Make BYPASSRLS behave like superuser RLS bypass. · 3cb0a7e7
      Noah Misch authored
      Specifically, make its effect independent from the row_security GUC, and
      make it affect permission checks pertinent to views the BYPASSRLS role
      owns.  The row_security GUC thereby ceases to change successful-query
      behavior; it can only make a query fail with an error.  Back-patch to
      9.5, where BYPASSRLS was introduced.
      3cb0a7e7
  3. 03 Oct, 2015 7 commits
    • Andres Freund's avatar
    • Andres Freund's avatar
      Add CASCADE support for CREATE EXTENSION. · b67aaf21
      Andres Freund authored
      Without CASCADE, if an extension has an unfullfilled dependency on
      another extension, CREATE EXTENSION ERRORs out with "required extension
      ... is not installed". That is annoying, especially when that dependency
      is an implementation detail of the extension, rather than something the
      extension's user can make sense of.
      
      In addition to CASCADE this also includes a small set of regression
      tests around CREATE EXTENSION.
      
      Author: Petr Jelinek, editorialized by Michael Paquier, Andres Freund
      Reviewed-By: Michael Paquier, Andres Freund, Jeff Janes
      Discussion: 557E0520.3040800@2ndquadrant.com
      b67aaf21
    • Tom Lane's avatar
      Add missing "static" specifier. · bf686796
      Tom Lane authored
      Per buildfarm (pademelon, at least, doesn't like this).
      bf686796
    • Andres Freund's avatar
      Improve errhint() about replication slot naming restrictions. · 920218cb
      Andres Freund authored
      The existing hint talked about "may only contain letters", but the
      actual requirement is more strict: only lower case letters are allowed.
      
      Reported-By: Rushabh Lathia
      Author: Rushabh Lathia
      Discussion: AGPqQf2x50qcwbYOBKzb4x75sO_V3g81ZsA8+Ji9iN5t_khFhQ@mail.gmail.com
      Backpatch: 9.4-, where replication slots were added
      920218cb
    • Andres Freund's avatar
      Fix several bugs related to ON CONFLICT's EXCLUDED pseudo relation. · ad227837
      Andres Freund authored
      Four related issues:
      
      1) attnos/varnos/resnos for EXCLUDED were out of sync when a column
         after one dropped in the underlying relation was referenced.
      2) References to whole-row variables (i.e. EXCLUDED.*) lead to errors.
      3) It was possible to reference system columns in the EXCLUDED pseudo
         relations, even though they would not have valid contents.
      4) References to EXCLUDED were rewritten by the RLS machinery, as
         EXCLUDED was treated as if it were the underlying relation.
      
      To fix the first two issues, generate the excluded targetlist with
      dropped columns in mind and add an entry for whole row
      variables. Instead of unconditionally adding a wholerow entry we could
      pull up the expression if needed, but doing it unconditionally seems
      simpler. The wholerow entry is only really needed for ruleutils/EXPLAIN
      support anyway.
      
      The remaining two issues are addressed by changing the EXCLUDED RTE to
      have relkind = composite. That fits with EXCLUDED not actually being a
      real relation, and allows to treat it differently in the relevant
      places. scanRTEForColumn now skips looking up system columns when the
      RTE has a composite relkind; fireRIRrules() already had a corresponding
      check, thereby preventing RLS expansion on EXCLUDED.
      
      Also add tests for these issues, and improve a few comments around
      excluded handling in setrefs.c.
      
      Reported-By: Peter Geoghegan, Geoff Winkless
      Author: Andres Freund, Amit Langote, Peter Geoghegan
      Discussion: CAEzk6fdzJ3xYQZGbcuYM2rBd2BuDkUksmK=mY9UYYDugg_GgZg@mail.gmail.com,
         CAM3SWZS+CauzbiCEcg-GdE6K6ycHE_Bz6Ksszy8AoixcMHOmsA@mail.gmail.com
      Backpatch: 9.5, where ON CONFLICT was introduced
      ad227837
    • Peter Eisentraut's avatar
      doc: Update URLs of external projects · 1023194b
      Peter Eisentraut authored
      1023194b
    • Peter Eisentraut's avatar
  4. 02 Oct, 2015 11 commits
    • Tom Lane's avatar
      Update time zone data files to tzdata release 2015g. · 241e6844
      Tom Lane authored
      DST law changes in Cayman Islands, Fiji, Moldova, Morocco, Norfolk Island,
      North Korea, Turkey, Uruguay.  New zone America/Fort_Nelson for Canadian
      Northern Rockies.
      241e6844
    • Robert Haas's avatar
      Clarify FDW documentation about ON CONFLICT. · 01bc589a
      Robert Haas authored
      Etsuro Fujita, reviewed by Peter Geoghegan
      01bc589a
    • Tom Lane's avatar
      Add recursion depth protection to LIKE matching. · 2e8cfcf4
      Tom Lane authored
      Since MatchText() recurses, it could in principle be driven to stack
      overflow, although quite a long pattern would be needed.
      2e8cfcf4
    • Tom Lane's avatar
      Add recursion depth protections to regular expression matching. · b63fc287
      Tom Lane authored
      Some of the functions in regex compilation and execution recurse, and
      therefore could in principle be driven to stack overflow.  The Tcl crew
      has seen this happen in practice in duptraverse(), though their fix was
      to put in a hard-wired limit on the number of recursive levels, which is
      not too appetizing --- fortunately, we have enough infrastructure to check
      the actually available stack.  Greg Stark has also seen it in other places
      while fuzz testing on a machine with limited stack space.  Let's put guards
      in to prevent crashes in all these places.
      
      Since the regex code would leak memory if we simply threw elog(ERROR),
      we have to introduce an API that checks for stack depth without throwing
      such an error.  Fortunately that's not difficult.
      b63fc287
    • Tom Lane's avatar
      Fix potential infinite loop in regular expression execution. · f2c4ffc3
      Tom Lane authored
      In cfindloop(), if the initial call to shortest() reports that a
      zero-length match is possible at the current search start point, but then
      it is unable to construct any actual match to that, it'll just loop around
      with the same start point, and thus make no progress.  We need to force the
      start point to be advanced.  This is safe because the loop over "begin"
      points has already tried and failed to match starting at "close", so there
      is surely no need to try that again.
      
      This bug was introduced in commit e2bd9049,
      wherein we allowed continued searching after we'd run out of match
      possibilities, but evidently failed to think hard enough about exactly
      where we needed to search next.
      
      Because of the way this code works, such a match failure is only possible
      in the presence of backrefs --- otherwise, shortest()'s judgment that a
      match is possible should always be correct.  That probably explains how
      come the bug has escaped detection for several years.
      
      The actual fix is a one-liner, but I took the trouble to add/improve some
      comments related to the loop logic.
      
      After fixing that, the submitted test case "()*\1" didn't loop anymore.
      But it reported failure, though it seems like it ought to match a
      zero-length string; both Tcl and Perl think it does.  That seems to be from
      overenthusiastic optimization on my part when I rewrote the iteration match
      logic in commit 173e29aa: we can't just
      "declare victory" for a zero-length match without bothering to set match
      data for capturing parens inside the iterator node.
      
      Per fuzz testing by Greg Stark.  The first part of this is a bug in all
      supported branches, and the second part is a bug since 9.2 where the
      iteration rewrite happened.
      f2c4ffc3
    • Tom Lane's avatar
      Add some more query-cancel checks to regular expression matching. · 9fe8fe9c
      Tom Lane authored
      Commit 9662143f added infrastructure to
      allow regular-expression operations to be terminated early in the event
      of SIGINT etc.  However, fuzz testing by Greg Stark disclosed that there
      are still cases where regex compilation could run for a long time without
      noticing a cancel request.  Specifically, the fixempties() phase never
      adds new states, only new arcs, so it doesn't hit the cancel check I'd put
      in newstate().  Add one to newarc() as well to cover that.
      
      Some experimentation of my own found that regex execution could also run
      for a long time despite a pending cancel.  We'd put a high-level cancel
      check into cdissect(), but there was none inside the core text-matching
      routines longest() and shortest().  Ordinarily those inner loops are very
      very fast ... but in the presence of lookahead constraints, not so much.
      As a compromise, stick a cancel check into the stateset cache-miss
      function, which is enough to guarantee a cancel check at least once per
      lookahead constraint test.
      
      Making this work required more attention to error handling throughout the
      regex executor.  Henry Spencer had apparently originally intended longest()
      and shortest() to be incapable of incurring errors while running, so
      neither they nor their subroutines had well-defined error reporting
      behaviors.  However, that was already broken by the lookahead constraint
      feature, since lacon() can surely suffer an out-of-memory failure ---
      which, in the code as it stood, might never be reported to the user at all,
      but just silently be treated as a non-match of the lookahead constraint.
      Normalize all that by inserting explicit error tests as needed.  I took the
      opportunity to add some more comments to the code, too.
      
      Back-patch to all supported branches, like the previous patch.
      9fe8fe9c
    • Tom Lane's avatar
      Docs: add disclaimer about hazards of using regexps from untrusted sources. · 558d4ada
      Tom Lane authored
      It's not terribly hard to devise regular expressions that take large
      amounts of time and/or memory to process.  Recent testing by Greg Stark has
      also shown that machines with small stack limits can be driven to stack
      overflow by suitably crafted regexps.  While we intend to fix these things
      as much as possible, it's probably impossible to eliminate slow-execution
      cases altogether.  In any case we don't want to treat such things as
      security issues.  The history of that code should already discourage
      prudent DBAs from allowing execution of regexp patterns coming from
      possibly-hostile sources, but it seems like a good idea to warn about the
      hazard explicitly.
      
      Currently, similar_escape() allows access to enough of the underlying
      regexp behavior that the warning has to apply to SIMILAR TO as well.
      We might be able to make it safer if we tightened things up to allow only
      SQL-mandated capabilities in SIMILAR TO; but that would be a subtly
      non-backwards-compatible change, so it requires discussion and probably
      could not be back-patched.
      
      Per discussion among pgsql-security list.
      558d4ada
    • Tom Lane's avatar
      Docs: add another example of creating a range type. · 27fddec1
      Tom Lane authored
      The "floatrange" example is a bit too simple because float8mi can be
      used without any additional type conversion.  Add an example that does
      have to account for that, and do some minor other wordsmithing.
      27fddec1
    • Alvaro Herrera's avatar
      Don't disable commit_ts in standby if enabled locally · e06b2e1d
      Alvaro Herrera authored
      Bug noticed by Fujii Masao
      e06b2e1d
    • Peter Eisentraut's avatar
      pg_rewind: Improve some messages · cdcae2b6
      Peter Eisentraut authored
      The output of a typical pg_rewind run contained a mix of capitalized and
      not-capitalized and punctuated and not-punctuated phrases for no
      apparent reason.  Make that consistent.  Also fix some problems in other
      messages.
      cdcae2b6
    • Peter Eisentraut's avatar
      87c2b517
  5. 01 Oct, 2015 2 commits
    • Tom Lane's avatar
      Fix pg_dump to handle inherited NOT VALID check constraints correctly. · 8ab4a6bd
      Tom Lane authored
      This case seems to have been overlooked when unvalidated check constraints
      were introduced, in 9.2.  The code would attempt to dump such constraints
      over again for each child table, even though adding them to the parent
      table is sufficient.
      
      In 9.2 and 9.3, also fix contrib/pg_upgrade/Makefile so that the "make
      clean" target fully cleans up after a failed test.  This evidently got
      dealt with at some point in 9.4, but it wasn't back-patched.  I ran into
      it while testing this fix ...
      
      Per bug #13656 from Ingmar Brouns.
      8ab4a6bd
    • Alvaro Herrera's avatar
      Fix commit_ts for standby · f12e814b
      Alvaro Herrera authored
      Module initialization was still not completely correct after commit
      6b619551, per crash report from Takashi Ohnishi.  To fix, instead of
      trying to monkey around with the value of the GUC setting directly, add
      a separate boolean flag that enables the feature on a standby, but only
      for the startup (recovery) process, when it sees that its master server
      has the feature enabled.
      Discussion: http://www.postgresql.org/message-id/ca44c6c7f9314868bdc521aea4f77cbf@MP-MSGSS-MBX004.msg.nttdata.co.jp
      
      Also change the deactivation routine to delete all segment files rather
      than leaving the last one around.  (This doesn't need separate
      WAL-logging, because on recovery we execute the same deactivation
      routine anyway.)
      
      In passing, clean up the code structure somewhat, particularly so that
      xlog.c doesn't know so much about when to activate/deactivate the
      feature.
      
      Thanks to Fujii Masao for testing and Petr Jelínek for off-list discussion.
      
      Back-patch to 9.5, where commit_ts was introduced.
      f12e814b