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