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