From 9be9f6d5d4ded27ec9b470c19ad530628365eb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Mon, 6 May 2024 10:24:11 +0200 Subject: [PATCH] fix emergency file replacement vulnerability Resolves: rhbz#2277586 --- nano-7.2-emergency-file-replace-vuln.patch | 96 ++++++++++++++++++++++ nano.spec | 7 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 nano-7.2-emergency-file-replace-vuln.patch diff --git a/nano-7.2-emergency-file-replace-vuln.patch b/nano-7.2-emergency-file-replace-vuln.patch new file mode 100644 index 0000000..cde32de --- /dev/null +++ b/nano-7.2-emergency-file-replace-vuln.patch @@ -0,0 +1,96 @@ +From 5e7a3c2e7e118c7f12d5dfda9f9140f638976aa2 Mon Sep 17 00:00:00 2001 +From: Benno Schulenberg +Date: Sun, 28 Apr 2024 10:51:52 +0200 +Subject: files: run `chmod` and `chown` on the descriptor, not on the filename + +This closes a window of opportunity where the emergency file could be +replaced by a malicious symlink. + +The issue was reported by `MartinJM` and `InvisibleMeerkat`. + +Problem existed since version 2.2.0, commit 123110c5, when chmodding +and chowning of the emergency .save file was added. +--- + src/definitions.h | 2 +- + src/files.c | 13 ++++++++++++- + src/nano.c | 12 +----------- + 3 files changed, 14 insertions(+), 13 deletions(-) + +diff --git a/src/definitions.h b/src/definitions.h +index af3a793..55d8235 100644 +--- a/src/definitions.h ++++ b/src/definitions.h +@@ -288,7 +288,7 @@ typedef enum { + } message_type; + + typedef enum { +- OVERWRITE, APPEND, PREPEND ++ OVERWRITE, APPEND, PREPEND, EMERGENCY + } kind_of_writing_type; + + typedef enum { +diff --git a/src/files.c b/src/files.c +index 57c2001..584b579 100644 +--- a/src/files.c ++++ b/src/files.c +@@ -1763,6 +1763,8 @@ bool write_file(const char *name, FILE *thefile, bool normal, + #endif + char *realname = real_dir_from_tilde(name); + /* The filename after tilde expansion. */ ++ int fd = 0; ++ /* The descriptor that is assigned when opening the file. */ + char *tempname = NULL; + /* The name of the temporary file we use when prepending. */ + linestruct *line = openfile->filetop; +@@ -1846,7 +1848,6 @@ bool write_file(const char *name, FILE *thefile, bool normal, + * For an emergency file, access is restricted to just the owner. */ + if (thefile == NULL) { + mode_t permissions = (normal ? RW_FOR_ALL : S_IRUSR|S_IWUSR); +- int fd; + + #ifndef NANO_TINY + block_sigwinch(TRUE); +@@ -1972,6 +1973,16 @@ bool write_file(const char *name, FILE *thefile, bool normal, + } + #endif + ++#if !defined(NANO_TINY) && defined(HAVE_CHMOD) && defined(HAVE_CHOWN) ++ /* Change permissions and owner of an emergency save file to the values ++ * of the original file, but ignore any failure as we are in a hurry. */ ++ if (method == EMERGENCY && fd && openfile->statinfo) { ++ IGNORE_CALL_RESULT(fchmod(fd, openfile->statinfo->st_mode)); ++ IGNORE_CALL_RESULT(fchown(fd, openfile->statinfo->st_uid, ++ openfile->statinfo->st_gid)); ++ } ++#endif ++ + if (fclose(thefile) != 0) { + statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); + +diff --git a/src/nano.c b/src/nano.c +index 90b4a0b..973054f 100644 +--- a/src/nano.c ++++ b/src/nano.c +@@ -337,18 +337,8 @@ void emergency_save(const char *filename) + + if (*targetname == '\0') + fprintf(stderr, _("\nToo many .save files\n")); +- else if (write_file(targetname, NULL, SPECIAL, OVERWRITE, NONOTES)) { ++ else if (write_file(targetname, NULL, SPECIAL, EMERGENCY, NONOTES)) + fprintf(stderr, _("\nBuffer written to %s\n"), targetname); +-#if !defined(NANO_TINY) && defined(HAVE_CHMOD) && defined(HAVE_CHOWN) +- /* Try to chmod/chown the saved file to the values of the original file, +- * but ignore any failure as we are in a hurry to get out. */ +- if (openfile->statinfo) { +- IGNORE_CALL_RESULT(chmod(targetname, openfile->statinfo->st_mode)); +- IGNORE_CALL_RESULT(chown(targetname, openfile->statinfo->st_uid, +- openfile->statinfo->st_gid)); +- } +-#endif +- } + + free(targetname); + free(plainname); +-- +cgit v1.1 + diff --git a/nano.spec b/nano.spec index 9297826..b97849d 100644 --- a/nano.spec +++ b/nano.spec @@ -8,7 +8,7 @@ Summary: A small text editor Name: nano Version: 7.2 -Release: 4%{?dist} +Release: 5%{?dist} License: GPL-3.0-or-later URL: https://www.nano-editor.org @@ -26,6 +26,8 @@ Source11: nano-default-editor.sh Source12: nano-default-editor.csh Source13: nano-default-editor.fish +Patch0: nano-7.2-emergency-file-replace-vuln.patch + BuildRequires: file-devel BuildRequires: gettext-devel BuildRequires: gcc @@ -133,6 +135,9 @@ install -Dpm 0644 %{SOURCE13} %{buildroot}%{_datadir}/fish/vendor_conf.d/%{basen %changelog +* Mon May 06 2024 Lukáš Zaoral - 7.2-5 +- fix emergency file replacement vulnerability (rhbz#2277586) + * Thu Jul 20 2023 Fedora Release Engineering - 7.2-4 - Rebuilt for https://fedoraproject.org/wiki/Fedora_39_Mass_Rebuild