blob: 6be5afe7287d63d80dc1cff0d668065e873525dc [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Lougher8256c8f62009-01-05 08:46:26 +00002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher8256c8f62009-01-05 08:46:26 +00007 *
Phillip Lougher8256c8f62009-01-05 08:46:26 +00008 * 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>
Phillip Lougher8256c8f62009-01-05 08:46:26 +000024
25#include "squashfs_fs.h"
26#include "squashfs_fs_sb.h"
Phillip Lougher8256c8f62009-01-05 08:46:26 +000027#include "squashfs.h"
28
29/*
30 * Map uid/gid index into real 32-bit uid/gid using the id look up table
31 */
32int 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,
Phillip Lougher37986f62011-05-24 04:05:22 +010056 u64 id_table_start, u64 next_table, unsigned short no_ids)
Phillip Lougher8256c8f62009-01-05 08:46:26 +000057{
58 unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
Phillip Lougher37986f62011-05-24 04:05:22 +010059 __le64 *table;
Phillip Lougher8256c8f62009-01-05 08:46:26 +000060
61 TRACE("In read_id_index_table, length %d\n", length);
62
Phillip Lougher37986f62011-05-24 04:05:22 +010063 /* 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 */
Phillip Lougherd5b72ce2011-05-29 00:38:46 +010083 if (!IS_ERR(table) && le64_to_cpu(table[0]) >= id_table_start) {
Phillip Lougher37986f62011-05-24 04:05:22 +010084 kfree(table);
85 return ERR_PTR(-EINVAL);
86 }
87
88 return table;
Phillip Lougher8256c8f62009-01-05 08:46:26 +000089}