xattr_id.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2010
  5. * Phillip Lougher <phillip@squashfs.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * xattr_id.c
  22. */
  23. /*
  24. * This file implements code to map the 32-bit xattr id stored in the inode
  25. * into the on disk location of the xattr data.
  26. */
  27. #include <linux/fs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/slab.h>
  30. #include "squashfs_fs.h"
  31. #include "squashfs_fs_sb.h"
  32. #include "squashfs.h"
  33. #include "xattr.h"
  34. /*
  35. * Map xattr id using the xattr id look up table
  36. */
  37. int squashfs_xattr_lookup(struct super_block *sb, unsigned int index,
  38. int *count, unsigned int *size, unsigned long long *xattr)
  39. {
  40. struct squashfs_sb_info *msblk = sb->s_fs_info;
  41. int block = SQUASHFS_XATTR_BLOCK(index);
  42. int offset = SQUASHFS_XATTR_BLOCK_OFFSET(index);
  43. u64 start_block;
  44. struct squashfs_xattr_id id;
  45. int err;
  46. if (index >= msblk->xattr_ids)
  47. return -EINVAL;
  48. start_block = le64_to_cpu(msblk->xattr_id_table[block]);
  49. err = squashfs_read_metadata(sb, &id, &start_block, &offset,
  50. sizeof(id));
  51. if (err < 0)
  52. return err;
  53. *xattr = le64_to_cpu(id.xattr);
  54. *size = le32_to_cpu(id.size);
  55. *count = le32_to_cpu(id.count);
  56. return 0;
  57. }
  58. /*
  59. * Read uncompressed xattr id lookup table indexes from disk into memory
  60. */
  61. __le64 *squashfs_read_xattr_id_table(struct super_block *sb, u64 table_start,
  62. u64 *xattr_table_start, int *xattr_ids)
  63. {
  64. struct squashfs_sb_info *msblk = sb->s_fs_info;
  65. unsigned int len, indexes;
  66. struct squashfs_xattr_id_table *id_table;
  67. __le64 *table;
  68. u64 start, end;
  69. int n;
  70. id_table = squashfs_read_table(sb, table_start, sizeof(*id_table));
  71. if (IS_ERR(id_table))
  72. return (__le64 *) id_table;
  73. *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
  74. *xattr_ids = le32_to_cpu(id_table->xattr_ids);
  75. kfree(id_table);
  76. /* Sanity check values */
  77. /* there is always at least one xattr id */
  78. if (*xattr_ids == 0)
  79. return ERR_PTR(-EINVAL);
  80. len = SQUASHFS_XATTR_BLOCK_BYTES(*xattr_ids);
  81. indexes = SQUASHFS_XATTR_BLOCKS(*xattr_ids);
  82. /*
  83. * The computed size of the index table (len bytes) should exactly
  84. * match the table start and end points
  85. */
  86. start = table_start + sizeof(*id_table);
  87. end = msblk->bytes_used;
  88. if (len != (end - start))
  89. return ERR_PTR(-EINVAL);
  90. table = squashfs_read_table(sb, start, len);
  91. if (IS_ERR(table))
  92. return table;
  93. /* table[0], table[1], ... table[indexes - 1] store the locations
  94. * of the compressed xattr id blocks. Each entry should be less than
  95. * the next (i.e. table[0] < table[1]), and the difference between them
  96. * should be SQUASHFS_METADATA_SIZE or less. table[indexes - 1]
  97. * should be less than table_start, and again the difference
  98. * shouls be SQUASHFS_METADATA_SIZE or less.
  99. *
  100. * Finally xattr_table_start should be less than table[0].
  101. */
  102. for (n = 0; n < (indexes - 1); n++) {
  103. start = le64_to_cpu(table[n]);
  104. end = le64_to_cpu(table[n + 1]);
  105. if (start >= end || (end - start) >
  106. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  107. kfree(table);
  108. return ERR_PTR(-EINVAL);
  109. }
  110. }
  111. start = le64_to_cpu(table[indexes - 1]);
  112. if (start >= table_start || (table_start - start) >
  113. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  114. kfree(table);
  115. return ERR_PTR(-EINVAL);
  116. }
  117. if (*xattr_table_start >= le64_to_cpu(table[0])) {
  118. kfree(table);
  119. return ERR_PTR(-EINVAL);
  120. }
  121. return table;
  122. }