xattr_id.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2010
  6. * Phillip Lougher <[email protected]>
  7. *
  8. * xattr_id.c
  9. */
  10. /*
  11. * This file implements code to map the 32-bit xattr id stored in the inode
  12. * into the on disk location of the xattr data.
  13. */
  14. #include <linux/fs.h>
  15. #include <linux/vfs.h>
  16. #include <linux/slab.h>
  17. #include "squashfs_fs.h"
  18. #include "squashfs_fs_sb.h"
  19. #include "squashfs.h"
  20. #include "xattr.h"
  21. /*
  22. * Map xattr id using the xattr id look up table
  23. */
  24. int squashfs_xattr_lookup(struct super_block *sb, unsigned int index,
  25. int *count, unsigned int *size, unsigned long long *xattr)
  26. {
  27. struct squashfs_sb_info *msblk = sb->s_fs_info;
  28. int block = SQUASHFS_XATTR_BLOCK(index);
  29. int offset = SQUASHFS_XATTR_BLOCK_OFFSET(index);
  30. u64 start_block;
  31. struct squashfs_xattr_id id;
  32. int err;
  33. if (index >= msblk->xattr_ids)
  34. return -EINVAL;
  35. start_block = le64_to_cpu(msblk->xattr_id_table[block]);
  36. err = squashfs_read_metadata(sb, &id, &start_block, &offset,
  37. sizeof(id));
  38. if (err < 0)
  39. return err;
  40. *xattr = le64_to_cpu(id.xattr);
  41. *size = le32_to_cpu(id.size);
  42. *count = le32_to_cpu(id.count);
  43. return 0;
  44. }
  45. /*
  46. * Read uncompressed xattr id lookup table indexes from disk into memory
  47. */
  48. __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,
  49. u64 *xattr_table_start, unsigned int *xattr_ids)
  50. {
  51. struct squashfs_sb_info *msblk = sb->s_fs_info;
  52. unsigned int len, indexes;
  53. struct squashfs_xattr_id_table *id_table;
  54. __le64 *table;
  55. u64 start, end;
  56. int n;
  57. id_table = squashfs_read_table(sb, table_start, sizeof(*id_table));
  58. if (IS_ERR(id_table))
  59. return (__le64 *) id_table;
  60. *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
  61. *xattr_ids = le32_to_cpu(id_table->xattr_ids);
  62. kfree(id_table);
  63. /* Sanity check values */
  64. /* there is always at least one xattr id */
  65. if (*xattr_ids == 0)
  66. return ERR_PTR(-EINVAL);
  67. len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
  68. indexes = SQUASHFS_XATTR_BLOCKS(*xattr_ids);
  69. /*
  70. * The computed size of the index table (len bytes) should exactly
  71. * match the table start and end points
  72. */
  73. start = table_start + sizeof(*id_table);
  74. end = msblk->bytes_used;
  75. if (len != (end - start))
  76. return ERR_PTR(-EINVAL);
  77. table = squashfs_read_table(sb, start, len);
  78. if (IS_ERR(table))
  79. return table;
  80. /* table[0], table[1], ... table[indexes - 1] store the locations
  81. * of the compressed xattr id blocks. Each entry should be less than
  82. * the next (i.e. table[0] < table[1]), and the difference between them
  83. * should be SQUASHFS_METADATA_SIZE or less. table[indexes - 1]
  84. * should be less than table_start, and again the difference
  85. * shouls be SQUASHFS_METADATA_SIZE or less.
  86. *
  87. * Finally xattr_table_start should be less than table[0].
  88. */
  89. for (n = 0; n < (indexes - 1); n++) {
  90. start = le64_to_cpu(table[n]);
  91. end = le64_to_cpu(table[n + 1]);
  92. if (start >= end || (end - start) >
  93. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  94. kfree(table);
  95. return ERR_PTR(-EINVAL);
  96. }
  97. }
  98. start = le64_to_cpu(table[indexes - 1]);
  99. if (start >= table_start || (table_start - start) >
  100. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  101. kfree(table);
  102. return ERR_PTR(-EINVAL);
  103. }
  104. if (*xattr_table_start >= le64_to_cpu(table[0])) {
  105. kfree(table);
  106. return ERR_PTR(-EINVAL);
  107. }
  108. return table;
  109. }