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