xattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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" /* DACL only */
  34. #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
  35. #define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
  36. #define CIFS_XATTR_CREATETIME "cifs.creationtime" /* user.cifs.creationtime */
  37. /*
  38. * Although these three are just aliases for the above, need to move away from
  39. * confusing users and using the 20+ year old term 'cifs' when it is no longer
  40. * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
  41. */
  42. #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
  43. #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
  44. #define SMB3_XATTR_ATTRIB "smb3.dosattrib" /* full name: user.smb3.dosattrib */
  45. #define SMB3_XATTR_CREATETIME "smb3.creationtime" /* user.smb3.creationtime */
  46. /* BB need to add server (Samba e.g) support for security and trusted prefix */
  47. enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
  48. XATTR_CIFS_NTSD };
  49. static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
  50. struct inode *inode, char *full_path,
  51. const void *value, size_t size)
  52. {
  53. ssize_t rc = -EOPNOTSUPP;
  54. __u32 *pattrib = (__u32 *)value;
  55. __u32 attrib;
  56. FILE_BASIC_INFO info_buf;
  57. if ((value == NULL) || (size != sizeof(__u32)))
  58. return -ERANGE;
  59. memset(&info_buf, 0, sizeof(info_buf));
  60. attrib = *pattrib;
  61. info_buf.Attributes = cpu_to_le32(attrib);
  62. if (pTcon->ses->server->ops->set_file_info)
  63. rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
  64. &info_buf, xid);
  65. if (rc == 0)
  66. CIFS_I(inode)->cifsAttrs = attrib;
  67. return rc;
  68. }
  69. static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
  70. struct inode *inode, char *full_path,
  71. const void *value, size_t size)
  72. {
  73. ssize_t rc = -EOPNOTSUPP;
  74. __u64 *pcreation_time = (__u64 *)value;
  75. __u64 creation_time;
  76. FILE_BASIC_INFO info_buf;
  77. if ((value == NULL) || (size != sizeof(__u64)))
  78. return -ERANGE;
  79. memset(&info_buf, 0, sizeof(info_buf));
  80. creation_time = *pcreation_time;
  81. info_buf.CreationTime = cpu_to_le64(creation_time);
  82. if (pTcon->ses->server->ops->set_file_info)
  83. rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
  84. &info_buf, xid);
  85. if (rc == 0)
  86. CIFS_I(inode)->createtime = creation_time;
  87. return rc;
  88. }
  89. static int cifs_xattr_set(const struct xattr_handler *handler,
  90. struct dentry *dentry, struct inode *inode,
  91. const char *name, const void *value,
  92. size_t size, int flags)
  93. {
  94. int rc = -EOPNOTSUPP;
  95. unsigned int xid;
  96. struct super_block *sb = dentry->d_sb;
  97. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  98. struct tcon_link *tlink;
  99. struct cifs_tcon *pTcon;
  100. char *full_path;
  101. tlink = cifs_sb_tlink(cifs_sb);
  102. if (IS_ERR(tlink))
  103. return PTR_ERR(tlink);
  104. pTcon = tlink_tcon(tlink);
  105. xid = get_xid();
  106. full_path = build_path_from_dentry(dentry);
  107. if (full_path == NULL) {
  108. rc = -ENOMEM;
  109. goto out;
  110. }
  111. /* return dos attributes as pseudo xattr */
  112. /* return alt name if available as pseudo attr */
  113. /* if proc/fs/cifs/streamstoxattr is set then
  114. search server for EAs or streams to
  115. returns as xattrs */
  116. if (size > MAX_EA_VALUE_SIZE) {
  117. cifs_dbg(FYI, "size of EA value too large\n");
  118. rc = -EOPNOTSUPP;
  119. goto out;
  120. }
  121. switch (handler->flags) {
  122. case XATTR_USER:
  123. cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
  124. if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
  125. (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
  126. rc = cifs_attrib_set(xid, pTcon, inode, full_path,
  127. value, size);
  128. if (rc == 0) /* force revalidate of the inode */
  129. CIFS_I(inode)->time = 0;
  130. break;
  131. } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
  132. (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
  133. rc = cifs_creation_time_set(xid, pTcon, inode,
  134. full_path, value, size);
  135. if (rc == 0) /* force revalidate of the inode */
  136. CIFS_I(inode)->time = 0;
  137. break;
  138. }
  139. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  140. goto out;
  141. if (pTcon->ses->server->ops->set_EA)
  142. rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
  143. full_path, name, value, (__u16)size,
  144. cifs_sb->local_nls, cifs_sb);
  145. break;
  146. case XATTR_CIFS_ACL:
  147. case XATTR_CIFS_NTSD: {
  148. struct cifs_ntsd *pacl;
  149. if (!value)
  150. goto out;
  151. pacl = kmalloc(size, GFP_KERNEL);
  152. if (!pacl) {
  153. rc = -ENOMEM;
  154. } else {
  155. memcpy(pacl, value, size);
  156. if (value &&
  157. pTcon->ses->server->ops->set_acl) {
  158. rc = 0;
  159. if (handler->flags == XATTR_CIFS_NTSD) {
  160. /* set owner and DACL */
  161. rc = pTcon->ses->server->ops->set_acl(
  162. pacl, size, inode,
  163. full_path,
  164. CIFS_ACL_OWNER);
  165. }
  166. if (rc == 0) {
  167. /* set DACL */
  168. rc = pTcon->ses->server->ops->set_acl(
  169. pacl, size, inode,
  170. full_path,
  171. CIFS_ACL_DACL);
  172. }
  173. } else {
  174. rc = -EOPNOTSUPP;
  175. }
  176. if (rc == 0) /* force revalidate of the inode */
  177. CIFS_I(inode)->time = 0;
  178. kfree(pacl);
  179. }
  180. break;
  181. }
  182. case XATTR_ACL_ACCESS:
  183. #ifdef CONFIG_CIFS_POSIX
  184. if (!value)
  185. goto out;
  186. if (sb->s_flags & SB_POSIXACL)
  187. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  188. value, (const int)size,
  189. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  190. cifs_remap(cifs_sb));
  191. #endif /* CONFIG_CIFS_POSIX */
  192. break;
  193. case XATTR_ACL_DEFAULT:
  194. #ifdef CONFIG_CIFS_POSIX
  195. if (!value)
  196. goto out;
  197. if (sb->s_flags & SB_POSIXACL)
  198. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  199. value, (const int)size,
  200. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  201. cifs_remap(cifs_sb));
  202. #endif /* CONFIG_CIFS_POSIX */
  203. break;
  204. }
  205. out:
  206. kfree(full_path);
  207. free_xid(xid);
  208. cifs_put_tlink(tlink);
  209. return rc;
  210. }
  211. static int cifs_attrib_get(struct dentry *dentry,
  212. struct inode *inode, void *value,
  213. size_t size)
  214. {
  215. ssize_t rc;
  216. __u32 *pattribute;
  217. rc = cifs_revalidate_dentry_attr(dentry);
  218. if (rc)
  219. return rc;
  220. if ((value == NULL) || (size == 0))
  221. return sizeof(__u32);
  222. else if (size < sizeof(__u32))
  223. return -ERANGE;
  224. /* return dos attributes as pseudo xattr */
  225. pattribute = (__u32 *)value;
  226. *pattribute = CIFS_I(inode)->cifsAttrs;
  227. return sizeof(__u32);
  228. }
  229. static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
  230. void *value, size_t size)
  231. {
  232. ssize_t rc;
  233. __u64 *pcreatetime;
  234. rc = cifs_revalidate_dentry_attr(dentry);
  235. if (rc)
  236. return rc;
  237. if ((value == NULL) || (size == 0))
  238. return sizeof(__u64);
  239. else if (size < sizeof(__u64))
  240. return -ERANGE;
  241. /* return dos attributes as pseudo xattr */
  242. pcreatetime = (__u64 *)value;
  243. *pcreatetime = CIFS_I(inode)->createtime;
  244. return sizeof(__u64);
  245. }
  246. static int cifs_xattr_get(const struct xattr_handler *handler,
  247. struct dentry *dentry, struct inode *inode,
  248. const char *name, void *value, size_t size, int flags)
  249. {
  250. ssize_t rc = -EOPNOTSUPP;
  251. unsigned int xid;
  252. struct super_block *sb = dentry->d_sb;
  253. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  254. struct tcon_link *tlink;
  255. struct cifs_tcon *pTcon;
  256. char *full_path;
  257. tlink = cifs_sb_tlink(cifs_sb);
  258. if (IS_ERR(tlink))
  259. return PTR_ERR(tlink);
  260. pTcon = tlink_tcon(tlink);
  261. xid = get_xid();
  262. full_path = build_path_from_dentry(dentry);
  263. if (full_path == NULL) {
  264. rc = -ENOMEM;
  265. goto out;
  266. }
  267. /* return alt name if available as pseudo attr */
  268. switch (handler->flags) {
  269. case XATTR_USER:
  270. cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
  271. if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
  272. (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
  273. rc = cifs_attrib_get(dentry, inode, value, size);
  274. break;
  275. } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
  276. (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
  277. rc = cifs_creation_time_get(dentry, inode, value, size);
  278. break;
  279. }
  280. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  281. goto out;
  282. if (pTcon->ses->server->ops->query_all_EAs)
  283. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  284. full_path, name, value, size, cifs_sb);
  285. break;
  286. case XATTR_CIFS_ACL:
  287. case XATTR_CIFS_NTSD: {
  288. /* the whole ntsd is fetched regardless */
  289. u32 acllen;
  290. struct cifs_ntsd *pacl;
  291. if (pTcon->ses->server->ops->get_acl == NULL)
  292. goto out; /* rc already EOPNOTSUPP */
  293. pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
  294. inode, full_path, &acllen);
  295. if (IS_ERR(pacl)) {
  296. rc = PTR_ERR(pacl);
  297. cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
  298. __func__, rc);
  299. } else {
  300. if (value) {
  301. if (acllen > size)
  302. acllen = -ERANGE;
  303. else
  304. memcpy(value, pacl, acllen);
  305. }
  306. rc = acllen;
  307. kfree(pacl);
  308. }
  309. break;
  310. }
  311. case XATTR_ACL_ACCESS:
  312. #ifdef CONFIG_CIFS_POSIX
  313. if (sb->s_flags & SB_POSIXACL)
  314. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  315. value, size, ACL_TYPE_ACCESS,
  316. cifs_sb->local_nls,
  317. cifs_remap(cifs_sb));
  318. #endif /* CONFIG_CIFS_POSIX */
  319. break;
  320. case XATTR_ACL_DEFAULT:
  321. #ifdef CONFIG_CIFS_POSIX
  322. if (sb->s_flags & SB_POSIXACL)
  323. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  324. value, size, ACL_TYPE_DEFAULT,
  325. cifs_sb->local_nls,
  326. cifs_remap(cifs_sb));
  327. #endif /* CONFIG_CIFS_POSIX */
  328. break;
  329. }
  330. /* We could add an additional check for streams ie
  331. if proc/fs/cifs/streamstoxattr is set then
  332. search server for EAs or streams to
  333. returns as xattrs */
  334. if (rc == -EINVAL)
  335. rc = -EOPNOTSUPP;
  336. out:
  337. kfree(full_path);
  338. free_xid(xid);
  339. cifs_put_tlink(tlink);
  340. return rc;
  341. }
  342. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  343. {
  344. ssize_t rc = -EOPNOTSUPP;
  345. unsigned int xid;
  346. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  347. struct tcon_link *tlink;
  348. struct cifs_tcon *pTcon;
  349. char *full_path;
  350. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  351. return -EOPNOTSUPP;
  352. tlink = cifs_sb_tlink(cifs_sb);
  353. if (IS_ERR(tlink))
  354. return PTR_ERR(tlink);
  355. pTcon = tlink_tcon(tlink);
  356. xid = get_xid();
  357. full_path = build_path_from_dentry(direntry);
  358. if (full_path == NULL) {
  359. rc = -ENOMEM;
  360. goto list_ea_exit;
  361. }
  362. /* return dos attributes as pseudo xattr */
  363. /* return alt name if available as pseudo attr */
  364. /* if proc/fs/cifs/streamstoxattr is set then
  365. search server for EAs or streams to
  366. returns as xattrs */
  367. if (pTcon->ses->server->ops->query_all_EAs)
  368. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  369. full_path, NULL, data, buf_size, cifs_sb);
  370. list_ea_exit:
  371. kfree(full_path);
  372. free_xid(xid);
  373. cifs_put_tlink(tlink);
  374. return rc;
  375. }
  376. static const struct xattr_handler cifs_user_xattr_handler = {
  377. .prefix = XATTR_USER_PREFIX,
  378. .flags = XATTR_USER,
  379. .get = cifs_xattr_get,
  380. .set = cifs_xattr_set,
  381. };
  382. /* os2.* attributes are treated like user.* attributes */
  383. static const struct xattr_handler cifs_os2_xattr_handler = {
  384. .prefix = XATTR_OS2_PREFIX,
  385. .flags = XATTR_USER,
  386. .get = cifs_xattr_get,
  387. .set = cifs_xattr_set,
  388. };
  389. static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
  390. .name = CIFS_XATTR_CIFS_ACL,
  391. .flags = XATTR_CIFS_ACL,
  392. .get = cifs_xattr_get,
  393. .set = cifs_xattr_set,
  394. };
  395. /*
  396. * Although this is just an alias for the above, need to move away from
  397. * confusing users and using the 20 year old term 'cifs' when it is no
  398. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  399. * SMB3 and later are highly secure.
  400. */
  401. static const struct xattr_handler smb3_acl_xattr_handler = {
  402. .name = SMB3_XATTR_CIFS_ACL,
  403. .flags = XATTR_CIFS_ACL,
  404. .get = cifs_xattr_get,
  405. .set = cifs_xattr_set,
  406. };
  407. static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
  408. .name = CIFS_XATTR_CIFS_NTSD,
  409. .flags = XATTR_CIFS_NTSD,
  410. .get = cifs_xattr_get,
  411. .set = cifs_xattr_set,
  412. };
  413. /*
  414. * Although this is just an alias for the above, need to move away from
  415. * confusing users and using the 20 year old term 'cifs' when it is no
  416. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  417. * SMB3 and later are highly secure.
  418. */
  419. static const struct xattr_handler smb3_ntsd_xattr_handler = {
  420. .name = SMB3_XATTR_CIFS_NTSD,
  421. .flags = XATTR_CIFS_NTSD,
  422. .get = cifs_xattr_get,
  423. .set = cifs_xattr_set,
  424. };
  425. static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
  426. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  427. .flags = XATTR_ACL_ACCESS,
  428. .get = cifs_xattr_get,
  429. .set = cifs_xattr_set,
  430. };
  431. static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
  432. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  433. .flags = XATTR_ACL_DEFAULT,
  434. .get = cifs_xattr_get,
  435. .set = cifs_xattr_set,
  436. };
  437. const struct xattr_handler *cifs_xattr_handlers[] = {
  438. &cifs_user_xattr_handler,
  439. &cifs_os2_xattr_handler,
  440. &cifs_cifs_acl_xattr_handler,
  441. &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
  442. &cifs_cifs_ntsd_xattr_handler,
  443. &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
  444. &cifs_posix_acl_access_xattr_handler,
  445. &cifs_posix_acl_default_xattr_handler,
  446. NULL
  447. };