xattr.c 13 KB

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