CVE-2015-4646 (published 2017-04-13T17:59Z)

(1) unsquash-1.c, (2) unsquash-2.c, (3) unsquash-3.c, and (4) unsquash-4.c in Squashfs and sasquatch allow remote attackers to cause a denial of service (application crash) via a crafted input.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:N/I:N/A:P)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H)

Weakness information:
cwe_id: CWE-20
cwe_name: Improper Input Validation
description: The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
extended_description: 
	   Input validation is a frequently-used technique
	   for checking potentially dangerous inputs in order to
	   ensure that the inputs are safe for processing within the
	   code, or when communicating with other components.  When
	   software does not validate input properly, an attacker is
	   able to craft the input in a form that is not expected by
	   the rest of the application. This will lead to parts of the
	   system receiving unintended input, which may result in
	   altered control flow, arbitrary control of a resource, or
	   arbitrary code execution.
	   Input validation is not the only technique for
	   processing input, however.  Other techniques attempt to
	   transform potentially-dangerous input into something safe, such
	   as filtering (CWE-790) - which attempts to remove dangerous
	   inputs - or encoding/escaping (CWE-116), which attempts to
	   ensure that the input is not misinterpreted when it is included
	   in output to another component. Other techniques exist as well
	   (see CWE-138 for more examples.)
	   Input validation can be applied to:
	   
	     raw data - strings, numbers, parameters, file contents, etc.
	     metadata - information about the raw data, such as headers or size
	   
	   Data can be simple or structured.  Structured data
	   can be composed of many nested layers, composed of
	   combinations of metadata and raw data, with other simple or
	   structured data.
	   Many properties of raw data or metadata may need
	   to be validated upon entry into the code, such
	   as:
	   
	     specified quantities such as size, length, frequency, price, rate, number of operations, time, etc.
	     implied or derived quantities, such as the actual size of a file instead of a specified size
	     indexes, offsets, or positions into more complex data structures
	     symbolic keys or other elements into hash tables, associative arrays, etc.
	     well-formedness, i.e. syntactic correctness - compliance with expected syntax 
	     lexical token correctness - compliance with rules for what is treated as a token
	     specified or derived type - the actual type of the input (or what the input appears to be)
	     consistency - between individual data elements, between raw data and metadata, between references, etc.
	     conformance to domain-specific rules, e.g. business logic 
	     equivalence - ensuring that equivalent inputs are treated the same
	     authenticity, ownership, or other attestations about the input, e.g. a cryptographic signature to prove the source of the data
	   
	   Implied or derived properties of data must often
	   be calculated or inferred by the code itself.  Errors in
	   deriving properties may be considered a contributing factor
	   to improper input validation.
	   
	   Note that "input validation" has very different
	   meanings to different people, or within different
	   classification schemes.  Caution must be used when
	   referencing this CWE entry or mapping to it.  For example,
	   some weaknesses might involve inadvertently giving control
	   to an attacker over an input when they should not be able
	   to provide an input at all, but sometimes this is referred
	   to as input validation.
	   Finally, it is important to emphasize that the
	   distinctions between input validation and output escaping
	   are often blurred, and developers must be careful to
	   understand the difference, including how input validation
	   is not always sufficient to prevent vulnerabilities,
	   especially when less stringent data types must be
	   supported, such as free-form text. Consider a SQL injection
	   scenario in which a person's last name is inserted into a
	   query. The name "O'Reilly" would likely pass the validation
	   step since it is a common last name in the English
	   language. However, this valid name cannot be directly
	   inserted into the database because it contains the "'"
	   apostrophe character, which would need to be escaped or
	   otherwise transformed. In this case, removing the
	   apostrophe might reduce the risk of SQL injection, but it
	   would produce incorrect behavior because the wrong name
	   would be recorded.
       
         

Fix information:
repo_url: https://github.com/plougher/squashfs-tools
commit_details: repo_url: https://github.com/plougher/squashfs-tools
author: Phillip Lougher
author_date: 2019-07-15 21:19:06+01:00
msg: unsquashfs-4: Add more sanity checks + fix CVE-2015-4645/6

Add more filesystem table sanity checks to Unsquashfs-4 and
also properly fix CVE-2015-4645 and CVE-2015-4646.

The CVEs were raised due to Unsquashfs having variable
oveflow and stack overflow in a number of vulnerable
functions.

The suggested patch only "fixed" one such function and fixed
it badly, and so it was buggy and introduced extra bugs!

