1CVE: CVE-2021-41072 2Upstream-Status: Backport [https://github.com/plougher/squashfs-tools/commit/e048580] 3 4Update on 20211109: 5Squash a follow-up fix for CVE-2021-41072 from upstream: 6https://github.com/plougher/squashfs-tools/commit/19fcc93 7 8Signed-off-by: Kai Kang <kai.kang@windriver.com> 9 10From e0485802ec72996c20026da320650d8362f555bd Mon Sep 17 00:00:00 2001 11From: Phillip Lougher <phillip@squashfs.org.uk> 12Date: Sun, 12 Sep 2021 23:50:06 +0100 13Subject: [PATCH] Unsquashfs: additional write outside destination directory 14 exploit fix 15 16An issue on github (https://github.com/plougher/squashfs-tools/issues/72) 17showed how some specially crafted Squashfs filesystems containing 18invalid file names (with '/' and '..') can cause Unsquashfs to write 19files outside of the destination directory. 20 21Since then it has been shown that specially crafted Squashfs filesystems 22that contain a symbolic link pointing outside of the destination directory, 23coupled with an identically named file within the same directory, can 24cause Unsquashfs to write files outside of the destination directory. 25 26Specifically the symbolic link produces a pathname pointing outside 27of the destination directory, which is then followed when writing the 28duplicate identically named file within the directory. 29 30This commit fixes this exploit by explictly checking for duplicate 31filenames within a directory. As directories in v2.1, v3.x, and v4.0 32filesystems are sorted, this is achieved by checking for consecutively 33identical filenames. Additionally directories are checked to 34ensure they are sorted, to avoid attempts to evade the duplicate 35check. 36 37Version 1.x and 2.0 filesystems (where the directories were unsorted) 38are sorted and then the above duplicate filename check is applied. 39 40Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk> 41--- 42 squashfs-tools/Makefile | 6 +- 43 squashfs-tools/unsquash-1.c | 6 ++ 44 squashfs-tools/unsquash-12.c | 110 +++++++++++++++++++++++++++++++++ 45 squashfs-tools/unsquash-1234.c | 21 +++++++ 46 squashfs-tools/unsquash-2.c | 16 +++++ 47 squashfs-tools/unsquash-3.c | 6 ++ 48 squashfs-tools/unsquash-4.c | 6 ++ 49 squashfs-tools/unsquashfs.h | 4 ++ 50 8 files changed, 173 insertions(+), 2 deletions(-) 51 create mode 100644 squashfs-tools/unsquash-12.c 52 53diff --git a/squashfs-tools/Makefile b/squashfs-tools/Makefile 54index 7262a2e..1b544ed 100755 55--- a/squashfs-tools/Makefile 56+++ b/squashfs-tools/Makefile 57@@ -160,8 +160,8 @@ MKSQUASHFS_OBJS = mksquashfs.o read_fs.o action.o swap.o pseudo.o compressor.o \ 58 caches-queues-lists.o reader.o tar.o 59 60 UNSQUASHFS_OBJS = unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o \ 61- unsquash-4.o unsquash-123.o unsquash-34.o unsquash-1234.o swap.o \ 62- compressor.o unsquashfs_info.o 63+ unsquash-4.o unsquash-123.o unsquash-34.o unsquash-1234.o unsquash-12.o \ 64+ swap.o compressor.o unsquashfs_info.o 65 66 CFLAGS ?= -O2 67 CFLAGS += $(EXTRA_CFLAGS) $(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 \ 68@@ -393,6 +393,8 @@ unsquash-34.o: unsquashfs.h unsquash-34.c unsquashfs_error.h 69 70 unsquash-1234.o: unsquash-1234.c unsquashfs_error.h 71 72+unsquash-12.o: unsquash-12.c unsquashfs.h 73+ 74 unsquashfs_xattr.o: unsquashfs_xattr.c unsquashfs.h squashfs_fs.h xattr.h unsquashfs_error.h 75 76 unsquashfs_info.o: unsquashfs.h squashfs_fs.h unsquashfs_error.h 77diff --git a/squashfs-tools/unsquash-1.c b/squashfs-tools/unsquash-1.c 78index b604434..88866fc 100644 79--- a/squashfs-tools/unsquash-1.c 80+++ b/squashfs-tools/unsquash-1.c 81@@ -370,6 +370,12 @@ static struct dir *squashfs_opendir(unsigned int block_start, unsigned int offse 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 @@ 244 static squashfs_fragment_entry_2 *fragment_table; 245 static unsigned int *uid_table, *guid_table; 246 static squashfs_operations ops; 247+static int needs_sorting = FALSE; 248 249 250 static void read_block_list(unsigned int *block_list, long long start, 251@@ -463,6 +464,17 @@ static struct dir *squashfs_opendir(unsigned int block_start, unsigned int offse 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@@ -596,6 +608,10 @@ int read_super_2(squashfs_operations **s_ops, void *s) 270 * 2.x filesystems use gzip compression. 271 */ 272 comp = lookup_compressor("gzip"); 273+ 274+ if(sBlk_3->s_minor == 0) 275+ needs_sorting = TRUE; 276+ 277 return TRUE; 278 } 279 280diff --git a/squashfs-tools/unsquash-3.c b/squashfs-tools/unsquash-3.c 281index 835a574..0123562 100644 282--- a/squashfs-tools/unsquash-3.c 283+++ b/squashfs-tools/unsquash-3.c 284@@ -497,6 +497,12 @@ static struct dir *squashfs_opendir(unsigned int block_start, unsigned int offse 285 } 286 } 287 288+ /* check directory for duplicate names and sorting */ 289+ if(check_directory(dir) == FALSE) { 290+ ERROR("File system corrupted: directory has duplicate names or is unsorted\n"); 291+ goto corrupted; 292+ } 293+ 294 return dir; 295 296 corrupted: 297diff --git a/squashfs-tools/unsquash-4.c b/squashfs-tools/unsquash-4.c 298index 694783d..c615bb8 100644 299--- a/squashfs-tools/unsquash-4.c 300+++ b/squashfs-tools/unsquash-4.c 301@@ -434,6 +434,12 @@ static struct dir *squashfs_opendir(unsigned int block_start, unsigned int offse 302 } 303 } 304 305+ /* check directory for duplicate names and sorting */ 306+ if(check_directory(dir) == FALSE) { 307+ ERROR("File system corrupted: directory has duplicate names or is unsorted\n"); 308+ goto corrupted; 309+ } 310+ 311 return dir; 312 313 corrupted: 314diff --git a/squashfs-tools/unsquashfs.h b/squashfs-tools/unsquashfs.h 315index f8cf78c..bf2a80d 100644 316--- a/squashfs-tools/unsquashfs.h 317+++ b/squashfs-tools/unsquashfs.h 318@@ -293,4 +293,8 @@ extern long long *alloc_index_table(int); 319 /* unsquash-1234.c */ 320 extern int check_name(char *, int); 321 extern void squashfs_closedir(struct dir *); 322+extern int check_directory(struct dir *); 323+ 324+/* unsquash-12.c */ 325+extern void sort_directory(struct dir *); 326 #endif 327-- 3282.17.1 329 330