xref: /OK3568_Linux_fs/kernel/fs/squashfs/id.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Squashfs - a compressed read only filesystem for Linux
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6*4882a593Smuzhiyun  * Phillip Lougher <phillip@squashfs.org.uk>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * id.c
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * This file implements code to handle uids and gids.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * For space efficiency regular files store uid and gid indexes, which are
15*4882a593Smuzhiyun  * converted to 32-bit uids/gids using an id look up table.  This table is
16*4882a593Smuzhiyun  * stored compressed into metadata blocks.  A second index table is used to
17*4882a593Smuzhiyun  * locate these.  This second index table for speed of access (and because it
18*4882a593Smuzhiyun  * is small) is read at mount time and cached in memory.
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/vfs.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include "squashfs_fs.h"
26*4882a593Smuzhiyun #include "squashfs_fs_sb.h"
27*4882a593Smuzhiyun #include "squashfs.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * Map uid/gid index into real 32-bit uid/gid using the id look up table
31*4882a593Smuzhiyun  */
squashfs_get_id(struct super_block * sb,unsigned int index,unsigned int * id)32*4882a593Smuzhiyun int squashfs_get_id(struct super_block *sb, unsigned int index,
33*4882a593Smuzhiyun 					unsigned int *id)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	struct squashfs_sb_info *msblk = sb->s_fs_info;
36*4882a593Smuzhiyun 	int block = SQUASHFS_ID_BLOCK(index);
37*4882a593Smuzhiyun 	int offset = SQUASHFS_ID_BLOCK_OFFSET(index);
38*4882a593Smuzhiyun 	u64 start_block;
39*4882a593Smuzhiyun 	__le32 disk_id;
40*4882a593Smuzhiyun 	int err;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	if (index >= msblk->ids)
43*4882a593Smuzhiyun 		return -EINVAL;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	start_block = le64_to_cpu(msblk->id_table[block]);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	err = squashfs_read_metadata(sb, &disk_id, &start_block, &offset,
48*4882a593Smuzhiyun 							sizeof(disk_id));
49*4882a593Smuzhiyun 	if (err < 0)
50*4882a593Smuzhiyun 		return err;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	*id = le32_to_cpu(disk_id);
53*4882a593Smuzhiyun 	return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * Read uncompressed id lookup table indexes from disk into memory
59*4882a593Smuzhiyun  */
squashfs_read_id_index_table(struct super_block * sb,u64 id_table_start,u64 next_table,unsigned short no_ids)60*4882a593Smuzhiyun __le64 *squashfs_read_id_index_table(struct super_block *sb,
61*4882a593Smuzhiyun 		u64 id_table_start, u64 next_table, unsigned short no_ids)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
64*4882a593Smuzhiyun 	unsigned int indexes = SQUASHFS_ID_BLOCKS(no_ids);
65*4882a593Smuzhiyun 	int n;
66*4882a593Smuzhiyun 	__le64 *table;
67*4882a593Smuzhiyun 	u64 start, end;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	TRACE("In read_id_index_table, length %d\n", length);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* Sanity check values */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* there should always be at least one id */
74*4882a593Smuzhiyun 	if (no_ids == 0)
75*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * The computed size of the index table (length bytes) should exactly
79*4882a593Smuzhiyun 	 * match the table start and end points
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 	if (length != (next_table - id_table_start))
82*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	table = squashfs_read_table(sb, id_table_start, length);
85*4882a593Smuzhiyun 	if (IS_ERR(table))
86*4882a593Smuzhiyun 		return table;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/*
89*4882a593Smuzhiyun 	 * table[0], table[1], ... table[indexes - 1] store the locations
90*4882a593Smuzhiyun 	 * of the compressed id blocks.   Each entry should be less than
91*4882a593Smuzhiyun 	 * the next (i.e. table[0] < table[1]), and the difference between them
92*4882a593Smuzhiyun 	 * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
93*4882a593Smuzhiyun 	 * should be less than id_table_start, and again the difference
94*4882a593Smuzhiyun 	 * should be SQUASHFS_METADATA_SIZE or less
95*4882a593Smuzhiyun 	 */
96*4882a593Smuzhiyun 	for (n = 0; n < (indexes - 1); n++) {
97*4882a593Smuzhiyun 		start = le64_to_cpu(table[n]);
98*4882a593Smuzhiyun 		end = le64_to_cpu(table[n + 1]);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		if (start >= end || (end - start) >
101*4882a593Smuzhiyun 				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
102*4882a593Smuzhiyun 			kfree(table);
103*4882a593Smuzhiyun 			return ERR_PTR(-EINVAL);
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	start = le64_to_cpu(table[indexes - 1]);
108*4882a593Smuzhiyun 	if (start >= id_table_start || (id_table_start - start) >
109*4882a593Smuzhiyun 				(SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
110*4882a593Smuzhiyun 		kfree(table);
111*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	return table;
115*4882a593Smuzhiyun }
116