18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010
68c2ecf20Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * xattr_id.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file implements code to map the 32-bit xattr id stored in the inode
138c2ecf20Sopenharmony_ci * into the on disk location of the xattr data.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/fs.h>
178c2ecf20Sopenharmony_ci#include <linux/vfs.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "squashfs_fs.h"
218c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h"
228c2ecf20Sopenharmony_ci#include "squashfs.h"
238c2ecf20Sopenharmony_ci#include "xattr.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * Map xattr id using the xattr id look up table
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ciint squashfs_xattr_lookup(struct super_block *sb, unsigned int index,
298c2ecf20Sopenharmony_ci		int *count, unsigned int *size, unsigned long long *xattr)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
328c2ecf20Sopenharmony_ci	int block = SQUASHFS_XATTR_BLOCK(index);
338c2ecf20Sopenharmony_ci	int offset = SQUASHFS_XATTR_BLOCK_OFFSET(index);
348c2ecf20Sopenharmony_ci	u64 start_block;
358c2ecf20Sopenharmony_ci	struct squashfs_xattr_id id;
368c2ecf20Sopenharmony_ci	int err;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	if (index >= msblk->xattr_ids)
398c2ecf20Sopenharmony_ci		return -EINVAL;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	start_block = le64_to_cpu(msblk->xattr_id_table[block]);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	err = squashfs_read_metadata(sb, &id, &start_block, &offset,
448c2ecf20Sopenharmony_ci							sizeof(id));
458c2ecf20Sopenharmony_ci	if (err < 0)
468c2ecf20Sopenharmony_ci		return err;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	*xattr = le64_to_cpu(id.xattr);
498c2ecf20Sopenharmony_ci	*size = le32_to_cpu(id.size);
508c2ecf20Sopenharmony_ci	*count = le32_to_cpu(id.count);
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * Read uncompressed xattr id lookup table indexes from disk into memory
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci__le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,
598c2ecf20Sopenharmony_ci		u64 *xattr_table_start, unsigned int *xattr_ids)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
628c2ecf20Sopenharmony_ci	unsigned int len, indexes;
638c2ecf20Sopenharmony_ci	struct squashfs_xattr_id_table *id_table;
648c2ecf20Sopenharmony_ci	__le64 *table;
658c2ecf20Sopenharmony_ci	u64 start, end;
668c2ecf20Sopenharmony_ci	int n;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	id_table = squashfs_read_table(sb, table_start, sizeof(*id_table));
698c2ecf20Sopenharmony_ci	if (IS_ERR(id_table))
708c2ecf20Sopenharmony_ci		return (__le64 *) id_table;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	*xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
738c2ecf20Sopenharmony_ci	*xattr_ids = le32_to_cpu(id_table->xattr_ids);
748c2ecf20Sopenharmony_ci	kfree(id_table);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* Sanity check values */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* there is always at least one xattr id */
798c2ecf20Sopenharmony_ci	if (*xattr_ids == 0)
808c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
838c2ecf20Sopenharmony_ci	indexes = SQUASHFS_XATTR_BLOCKS(*xattr_ids);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/*
868c2ecf20Sopenharmony_ci	 * The computed size of the index table (len bytes) should exactly
878c2ecf20Sopenharmony_ci	 * match the table start and end points
888c2ecf20Sopenharmony_ci	 */
898c2ecf20Sopenharmony_ci	start = table_start + sizeof(*id_table);
908c2ecf20Sopenharmony_ci	end = msblk->bytes_used;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (len != (end - start))
938c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	table = squashfs_read_table(sb, start, len);
968c2ecf20Sopenharmony_ci	if (IS_ERR(table))
978c2ecf20Sopenharmony_ci		return table;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* table[0], table[1], ... table[indexes - 1] store the locations
1008c2ecf20Sopenharmony_ci	 * of the compressed xattr id blocks.  Each entry should be less than
1018c2ecf20Sopenharmony_ci	 * the next (i.e. table[0] < table[1]), and the difference between them
1028c2ecf20Sopenharmony_ci	 * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
1038c2ecf20Sopenharmony_ci	 * should be less than table_start, and again the difference
1048c2ecf20Sopenharmony_ci	 * shouls be SQUASHFS_METADATA_SIZE or less.
1058c2ecf20Sopenharmony_ci	 *
1068c2ecf20Sopenharmony_ci	 * Finally xattr_table_start should be less than table[0].
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	for (n = 0; n < (indexes - 1); n++) {
1098c2ecf20Sopenharmony_ci		start = le64_to_cpu(table[n]);
1108c2ecf20Sopenharmony_ci		end = le64_to_cpu(table[n + 1]);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		if (start >= end || (end - start) >
1138c2ecf20Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
1148c2ecf20Sopenharmony_ci			kfree(table);
1158c2ecf20Sopenharmony_ci			return ERR_PTR(-EINVAL);
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	start = le64_to_cpu(table[indexes - 1]);
1208c2ecf20Sopenharmony_ci	if (start >= table_start || (table_start - start) >
1218c2ecf20Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
1228c2ecf20Sopenharmony_ci		kfree(table);
1238c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (*xattr_table_start >= le64_to_cpu(table[0])) {
1278c2ecf20Sopenharmony_ci		kfree(table);
1288c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return table;
1328c2ecf20Sopenharmony_ci}
133