Skip to contentSkip to conversation

fs/squashfs: Fix reading on big-endian systems

PATCH New Josef Schlehofer 1 participant 1 message Nov 25 SquashFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/squashfs: Fix reading on big-endian systems

Squashfs stores its metadata in little-endian byte order. U-Boot reads the headers of the inode and directory table blocks, the directory headers and entries, the fragment table entries and the data block sizes in host byte order, so on a big-endian system it cannot list a directory or read a file.

Convert them to host byte order where they are used, as is already done for the super block and the inodes, and mark the on-disk fields as little-endian.

Fixes c51006130370 ("fs/squashfs: new filesystem")

---
Tested on qemu-ppce500 and qemu_arm with a gzip and an uncompressed image holding a directory of 600 entries, small files in a fragment block, a sparse file, a hard link and a symlink. ls, size and load now match the host on both. Before, they all failed on qemu-ppce500. On qemu_arm the code generated for fs/squashfs does not change.

 fs/squashfs/sqfs.c            | 53 ++++++++++++++++++++---------------
 fs/squashfs/sqfs_filesystem.h | 28 +++++++++---------
 fs/squashfs/sqfs_inode.c      |  2 +-
 3 files changed, 45 insertions(+), 38 deletions(-)
fs/squashfs/sqfs.c +30 -23
index 2092e3c1c71..49430f7edfb 100644
@@ -249,7 +249,7 @@ static int sqfs_frag_lookup(u32 inode_fragment_index,
 	}
 
 	*e = entries[offset];
-	ret = SQFS_COMPRESSED_BLOCK(e->size);
+	ret = SQFS_COMPRESSED_BLOCK(le32_to_cpu(e->size));
 
 out:
 	free(entries);
@@ -557,7 +557,7 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 	/* Initialize squashfs_dir_stream members */
 	dirs->table += SQFS_DIR_HEADER_SIZE;
 	dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE;
-	dirs->entry_count = dirs->dir_header->count + 1;
+	dirs->entry_count = le32_to_cpu(dirs->dir_header->count) + 1;
 
 	/* No path given -> root directory */
 	if (!strcmp(token_list[0], "/")) {
@@ -588,8 +588,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 		}
 
 		/* Redefine inode as the found token */
-		new_inode_number = dirs->entry->inode_offset +
-			dirs->dir_header->inode_number;
+		new_inode_number = (s16)le16_to_cpu(dirs->entry->inode_offset) +
+			le32_to_cpu(dirs->dir_header->inode_number);
 
 		/* Get reference to inode in the inode table */
 		table = sqfs_find_inode(dirs->inode_table,
@@ -696,7 +696,7 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 
 		dirs->table += SQFS_DIR_HEADER_SIZE;
 		dirs->size = get_unaligned_le16(&dir->file_size);
-		dirs->entry_count = dirs->dir_header->count + 1;
+		dirs->entry_count = le32_to_cpu(dirs->dir_header->count) + 1;
 		dirs->size -= SQFS_DIR_HEADER_SIZE;
 		free(dirs->entry);
 		dirs->entry = NULL;
@@ -1065,7 +1065,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
 
 	/* Setup directory header */
 	memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE);
-	dirs->entry_count = dirs->dir_header->count + 1;
+	dirs->entry_count = le32_to_cpu(dirs->dir_header->count) + 1;
 	dirs->size -= SQFS_DIR_HEADER_SIZE;
 
 	/* Setup entry */
@@ -1135,7 +1135,7 @@ static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **d
 			/* Read follow-up (emitted) dir. header */
 			memcpy(dirs->dir_header, dirs->table,
 			       SQFS_DIR_HEADER_SIZE);
-			dirs->entry_count = dirs->dir_header->count + 1;
+			dirs->entry_count = le32_to_cpu(dirs->dir_header->count) + 1;
 			ret = sqfs_read_entry(&dirs->entry, dirs->table +
 					      SQFS_DIR_HEADER_SIZE);
 			if (ret)
