Skip to content

Commit 2ab7702

Browse files
Patch CVE-2024-5742 in nano (#9404)
1 parent 30f1ce2 commit 2ab7702

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

SPECS/nano/CVE-2024-5742.patch

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+

SPECS/nano/nano.spec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Summary: Text editor
22
Name: nano
33
Version: 6.0
4-
Release: 2%{?dist}
4+
Release: 3%{?dist}
55
License: GPLv3+
66
Vendor: Microsoft Corporation
77
Distribution: Mariner
88
Group: Applications/Editors
99
URL: https://www.nano-editor.org/
1010
Source0: http://www.nano-editor.org/dist/v6/%{name}-%{version}.tar.xz
11+
Patch0: CVE-2024-5742.patch
1112
BuildRequires: ncurses-devel
1213
Requires: ncurses
1314

@@ -22,7 +23,8 @@ Requires: %{name} = %{version}-%{release}
2223
Lang for nano
2324

2425
%prep
25-
%setup -q
26+
%autosetup -p1
27+
2628

2729
%build
2830
%configure --enable-utf8 \
@@ -52,6 +54,8 @@ make %{?_smp_mflags} check
5254
%{_docdir}/%{name}-%{version}/*
5355

5456
%changelog
57+
* Fri Jun 14 2024 Suresh Thelkar <sthelkar@microsoft.com> - 6.0-3
58+
- Backporting the patch for CVE-2024-5742
5559
* Wed Sep 20 2023 Jon Slobodzian <joslobo@microsoft.com> - 6.0-2
5660
- Recompile with stack-protection fixed gcc version (CVE-2023-4039)
5761

0 commit comments

Comments
 (0)