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