pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix failure to check for open() or fsync() failures.
7+ messages / 2 participants
[nested] [flat]

* pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-26 21:08  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2018-12-26 21:08 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix failure to check for open() or fsync() failures.

While it seems OK to not be concerned about fsync() failure for a
pre-existing signal file, it's not OK to not even check for open()
failure.  This at least causes complaints from static analyzers,
and I think on some platforms passing -1 to fsync() or close() might
trigger assertion-type failures.  Also add (void) casts to make clear
that we're ignoring fsync's result intentionally.

Oversights in commit 2dedf4d9a, noted by Coverity.

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8528e3d849a896f8711c56fb41eae56f8c986729

Modified Files
--------------
src/backend/access/transam/xlog.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-26 22:43  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2018-12-26 22:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

On Wed, Dec 26, 2018 at 09:08:23PM +0000, Tom Lane wrote:
> Fix failure to check for open() or fsync() failures.
> 
> While it seems OK to not be concerned about fsync() failure for a
> pre-existing signal file, it's not OK to not even check for open()
> failure.  This at least causes complaints from static analyzers,
> and I think on some platforms passing -1 to fsync() or close() might
> trigger assertion-type failures.  Also add (void) casts to make clear
> that we're ignoring fsync's result intentionally.
> 
> Oversights in commit 2dedf4d9a, noted by Coverity.

 fd = BasicOpenFilePerm(STANDBY_SIGNAL_FILE, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
                        S_IRUSR | S_IWUSR);
-   pg_fsync(fd);
-   close(fd);
+   if (fd >= 0)
+   {
+       (void) pg_fsync(fd);
+       close(fd);
+   }

Wouldn't it be more simple to remove stat() and just call
BasicOpenFilePerm, complaining with FATAL about any failures,
including EACCES, on the way?  The code is racy as designed, even if
that's not a big deal for recovery purposes.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-26 22:55  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2018-12-26 22:55 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

Michael Paquier <michael@paquier.xyz> writes:
> On Wed, Dec 26, 2018 at 09:08:23PM +0000, Tom Lane wrote:
>> Fix failure to check for open() or fsync() failures.
>> 
>> While it seems OK to not be concerned about fsync() failure for a
>> pre-existing signal file, it's not OK to not even check for open()
>> failure.  This at least causes complaints from static analyzers,

> Wouldn't it be more simple to remove stat() and just call
> BasicOpenFilePerm, complaining with FATAL about any failures,
> including EACCES, on the way?  The code is racy as designed, even if
> that's not a big deal for recovery purposes.

It appears to me that the code is intentionally not worrying about
fsync failure, so it seems wrong for it to FATAL out if it's unable
to open the file to fsync it.  And it surely shouldn't do so if the
file isn't there.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-27 01:09  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2018-12-27 01:09 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

On Wed, Dec 26, 2018 at 05:55:36PM -0500, Tom Lane wrote:
> It appears to me that the code is intentionally not worrying about
> fsync failure, so it seems wrong for it to FATAL out if it's unable
> to open the file to fsync it.  And it surely shouldn't do so if the
> file isn't there.

My point is a bit different though: it seems to me that we could just
call BasicOpenFilePerm() and remove the stat() to do exactly the same
things, simplifying the code.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-27 01:35  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2018-12-27 01:35 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

Michael Paquier <michael@paquier.xyz> writes:
> On Wed, Dec 26, 2018 at 05:55:36PM -0500, Tom Lane wrote:
>> It appears to me that the code is intentionally not worrying about
>> fsync failure, so it seems wrong for it to FATAL out if it's unable
>> to open the file to fsync it.  And it surely shouldn't do so if the
>> file isn't there.

> My point is a bit different though: it seems to me that we could just
> call BasicOpenFilePerm() and remove the stat() to do exactly the same
> things, simplifying the code.

