summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch')
-rw-r--r--meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch316
1 files changed, 316 insertions, 0 deletions
diff --git a/meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch b/meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch
new file mode 100644
index 0000000000..29ec3bbeab
--- /dev/null
+++ b/meta/recipes-devtools/squashfs-tools/files/CVE-2021-41072.patch
@@ -0,0 +1,316 @@
1CVE: CVE-2021-41072
2Upstream-Status: Backport [https://github.com/plougher/squashfs-tools/commit/e048580]
3
4Backport commit to fix CVE-2021-41072. And squash a follow-up fix for
5CVE-2021-41072 from upstream:
6https://github.com/plougher/squashfs-tools/commit/19fcc93
7
8Update context for version 4.4.
9
10Signed-off-by: Kai Kang <kai.kang@windriver.com>
11
12From e0485802ec72996c20026da320650d8362f555bd Mon Sep 17 00:00:00 2001
13From: Phillip Lougher <phillip@squashfs.org.uk>
14Date: Sun, 12 Sep 2021 23:50:06 +0100
15Subject: [PATCH] Unsquashfs: additional write outside destination directory
16 exploit fix
17
18An issue on github (https://github.com/plougher/squashfs-tools/issues/72)
19showed how some specially crafted Squashfs filesystems containing
20invalid file names (with '/' and '..') can cause Unsquashfs to write
21files outside of the destination directory.
22
23Since then it has been shown that specially crafted Squashfs filesystems
24that contain a symbolic link pointing outside of the destination directory,
25coupled with an identically named file within the same directory, can
26cause Unsquashfs to write files outside of the destination directory.
27
28Specifically the symbolic link produces a pathname pointing outside
29of the destination directory, which is then followed when writing the
30duplicate identically named file within the directory.
31
32This commit fixes this exploit by explictly checking for duplicate
33filenames within a directory. As directories in v2.1, v3.x, and v4.0
34filesystems are sorted, this is achieved by checking for consecutively
35identical filenames. Additionally directories are checked to
36ensure they are sorted, to avoid attempts to evade the duplicate
37check.
38
39Version 1.x and 2.0 filesystems (where the directories were unsorted)
40are sorted and then the above duplicate filename check is applied.
41
42Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
43---
44 squashfs-tools/Makefile | 6 +-
45 squashfs-tools/unsquash-1.c | 6 ++
46 squashfs-tools/unsquash-12.c | 110 +++++++++++++++++++++++++++++++++
47 squashfs-tools/unsquash-1234.c | 21 +++++++
48 squashfs-tools/unsquash-2.c | 16 +++++
49 squashfs-tools/unsquash-3.c | 6 ++
50 squashfs-tools/unsquash-4.c | 6 ++
51 squashfs-tools/unsquashfs.h | 4 ++
52 8 files changed, 173 insertions(+), 2 deletions(-)
53 create mode 100644 squashfs-tools/unsquash-12.c
54
55diff --git a/squashfs-tools/Makefile b/squashfs-tools/Makefile
56index 7262a2e..1b544ed 100755
57--- a/squashfs-tools/Makefile
58+++ b/squashfs-tools/Makefile
59@@ -156,8 +156,8 @@ MKSQUASHFS_OBJS = mksquashfs.o read_fs.o
60 caches-queues-lists.o
61
62 UNSQUASHFS_OBJS = unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o \
63- unsquash-4.o unsquash-123.o unsquash-34.o unsquash-1234.o swap.o \
64- compressor.o unsquashfs_info.o
65+ unsquash-4.o unsquash-123.o unsquash-34.o unsquash-1234.o unsquash-12.o \
66+ swap.o compressor.o unsquashfs_info.o
67
68 CFLAGS ?= -O2
69 CFLAGS += $(EXTRA_CFLAGS) $(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 \
70@@ -353,6 +353,8 @@ unsquash-34.o: unsquashfs.h unsquash-34.c unsquashfs_error.h
71
72 unsquash-1234.o: unsquash-1234.c
73
74+unsquash-12.o: unsquash-12.c unsquashfs.h
75+
76 unsquashfs_xattr.o: unsquashfs_xattr.c unsquashfs.h squashfs_fs.h xattr.h
77
78 unsquashfs_info.o: unsquashfs.h squashfs_fs.h
79--- a/squashfs-tools/unsquash-1.c
80+++ b/squashfs-tools/unsquash-1.c
81@@ -314,6 +314,12 @@ static struct dir *squashfs_opendir(unsi
82 }
83 }
84
85+ /* check directory for duplicate names. Need to sort directory first */
86+ sort_directory(dir);
87+ if(check_directory(dir) == FALSE) {
88+ ERROR("File system corrupted: directory has duplicate names\n");
89+ goto corrupted;
90+ }
91 return dir;
92
93 corrupted:
94diff --git a/squashfs-tools/unsquash-12.c b/squashfs-tools/unsquash-12.c
95new file mode 100644
96index 0000000..61bf128
97--- /dev/null
98+++ b/squashfs-tools/unsquash-12.c
99@@ -0,0 +1,110 @@
100+/*
101+ * Unsquash a squashfs filesystem. This is a highly compressed read only
102+ * filesystem.
103+ *
104+ * Copyright (c) 2021
105+ * Phillip Lougher <phillip@squashfs.org.uk>
106+ *
107+ * This program is free software; you can redistribute it and/or
108+ * modify it under the terms of the GNU General Public License
109+ * as published by the Free Software Foundation; either version 2,
110+ * or (at your option) any later version.
111+ *
112+ * This program is distributed in the hope that it will be useful,
113+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
114+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
115+ * GNU General Public License for more details.
116+ *
117+ * You should have received a copy of the GNU General Public License
118+ * along with this program; if not, write to the Free Software
119+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
120+ *
121+ * unsquash-12.c
122+ *
123+ * Helper functions used by unsquash-1 and unsquash-2.
124+ */
125+
126+#include "unsquashfs.h"
127+
128+/*
129+ * Bottom up linked list merge sort.
130+ *
131+ */
132+void sort_directory(struct dir *dir)
133+{
134+ struct dir_ent *cur, *l1, *l2, *next;
135+ int len1, len2, stride = 1;
136+
137+ if(dir->dir_count < 2)
138+ return;
139+
140+ /*
141+ * We can consider our linked-list to be made up of stride length
142+ * sublists. Eacn iteration around this loop merges adjacent
143+ * stride length sublists into larger 2*stride sublists. We stop
144+ * when stride becomes equal to the entire list.
145+ *
146+ * Initially stride = 1 (by definition a sublist of 1 is sorted), and
147+ * these 1 element sublists are merged into 2 element sublists, which
148+ * are then merged into 4 element sublists and so on.
149+ */
150+ do {
151+ l2 = dir->dirs; /* head of current linked list */
152+ cur = NULL; /* empty output list */
153+
154+ /*
155+ * Iterate through the linked list, merging adjacent sublists.
156+ * On each interation l2 points to the next sublist pair to be
157+ * merged (if there's only one sublist left this is simply added
158+ * to the output list)
159+ */
160+ while(l2) {
161+ l1 = l2;
162+ for(len1 = 0; l2 && len1 < stride; len1 ++, l2 = l2->next);
163+ len2 = stride;
164+
165+ /*
166+ * l1 points to first sublist.
167+ * l2 points to second sublist.
168+ * Merge them onto the output list
169+ */
170+ while(len1 && l2 && len2) {
171+ if(strcmp(l1->name, l2->name) <= 0) {
172+ next = l1;
173+ l1 = l1->next;
174+ len1 --;
175+ } else {
176+ next = l2;
177+ l2 = l2->next;
178+ len2 --;
179+ }
180+
181+ if(cur) {
182+ cur->next = next;
183+ cur = next;
184+ } else
185+ dir->dirs = cur = next;
186+ }
187+ /*
188+ * One sublist is now empty, copy the other one onto the
189+ * output list
190+ */
191+ for(; len1; len1 --, l1 = l1->next) {
192+ if(cur) {
193+ cur->next = l1;
194+ cur = l1;
195+ } else
196+ dir->dirs = cur = l1;
197+ }
198+ for(; l2 && len2; len2 --, l2 = l2->next) {
199+ if(cur) {
200+ cur->next = l2;
201+ cur = l2;
202+ } else
203+ dir->dirs = cur = l2;
204+ }
205+ }
206+ cur->next = NULL;
207+ stride = stride << 1;
208+ } while(stride < dir->dir_count);
209+}
210diff --git a/squashfs-tools/unsquash-1234.c b/squashfs-tools/unsquash-1234.c
211index e389f8d..98a81ed 100644
212--- a/squashfs-tools/unsquash-1234.c
213+++ b/squashfs-tools/unsquash-1234.c
214@@ -72,3 +72,24 @@ void squashfs_closedir(struct dir *dir)
215
216 free(dir);
217 }
218+
219+
220+/*
221+ * Check directory for duplicate names. As the directory should be sorted,
222+ * duplicates will be consecutive. Obviously we also need to check if the
223+ * directory has been deliberately unsorted, to evade this check.
224+ */
225+int check_directory(struct dir *dir)
226+{
227+ int i;
228+ struct dir_ent *ent;
229+
230+ if(dir->dir_count < 2)
231+ return TRUE;
232+
233+ for(ent = dir->dirs, i = 0; i < dir->dir_count - 1; ent = ent->next, i++)
234+ if(strcmp(ent->name, ent->next->name) >= 0)
235+ return FALSE;
236+
237+ return TRUE;
238+}
239diff --git a/squashfs-tools/unsquash-2.c b/squashfs-tools/unsquash-2.c
240index 956f96f..0e36f7d 100644
241--- a/squashfs-tools/unsquash-2.c
242+++ b/squashfs-tools/unsquash-2.c
243@@ -29,6 +29,7 @@ static squashfs_fragment_entry_2 *fragme
244 static unsigned int *uid_table, *guid_table;
245 static char *inode_table, *directory_table;
246 static squashfs_operations ops;
247+static int needs_sorting = FALSE;
248
249 static void read_block_list(unsigned int *block_list, char *block_ptr, int blocks)
250 {
251@@ -415,6 +416,17 @@ static struct dir *squashfs_opendir(unsi
252 }
253 }
254
255+ if(needs_sorting)
256+ sort_directory(dir);
257+
258+ /* check directory for duplicate names and sorting */
259+ if(check_directory(dir) == FALSE) {
260+ if(needs_sorting)
261+ ERROR("File system corrupted: directory has duplicate names\n");
262+ else
263+ ERROR("File system corrupted: directory has duplicate names or is unsorted\n");
264+ goto corrupted;
265+ }
266 return dir;
267
268 corrupted:
269--- a/squashfs-tools/unsquash-3.c
270+++ b/squashfs-tools/unsquash-3.c
271@@ -442,6 +442,12 @@ static struct dir *squashfs_opendir(unsi
272 }
273 }
274
275+ /* check directory for duplicate names and sorting */
276+ if(check_directory(dir) == FALSE) {
277+ ERROR("File system corrupted: directory has duplicate names or is unsorted\n");
278+ goto corrupted;
279+ }
280+
281 return dir;
282
283 corrupted:
284diff --git a/squashfs-tools/unsquash-4.c b/squashfs-tools/unsquash-4.c
285index 694783d..c615bb8 100644
286--- a/squashfs-tools/unsquash-4.c
287+++ b/squashfs-tools/unsquash-4.c
288@@ -378,6 +378,12 @@ static struct dir *squashfs_opendir(unsi
289 }
290 }
291
292+ /* check directory for duplicate names and sorting */
293+ if(check_directory(dir) == FALSE) {
294+ ERROR("File system corrupted: directory has duplicate names or is unsorted\n");
295+ goto corrupted;
296+ }
297+
298 return dir;
299
300 corrupted:
301diff --git a/squashfs-tools/unsquashfs.h b/squashfs-tools/unsquashfs.h
302index f8cf78c..bf2a80d 100644
303--- a/squashfs-tools/unsquashfs.h
304+++ b/squashfs-tools/unsquashfs.h
305@@ -266,4 +266,8 @@ extern long long *alloc_index_table(int)
306 /* unsquash-1234.c */
307 extern int check_name(char *, int);
308 extern void squashfs_closedir(struct dir *);
309+extern int check_directory(struct dir *);
310+
311+/* unsquash-12.c */
312+extern void sort_directory(struct dir *);
313 #endif
314--
3152.17.1
316