From 8d31ad8a705ea2b94bafb61c75cc47d88b63a103 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 28 May 2020 14:15:40 +0200 Subject: [PATCH] files: ignore errors when calling chmod() on a backup file First, it is very unlikely that chmod() would fail as the user just created the file herself. Second, even if chmod() would fail, this is not a problem, because we have created the file with read+write permissions for the owner only, so the file cannot accidentally be left accessible to unintended others. But most of all, such a failure should not stop nano from trying to write the backup file. Only when the actual *writing* fails, should we bother the user with a prompt. --- src/files.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/files.c b/src/files.c index 1d183818..0ab41e96 100644 --- a/src/files.c +++ b/src/files.c @@ -1666,7 +1666,7 @@ bool write_file(const char *name, FILE *thefile, bool tmp, backup_cflags = O_WRONLY | O_CREAT | O_EXCL; /* Create the backup file (or truncate the existing one). */ - backup_fd = open(backupname, backup_cflags, RW_FOR_ALL); + backup_fd = open(backupname, backup_cflags, S_IRUSR|S_IWUSR); if (backup_fd >= 0) backup_file = fdopen(backup_fd, "wb"); @@ -1683,17 +1683,10 @@ bool write_file(const char *name, FILE *thefile, bool tmp, IGNORE_CALL_RESULT(fchown(backup_fd, openfile->current_stat->st_uid, openfile->current_stat->st_gid)); - /* Set the backup's mode bits. */ - if (fchmod(backup_fd, openfile->current_stat->st_mode) == -1 && - !ISSET(INSECURE_BACKUP)) { - fclose(backup_file); - if (prompt_failed_backupwrite(backupname)) - goto skip_backup; - statusline(HUSH, _("Error writing backup file %s: %s"), - backupname, strerror(errno)); - free(backupname); - goto cleanup_and_exit; - } + /* Set the backup's permissions to those of the original file. + * It is not a security issue if this fails, as we have created + * the file with just read and write permission for the owner. */ + IGNORE_CALL_RESULT(fchmod(backup_fd, openfile->current_stat->st_mode)); /* Copy the existing file to the backup. */ verdict = copy_file(original, backup_file, FALSE);