@@ -1149,7 +1149,8 @@ static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **d
 			return -SQFS_STOP_READDIR;
 	}
 
-	i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
+	i_number = le32_to_cpu(dirs->dir_header->inode_number) +
+		(s16)le16_to_cpu(dirs->entry->inode_offset);
 	ipos = sqfs_find_inode(dirs->inode_table, dirs->inode_table_size,
 			       i_number, sblk->inodes, sblk->block_size);
 	if (!ipos)
@@ -1158,7 +1159,7 @@ static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **d
 	base = (struct squashfs_base_inode *)ipos;
 
 	/* Set entry type and size */
-	switch (dirs->entry->type) {
+	switch (le16_to_cpu(dirs->entry->type)) {
 	case SQFS_DIR_TYPE:
 	case SQFS_LDIR_TYPE:
 		dent->type = FS_DT_DIR;
@@ -1198,11 +1199,12 @@ static int sqfs_readdir_nest(struct fs_dir_stream *fs_dirs, struct fs_dirent **d
 	}
 
 	/* Set entry name (capped at FS_DIRENT_NAME_LEN which is a U-Boot limitation) */
-	name_size = min_t(u16, dirs->entry->name_size + 1, FS_DIRENT_NAME_LEN - 1);
+	name_size = min_t(u16, le16_to_cpu(dirs->entry->name_size) + 1,
+			  FS_DIRENT_NAME_LEN - 1);
 	strncpy(dent->name, dirs->entry->name, name_size);
 	dent->name[name_size] = '\0';
 
-	offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH;
+	offset = le16_to_cpu(dirs->entry->name_size) + 1 + SQFS_ENTRY_BASE_LENGTH;
 	dirs->entry_count--;
 
 	/* Decrement size to be read */
@@ -1401,7 +1403,8 @@ static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg,
 		if (ret < 0)
 			return -EINVAL;
 		finfo->comp = ret;
-		if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
+		if (le32_to_cpu(fentry->size) < 1 ||
+		    le64_to_cpu(fentry->start) == 0x7FFFFFFF)
 			return -EINVAL;
 	} else {
 		datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
@@ -1439,7 +1442,8 @@ static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg,
 		if (ret < 0)
 			return -EINVAL;
 		finfo->comp = ret;
-		if (fentry->size < 1 || fentry->start == 0x7FFFFFFF)
+		if (le32_to_cpu(fentry->size) < 1 ||
+		    le64_to_cpu(fentry->start) == 0x7FFFFFFF)
 			return -EINVAL;
 	} else {
 		datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz));
@@ -1512,7 +1516,8 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		goto out;
 	}
 
