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) 2010
0006  * Phillip Lougher <phillip@squashfs.org.uk>
0007  *
0008  * xattr_id.c
0009  */
0010 
0011 /*
0012  * This file implements code to map the 32-bit xattr id stored in the inode
0013  * into the on disk location of the xattr data.
0014  */
0015 
0016 #include <linux/fs.h>
0017 #include <linux/vfs.h>
0018 #include <linux/slab.h>
0019 
0020 #include "squashfs_fs.h"
0021 #include "squashfs_fs_sb.h"
0022 #include "squashfs.h"
0023 #include "xattr.h"
0024 
0025 /*
0026  * Map xattr id using the xattr id look up table
0027  */
0028 int squashfs_xattr_lookup(struct super_block *sb, unsigned int index,
0029         int *count, unsigned int *size, unsigned long long *xattr)
0030 {
0031     struct squashfs_sb_info *msblk = sb->s_fs_info;
0032     int block = SQUASHFS_XATTR_BLOCK(index);
0033     int offset = SQUASHFS_XATTR_BLOCK_OFFSET(index);
0034     u64 start_block;
0035     struct squashfs_xattr_id id;
0036     int err;
0037 
0038     if (index >= msblk->xattr_ids)
0039         return -EINVAL;
0040 
0041     start_block = le64_to_cpu(msblk->xattr_id_table[block]);
0042 
0043     err = squashfs_read_metadata(sb, &id, &start_block, &offset,
0044                             sizeof(id));
0045     if (err < 0)
0046         return err;
0047 
0048     *xattr = le64_to_cpu(id.xattr);
0049     *size = le32_to_cpu(id.size);
0050     *count = le32_to_cpu(id.count);
0051     return 0;
0052 }
0053 
0054 
0055 /*
0056  * Read uncompressed xattr id lookup table indexes from disk into memory
0057  */
0058 __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,
0059         u64 *xattr_table_start, int *xattr_ids)
0060 {
0061     struct squashfs_sb_info *msblk = sb->s_fs_info;
0062     unsigned int len, indexes;
0063     struct squashfs_xattr_id_table *id_table;
0064     __le64 *table;
0065     u64 start, end;
0066     int n;
0067 
0068     id_table = squashfs_read_table(sb, table_start, sizeof(*id_table));
0069     if (IS_ERR(id_table))
0070         return (__le64 *) id_table;
0071 
0072     *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
0073     *xattr_ids = le32_to_cpu(id_table->xattr_ids);
0074     kfree(id_table);
0075 
0076     /* Sanity check values */
0077 
0078     /* there is always at least one xattr id */
0079     if (*xattr_ids == 0)
0080         return ERR_PTR(-EINVAL);
0081 
0082     len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
0083     indexes = SQUASHFS_XATTR_BLOCKS(*xattr_ids);
0084 
0085     /*
0086      * The computed size of the index table (len bytes) should exactly
0087      * match the table start and end points
0088      */
0089     start = table_start + sizeof(*id_table);
0090     end = msblk->bytes_used;
0091 
0092     if (len != (end - start))
0093         return ERR_PTR(-EINVAL);
0094 
0095     table = squashfs_read_table(sb, start, len);
0096     if (IS_ERR(table))
0097         return table;
0098 
0099     /* table[0], table[1], ... table[indexes - 1] store the locations
0100      * of the compressed xattr id blocks.  Each entry should be less than
0101      * the next (i.e. table[0] < table[1]), and the difference between them
0102      * should be SQUASHFS_METADATA_SIZE or less.  table[indexes - 1]
0103      * should be less than table_start, and again the difference
0104      * shouls be SQUASHFS_METADATA_SIZE or less.
0105      *
0106      * Finally xattr_table_start should be less than table[0].
0107      */
0108     for (n = 0; n < (indexes - 1); n++) {
0109         start = le64_to_cpu(table[n]);
0110         end = le64_to_cpu(table[n + 1]);
0111 
0112         if (start >= end || (end - start) >
0113                 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
0114             kfree(table);
0115             return ERR_PTR(-EINVAL);
0116         }
0117     }
0118 
0119     start = le64_to_cpu(table[indexes - 1]);
0120     if (start >= table_start || (table_start - start) >
0121                 (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
0122         kfree(table);
0123         return ERR_PTR(-EINVAL);
0124     }
0125 
0126     if (*xattr_table_start >= le64_to_cpu(table[0])) {
0127         kfree(table);
0128         return ERR_PTR(-EINVAL);
0129     }
0130 
0131     return table;
0132 }