^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) 2010
^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) * xattr_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 map the 32-bit xattr id stored in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * into the on disk location of the xattr data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "squashfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "squashfs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "squashfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Map xattr id using the xattr id look up table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int squashfs_xattr_lookup(struct super_block *sb, unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int *count, unsigned int *size, unsigned long long *xattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct squashfs_sb_info *msblk = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int block = SQUASHFS_XATTR_BLOCK(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int offset = SQUASHFS_XATTR_BLOCK_OFFSET(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u64 start_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct squashfs_xattr_id id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (index >= msblk->xattr_ids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) start_block = le64_to_cpu(msblk->xattr_id_table[block]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = squashfs_read_metadata(sb, &id, &start_block, &offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) sizeof(id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *xattr = le64_to_cpu(id.xattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *size = le32_to_cpu(id.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *count = le32_to_cpu(id.count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^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) * Read uncompressed xattr id lookup table indexes from disk into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u64 *xattr_table_start, int *xattr_ids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct squashfs_sb_info *msblk = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int len, indexes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct squashfs_xattr_id_table *id_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) __le64 *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) id_table = squashfs_read_table(sb, table_start, sizeof(*id_table));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (IS_ERR(id_table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return (__le64 *) id_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *xattr_ids = le32_to_cpu(id_table->xattr_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) kfree(id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Sanity check values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* there is always at least one xattr id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (*xattr_ids == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) indexes = SQUASHFS_XATTR_BLOCKS(*xattr_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * The computed size of the index table (len bytes) should exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * match the table start and end points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) start = table_start + sizeof(*id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) end = msblk->bytes_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (len != (end - start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) table = squashfs_read_table(sb, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (IS_ERR(table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* table[0], table[1], ... table[indexes - 1] store the locations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * of the compressed xattr id blocks. Each entry should be less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * the next (i.e. table[0] < table[1]), and the difference between them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * should be SQUASHFS_METADATA_SIZE or less. table[indexes - 1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * should be less than table_start, and again the difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * shouls be SQUASHFS_METADATA_SIZE or less.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Finally xattr_table_start should be less than table[0].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (n = 0; n < (indexes - 1); n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) start = le64_to_cpu(table[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) end = le64_to_cpu(table[n + 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (start >= end || (end - start) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) start = le64_to_cpu(table[indexes - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (start >= table_start || (table_start - start) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (*xattr_table_start >= le64_to_cpu(table[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }