Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Squashfs - a compressed read only filesystem for Linux
0004  *
0005  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
0006  * Phillip Lougher <phillip@squashfs.org.uk>
0007  *
0008  * id.c
0009  */
0010 
0011 /*
0012  * This file implements code to handle uids and gids.
0013  *
0014  * For space efficiency regular files store uid and gid indexes, which are
0015  * converted to 32-bit uids/gids using an id look up table.  This table is
0016  * stored compressed into metadata blocks.  A second index table is used to
0017  * locate these.  This second index table for speed of access (and because it
0018  * is small) is read at mount time and cached in memory.
0019  */
0020 
0021 #include <linux/fs.h>
0022 #include <linux/vfs.h>
0023 #include <linux/slab.h>
0024 
0025 #include "squashfs_fs.h"
0026 #include "squashfs_fs_sb.h"
0027 #include "squashfs.h"
0028 
0029 /*
0030  * Map uid/gid index into real 32-bit uid/gid using the id look up table
0031  */
0032 int squashfs_get_id(struct super_block *sb, unsigned int index,
0033                     unsigned int *id)
0034 {
0035     struct squashfs_sb_info *msblk = sb->s_fs_info;
0036     int block = SQUASHFS_ID_BLOCK(index);
0037     int offset = SQUASHFS_ID_BLOCK_OFFSET(index);
0038     u64 start_block;
0039     __le32 disk_id;
0040     int err;
0041 
0042     if (index >= msblk->ids)
0043         return -EINVAL;
0044 
0045     start_block = le64_to_cpu(msblk->id_table[block]);
0046 
0047     err = squashfs_read_metadata(sb, &disk_id, &start_block, &offset,
0048                             sizeof(disk_id));
0049     if (err < 0)
0050         return err;
0051 
0052     *id = le32_to_cpu(disk_id);
0053     return 0;
0054 }
0055 
0056 
0057 /*
0058  * Read uncompressed id lookup table indexes from disk into memory
0059  */
0060 __le64 *squashfs_read_id_index_table(struct super_block *sb,
0061         u64 id_table_start, u64 next_table, unsigned short no_ids)
0062 {
0063     unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
0064     unsigned int indexes = SQUASHFS_ID_BLOCKS(no_ids);
0065     int n;
0066     __le64 *table;
0067     u64 start, end;
0068 
0069     TRACE("In read_id_index_table, length %d\n", length);
0070 
0071     /* Sanity check values */
0072 
0073     /* there should always be at least one id */
0074     if (no_ids == 0)
0075         return ERR_PTR(-EINVAL);
0076 
0077     /*
0078      * The computed size of the index table (length bytes) should exactly
0079      * match the table start and end points
0080      */
0081     if (length != (next_table - id_table_start))
0082         return ERR_PTR(-EINVAL);
0083 
0084     table = squashfs_read_table(sb, id_table_start, length);
0085     if (IS_ERR(table))
0086         return table;
0087 
0088     /*
0089      * table[0], table[1], ... table[indexes - 1] store the locations
0090      * of the compressed id blocks.   Each entry should be less than
0091      * the next (i.e. table[0] < table[1]), and the difference between them
0092      * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
0093      * should be less than id_table_start, and again the difference
0094      * should be SQUASHFS_METADATA_SIZE or less
0095      */
0096     for (n = 0; n < (indexes - 1); n++) {
0097         start = le64_to_cpu(table[n]);
0098         end = le64_to_cpu(table[n + 1]);
0099 
0100         if (start >= end || (end - start) >
0101                 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
0102             kfree(table);
0103             return ERR_PTR(-EINVAL);
0104         }
0105     }
0106 
0107     start = le64_to_cpu(table[indexes - 1]);
0108     if (start >= id_table_start || (id_table_start - start) >
0109                 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
0110         kfree(table);
0111         return ERR_PTR(-EINVAL);
0112     }
0113 
0114     return table;
0115 }