commit 5449d1e6a523e0ae53f3a45d35b390f9a3b4dab3
parent d7743b05f00d8bb8e12444ec39c7d00a105ac135
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Sun, 24 May 2020 15:55:23 +0200
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.
Diffstat:
1 file changed, 4 insertions(+), 13 deletions(-)
diff --git 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 &&