Thomas Gleixner | 68252eb | 2019-05-20 19:08:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Squashfs - a compressed read only filesystem for Linux |
| 4 | * |
| 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 6 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 7 | * |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 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> |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 24 | |
| 25 | #include "squashfs_fs.h" |
| 26 | #include "squashfs_fs_sb.h" |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 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, |
Phillip Lougher | 37986f6 | 2011-05-24 04:05:22 +0100 | [diff] [blame] | 56 | u64 id_table_start, u64 next_table, unsigned short no_ids) |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids); |
Phillip Lougher | 37986f6 | 2011-05-24 04:05:22 +0100 | [diff] [blame] | 59 | __le64 *table; |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 60 | |
| 61 | TRACE("In read_id_index_table, length %d\n", length); |
| 62 | |
Phillip Lougher | 37986f6 | 2011-05-24 04:05:22 +0100 | [diff] [blame] | 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 | */ |
Phillip Lougher | d5b72ce | 2011-05-29 00:38:46 +0100 | [diff] [blame] | 83 | if (!IS_ERR(table) && le64_to_cpu(table[0]) >= id_table_start) { |
Phillip Lougher | 37986f6 | 2011-05-24 04:05:22 +0100 | [diff] [blame] | 84 | kfree(table); |
| 85 | return ERR_PTR(-EINVAL); |
| 86 | } |
| 87 | |
| 88 | return table; |
Phillip Lougher | 8256c8f6 | 2009-01-05 08:46:26 +0000 | [diff] [blame] | 89 | } |