xattr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, prefix_size = 0;
  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)
  63. prefix_size = handler->list(handler, d, buffer, rest,
  64. NULL, name_size);
  65. if (prefix_size) {
  66. if (buffer) {
  67. if (prefix_size + name_size + 1 > rest) {
  68. err = -ERANGE;
  69. goto failed;
  70. }
  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 size_t squashfs_xattr_handler_list(const struct xattr_handler *handler,
  191. struct dentry *d, char *list,
  192. size_t list_size, const char *name,
  193. size_t name_len)
  194. {
  195. int len = strlen(handler->prefix);
  196. if (list && len <= list_size)
  197. memcpy(list, handler->prefix, len);
  198. return len;
  199. }
  200. static int squashfs_xattr_handler_get(const struct xattr_handler *handler,
  201. struct dentry *d, const char *name,
  202. void *buffer, size_t size)
  203. {
  204. if (name[0] == '\0')
  205. return -EINVAL;
  206. return squashfs_xattr_get(d_inode(d), handler->flags, name,
  207. buffer, size);
  208. }
  209. /*
  210. * User namespace support
  211. */
  212. static const struct xattr_handler squashfs_xattr_user_handler = {
  213. .prefix = XATTR_USER_PREFIX,
  214. .flags = SQUASHFS_XATTR_USER,
  215. .list = squashfs_xattr_handler_list,
  216. .get = squashfs_xattr_handler_get
  217. };
  218. /*
  219. * Trusted namespace support
  220. */
  221. static size_t squashfs_trusted_xattr_handler_list(const struct xattr_handler *handler,
  222. struct dentry *d, char *list,
  223. size_t list_size, const char *name,
  224. size_t name_len)
  225. {
  226. if (!capable(CAP_SYS_ADMIN))
  227. return 0;
  228. return squashfs_xattr_handler_list(handler, d, list, list_size, name,
  229. name_len);
  230. }
  231. static const struct xattr_handler squashfs_xattr_trusted_handler = {
  232. .prefix = XATTR_TRUSTED_PREFIX,
  233. .flags = SQUASHFS_XATTR_TRUSTED,
  234. .list = squashfs_trusted_xattr_handler_list,
  235. .get = squashfs_xattr_handler_get
  236. };
  237. /*
  238. * Security namespace support
  239. */
  240. static const struct xattr_handler squashfs_xattr_security_handler = {
  241. .prefix = XATTR_SECURITY_PREFIX,
  242. .flags = SQUASHFS_XATTR_SECURITY,
  243. .list = squashfs_xattr_handler_list,
  244. .get = squashfs_xattr_handler_get
  245. };
  246. static const struct xattr_handler *squashfs_xattr_handler(int type)
  247. {
  248. if (type & ~(SQUASHFS_XATTR_PREFIX_MASK | SQUASHFS_XATTR_VALUE_OOL))
  249. /* ignore unrecognised type */
  250. return NULL;
  251. switch (type & SQUASHFS_XATTR_PREFIX_MASK) {
  252. case SQUASHFS_XATTR_USER:
  253. return &squashfs_xattr_user_handler;
  254. case SQUASHFS_XATTR_TRUSTED:
  255. return &squashfs_xattr_trusted_handler;
  256. case SQUASHFS_XATTR_SECURITY:
  257. return &squashfs_xattr_security_handler;
  258. default:
  259. /* ignore unrecognised type */
  260. return NULL;
  261. }
  262. }
  263. const struct xattr_handler *squashfs_xattr_handlers[] = {
  264. &squashfs_xattr_user_handler,
  265. &squashfs_xattr_trusted_handler,
  266. &squashfs_xattr_security_handler,
  267. NULL
  268. };