xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * fs/cifs/xattr.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2003, 2007
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library 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
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/posix_acl_xattr.h>
  23. #include <linux/slab.h>
  24. #include <linux/xattr.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_fs_sb.h"
  31. #include "cifs_unicode.h"
  32. #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
  33. #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
  34. #define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
  35. #define CIFS_XATTR_CREATETIME "cifs.creationtime" /* user.cifs.creationtime */
  36. /*
  37. * Although these three are just aliases for the above, need to move away from
  38. * confusing users and using the 20+ year old term 'cifs' when it is no longer
  39. * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
  40. */
  41. #define SMB3_XATTR_CIFS_ACL "system.smb3_acl"
  42. #define SMB3_XATTR_ATTRIB "smb3.dosattrib" /* full name: user.smb3.dosattrib */
  43. #define SMB3_XATTR_CREATETIME "smb3.creationtime" /* user.smb3.creationtime */
  44. /* BB need to add server (Samba e.g) support for security and trusted prefix */
  45. enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT };
  46. static int cifs_xattr_set(const struct xattr_handler *handler,
  47. struct dentry *dentry, struct inode *inode,
  48. const char *name, const void *value,
  49. size_t size, int flags)
  50. {
  51. int rc = -EOPNOTSUPP;
  52. unsigned int xid;
  53. struct super_block *sb = dentry->d_sb;
  54. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  55. struct tcon_link *tlink;
  56. struct cifs_tcon *pTcon;
  57. char *full_path;
  58. tlink = cifs_sb_tlink(cifs_sb);
  59. if (IS_ERR(tlink))
  60. return PTR_ERR(tlink);
  61. pTcon = tlink_tcon(tlink);
  62. xid = get_xid();
  63. full_path = build_path_from_dentry(dentry);
  64. if (full_path == NULL) {
  65. rc = -ENOMEM;
  66. goto out;
  67. }
  68. /* return dos attributes as pseudo xattr */
  69. /* return alt name if available as pseudo attr */
  70. /* if proc/fs/cifs/streamstoxattr is set then
  71. search server for EAs or streams to
  72. returns as xattrs */
  73. if (size > MAX_EA_VALUE_SIZE) {
  74. cifs_dbg(FYI, "size of EA value too large\n");
  75. rc = -EOPNOTSUPP;
  76. goto out;
  77. }
  78. switch (handler->flags) {
  79. case XATTR_USER:
  80. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  81. goto out;
  82. if (pTcon->ses->server->ops->set_EA)
  83. rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
  84. full_path, name, value, (__u16)size,
  85. cifs_sb->local_nls, cifs_sb);
  86. break;
  87. case XATTR_CIFS_ACL: {
  88. #ifdef CONFIG_CIFS_ACL
  89. struct cifs_ntsd *pacl;
  90. if (!value)
  91. goto out;
  92. pacl = kmalloc(size, GFP_KERNEL);
  93. if (!pacl) {
  94. rc = -ENOMEM;
  95. } else {
  96. memcpy(pacl, value, size);
  97. if (value &&
  98. pTcon->ses->server->ops->set_acl)
  99. rc = pTcon->ses->server->ops->set_acl(pacl,
  100. size, inode,
  101. full_path, CIFS_ACL_DACL);
  102. else
  103. rc = -EOPNOTSUPP;
  104. if (rc == 0) /* force revalidate of the inode */
  105. CIFS_I(inode)->time = 0;
  106. kfree(pacl);
  107. }
  108. #endif /* CONFIG_CIFS_ACL */
  109. break;
  110. }
  111. case XATTR_ACL_ACCESS:
  112. #ifdef CONFIG_CIFS_POSIX
  113. if (!value)
  114. goto out;
  115. if (sb->s_flags & SB_POSIXACL)
  116. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  117. value, (const int)size,
  118. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  119. cifs_remap(cifs_sb));
  120. #endif /* CONFIG_CIFS_POSIX */
  121. break;
  122. case XATTR_ACL_DEFAULT:
  123. #ifdef CONFIG_CIFS_POSIX
  124. if (!value)
  125. goto out;
  126. if (sb->s_flags & SB_POSIXACL)
  127. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  128. value, (const int)size,
  129. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  130. cifs_remap(cifs_sb));
  131. #endif /* CONFIG_CIFS_POSIX */
  132. break;
  133. }
  134. out:
  135. kfree(full_path);
  136. free_xid(xid);
  137. cifs_put_tlink(tlink);
  138. return rc;
  139. }
  140. static int cifs_attrib_get(struct dentry *dentry,
  141. struct inode *inode, void *value,
  142. size_t size)
  143. {
  144. ssize_t rc;
  145. __u32 *pattribute;
  146. rc = cifs_revalidate_dentry_attr(dentry);
  147. if (rc)
  148. return rc;
  149. if ((value == NULL) || (size == 0))
  150. return sizeof(__u32);
  151. else if (size < sizeof(__u32))
  152. return -ERANGE;
  153. /* return dos attributes as pseudo xattr */
  154. pattribute = (__u32 *)value;
  155. *pattribute = CIFS_I(inode)->cifsAttrs;
  156. return sizeof(__u32);
  157. }
  158. static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
  159. void *value, size_t size)
  160. {
  161. ssize_t rc;
  162. __u64 * pcreatetime;
  163. rc = cifs_revalidate_dentry_attr(dentry);
  164. if (rc)
  165. return rc;
  166. if ((value == NULL) || (size == 0))
  167. return sizeof(__u64);
  168. else if (size < sizeof(__u64))
  169. return -ERANGE;
  170. /* return dos attributes as pseudo xattr */
  171. pcreatetime = (__u64 *)value;
  172. *pcreatetime = CIFS_I(inode)->createtime;
  173. return sizeof(__u64);
  174. }
  175. static int cifs_xattr_get(const struct xattr_handler *handler,
  176. struct dentry *dentry, struct inode *inode,
  177. const char *name, void *value, size_t size)
  178. {
  179. ssize_t rc = -EOPNOTSUPP;
  180. unsigned int xid;
  181. struct super_block *sb = dentry->d_sb;
  182. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  183. struct tcon_link *tlink;
  184. struct cifs_tcon *pTcon;
  185. char *full_path;
  186. tlink = cifs_sb_tlink(cifs_sb);
  187. if (IS_ERR(tlink))
  188. return PTR_ERR(tlink);
  189. pTcon = tlink_tcon(tlink);
  190. xid = get_xid();
  191. full_path = build_path_from_dentry(dentry);
  192. if (full_path == NULL) {
  193. rc = -ENOMEM;
  194. goto out;
  195. }
  196. /* return alt name if available as pseudo attr */
  197. switch (handler->flags) {
  198. case XATTR_USER:
  199. cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
  200. if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
  201. (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
  202. rc = cifs_attrib_get(dentry, inode, value, size);
  203. break;
  204. } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
  205. (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
  206. rc = cifs_creation_time_get(dentry, inode, value, size);
  207. break;
  208. }
  209. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  210. goto out;
  211. if (pTcon->ses->server->ops->query_all_EAs)
  212. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  213. full_path, name, value, size, cifs_sb);
  214. break;
  215. case XATTR_CIFS_ACL: {
  216. #ifdef CONFIG_CIFS_ACL
  217. u32 acllen;
  218. struct cifs_ntsd *pacl;
  219. if (pTcon->ses->server->ops->get_acl == NULL)
  220. goto out; /* rc already EOPNOTSUPP */
  221. pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
  222. inode, full_path, &acllen);
  223. if (IS_ERR(pacl)) {
  224. rc = PTR_ERR(pacl);
  225. cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
  226. __func__, rc);
  227. } else {
  228. if (value) {
  229. if (acllen > size)
  230. acllen = -ERANGE;
  231. else
  232. memcpy(value, pacl, acllen);
  233. }
  234. rc = acllen;
  235. kfree(pacl);
  236. }
  237. #endif /* CONFIG_CIFS_ACL */
  238. break;
  239. }
  240. case XATTR_ACL_ACCESS:
  241. #ifdef CONFIG_CIFS_POSIX
  242. if (sb->s_flags & SB_POSIXACL)
  243. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  244. value, size, ACL_TYPE_ACCESS,
  245. cifs_sb->local_nls,
  246. cifs_remap(cifs_sb));
  247. #endif /* CONFIG_CIFS_POSIX */
  248. break;
  249. case XATTR_ACL_DEFAULT:
  250. #ifdef CONFIG_CIFS_POSIX
  251. if (sb->s_flags & SB_POSIXACL)
  252. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  253. value, size, ACL_TYPE_DEFAULT,
  254. cifs_sb->local_nls,
  255. cifs_remap(cifs_sb));
  256. #endif /* CONFIG_CIFS_POSIX */
  257. break;
  258. }
  259. /* We could add an additional check for streams ie
  260. if proc/fs/cifs/streamstoxattr is set then
  261. search server for EAs or streams to
  262. returns as xattrs */
  263. if (rc == -EINVAL)
  264. rc = -EOPNOTSUPP;
  265. out:
  266. kfree(full_path);
  267. free_xid(xid);
  268. cifs_put_tlink(tlink);
  269. return rc;
  270. }
  271. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  272. {
  273. ssize_t rc = -EOPNOTSUPP;
  274. unsigned int xid;
  275. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  276. struct tcon_link *tlink;
  277. struct cifs_tcon *pTcon;
  278. char *full_path;
  279. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  280. return -EOPNOTSUPP;
  281. tlink = cifs_sb_tlink(cifs_sb);
  282. if (IS_ERR(tlink))
  283. return PTR_ERR(tlink);
  284. pTcon = tlink_tcon(tlink);
  285. xid = get_xid();
  286. full_path = build_path_from_dentry(direntry);
  287. if (full_path == NULL) {
  288. rc = -ENOMEM;
  289. goto list_ea_exit;
  290. }
  291. /* return dos attributes as pseudo xattr */
  292. /* return alt name if available as pseudo attr */
  293. /* if proc/fs/cifs/streamstoxattr is set then
  294. search server for EAs or streams to
  295. returns as xattrs */
  296. if (pTcon->ses->server->ops->query_all_EAs)
  297. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  298. full_path, NULL, data, buf_size, cifs_sb);
  299. list_ea_exit:
  300. kfree(full_path);
  301. free_xid(xid);
  302. cifs_put_tlink(tlink);
  303. return rc;
  304. }
  305. static const struct xattr_handler cifs_user_xattr_handler = {
  306. .prefix = XATTR_USER_PREFIX,
  307. .flags = XATTR_USER,
  308. .get = cifs_xattr_get,
  309. .set = cifs_xattr_set,
  310. };
  311. /* os2.* attributes are treated like user.* attributes */
  312. static const struct xattr_handler cifs_os2_xattr_handler = {
  313. .prefix = XATTR_OS2_PREFIX,
  314. .flags = XATTR_USER,
  315. .get = cifs_xattr_get,
  316. .set = cifs_xattr_set,
  317. };
  318. static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
  319. .name = CIFS_XATTR_CIFS_ACL,
  320. .flags = XATTR_CIFS_ACL,
  321. .get = cifs_xattr_get,
  322. .set = cifs_xattr_set,
  323. };
  324. /*
  325. * Although this is just an alias for the above, need to move away from
  326. * confusing users and using the 20 year old term 'cifs' when it is no
  327. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  328. * SMB3 and later are highly secure.
  329. */
  330. static const struct xattr_handler smb3_acl_xattr_handler = {
  331. .name = SMB3_XATTR_CIFS_ACL,
  332. .flags = XATTR_CIFS_ACL,
  333. .get = cifs_xattr_get,
  334. .set = cifs_xattr_set,
  335. };
  336. static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
  337. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  338. .flags = XATTR_ACL_ACCESS,
  339. .get = cifs_xattr_get,
  340. .set = cifs_xattr_set,
  341. };
  342. static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
  343. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  344. .flags = XATTR_ACL_DEFAULT,
  345. .get = cifs_xattr_get,
  346. .set = cifs_xattr_set,
  347. };
  348. const struct xattr_handler *cifs_xattr_handlers[] = {
  349. &cifs_user_xattr_handler,
  350. &cifs_os2_xattr_handler,
  351. &cifs_cifs_acl_xattr_handler,
  352. &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
  353. &cifs_posix_acl_access_xattr_handler,
  354. &cifs_posix_acl_default_xattr_handler,
  355. NULL
  356. };