-	i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
+	i_number = le32_to_cpu(dirs->dir_header->inode_number) +
+		(s16)le16_to_cpu(dirs->entry->inode_offset);
 	ipos = sqfs_find_inode(dirs->inode_table, dirs->inode_table_size,
 			       i_number, sblk->inodes, sblk->block_size);
 	if (!ipos) {
@@ -1609,13 +1614,13 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		char *data_buffer;
 
 		start = lldiv(data_offset, ctxt.cur_dev->blksz);
-		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
+		table_size = SQFS_BLOCK_SIZE(le32_to_cpu(finfo.blk_sizes[j]));
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
 		n_blks = DIV_ROUND_UP(table_size + table_offset,
 				      ctxt.cur_dev->blksz);
 
 		/* Don't load any data for sparse blocks */
-		if (finfo.blk_sizes[j] == 0) {
+		if (le32_to_cpu(finfo.blk_sizes[j]) == 0) {
 			n_blks = 0;
 			table_offset = 0;
 			data_buffer = NULL;
@@ -1643,14 +1648,14 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		}
 
 		/* Load the data */
-		if (finfo.blk_sizes[j] == 0) {
+		if (le32_to_cpu(finfo.blk_sizes[j]) == 0) {
 			/* This is a sparse block */
 			sparse_size = get_unaligned_le32(&sblk->block_size);
 			if ((*actread + sparse_size) > len)
 				sparse_size = len - *actread;
 			memset(buf + *actread, 0, sparse_size);
 			*actread += sparse_size;
-		} else if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) {
+		} else if (SQFS_COMPRESSED_BLOCK(le32_to_cpu(finfo.blk_sizes[j]))) {
 			dest_len = get_unaligned_le32(&sblk->block_size);
 			ret = sqfs_decompress(&ctxt, datablock, &dest_len,
 					      data, table_size);
@@ -1682,9 +1687,10 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		goto out;
 	}
 
-	start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
-	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
-	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
+	start = lldiv(le64_to_cpu(frag_entry.start), ctxt.cur_dev->blksz);
+	table_size = SQFS_BLOCK_SIZE(le32_to_cpu(frag_entry.size));
+	table_offset = le64_to_cpu(frag_entry.start) -
+		(start * ctxt.cur_dev->blksz);
 	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
 
 	if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size)) {
@@ -1714,7 +1720,7 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 
 		ret = sqfs_decompress(&ctxt, fragment_block, &dest_len,
 				      (void *)fragment  + table_offset,
-				      frag_entry.size);
+				      le32_to_cpu(frag_entry.size));
 		if (ret) {
 			free(fragment_block);
 			goto out;
@@ -1818,7 +1824,8 @@ static int sqfs_size_nest(const char *filename, loff_t *size)
 		goto free_strings;
 	}
 
-	i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset;
+	i_number = le32_to_cpu(dirs->dir_header->inode_number) +
+		(s16)le16_to_cpu(dirs->entry->inode_offset);
 	ipos = sqfs_find_inode(dirs->inode_table, dirs->inode_table_size,
 			       i_number, sblk->inodes, sblk->block_size);
 
fs/squashfs/sqfs_filesystem.h +14 -14
index 6c97b2c3a9c..1f9960f5abd 100644
@@ -81,9 +81,9 @@ struct squashfs_ctxt {
 };
 
 struct squashfs_directory_index {
-	u32 index;
-	u32 start;
-	u32 size;
+	__le32 index;
+	__le32 start;
+	__le32 size;
 	char name[0];
 };
 
@@ -228,23 +228,23 @@ union squashfs_inode {
 };
 
 struct squashfs_directory_entry {
-	u16 offset;
-	s16 inode_offset;
-	u16 type;
-	u16 name_size;
+	__le16 offset;
+	__le16 inode_offset;
+	__le16 type;
+	__le16 name_size;
 	char name[0];
 };
 
 struct squashfs_directory_header {
-	u32 count;
-	u32 start;
-	u32 inode_number;
+	__le32 count;
+	__le32 start;
+	__le32 inode_number;
 };
 
 struct squashfs_fragment_block_entry {
-	u64 start;
-	u32 size;
-	u32 _unused;
+	__le64 start;
+	__le32 size;
+	__le32 _unused;
 };
 
 struct squashfs_dir_stream {
@@ -284,7 +284,7 @@ struct squashfs_file_info {
 	/* File size in bytes (uncompressed) */
 	size_t size;
 	/* Reference to list of data blocks's sizes */
-	u32 *blk_sizes;
+	__le32 *blk_sizes;
 	/* Offset into the fragment block */
 	u32 offset;
 	/* Offset in which the data blocks begin */
fs/squashfs/sqfs_inode.c +1 -1
index 20085f8912b..4cb9205efb5 100644
@@ -227,7 +227,7 @@ int sqfs_read_metablock(unsigned char *file_mapping, int offset,
 		return -EFAULT;
 	data = file_mapping + offset;
 
-	header = get_unaligned((u16 *)data);
+	header = get_unaligned_le16(data);
 	if (!header)
 		return -EINVAL;
 
Signature · 1 line
2.54.0 (Apple Git-157)