xattr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2010
  5. * Phillip Lougher <phillip@lougher.demon.co.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->d_inode;
  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(d, buffer, rest, NULL,
  64. name_size, handler->flags);
  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;
  142. /* val is a reference to the real location */
  143. err = squashfs_read_metadata(sb, &val, &start,
  144. &offset, sizeof(val));
  145. if (err < 0)
  146. goto failed;
  147. err = squashfs_read_metadata(sb, &xattr, &start,
  148. &offset, sizeof(xattr));
  149. if (err < 0)
  150. goto failed;
  151. xattr = le64_to_cpu(xattr);
  152. start = SQUASHFS_XATTR_BLK(xattr) +
  153. msblk->xattr_table;
  154. offset = SQUASHFS_XATTR_OFFSET(xattr);
  155. }
  156. /* read xattr value */
  157. err = squashfs_read_metadata(sb, &val, &start, &offset,
  158. sizeof(val));
  159. if (err < 0)
  160. goto failed;
  161. vsize = le32_to_cpu(val.vsize);
  162. if (buffer) {
  163. if (vsize > buffer_size) {
  164. err = -ERANGE;
  165. goto failed;
  166. }
  167. err = squashfs_read_metadata(sb, buffer, &start,
  168. &offset, vsize);
  169. if (err < 0)
  170. goto failed;
  171. }
  172. break;
  173. }
  174. /* no match, skip remaining xattr entry */
  175. err = squashfs_read_metadata(sb, &val, &start, &offset,
  176. sizeof(val));
  177. if (err < 0)
  178. goto failed;
  179. err = squashfs_read_metadata(sb, NULL, &start, &offset,
  180. le32_to_cpu(val.vsize));
  181. if (err < 0)
  182. goto failed;
  183. }
  184. err = count ? vsize : -ENODATA;
  185. failed:
  186. kfree(target);
  187. return err;
  188. }
  189. /*
  190. * User namespace support
  191. */
  192. static size_t squashfs_user_list(struct dentry *d, char *list, size_t list_size,
  193. const char *name, size_t name_len, int type)
  194. {
  195. if (list && XATTR_USER_PREFIX_LEN <= list_size)
  196. memcpy(list, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  197. return XATTR_USER_PREFIX_LEN;
  198. }
  199. static int squashfs_user_get(struct dentry *d, const char *name, void *buffer,
  200. size_t size, int type)
  201. {
  202. if (name[0] == '\0')
  203. return -EINVAL;
  204. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_USER, name,
  205. buffer, size);
  206. }
  207. static const struct xattr_handler squashfs_xattr_user_handler = {
  208. .prefix = XATTR_USER_PREFIX,
  209. .list = squashfs_user_list,
  210. .get = squashfs_user_get
  211. };
  212. /*
  213. * Trusted namespace support
  214. */
  215. static size_t squashfs_trusted_list(struct dentry *d, char *list,
  216. size_t list_size, const char *name, size_t name_len, int type)
  217. {
  218. if (!capable(CAP_SYS_ADMIN))
  219. return 0;
  220. if (list && XATTR_TRUSTED_PREFIX_LEN <= list_size)
  221. memcpy(list, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  222. return XATTR_TRUSTED_PREFIX_LEN;
  223. }
  224. static int squashfs_trusted_get(struct dentry *d, const char *name,
  225. void *buffer, size_t size, int type)
  226. {
  227. if (name[0] == '\0')
  228. return -EINVAL;
  229. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_TRUSTED, name,
  230. buffer, size);
  231. }
  232. static const struct xattr_handler squashfs_xattr_trusted_handler = {
  233. .prefix = XATTR_TRUSTED_PREFIX,
  234. .list = squashfs_trusted_list,
  235. .get = squashfs_trusted_get
  236. };
  237. /*
  238. * Security namespace support
  239. */
  240. static size_t squashfs_security_list(struct dentry *d, char *list,
  241. size_t list_size, const char *name, size_t name_len, int type)
  242. {
  243. if (list && XATTR_SECURITY_PREFIX_LEN <= list_size)
  244. memcpy(list, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
  245. return XATTR_SECURITY_PREFIX_LEN;
  246. }
  247. static int squashfs_security_get(struct dentry *d, const char *name,
  248. void *buffer, size_t size, int type)
  249. {
  250. if (name[0] == '\0')
  251. return -EINVAL;
  252. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_SECURITY, name,
  253. buffer, size);
  254. }
  255. static const struct xattr_handler squashfs_xattr_security_handler = {
  256. .prefix = XATTR_SECURITY_PREFIX,
  257. .list = squashfs_security_list,
  258. .get = squashfs_security_get
  259. };
  260. static const struct xattr_handler *squashfs_xattr_handler(int type)
  261. {
  262. if (type & ~(SQUASHFS_XATTR_PREFIX_MASK | SQUASHFS_XATTR_VALUE_OOL))
  263. /* ignore unrecognised type */
  264. return NULL;
  265. switch (type & SQUASHFS_XATTR_PREFIX_MASK) {
  266. case SQUASHFS_XATTR_USER:
  267. return &squashfs_xattr_user_handler;
  268. case SQUASHFS_XATTR_TRUSTED:
  269. return &squashfs_xattr_trusted_handler;
  270. case SQUASHFS_XATTR_SECURITY:
  271. return &squashfs_xattr_security_handler;
  272. default:
  273. /* ignore unrecognised type */
  274. return NULL;
  275. }
  276. }
  277. const struct xattr_handler *squashfs_xattr_handlers[] = {
  278. &squashfs_xattr_user_handler,
  279. &squashfs_xattr_trusted_handler,
  280. &squashfs_xattr_security_handler,
  281. NULL
  282. };