162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
662306a36Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * id.c
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * This file implements code to handle uids and gids.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * For space efficiency regular files store uid and gid indexes, which are
1562306a36Sopenharmony_ci * converted to 32-bit uids/gids using an id look up table.  This table is
1662306a36Sopenharmony_ci * stored compressed into metadata blocks.  A second index table is used to
1762306a36Sopenharmony_ci * locate these.  This second index table for speed of access (and because it
1862306a36Sopenharmony_ci * is small) is read at mount time and cached in memory.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#include <linux/fs.h>
2262306a36Sopenharmony_ci#include <linux/vfs.h>
2362306a36Sopenharmony_ci#include <linux/slab.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "squashfs_fs.h"
2662306a36Sopenharmony_ci#include "squashfs_fs_sb.h"
2762306a36Sopenharmony_ci#include "squashfs.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Map uid/gid index into real 32-bit uid/gid using the id look up table
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ciint squashfs_get_id(struct super_block *sb, unsigned int index,
3362306a36Sopenharmony_ci					unsigned int *id)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	struct squashfs_sb_info *msblk = sb->s_fs_info;
3662306a36Sopenharmony_ci	int block = SQUASHFS_ID_BLOCK(index);
3762306a36Sopenharmony_ci	int offset = SQUASHFS_ID_BLOCK_OFFSET(index);
3862306a36Sopenharmony_ci	u64 start_block;
3962306a36Sopenharmony_ci	__le32 disk_id;
4062306a36Sopenharmony_ci	int err;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	if (index >= msblk->ids)
4362306a36Sopenharmony_ci		return -EINVAL;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	start_block = le64_to_cpu(msblk->id_table[block]);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	err = squashfs_read_metadata(sb, &disk_id, &start_block, &offset,
4862306a36Sopenharmony_ci							sizeof(disk_id));
4962306a36Sopenharmony_ci	if (err < 0)
5062306a36Sopenharmony_ci		return err;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	*id = le32_to_cpu(disk_id);
5362306a36Sopenharmony_ci	return 0;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/*
5862306a36Sopenharmony_ci * Read uncompressed id lookup table indexes from disk into memory
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci__le64 *squashfs_read_id_index_table(struct super_block *sb,
6162306a36Sopenharmony_ci		u64 id_table_start, u64 next_table, unsigned short no_ids)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
6462306a36Sopenharmony_ci	unsigned int indexes = SQUASHFS_ID_BLOCKS(no_ids);
6562306a36Sopenharmony_ci	int n;
6662306a36Sopenharmony_ci	__le64 *table;
6762306a36Sopenharmony_ci	u64 start, end;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	TRACE("In read_id_index_table, length %d\n", length);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	/* Sanity check values */
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/* there should always be at least one id */
7462306a36Sopenharmony_ci	if (no_ids == 0)
7562306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/*
7862306a36Sopenharmony_ci	 * The computed size of the index table (length bytes) should exactly
7962306a36Sopenharmony_ci	 * match the table start and end points
8062306a36Sopenharmony_ci	 */
8162306a36Sopenharmony_ci	if (length != (next_table - id_table_start))
8262306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	table = squashfs_read_table(sb, id_table_start, length);
8562306a36Sopenharmony_ci	if (IS_ERR(table))
8662306a36Sopenharmony_ci		return table;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/*
8962306a36Sopenharmony_ci	 * table[0], table[1], ... table[indexes - 1] store the locations
9062306a36Sopenharmony_ci	 * of the compressed id blocks.   Each entry should be less than
9162306a36Sopenharmony_ci	 * the next (i.e. table[0] < table[1]), and the difference between them
9262306a36Sopenharmony_ci	 * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
9362306a36Sopenharmony_ci	 * should be less than id_table_start, and again the difference
9462306a36Sopenharmony_ci	 * should be SQUASHFS_METADATA_SIZE or less
9562306a36Sopenharmony_ci	 */
9662306a36Sopenharmony_ci	for (n = 0; n < (indexes - 1); n++) {
9762306a36Sopenharmony_ci		start = le64_to_cpu(table[n]);
9862306a36Sopenharmony_ci		end = le64_to_cpu(table[n + 1]);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci		if (start >= end || (end - start) >
10162306a36Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
10262306a36Sopenharmony_ci			kfree(table);
10362306a36Sopenharmony_ci			return ERR_PTR(-EINVAL);
10462306a36Sopenharmony_ci		}
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	start = le64_to_cpu(table[indexes - 1]);
10862306a36Sopenharmony_ci	if (start >= id_table_start || (id_table_start - start) >
10962306a36Sopenharmony_ci				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
11062306a36Sopenharmony_ci		kfree(table);
11162306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return table;
11562306a36Sopenharmony_ci}
116