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) 2002, 2003, 2004, 2005, 2006, 2007, 2008
68c2ecf20Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * id.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file implements code to handle uids and gids.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * For space efficiency regular files store uid and gid indexes, which are
158c2ecf20Sopenharmony_ci * converted to 32-bit uids/gids using an id look up table.  This table is
168c2ecf20Sopenharmony_ci * stored compressed into metadata blocks.  A second index table is used to
178c2ecf20Sopenharmony_ci * locate these.  This second index table for speed of access (and because it
188c2ecf20Sopenharmony_ci * is small) is read at mount time and cached in memory.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/fs.h>
228c2ecf20Sopenharmony_ci#include <linux/vfs.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "squashfs_fs.h"
268c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h"
278c2ecf20Sopenharmony_ci#include "squashfs.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Map uid/gid index into real 32-bit uid/gid using the id look up table
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ciint squashfs_get_id(struct super_block *sb, unsigned int index,
338c2ecf20Sopenharmony_ci					unsigned int *id)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
368c2ecf20Sopenharmony_ci	int block = SQUASHFS_ID_BLOCK(index);
378c2ecf20Sopenharmony_ci	int offset = SQUASHFS_ID_BLOCK_OFFSET(index);
388c2ecf20Sopenharmony_ci	u64 start_block;
398c2ecf20Sopenharmony_ci	__le32 disk_id;
408c2ecf20Sopenharmony_ci	int err;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (index >= msblk->ids)
438c2ecf20Sopenharmony_ci		return -EINVAL;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	start_block = le64_to_cpu(msblk->id_table[block]);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	err = squashfs_read_metadata(sb, &disk_id, &start_block, &offset,
488c2ecf20Sopenharmony_ci							sizeof(disk_id));
498c2ecf20Sopenharmony_ci	if (err < 0)
508c2ecf20Sopenharmony_ci		return err;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	*id = le32_to_cpu(disk_id);
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * Read uncompressed id lookup table indexes from disk into memory
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci__le64 *squashfs_read_id_index_table(struct super_block *sb,
618c2ecf20Sopenharmony_ci		u64 id_table_start, u64 next_table, unsigned short no_ids)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
648c2ecf20Sopenharmony_ci	unsigned int indexes = SQUASHFS_ID_BLOCKS(no_ids);
658c2ecf20Sopenharmony_ci	int n;
668c2ecf20Sopenharmony_ci	__le64 *table;
678c2ecf20Sopenharmony_ci	u64 start, end;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	TRACE("In read_id_index_table, length %d\n", length);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Sanity check values */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* there should always be at least one id */
748c2ecf20Sopenharmony_ci	if (no_ids == 0)
758c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * The computed size of the index table (length bytes) should exactly
798c2ecf20Sopenharmony_ci	 * match the table start and end points
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	if (length != (next_table - id_table_start))
828c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	table = squashfs_read_table(sb, id_table_start, length);
858c2ecf20Sopenharmony_ci	if (IS_ERR(table))
868c2ecf20Sopenharmony_ci		return table;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * table[0], table[1], ... table[indexes - 1] store the locations
908c2ecf20Sopenharmony_ci	 * of the compressed id blocks.   Each entry should be less than
918c2ecf20Sopenharmony_ci	 * the next (i.e. table[0] < table[1]), and the difference between them
928c2ecf20Sopenharmony_ci	 * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
938c2ecf20Sopenharmony_ci	 * should be less than id_table_start, and again the difference
948c2ecf20Sopenharmony_ci	 * should be SQUASHFS_METADATA_SIZE or less
958c2ecf20Sopenharmony_ci	 */
968c2ecf20Sopenharmony_ci	for (n = 0; n < (indexes - 1); n++) {
978c2ecf20Sopenharmony_ci		start = le64_to_cpu(table[n]);
988c2ecf20Sopenharmony_ci		end = le64_to_cpu(table[n + 1]);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		if (start >= end || (end - start) >
1018c2ecf20Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
1028c2ecf20Sopenharmony_ci			kfree(table);
1038c2ecf20Sopenharmony_ci			return ERR_PTR(-EINVAL);
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	start = le64_to_cpu(table[indexes - 1]);
1088c2ecf20Sopenharmony_ci	if (start >= id_table_start || (id_table_start - start) >
1098c2ecf20Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
1108c2ecf20Sopenharmony_ci		kfree(table);
1118c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return table;
1158c2ecf20Sopenharmony_ci}
116