id.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  6. * Phillip Lougher <phillip@squashfs.org.uk>
  7. *
  8. * id.c
  9. */
  10. /*
  11. * This file implements code to handle uids and gids.
  12. *
  13. * For space efficiency regular files store uid and gid indexes, which are
  14. * converted to 32-bit uids/gids using an id look up table. This table is
  15. * stored compressed into metadata blocks. A second index table is used to
  16. * locate these. This second index table for speed of access (and because it
  17. * is small) is read at mount time and cached in memory.
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/vfs.h>
  21. #include <linux/slab.h>
  22. #include "squashfs_fs.h"
  23. #include "squashfs_fs_sb.h"
  24. #include "squashfs.h"
  25. /*
  26. * Map uid/gid index into real 32-bit uid/gid using the id look up table
  27. */
  28. int squashfs_get_id(struct super_block *sb, unsigned int index,
  29. unsigned int *id)
  30. {
  31. struct squashfs_sb_info *msblk = sb->s_fs_info;
  32. int block = SQUASHFS_ID_BLOCK(index);
  33. int offset = SQUASHFS_ID_BLOCK_OFFSET(index);
  34. u64 start_block;
  35. __le32 disk_id;
  36. int err;
  37. if (index >= msblk->ids)
  38. return -EINVAL;
  39. start_block = le64_to_cpu(msblk->id_table[block]);
  40. err = squashfs_read_metadata(sb, &disk_id, &start_block, &offset,
  41. sizeof(disk_id));
  42. if (err < 0)
  43. return err;
  44. *id = le32_to_cpu(disk_id);
  45. return 0;
  46. }
  47. /*
  48. * Read uncompressed id lookup table indexes from disk into memory
  49. */
  50. __le64 *squashfs_read_id_index_table(struct super_block *sb,
  51. u64 id_table_start, u64 next_table, unsigned short no_ids)
  52. {
  53. unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
  54. unsigned int indexes = SQUASHFS_ID_BLOCKS(no_ids);
  55. int n;
  56. __le64 *table;
  57. u64 start, end;
  58. TRACE("In read_id_index_table, length %d\n", length);
  59. /* Sanity check values */
  60. /* there should always be at least one id */
  61. if (no_ids == 0)
  62. return ERR_PTR(-EINVAL);
  63. /*
  64. * The computed size of the index table (length bytes) should exactly
  65. * match the table start and end points
  66. */
  67. if (length != (next_table - id_table_start))
  68. return ERR_PTR(-EINVAL);
  69. table = squashfs_read_table(sb, id_table_start, length);
  70. if (IS_ERR(table))
  71. return table;
  72. /*
  73. * table[0], table[1], ... table[indexes - 1] store the locations
  74. * of the compressed id blocks. Each entry should be less than
  75. * the next (i.e. table[0] < table[1]), and the difference between them
  76. * should be SQUASHFS_METADATA_SIZE or less. table[indexes - 1]
  77. * should be less than id_table_start, and again the difference
  78. * should be SQUASHFS_METADATA_SIZE or less
  79. */
  80. for (n = 0; n < (indexes - 1); n++) {
  81. start = le64_to_cpu(table[n]);
  82. end = le64_to_cpu(table[n + 1]);
  83. if (start >= end || (end - start) >
  84. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  85. kfree(table);
  86. return ERR_PTR(-EINVAL);
  87. }
  88. }
  89. start = le64_to_cpu(table[indexes - 1]);
  90. if (start >= id_table_start || (id_table_start - start) >
  91. (SQUASHFS_METADATA_SIZE + SQUASHFS_BLOCK_OFFSET)) {
  92. kfree(table);
  93. return ERR_PTR(-EINVAL);
  94. }
  95. return table;
  96. }