xattr.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.c
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/string.h>
  26. #include <linux/fs.h>
  27. #include <linux/vfs.h>
  28. #include <linux/xattr.h>
  29. #include <linux/slab.h>
  30. #include "squashfs_fs.h"
  31. #include "squashfs_fs_sb.h"
  32. #include "squashfs_fs_i.h"
  33. #include "squashfs.h"
  34. static const struct xattr_handler *squashfs_xattr_handler(int);
  35. ssize_t squashfs_listxattr(struct dentry *d, char *buffer,
  36. size_t buffer_size)
  37. {
  38. struct inode *inode = d_inode(d);
  39. struct super_block *sb = inode->i_sb;
  40. struct squashfs_sb_info *msblk = sb->s_fs_info;
  41. u64 start = SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr)
  42. + msblk->xattr_table;
  43. int offset = SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr);
  44. int count = squashfs_i(inode)->xattr_count;
  45. size_t rest = buffer_size;
  46. int err;
  47. /* check that the file system has xattrs */
  48. if (msblk->xattr_id_table == NULL)
  49. return -EOPNOTSUPP;
  50. /* loop reading each xattr name */
  51. while (count--) {
  52. struct squashfs_xattr_entry entry;
  53. struct squashfs_xattr_val val;
  54. const struct xattr_handler *handler;
  55. int name_size;
  56. err = squashfs_read_metadata(sb, &entry, &start, &offset,
  57. sizeof(entry));
  58. if (err < 0)
  59. goto failed;
  60. name_size = le16_to_cpu(entry.size);
  61. handler = squashfs_xattr_handler(le16_to_cpu(entry.type));
  62. if (handler && (!handler->list || handler->list(d))) {
  63. const char *prefix = handler->prefix ?: handler->name;
  64. size_t prefix_size = strlen(prefix);
  65. if (buffer) {
  66. if (prefix_size + name_size + 1 > rest) {
  67. err = -ERANGE;
  68. goto failed;
  69. }
  70. memcpy(buffer, prefix, prefix_size);
  71. buffer += prefix_size;
  72. }
  73. err = squashfs_read_metadata(sb, buffer, &start,
  74. &offset, name_size);
  75. if (err < 0)
  76. goto failed;
  77. if (buffer) {
  78. buffer[name_size] = '\0';
  79. buffer += name_size + 1;
  80. }
  81. rest -= prefix_size + name_size + 1;
  82. } else {
  83. /* no handler or insuffficient privileges, so skip */
  84. err = squashfs_read_metadata(sb, NULL, &start,
  85. &offset, name_size);
  86. if (err < 0)
  87. goto failed;
  88. }
  89. /* skip remaining xattr entry */
  90. err = squashfs_read_metadata(sb, &val, &start, &offset,
  91. sizeof(val));
  92. if (err < 0)
  93. goto failed;
  94. err = squashfs_read_metadata(sb, NULL, &start, &offset,
  95. le32_to_cpu(val.vsize));
  96. if (err < 0)
  97. goto failed;
  98. }
  99. err = buffer_size - rest;
  100. failed:
  101. return err;
  102. }
  103. static int squashfs_xattr_get(struct inode *inode, int name_index,
  104. const char *name, void *buffer, size_t buffer_size)
  105. {
  106. struct super_block *sb = inode->i_sb;
  107. struct squashfs_sb_info *msblk = sb->s_fs_info;
  108. u64 start = SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr)
  109. + msblk->xattr_table;
  110. int offset = SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr);
  111. int count = squashfs_i(inode)->xattr_count;
  112. int name_len = strlen(name);
  113. int err, vsize;
  114. char *target = kmalloc(name_len, GFP_KERNEL);
  115. if (target == NULL)
  116. return -ENOMEM;
  117. /* loop reading each xattr name */
  118. for (; count; count--) {
  119. struct squashfs_xattr_entry entry;
  120. struct squashfs_xattr_val val;
  121. int type, prefix, name_size;
  122. err = squashfs_read_metadata(sb, &entry, &start, &offset,
  123. sizeof(entry));
  124. if (err < 0)
  125. goto failed;
  126. name_size = le16_to_cpu(entry.size);
  127. type = le16_to_cpu(entry.type);
  128. prefix = type & SQUASHFS_XATTR_PREFIX_MASK;
  129. if (prefix == name_index && name_size == name_len)
  130. err = squashfs_read_metadata(sb, target, &start,
  131. &offset, name_size);
  132. else
  133. err = squashfs_read_metadata(sb, NULL, &start,
  134. &offset, name_size);
  135. if (err < 0)
  136. goto failed;
  137. if (prefix == name_index && name_size == name_len &&
  138. strncmp(target, name, name_size) == 0) {
  139. /* found xattr */
  140. if (type & SQUASHFS_XATTR_VALUE_OOL) {
  141. __le64 xattr_val;
  142. u64 xattr;
  143. /* val is a reference to the real location */
  144. err = squashfs_read_metadata(sb, &val, &start,
  145. &offset, sizeof(val));
  146. if (err < 0)
  147. goto failed;
  148. err = squashfs_read_metadata(sb, &xattr_val,
  149. &start, &offset, sizeof(xattr_val));
  150. if (err < 0)
  151. goto failed;
  152. xattr = le64_to_cpu(xattr_val);
  153. start = SQUASHFS_XATTR_BLK(xattr) +
  154. msblk->xattr_table;
  155. offset = SQUASHFS_XATTR_OFFSET(xattr);
  156. }
  157. /* read xattr value */
  158. err = squashfs_read_metadata(sb, &val, &start, &offset,
  159. sizeof(val));
  160. if (err < 0)
  161. goto failed;
  162. vsize = le32_to_cpu(val.vsize);
  163. if (buffer) {
  164. if (vsize > buffer_size) {
  165. err = -ERANGE;
  166. goto failed;
  167. }
  168. err = squashfs_read_metadata(sb, buffer, &start,
  169. &offset, vsize);
  170. if (err < 0)
  171. goto failed;
  172. }
  173. break;
  174. }
  175. /* no match, skip remaining xattr entry */
  176. err = squashfs_read_metadata(sb, &val, &start, &offset,
  177. sizeof(val));
  178. if (err < 0)
  179. goto failed;
  180. err = squashfs_read_metadata(sb, NULL, &start, &offset,
  181. le32_to_cpu(val.vsize));
  182. if (err < 0)
  183. goto failed;
  184. }
  185. err = count ? vsize : -ENODATA;
  186. failed:
  187. kfree(target);
  188. return err;
  189. }
  190. static int squashfs_xattr_handler_get(const struct xattr_handler *handler,
  191. struct dentry *unused,
  192. struct inode *inode,
  193. const char *name,
  194. void *buffer, size_t size)
  195. {
  196. return squashfs_xattr_get(inode, handler->flags, name,
  197. buffer, size);
  198. }
  199. /*
  200. * User namespace support
  201. */
  202. static const struct xattr_handler squashfs_xattr_user_handler = {
  203. .prefix = XATTR_USER_PREFIX,
  204. .flags = SQUASHFS_XATTR_USER,
  205. .get = squashfs_xattr_handler_get
  206. };
  207. /*
  208. * Trusted namespace support
  209. */
  210. static bool squashfs_trusted_xattr_handler_list(struct dentry *d)
  211. {
  212. return capable(CAP_SYS_ADMIN);
  213. }
  214. static const struct xattr_handler squashfs_xattr_trusted_handler = {
  215. .prefix = XATTR_TRUSTED_PREFIX,
  216. .flags = SQUASHFS_XATTR_TRUSTED,
  217. .list = squashfs_trusted_xattr_handler_list,
  218. .get = squashfs_xattr_handler_get
  219. };
  220. /*
  221. * Security namespace support
  222. */
  223. static const struct xattr_handler squashfs_xattr_security_handler = {
  224. .prefix = XATTR_SECURITY_PREFIX,
  225. .flags = SQUASHFS_XATTR_SECURITY,
  226. .get = squashfs_xattr_handler_get
  227. };
  228. static const struct xattr_handler *squashfs_xattr_handler(int type)
  229. {
  230. if (type & ~(SQUASHFS_XATTR_PREFIX_MASK | SQUASHFS_XATTR_VALUE_OOL))
  231. /* ignore unrecognised type */
  232. return NULL;
  233. switch (type & SQUASHFS_XATTR_PREFIX_MASK) {
  234. case SQUASHFS_XATTR_USER:
  235. return &squashfs_xattr_user_handler;
  236. case SQUASHFS_XATTR_TRUSTED:
  237. return &squashfs_xattr_trusted_handler;
  238. case SQUASHFS_XATTR_SECURITY:
  239. return &squashfs_xattr_security_handler;
  240. default:
  241. /* ignore unrecognised type */
  242. return NULL;
  243. }
  244. }
  245. const struct xattr_handler *squashfs_xattr_handlers[] = {
  246. &squashfs_xattr_user_handler,
  247. &squashfs_xattr_trusted_handler,
  248. &squashfs_xattr_security_handler,
  249. NULL
  250. };