Make current_logfiles use permissions assigned to files in data directory
authorMichael Paquier <michael@paquier.xyz>
Sun, 24 Mar 2019 12:00:35 +0000 (21:00 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sun, 24 Mar 2019 12:00:35 +0000 (21:00 +0900)
Since its introduction in 19dc233c, current_logfiles has been assigned
the same permissions as a log file, which can be enforced with
log_file_mode.  This setup can lead to incompatibility problems with
group access permissions as current_logfiles is not located in the log
directory, but at the root of the data folder.  Hence, if group
permissions are used but log_file_mode is more restrictive, a backup
with a user in the group having read access could fail even if the log
directory is located outside of the data folder.

Per discussion with the folks mentioned below, we have concluded that
current_logfiles should not be treated as a log file as it only stores
metadata related to log files, and that it should use the same
permissions as all other files in the data directory.  This solution has
the merit to be simple and fixes all the interaction problems between
group access and log_file_mode.

Author: Haribabu Kommi
Reviewed-by: Stephen Frost, Robert Haas, Tom Lane, Michael Paquier
Discussion: https://postgr.es/m/CAJrrPGcEotF1P7AWoeQyD3Pqr-0xkQg_Herv98DjbaMj+naozw@mail.gmail.com
Backpatch-through: 11, where group access has been added.

src/backend/postmaster/syslogger.c

index d1ea46deb87d6efe0ebbfc7a9c62ff2ec808f552..a47183208621922635272f5c716b6e0457990629 100644 (file)
@@ -31,6 +31,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 
+#include "common/file_perm.h"
 #include "lib/stringinfo.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
@@ -1447,12 +1448,14 @@ set_next_rotation_time(void)
  * log messages.  Useful for finding the name(s) of the current log file(s)
  * when there is time-based logfile rotation.  Filenames are stored in a
  * temporary file and which is renamed into the final destination for
- * atomicity.
+ * atomicity.  The file is opened with the same permissions as what gets
+ * created in the data directory and has proper buffering options.
  */
 static void
 update_metainfo_datafile(void)
 {
    FILE       *fh;
+   mode_t      oumask;
 
    if (!(Log_destination & LOG_DESTINATION_STDERR) &&
        !(Log_destination & LOG_DESTINATION_CSVLOG))
@@ -1465,7 +1468,21 @@ update_metainfo_datafile(void)
        return;
    }
 
-   if ((fh = logfile_open(LOG_METAINFO_DATAFILE_TMP, "w", true)) == NULL)
+   /* use the same permissions as the data directory for the new file */
+   oumask = umask(pg_mode_mask);
+   fh = fopen(LOG_METAINFO_DATAFILE_TMP, "w");
+   umask(oumask);
+
+   if (fh)
+   {
+       setvbuf(fh, NULL, PG_IOLBF, 0);
+
+#ifdef WIN32
+       /* use CRLF line endings on Windows */
+       _setmode(_fileno(fh), _O_TEXT);
+#endif
+   }
+   else
    {
        ereport(LOG,
                (errcode_for_file_access(),