Skip to content

Commit 1242053

Browse files
[AUTO-CHERRYPICK] Fix CVE-2024-5742 for nano :3.0 - branch 3.0-dev (#11214)
Co-authored-by: KavyaSree2610 <92566732+KavyaSree2610@users.noreply.github.com>
1 parent 9277311 commit 1242053

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

SPECS/nano/CVE-2024-5742.patch

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

SPECS/nano/nano.spec

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
Summary: Text editor
22
Name: nano
33
Version: 6.4
4-
Release: 1%{?dist}
4+
Release: 2%{?dist}
55
License: GPLv3+
66
Vendor: Microsoft Corporation
77
Distribution: Azure Linux
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
12+
1113
BuildRequires: ncurses-devel
1214
Requires: ncurses
1315

@@ -22,7 +24,7 @@ Requires: %{name} = %{version}-%{release}
2224
Lang for nano
2325

2426
%prep
25-
%setup -q
27+
%autosetup -p1
2628

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

5456
%changelog
57+
* Thu Nov 21 2024 Kavya Sree Kaitepalli <kkaitepalli@microsoft.com> - 6.4-2
58+
- Patch for CVE-2024-5742
59+
5560
* Fri Oct 27 2023 CBL-Mariner Servicing Account <cblmargh@microsoft.com> - 6.4-1
5661
- Auto-upgrade to 6.4 - Azure Linux 3.0 - package upgrades
5762

0 commit comments

Comments
 (0)