From 5449d1e6a523e0ae53f3a45d35b390f9a3b4dab3 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 24 May 2020 15:55:23 +0200 Subject: [PATCH] files: ignore errors when calling chown() on a backup file A normal user can change the group of a file (if the user is a member of that group), but cannot change the owner of that file. So, when a user edits a file that belongs to a different user, the call of fchown() will fail. But there is no harm in that. Also when the user is root, there is no harm in fchown() failing -- it will simply mean that the backup file will remain owned by root and will not be writable by the intended owner (when root has the normal umask of 0022). This fixes https://savannah.gnu.org/bugs/?58383. Bug existed since version 2.2.5, commit 86be3af7. --- src/files.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/files.c b/src/files.c index 8eb46a31..d29a6a58 100644 --- a/src/files.c +++ b/src/files.c @@ -1687,19 +1687,10 @@ bool write_file(const char *name, FILE *thefile, bool tmp, goto cleanup_and_exit; } - /* Only try chowning the backup when we're root. */ - if (geteuid() == NANO_ROOT_UID && - fchown(backup_fd, openfile->current_stat->st_uid, - openfile->current_stat->st_gid) == -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; - } + /* Try to change owner and group to those of the original file; + * ignore errors, as a normal user cannot change the owner. */ + 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 &&