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.
master
Benno Schulenberg 2020-05-24 15:55:23 +02:00
parent d7743b05f0
commit 5449d1e6a5
1 changed files with 4 additions and 13 deletions

View File

@ -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 &&