|
| 1 | +From 53837ad424b08e65459d1d655e8aeef85dacb1fe Mon Sep 17 00:00:00 2001 |
| 2 | +From: Suresh Thelkar <sthelkar@microsoft.com> |
| 3 | +Date: Fri, 14 Jun 2024 11:47:05 +0530 |
| 4 | +Subject: [PATCH] Patch CVE-2024-5742 |
| 5 | + |
| 6 | +Upstream patch details can be found from the following |
| 7 | +https://git.savannah.gnu.org/cgit/nano.git/commit/?id=5e7a3c2e7e118c7f12d5dfda9f9140f638976aa2 |
| 8 | +--- |
| 9 | + src/definitions.h | 2 +- |
| 10 | + src/files.c | 13 ++++++++++++- |
| 11 | + src/nano.c | 11 +---------- |
| 12 | + 3 files changed, 14 insertions(+), 12 deletions(-) |
| 13 | + |
| 14 | +diff --git a/src/definitions.h b/src/definitions.h |
| 15 | +index eed5f10..22953fd 100644 |
| 16 | +--- a/src/definitions.h |
| 17 | ++++ b/src/definitions.h |
| 18 | +@@ -269,7 +269,7 @@ typedef enum { |
| 19 | + } message_type; |
| 20 | + |
| 21 | + typedef enum { |
| 22 | +- OVERWRITE, APPEND, PREPEND |
| 23 | ++ OVERWRITE, APPEND, PREPEND, EMERGENCY |
| 24 | + } kind_of_writing_type; |
| 25 | + |
| 26 | + typedef enum { |
| 27 | +diff --git a/src/files.c b/src/files.c |
| 28 | +index 9c41936..6a8ff5f 100644 |
| 29 | +--- a/src/files.c |
| 30 | ++++ b/src/files.c |
| 31 | +@@ -1760,6 +1760,8 @@ bool write_file(const char *name, FILE *thefile, bool normal, |
| 32 | + #endif |
| 33 | + char *realname = real_dir_from_tilde(name); |
| 34 | + /* The filename after tilde expansion. */ |
| 35 | ++ int fd = 0; |
| 36 | ++ /* The descriptor that is assigned when opening the file. */ |
| 37 | + char *tempname = NULL; |
| 38 | + /* The name of the temporary file we use when prepending. */ |
| 39 | + linestruct *line = openfile->filetop; |
| 40 | +@@ -1843,7 +1845,6 @@ bool write_file(const char *name, FILE *thefile, bool normal, |
| 41 | + * For an emergency file, access is restricted to just the owner. */ |
| 42 | + if (thefile == NULL) { |
| 43 | + mode_t permissions = (normal ? RW_FOR_ALL : S_IRUSR|S_IWUSR); |
| 44 | +- int fd; |
| 45 | + |
| 46 | + #ifndef NANO_TINY |
| 47 | + block_sigwinch(TRUE); |
| 48 | +@@ -1970,6 +1971,16 @@ bool write_file(const char *name, FILE *thefile, bool normal, |
| 49 | + } |
| 50 | + #endif |
| 51 | + |
| 52 | ++#if !defined(NANO_TINY) && defined(HAVE_CHMOD) && defined(HAVE_CHOWN) |
| 53 | ++ /* Change permissions and owner of an emergency save file to the values |
| 54 | ++ * of the original file, but ignore any failure as we are in a hurry. */ |
| 55 | ++ if (method == EMERGENCY && fd && openfile->statinfo) { |
| 56 | ++ IGNORE_CALL_RESULT(fchmod(fd, openfile->statinfo->st_mode)); |
| 57 | ++ IGNORE_CALL_RESULT(fchown(fd, openfile->statinfo->st_uid, |
| 58 | ++ openfile->statinfo->st_gid)); |
| 59 | ++ } |
| 60 | ++#endif |
| 61 | ++ |
| 62 | + if (fclose(thefile) != 0) { |
| 63 | + statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); |
| 64 | + |
| 65 | +diff --git a/src/nano.c b/src/nano.c |
| 66 | +index 9f614c6..4adfc9c 100644 |
| 67 | +--- a/src/nano.c |
| 68 | ++++ b/src/nano.c |
| 69 | +@@ -337,17 +337,8 @@ void emergency_save(const char *filename) |
| 70 | + |
| 71 | + if (*targetname == '\0') |
| 72 | + fprintf(stderr, _("\nToo many .save files\n")); |
| 73 | +- else if (write_file(targetname, NULL, SPECIAL, OVERWRITE, NONOTES)) { |
| 74 | ++ else if (write_file(targetname, NULL, SPECIAL, EMERGENCY, NONOTES)) { |
| 75 | + fprintf(stderr, _("\nBuffer written to %s\n"), targetname); |
| 76 | +-#ifndef NANO_TINY |
| 77 | +- /* Try to chmod/chown the saved file to the values of the original file, |
| 78 | +- * but ignore any failure as we are in a hurry to get out. */ |
| 79 | +- if (openfile->statinfo) { |
| 80 | +- IGNORE_CALL_RESULT(chmod(targetname, openfile->statinfo->st_mode)); |
| 81 | +- IGNORE_CALL_RESULT(chown(targetname, openfile->statinfo->st_uid, |
| 82 | +- openfile->statinfo->st_gid)); |
| 83 | +- } |
| 84 | +-#endif |
| 85 | + } |
| 86 | + |
| 87 | + free(targetname); |
| 88 | +-- |
| 89 | +2.34.1 |
| 90 | + |
0 commit comments