xattr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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->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_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. /*
  191. * User namespace support
  192. */
  193. static size_t squashfs_user_list(struct dentry *d, char *list, size_t list_size,
  194. const char *name, size_t name_len, int type)
  195. {
  196. if (list && XATTR_USER_PREFIX_LEN <= list_size)
  197. memcpy(list, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  198. return XATTR_USER_PREFIX_LEN;
  199. }
  200. static int squashfs_user_get(struct dentry *d, const char *name, void *buffer,
  201. size_t size, int type)
  202. {
  203. if (name[0] == '\0')
  204. return -EINVAL;
  205. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_USER, name,
  206. buffer, size);
  207. }
  208. static const struct xattr_handler squashfs_xattr_user_handler = {
  209. .prefix = XATTR_USER_PREFIX,
  210. .list = squashfs_user_list,
  211. .get = squashfs_user_get
  212. };
  213. /*
  214. * Trusted namespace support
  215. */
  216. static size_t squashfs_trusted_list(struct dentry *d, char *list,
  217. size_t list_size, const char *name, size_t name_len, int type)
  218. {
  219. if (!capable(CAP_SYS_ADMIN))
  220. return 0;
  221. if (list && XATTR_TRUSTED_PREFIX_LEN <= list_size)
  222. memcpy(list, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  223. return XATTR_TRUSTED_PREFIX_LEN;
  224. }
  225. static int squashfs_trusted_get(struct dentry *d, const char *name,
  226. void *buffer, size_t size, int type)
  227. {
  228. if (name[0] == '\0')
  229. return -EINVAL;
  230. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_TRUSTED, name,
  231. buffer, size);
  232. }
  233. static const struct xattr_handler squashfs_xattr_trusted_handler = {
  234. .prefix = XATTR_TRUSTED_PREFIX,
  235. .list = squashfs_trusted_list,
  236. .get = squashfs_trusted_get
  237. };
  238. /*
  239. * Security namespace support
  240. */
  241. static size_t squashfs_security_list(struct dentry *d, char *list,
  242. size_t list_size, const char *name, size_t name_len, int type)
  243. {
  244. if (list && XATTR_SECURITY_PREFIX_LEN <= list_size)
  245. memcpy(list, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
  246. return XATTR_SECURITY_PREFIX_LEN;
  247. }
  248. static int squashfs_security_get(struct dentry *d, const char *name,
  249. void *buffer, size_t size, int type)
  250. {
  251. if (name[0] == '\0')
  252. return -EINVAL;
  253. return squashfs_xattr_get(d->d_inode, SQUASHFS_XATTR_SECURITY, name,
  254. buffer, size);
  255. }
  256. static const struct xattr_handler squashfs_xattr_security_handler = {
  257. .prefix = XATTR_SECURITY_PREFIX,
  258. .list = squashfs_security_list,
  259. .get = squashfs_security_get
  260. };
  261. static const struct xattr_handler *squashfs_xattr_handler(int type)
  262. {
  263. if (type & ~(SQUASHFS_XATTR_PREFIX_MASK | SQUASHFS_XATTR_VALUE_OOL))
  264. /* ignore unrecognised type */
  265. return NULL;
  266. switch (type & SQUASHFS_XATTR_PREFIX_MASK) {
  267. case SQUASHFS_XATTR_USER:
  268. return &squashfs_xattr_user_handler;
  269. case SQUASHFS_XATTR_TRUSTED:
  270. return &squashfs_xattr_trusted_handler;
  271. case SQUASHFS_XATTR_SECURITY:
  272. return &squashfs_xattr_security_handler;
  273. default:
  274. /* ignore unrecognised type */
  275. return NULL;
  276. }
  277. }
  278. const struct xattr_handler *squashfs_xattr_handlers[] = {
  279. &squashfs_xattr_user_handler,
  280. &squashfs_xattr_trusted_handler,
  281. &squashfs_xattr_security_handler,
  282. NULL
  283. };