The suggested patch was not only buggy, but, it used the
essentially wrong approach too.  It was "fixing" the
symptom but not the cause.  The symptom is wrong values
causing overflow, the cause is filesystem corruption.
This corruption should be detected and the filesystem
rejected *before* trying to allocate memory.

This patch applies the following fixes:

1. The filesystem super-block tables are checked, and the values
   must match across the filesystem.

   This will trap corrupted filesystems created by Mksquashfs.

2. The maximum (theorectical) size the filesystem tables could grow
   to, were analysed, and some variables were increased from int to
   long long.

   This analysis has been added as comments.

3. Stack allocation was removed, and a shared buffer (which is
   checked and increased as necessary) is used to read the
   table indexes.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
repository: repo_url: https://github.com/plougher/squashfs-tools
repo_name: plougher/squashfs-tools
description: tools to create and extract Squashfs filesystems
repo_language: C
owner: plougher
file_changes: filename: read_xattrs.c
change_type: ModificationType.MODIFY
diff: @@ -150,7 +150,16 @@ static int read_xattr_entry(struct xattr_list *xattr,
  */
 int read_xattrs_from_disk(int fd, struct squashfs_super_block *sBlk, int flag, long long *table_start)
 {
-	int res, bytes, i, indexes, index_bytes, ids;
+	/*
+	 * Note on overflow limits:
+	 * Size of ids (id_table.xattr_ids) is 2^32 (unsigned int)
+	 * Max size of bytes is 2^32*16 or 2^36
+	 * Max indexes is (2^32*16)/8K or 2^23
+	 * Max index_bytes is ((2^32*16)/8K)*8 or 2^26 or 64M
+	 */
+	int res, i, indexes, index_bytes;
+	unsigned int ids;
+	long long bytes;
 	long long *index, start, end;
 	struct squashfs_xattr_table id_table;
 
@@ -170,24 +179,44 @@ int read_xattrs_from_disk(int fd, struct squashfs_super_block *sBlk, int flag, l
 
 	SQUASHFS_INSWAP_XATTR_TABLE(&id_table);
 
-	if(flag) {
-		/*
-		 * id_table.xattr_table_start stores the start of the compressed xattr
-		 * * metadata blocks.  This by definition is also the end of the previous
-		 * filesystem table - the id lookup table.
-		 */
+	/*
+	 * Compute index table values
+	 */
+	ids = id_table.xattr_ids;
+	xattr_table_start = id_table.xattr_table_start;
+	index_bytes = SQUASHFS_XATTR_BLOCK_BYTES((long long) ids);
+	indexes = SQUASHFS_XATTR_BLOCKS((long long) ids);
+
+	/*
+	 * The size of the index table (index_bytes) should match the
+	 * table start and end points
+	 */
+	if(index_bytes != (sBlk->bytes_used - (sBlk->xattr_id_table_start + sizeof(id_table)))) {
+		ERROR("read_xattrs_from_disk: Bad xattr_ids count in super block\n");
+		return 0;
+	}
+
+	/*
+	 * id_table.xattr_table_start stores the start of the compressed xattr
+	 * metadata blocks.  This by definition is also the end of the previous
+	 * filesystem table - the id lookup table.
+	 */
+	if(table_start != NULL)
 		*table_start = id_table.xattr_table_start;
+
+	/*
+	 * If flag is set then return once we've read the above
+	 * table_start.  That value is necessary for sanity checking,
+	 * but we don't actually want to extract the xattrs, and so
+	 * stop here.
+	 */
+	if(flag)
 		return id_table.xattr_ids;
-	}
 
 	/*
 	 * Allocate and read the index to the xattr id table metadata
 	 * blocks
 	 */
-	ids = id_table.xattr_ids;
-	xattr_table_start = id_table.xattr_table_start;
-	index_bytes = SQUASHFS_XATTR_BLOCK_BYTES(ids);
-	indexes = SQUASHFS_XATTR_BLOCKS(ids);
 	index = malloc(index_bytes);
 	if(index == NULL)
 		MEM_ERROR();
@@ -203,7 +232,7 @@ int read_xattrs_from_disk(int fd, struct squashfs_super_block *sBlk, int flag, l
 	 * Allocate enough space for the uncompressed xattr id table, and
 	 * read and decompress it
 	 */
-	bytes = SQUASHFS_XATTR_BYTES(ids);
+	bytes = SQUASHFS_XATTR_BYTES((long long) ids);
 	xattr_ids = malloc(bytes);
 	if(xattr_ids == NULL)
 		MEM_ERROR();
@@ -213,7 +242,7 @@ int read_xattrs_from_disk(int fd, struct squashfs_super_block *sBlk, int flag, l
 					bytes & (SQUASHFS_METADATA_SIZE - 1);
 		int length = read_block(fd, index[i], NULL, expected,
 			((unsigned char *) xattr_ids) +
-			(i * SQUASHFS_METADATA_SIZE));
+			((long long) i * SQUASHFS_METADATA_SIZE));
 		TRACE("Read xattr id table block %d, from 0x%llx, length "
 			"%d\n", i, index[i], length);
 		if(length == 0) {

programming_language: C
filename: unsquash-4.c
change_type: ModificationType.MODIFY
diff: @@ -29,30 +29,63 @@
 static struct squashfs_fragment_entry *fragment_table;
 static unsigned int *id_table;
 
-static int read_fragment_table(long long *directory_table_end)
+long long *alloc_index_table(int indexes)
 {
+	static long long *alloc_table = NULL;
+	static int alloc_size = 0;
+	int length = indexes * sizeof(long long);
+
+	if(alloc_size < length) {
+		long long *table = realloc(alloc_table, length);
+
+		if(table == NULL)
+			EXIT_UNSQUASH("alloc_index_table: failed to allocate "
+				"index table\n");
+
+		alloc_table = table;
+		alloc_size = length;
+	}
+
+	return alloc_table;
+}
+
+
+static int read_fragment_table(long long *table_start)
+{
+	/*
+	 * Note on overflow limits:
+	 * Size of SBlk.s.fragments is 2^32 (unsigned int)
+	 * Max size of bytes is 2^32*16 or 2^36
+	 * Max indexes is (2^32*16)/8K or 2^23
+	 * Max length is ((2^32*16)/8K)*8 or 2^26 or 64M
+	 */
 	int res, i;
-	int bytes = SQUASHFS_FRAGMENT_BYTES(sBlk.s.fragments);
-	int  indexes = SQUASHFS_FRAGMENT_INDEXES(sBlk.s.fragments);
-	long long fragment_table_index[indexes];
+	long long bytes = SQUASHFS_FRAGMENT_BYTES((long long) sBlk.s.fragments);
+	int indexes = SQUASHFS_FRAGMENT_INDEXES((long long) sBlk.s.fragments);
+	int length = SQUASHFS_FRAGMENT_INDEX_BYTES((long long) sBlk.s.fragments);
+	long long *fragment_table_index;
+
+	/*
+	 * The size of the index table (length bytes) should match the
+	 * table start and end points
+	 */
+	if(length != (*table_start - sBlk.s.fragment_table_start)) {
+		ERROR("read_fragment_table: Bad fragment count in super block\n");
+		return FALSE;
+	}
 
 	TRACE("read_fragment_table: %d fragments, reading %d fragment indexes "
 		"from 0x%llx\n", sBlk.s.fragments, indexes,
 		sBlk.s.fragment_table_start);
 
-	if(sBlk.s.fragments == 0) {
-		*directory_table_end = sBlk.s.fragment_table_start;
-		return TRUE;
-	}
-
+	fragment_table_index = alloc_index_table(indexes);
 	fragment_table = malloc(bytes);
 	if(fragment_table == NULL)
 		EXIT_UNSQUASH("read_fragment_table: failed to allocate "
 			"fragment table\n");
 
-	res = read_fs_bytes(fd, sBlk.s.fragment_table_start,
-		SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk.s.fragments),
-		fragment_table_index);
+	res = read_fs_bytes(fd, sBlk.s.fragment_table_start, length,
+							fragment_table_index);
 	if(res == FALSE) {
 		ERROR("read_fragment_table: failed to read fragment table "
 			"index\n");
@@ -78,7 +111,7 @@ static int read_fragment_table(long long *directory_table_end)
 	for(i = 0; i < sBlk.s.fragments; i++) 
 		SQUASHFS_INSWAP_FRAGMENT_ENTRY(&fragment_table[i]);
 
-	*directory_table_end = fragment_table_index[0];
+	*table_start = fragment_table_index[0];
 	return TRUE;
 }
 
@@ -356,25 +389,42 @@ corrupted:
 }
 
 
-static int read_uids_guids(long long *table_start)
+static int read_id_table(long long *table_start)
 {
+	/*
+	 * Note on overflow limits:
+	 * Size of SBlk.s.no_ids is 2^16 (unsigned short)
+	 * Max size of bytes is 2^16*4 or 256K
+	 * Max indexes is (2^16*4)/8K or 32
+	 * Max length is ((2^16*4)/8K)*8 or 256
+	 */
 	int res, i;
 	int bytes = SQUASHFS_ID_BYTES(sBlk.s.no_ids);
 	int indexes = SQUASHFS_ID_BLOCKS(sBlk.s.no_ids);
-	long long id_index_table[indexes];
+	int length = SQUASHFS_ID_BLOCK_BYTES(sBlk.s.no_ids);
+	long long *id_index_table;
+
+	/*
+	 * The size of the index table (length bytes) should match the
+	 * table start and end points
+	 */
+	if(length != (*table_start - sBlk.s.id_table_start)) {
+		ERROR("read_id_table: Bad id count in super block\n");
+		return FALSE;
+	}
 
-	TRACE("read_uids_guids: no_ids %d\n", sBlk.s.no_ids);
+	TRACE("read_id_table: no_ids %d\n", sBlk.s.no_ids);
 
+	id_index_table = alloc_index_table(indexes);
 	id_table = malloc(bytes);
 	if(id_table == NULL) {
-		ERROR("read_uids_guids: failed to allocate id table\n");
+		ERROR("read_id_table: failed to allocate id table\n");
 		return FALSE;
 	}
 
-	res = read_fs_bytes(fd, sBlk.s.id_table_start,
-		SQUASHFS_ID_BLOCK_BYTES(sBlk.s.no_ids), id_index_table);
+	res = read_fs_bytes(fd, sBlk.s.id_table_start, length, id_index_table);
 	if(res == FALSE) {
-		ERROR("read_uids_guids: failed to read id index table\n");
+		ERROR("read_id_table: failed to read id index table\n");
 		return FALSE;
 	}
 	SQUASHFS_INSWAP_ID_BLOCKS(id_index_table, indexes);
@@ -393,7 +443,7 @@ static int read_uids_guids(long long *table_start)
 		res = read_block(fd, id_index_table[i], NULL, expected,
 			((char *) id_table) + i * SQUASHFS_METADATA_SIZE);
 		if(res == FALSE) {
-			ERROR("read_uids_guids: failed to read id table block"
+			ERROR("read_id_table: failed to read id table block"
 				"\n");
 			return FALSE;
 		}
@@ -407,12 +457,30 @@ static int read_uids_guids(long long *table_start)
 
 static int parse_exports_table(long long *table_start)
 {
+	/*
+	 * Note on overflow limits:
+	 * Size of SBlk.s.inodes is 2^32 (unsigned int)
+	 * Max indexes is (2^32*8)/8K or 2^22
+	 * Max length is ((2^32*8)/8K)*8 or 2^25
+	 */
 	int res;
-	int indexes = SQUASHFS_LOOKUP_BLOCKS(sBlk.s.inodes);
-	long long export_index_table[indexes];
+	int indexes = SQUASHFS_LOOKUP_BLOCKS((long long) sBlk.s.inodes);
+	int length = SQUASHFS_LOOKUP_BLOCK_BYTES((long long) sBlk.s.inodes);
+	long long *export_index_table;
+
+	/*
+	 * The size of the index table (length bytes) should match the
+	 * table start and end points
+	 */
+	if(length != (*table_start - sBlk.s.lookup_table_start)) {
+		ERROR("parse_exports_table: Bad inode count in super block\n");
+		return FALSE;
+	}
 
-	res = read_fs_bytes(fd, sBlk.s.lookup_table_start,
-		SQUASHFS_LOOKUP_BLOCK_BYTES(sBlk.s.inodes), export_index_table);
+	export_index_table = alloc_index_table(indexes);
+
+	res = read_fs_bytes(fd, sBlk.s.lookup_table_start, length,
+							export_index_table);
 	if(res == FALSE) {
 		ERROR("parse_exports_table: failed to read export index table\n");
 		return FALSE;
@@ -432,30 +500,118 @@ static int parse_exports_table(long long *table_start)
 
 int read_filesystem_tables_4()
 {
-	long long directory_table_end, table_start;
+	long long table_start;
 
-	if(read_xattrs_from_disk(fd, &sBlk.s, no_xattrs, &table_start) == 0)
-		return FALSE;
+	/* Read xattrs */
+	if(sBlk.s.xattr_id_table_start != SQUASHFS_INVALID_BLK) {
+		/* sanity check super block contents */
+		if(sBlk.s.xattr_id_table_start >= sBlk.s.bytes_used) {
+			ERROR("read_filesystem_tables: xattr id table start too large in super block\n");
+			goto corrupted;
+		}
 
-	if(read_uids_guids(&table_start) == FALSE)
-		return FALSE;
+		if(read_xattrs_from_disk(fd, &sBlk.s, no_xattrs, &table_start) == 0)
+			goto corrupted;
+	} else
+		table_start = sBlk.s.bytes_used;
 
-	if(parse_exports_table(&table_start) == FALSE)
-		return FALSE;
+	/* Read id lookup table */
 
-	if(read_fragment_table(&directory_table_end) == FALSE)
-		return FALSE;
+	/* Sanity check super block contents */
+	if(sBlk.s.id_table_start >= table_start) {
+		ERROR("read_filesystem_tables: id table start too large in super block\n");
+		goto corrupted;
+	}
 
-	if(read_inode_table(sBlk.s.inode_table_start,
-				sBlk.s.directory_table_start) == FALSE)
-		return FALSE;
+	/* there should always be at least one id */
+	if(sBlk.s.no_ids == 0) {
+		ERROR("read_filesystem_tables: Bad id count in super block\n");
+		goto corrupted;
+	}
+
+	/*
+	 * the number of ids can never be more than double the number of inodes
+	 * (the maximum is a unique uid and gid for each inode).
+	 */
+	if(sBlk.s.no_ids > (sBlk.s.inodes * 2L)) {
+		ERROR("read_filesystem_tables: Bad id count in super block\n");
+		goto corrupted;
+	}
+
+	if(read_id_table(&table_start) == FALSE)
+		goto corrupted;
+
+	/* Read exports table */
+	if(sBlk.s.lookup_table_start != SQUASHFS_INVALID_BLK) {
+
+		/* sanity check super block contents */
+		if(sBlk.s.lookup_table_start >= table_start) {
+			ERROR("read_filesystem_tables: lookup table start too large in super block\n");
+			goto corrupted;
+		}
+
+		if(parse_exports_table(&table_start) == FALSE)
+			goto corrupted;
+	}
+
+	/* Read fragment table */
+	if(sBlk.s.fragments != 0) {
+
+		/* Sanity check super block contents */
+		if(sBlk.s.fragment_table_start >= table_start) {
+			ERROR("read_filesystem_tables: fragment table start too large in super block\n");
+			goto corrupted;
+		}
+
+		/* The number of fragments should not exceed the number of inodes */
+		if(sBlk.s.fragments > sBlk.s.inodes) {
+			ERROR("read_filesystem_tables: Bad fragment count in super block\n");
+			goto corrupted;
+		}
+
+		if(read_fragment_table(&table_start) == FALSE)
+			goto corrupted;
+	} else {
+		/*
+		 * Sanity check super block contents - with 0 fragments,
+		 * the fragment table should be empty
+		 */
+		if(sBlk.s.fragment_table_start != table_start) {
+			ERROR("read_filesystem_tables: fragment table start invalid in super block\n");
+			goto corrupted;
+		}
+	}
+
+	/* Read directory table */
+
+	/* Sanity check super block contents */
+	if(sBlk.s.directory_table_start >= table_start) {
+		ERROR("read_filesystem_tables: directory table start too large in super block\n");
+		goto corrupted;
+	}
 
 	if(read_directory_table(sBlk.s.directory_table_start,
-				directory_table_end) == FALSE)
-		return FALSE;
+				table_start) == FALSE)
+		goto corrupted;
+
+	/* Read inode table */
+
+	/* Sanity check super block contents */
+	if(sBlk.s.inode_table_start >= sBlk.s.directory_table_start) {
+		ERROR("read_filesystem_tables: inode table start too large in super block\n");
+		goto corrupted;
+	}
+
+	if(read_inode_table(sBlk.s.inode_table_start,
+				sBlk.s.directory_table_start) == FALSE)
+		goto corrupted;
 
 	if(no_xattrs)
 		sBlk.s.xattr_id_table_start = SQUASHFS_INVALID_BLK;
 
 	return TRUE;
+
+corrupted:
+	ERROR("File system corruption detected\n");
+	return FALSE;
 }

programming_language: C
filename: unsquashfs.c
change_type: ModificationType.MODIFY
diff: @@ -2465,7 +2465,7 @@ int parse_number(char *arg, int *res)
 
 
 #define VERSION() \
-	printf("unsquashfs version 4.3-git (2019/07/05)\n");\
+	printf("unsquashfs version 4.3-git (2019/07/15)\n");\
 	printf("copyright (C) 2019 Phillip Lougher "\
 		"<phillip@squashfs.org.uk>\n\n");\
     	printf("This program is free software; you can redistribute it and/or"\

programming_language: C