Oh, I see.  Yeah, if we're ignoring errors anyway, the stat calls
seem redundant.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-27 02:10  Michael Paquier <michael@paquier.xyz>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2018-12-27 02:10 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

On Wed, Dec 26, 2018 at 08:35:22PM -0500, Tom Lane wrote:
> Oh, I see.  Yeah, if we're ignoring errors anyway, the stat calls
> seem redundant.

For this one, I think that we could simplify as attached (this causes
open() to fail additionally because of the sync flags, but that's not
really worth worrying).  Thoughts?
--
Michael

Attachments:

recovery-signal-simplify.patch (text/x-diff, 1.5K) download diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 5729867879..34ebf6716e 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5307,6 +5307,7 @@ static void readRecoverySignalFile(void) { struct stat stat_buf; + int fd; if (IsBootstrapProcessingMode()) return; @@ -5333,30 +5334,20 @@ readRecoverySignalFile(void) * If present, standby signal file takes precedence. If neither is present * then we won't enter archive recovery. */ - if (stat(STANDBY_SIGNAL_FILE, &stat_buf) == 0) + if ((fd = BasicOpenFilePerm(STANDBY_SIGNAL_FILE, + O_RDWR | PG_BINARY | get_sync_bit(sync_method), + S_IRUSR | S_IWUSR)) >= 0) { - int fd; - - fd = BasicOpenFilePerm(STANDBY_SIGNAL_FILE, O_RDWR | PG_BINARY | get_sync_bit(sync_method), - S_IRUSR | S_IWUSR); - if (fd >= 0) - { - (void) pg_fsync(fd); - close(fd); - } + (void) pg_fsync(fd); + close(fd); standby_signal_file_found = true; } - else if (stat(RECOVERY_SIGNAL_FILE, &stat_buf) == 0) + else if ((fd = BasicOpenFilePerm(RECOVERY_SIGNAL_FILE, + O_RDWR | PG_BINARY | get_sync_bit(sync_method), + S_IRUSR | S_IWUSR)) >= 0) { - int fd; - - fd = BasicOpenFilePerm(RECOVERY_SIGNAL_FILE, O_RDWR | PG_BINARY | get_sync_bit(sync_method), - S_IRUSR | S_IWUSR); - if (fd >= 0) - { - (void) pg_fsync(fd); - close(fd); - } + (void) pg_fsync(fd); + close(fd); recovery_signal_file_found = true; }
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: pgsql: Fix failure to check for open() or fsync() failures.
@ 2018-12-27 02:30  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2018-12-27 02:30 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Postgres hackers <pgsql-hackers@postgresql.org>; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

Michael Paquier <michael@paquier.xyz> writes:
> On Wed, Dec 26, 2018 at 08:35:22PM -0500, Tom Lane wrote:
>> Oh, I see.  Yeah, if we're ignoring errors anyway, the stat calls
>> seem redundant.

> For this one, I think that we could simplify as attached (this causes
> open() to fail additionally because of the sync flags, but that's not
> really worth worrying).  Thoughts?

Actually, now that I think a bit more, this isn't a good idea.  We want
standby_signal_file_found (resp. recovery_signal_file_found) to get set
if the file exists, even if we're unable to fsync it for some reason.
A counterexample to the proposed patch is that a signal file that's
read-only to the server will get ignored, which it should not be.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2018-12-27 02:30 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-26 21:08 pgsql: Fix failure to check for open() or fsync() failures. Tom Lane <tgl@sss.pgh.pa.us>
2018-12-26 22:43 ` Michael Paquier <michael@paquier.xyz>
2018-12-26 22:55   ` Tom Lane <tgl@sss.pgh.pa.us>
2018-12-27 01:09     ` Michael Paquier <michael@paquier.xyz>
2018-12-27 01:35       ` Tom Lane <tgl@sss.pgh.pa.us>
2018-12-27 02:10         ` Michael Paquier <michael@paquier.xyz>
2018-12-27 02:30           ` Tom Lane <tgl@sss.pgh.pa.us>

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