xattr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (c) International Business Machines Corp., 2003, 2007
  5. * Author(s): Steve French ([email protected])
  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 user_namespace *mnt_userns,
  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 cifs_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. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  184. case XATTR_ACL_ACCESS:
  185. #ifdef CONFIG_CIFS_POSIX
  186. if (!value)
  187. goto out;
  188. if (sb->s_flags & SB_POSIXACL)
  189. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  190. value, (const int)size,
  191. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  192. cifs_remap(cifs_sb));
  193. #endif /* CONFIG_CIFS_POSIX */
  194. break;
  195. case XATTR_ACL_DEFAULT:
  196. #ifdef CONFIG_CIFS_POSIX
  197. if (!value)
  198. goto out;
  199. if (sb->s_flags & SB_POSIXACL)
  200. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  201. value, (const int)size,
  202. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  203. cifs_remap(cifs_sb));
  204. #endif /* CONFIG_CIFS_POSIX */
  205. break;
  206. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  207. }
  208. out:
  209. free_dentry_path(page);
  210. free_xid(xid);
  211. cifs_put_tlink(tlink);
  212. return rc;
  213. }
  214. static int cifs_attrib_get(struct dentry *dentry,
  215. struct inode *inode, void *value,
  216. size_t size)
  217. {
  218. ssize_t rc;
  219. __u32 *pattribute;
  220. rc = cifs_revalidate_dentry_attr(dentry);
  221. if (rc)
  222. return rc;
  223. if ((value == NULL) || (size == 0))
  224. return sizeof(__u32);
  225. else if (size < sizeof(__u32))
  226. return -ERANGE;
  227. /* return dos attributes as pseudo xattr */
  228. pattribute = (__u32 *)value;
  229. *pattribute = CIFS_I(inode)->cifsAttrs;
  230. return sizeof(__u32);
  231. }
  232. static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
  233. void *value, size_t size)
  234. {
  235. ssize_t rc;
  236. __u64 *pcreatetime;
  237. rc = cifs_revalidate_dentry_attr(dentry);
  238. if (rc)
  239. return rc;
  240. if ((value == NULL) || (size == 0))
  241. return sizeof(__u64);
  242. else if (size < sizeof(__u64))
  243. return -ERANGE;
  244. /* return dos attributes as pseudo xattr */
  245. pcreatetime = (__u64 *)value;
  246. *pcreatetime = CIFS_I(inode)->createtime;
  247. return sizeof(__u64);
  248. }
  249. static int cifs_xattr_get(const struct xattr_handler *handler,
  250. struct dentry *dentry, struct inode *inode,
  251. const char *name, void *value, size_t size)
  252. {
  253. ssize_t rc = -EOPNOTSUPP;
  254. unsigned int xid;
  255. struct super_block *sb = dentry->d_sb;
  256. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  257. struct tcon_link *tlink;
  258. struct cifs_tcon *pTcon;
  259. const char *full_path;
  260. void *page;
  261. tlink = cifs_sb_tlink(cifs_sb);
  262. if (IS_ERR(tlink))
  263. return PTR_ERR(tlink);
  264. pTcon = tlink_tcon(tlink);
  265. xid = get_xid();
  266. page = alloc_dentry_path();
  267. full_path = build_path_from_dentry(dentry, page);
  268. if (IS_ERR(full_path)) {
  269. rc = PTR_ERR(full_path);
  270. goto out;
  271. }
  272. /* return alt name if available as pseudo attr */
  273. switch (handler->flags) {
  274. case XATTR_USER:
  275. cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
  276. if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
  277. (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
  278. rc = cifs_attrib_get(dentry, inode, value, size);
  279. break;
  280. } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
  281. (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
  282. rc = cifs_creation_time_get(dentry, inode, value, size);
  283. break;
  284. }
  285. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  286. goto out;
  287. if (pTcon->ses->server->ops->query_all_EAs)
  288. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  289. full_path, name, value, size, cifs_sb);
  290. break;
  291. case XATTR_CIFS_ACL:
  292. case XATTR_CIFS_NTSD:
  293. case XATTR_CIFS_NTSD_FULL: {
  294. /*
  295. * fetch owner, DACL, and SACL if asked for full descriptor,
  296. * fetch owner and DACL otherwise
  297. */
  298. u32 acllen, extra_info;
  299. struct cifs_ntsd *pacl;
  300. if (pTcon->ses->server->ops->get_acl == NULL)
  301. goto out; /* rc already EOPNOTSUPP */
  302. if (handler->flags == XATTR_CIFS_NTSD_FULL) {
  303. extra_info = SACL_SECINFO;
  304. } else {
  305. extra_info = 0;
  306. }
  307. pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
  308. inode, full_path, &acllen, extra_info);
  309. if (IS_ERR(pacl)) {
  310. rc = PTR_ERR(pacl);
  311. cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
  312. __func__, rc);
  313. } else {
  314. if (value) {
  315. if (acllen > size)
  316. acllen = -ERANGE;
  317. else
  318. memcpy(value, pacl, acllen);
  319. }
  320. rc = acllen;
  321. kfree(pacl);
  322. }
  323. break;
  324. }
  325. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  326. case XATTR_ACL_ACCESS:
  327. #ifdef CONFIG_CIFS_POSIX
  328. if (sb->s_flags & SB_POSIXACL)
  329. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  330. value, size, ACL_TYPE_ACCESS,
  331. cifs_sb->local_nls,
  332. cifs_remap(cifs_sb));
  333. #endif /* CONFIG_CIFS_POSIX */
  334. break;
  335. case XATTR_ACL_DEFAULT:
  336. #ifdef CONFIG_CIFS_POSIX
  337. if (sb->s_flags & SB_POSIXACL)
  338. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  339. value, size, ACL_TYPE_DEFAULT,
  340. cifs_sb->local_nls,
  341. cifs_remap(cifs_sb));
  342. #endif /* CONFIG_CIFS_POSIX */
  343. break;
  344. #endif /* ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  345. }
  346. /* We could add an additional check for streams ie
  347. if proc/fs/cifs/streamstoxattr is set then
  348. search server for EAs or streams to
  349. returns as xattrs */
  350. if (rc == -EINVAL)
  351. rc = -EOPNOTSUPP;
  352. out:
  353. free_dentry_path(page);
  354. free_xid(xid);
  355. cifs_put_tlink(tlink);
  356. return rc;
  357. }
  358. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  359. {
  360. ssize_t rc = -EOPNOTSUPP;
  361. unsigned int xid;
  362. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  363. struct tcon_link *tlink;
  364. struct cifs_tcon *pTcon;
  365. const char *full_path;
  366. void *page;
  367. if (unlikely(cifs_forced_shutdown(cifs_sb)))
  368. return -EIO;
  369. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  370. return -EOPNOTSUPP;
  371. tlink = cifs_sb_tlink(cifs_sb);
  372. if (IS_ERR(tlink))
  373. return PTR_ERR(tlink);
  374. pTcon = tlink_tcon(tlink);
  375. xid = get_xid();
  376. page = alloc_dentry_path();
  377. full_path = build_path_from_dentry(direntry, page);
  378. if (IS_ERR(full_path)) {
  379. rc = PTR_ERR(full_path);
  380. goto list_ea_exit;
  381. }
  382. /* return dos attributes as pseudo xattr */
  383. /* return alt name if available as pseudo attr */
  384. /* if proc/fs/cifs/streamstoxattr is set then
  385. search server for EAs or streams to
  386. returns as xattrs */
  387. if (pTcon->ses->server->ops->query_all_EAs)
  388. rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
  389. full_path, NULL, data, buf_size, cifs_sb);
  390. list_ea_exit:
  391. free_dentry_path(page);
  392. free_xid(xid);
  393. cifs_put_tlink(tlink);
  394. return rc;
  395. }
  396. static const struct xattr_handler cifs_user_xattr_handler = {
  397. .prefix = XATTR_USER_PREFIX,
  398. .flags = XATTR_USER,
  399. .get = cifs_xattr_get,
  400. .set = cifs_xattr_set,
  401. };
  402. /* os2.* attributes are treated like user.* attributes */
  403. static const struct xattr_handler cifs_os2_xattr_handler = {
  404. .prefix = XATTR_OS2_PREFIX,
  405. .flags = XATTR_USER,
  406. .get = cifs_xattr_get,
  407. .set = cifs_xattr_set,
  408. };
  409. static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
  410. .name = CIFS_XATTR_CIFS_ACL,
  411. .flags = XATTR_CIFS_ACL,
  412. .get = cifs_xattr_get,
  413. .set = cifs_xattr_set,
  414. };
  415. /*
  416. * Although this is just an alias for the above, need to move away from
  417. * confusing users and using the 20 year old term 'cifs' when it is no
  418. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  419. * SMB3 and later are highly secure.
  420. */
  421. static const struct xattr_handler smb3_acl_xattr_handler = {
  422. .name = SMB3_XATTR_CIFS_ACL,
  423. .flags = XATTR_CIFS_ACL,
  424. .get = cifs_xattr_get,
  425. .set = cifs_xattr_set,
  426. };
  427. static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
  428. .name = CIFS_XATTR_CIFS_NTSD,
  429. .flags = XATTR_CIFS_NTSD,
  430. .get = cifs_xattr_get,
  431. .set = cifs_xattr_set,
  432. };
  433. /*
  434. * Although this is just an alias for the above, need to move away from
  435. * confusing users and using the 20 year old term 'cifs' when it is no
  436. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  437. * SMB3 and later are highly secure.
  438. */
  439. static const struct xattr_handler smb3_ntsd_xattr_handler = {
  440. .name = SMB3_XATTR_CIFS_NTSD,
  441. .flags = XATTR_CIFS_NTSD,
  442. .get = cifs_xattr_get,
  443. .set = cifs_xattr_set,
  444. };
  445. static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
  446. .name = CIFS_XATTR_CIFS_NTSD_FULL,
  447. .flags = XATTR_CIFS_NTSD_FULL,
  448. .get = cifs_xattr_get,
  449. .set = cifs_xattr_set,
  450. };
  451. /*
  452. * Although this is just an alias for the above, need to move away from
  453. * confusing users and using the 20 year old term 'cifs' when it is no
  454. * longer secure and was replaced by SMB2/SMB3 a long time ago, and
  455. * SMB3 and later are highly secure.
  456. */
  457. static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
  458. .name = SMB3_XATTR_CIFS_NTSD_FULL,
  459. .flags = XATTR_CIFS_NTSD_FULL,
  460. .get = cifs_xattr_get,
  461. .set = cifs_xattr_set,
  462. };
  463. static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
  464. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  465. .flags = XATTR_ACL_ACCESS,
  466. .get = cifs_xattr_get,
  467. .set = cifs_xattr_set,
  468. };
  469. static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
  470. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  471. .flags = XATTR_ACL_DEFAULT,
  472. .get = cifs_xattr_get,
  473. .set = cifs_xattr_set,
  474. };
  475. const struct xattr_handler *cifs_xattr_handlers[] = {
  476. &cifs_user_xattr_handler,
  477. &cifs_os2_xattr_handler,
  478. &cifs_cifs_acl_xattr_handler,
  479. &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
  480. &cifs_cifs_ntsd_xattr_handler,
  481. &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
  482. &cifs_cifs_ntsd_full_xattr_handler,
  483. &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
  484. &cifs_posix_acl_access_xattr_handler,
  485. &cifs_posix_acl_default_xattr_handler,
  486. NULL
  487. };