xattr.c 8.2 KB

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