Skip to content

Commit 1219823

Browse files
committed
Add safeguards for pg_fsync() called with incorrectly-opened fds
On some platforms, fsync() returns EBADFD when opening a file descriptor with O_RDONLY (read-only), leading ultimately now to a PANIC to prevent data corruption. This commit adds a new sanity check in pg_fsync() based on fcntl() to make sure that we don't repeat again mistakes with incorrectly-set file descriptors so as problems are detected at an early stage. Without that, such errors could only be detected after running Postgres on a specific supported platform for the culprit code path, which could take some time before being found. b8e19b9 was a fix for such a problem, which got undetected for more than 5 years, and a586cc4 fixed another similar issue. Note that the new check added works as well when fsync=off is configured, so as all regression tests would detect problems as long as assertions are enabled. fcntl() being not available on Windows, the new checks do not happen there. Author: Michael Paquier Reviewed-by: Mark Dilger Discussion: https://postgr.es/m/[email protected]
1 parent 080313f commit 1219823

File tree

1 file changed

+38
-0
lines changed
  • src/backend/storage/file

1 file changed

+38
-0
lines changed

src/backend/storage/file/fd.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,44 @@ static int fsync_parent_path(const char *fname, int elevel);
329329
int
330330
pg_fsync(int fd)
331331
{
332+
#if !defined(WIN32) && defined(USE_ASSERT_CHECKING)
333+
struct stat st;
334+
335+
/*
336+
* Some operating system implementations of fsync() have requirements
337+
* about the file access modes that were used when their file descriptor
338+
* argument was opened, and these requirements differ depending on whether
339+
* the file descriptor is for a directory.
340+
*
341+
* For any file descriptor that may eventually be handed to fsync(), we
342+
* should have opened it with access modes that are compatible with
343+
* fsync() on all supported systems, otherwise the code may not be
344+
* portable, even if it runs ok on the current system.
345+
*
346+
* We assert here that a descriptor for a file was opened with write
347+
* permissions (either O_RDWR or O_WRONLY) and for a directory without
348+
* write permissions (O_RDONLY).
349+
*
350+
* Ignore any fstat errors and let the follow-up fsync() do its work.
351+
* Doing this sanity check here counts for the case where fsync() is
352+
* disabled.
353+
*/
354+
if (fstat(fd, &st) == 0)
355+
{
356+
int desc_flags = fcntl(fd, F_GETFL);
357+
358+
/*
359+
* O_RDONLY is historically 0, so just make sure that for directories
360+
* no write flags are used.
361+
*/
362+
if (S_ISDIR(st.st_mode))
363+
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
364+
else
365+
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
366+
}
367+
errno = 0;
368+
#endif
369+
332370
/* #if is to skip the sync_method test if there's no need for it */
333371
#if defined(HAVE_FSYNC_WRITETHROUGH) && !defined(FSYNC_WRITETHROUGH_IS_FSYNC)
334372
if (sync_method == SYNC_METHOD_FSYNC_WRITETHROUGH)

0 commit comments

Comments
 (0)