xfs_acl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_alloc_btree.h"
  27. #include "xfs_ialloc_btree.h"
  28. #include "xfs_dir2_sf.h"
  29. #include "xfs_attr_sf.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_acl.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_vnodeops.h"
  36. #include <linux/capability.h>
  37. #include <linux/posix_acl_xattr.h>
  38. STATIC int xfs_acl_setmode(bhv_vnode_t *, xfs_acl_t *, int *);
  39. STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
  40. STATIC void xfs_acl_get_endian(xfs_acl_t *);
  41. STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
  42. STATIC int xfs_acl_invalid(xfs_acl_t *);
  43. STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
  44. STATIC void xfs_acl_get_attr(bhv_vnode_t *, xfs_acl_t *, int, int, int *);
  45. STATIC void xfs_acl_set_attr(bhv_vnode_t *, xfs_acl_t *, int, int *);
  46. STATIC int xfs_acl_allow_set(bhv_vnode_t *, int);
  47. kmem_zone_t *xfs_acl_zone;
  48. /*
  49. * Test for existence of access ACL attribute as efficiently as possible.
  50. */
  51. int
  52. xfs_acl_vhasacl_access(
  53. bhv_vnode_t *vp)
  54. {
  55. int error;
  56. xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
  57. return (error == 0);
  58. }
  59. /*
  60. * Test for existence of default ACL attribute as efficiently as possible.
  61. */
  62. int
  63. xfs_acl_vhasacl_default(
  64. bhv_vnode_t *vp)
  65. {
  66. int error;
  67. if (!S_ISDIR(vp->i_mode))
  68. return 0;
  69. xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
  70. return (error == 0);
  71. }
  72. /*
  73. * Convert from extended attribute representation to in-memory for XFS.
  74. */
  75. STATIC int
  76. posix_acl_xattr_to_xfs(
  77. posix_acl_xattr_header *src,
  78. size_t size,
  79. xfs_acl_t *dest)
  80. {
  81. posix_acl_xattr_entry *src_entry;
  82. xfs_acl_entry_t *dest_entry;
  83. int n;
  84. if (!src || !dest)
  85. return EINVAL;
  86. if (size < sizeof(posix_acl_xattr_header))
  87. return EINVAL;
  88. if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  89. return EOPNOTSUPP;
  90. memset(dest, 0, sizeof(xfs_acl_t));
  91. dest->acl_cnt = posix_acl_xattr_count(size);
  92. if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
  93. return EINVAL;
  94. /*
  95. * acl_set_file(3) may request that we set default ACLs with
  96. * zero length -- defend (gracefully) against that here.
  97. */
  98. if (!dest->acl_cnt)
  99. return 0;
  100. src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
  101. dest_entry = &dest->acl_entry[0];
  102. for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
  103. dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
  104. if (_ACL_PERM_INVALID(dest_entry->ae_perm))
  105. return EINVAL;
  106. dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
  107. switch(dest_entry->ae_tag) {
  108. case ACL_USER:
  109. case ACL_GROUP:
  110. dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
  111. break;
  112. case ACL_USER_OBJ:
  113. case ACL_GROUP_OBJ:
  114. case ACL_MASK:
  115. case ACL_OTHER:
  116. dest_entry->ae_id = ACL_UNDEFINED_ID;
  117. break;
  118. default:
  119. return EINVAL;
  120. }
  121. }
  122. if (xfs_acl_invalid(dest))
  123. return EINVAL;
  124. return 0;
  125. }
  126. /*
  127. * Comparison function called from xfs_sort().
  128. * Primary key is ae_tag, secondary key is ae_id.
  129. */
  130. STATIC int
  131. xfs_acl_entry_compare(
  132. const void *va,
  133. const void *vb)
  134. {
  135. xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
  136. *b = (xfs_acl_entry_t *)vb;
  137. if (a->ae_tag == b->ae_tag)
  138. return (a->ae_id - b->ae_id);
  139. return (a->ae_tag - b->ae_tag);
  140. }
  141. /*
  142. * Convert from in-memory XFS to extended attribute representation.
  143. */
  144. STATIC int
  145. posix_acl_xfs_to_xattr(
  146. xfs_acl_t *src,
  147. posix_acl_xattr_header *dest,
  148. size_t size)
  149. {
  150. int n;
  151. size_t new_size = posix_acl_xattr_size(src->acl_cnt);
  152. posix_acl_xattr_entry *dest_entry;
  153. xfs_acl_entry_t *src_entry;
  154. if (size < new_size)
  155. return -ERANGE;
  156. /* Need to sort src XFS ACL by <ae_tag,ae_id> */
  157. xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
  158. xfs_acl_entry_compare);
  159. dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  160. dest_entry = &dest->a_entries[0];
  161. src_entry = &src->acl_entry[0];
  162. for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
  163. dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
  164. if (_ACL_PERM_INVALID(src_entry->ae_perm))
  165. return -EINVAL;
  166. dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
  167. switch (src_entry->ae_tag) {
  168. case ACL_USER:
  169. case ACL_GROUP:
  170. dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
  171. break;
  172. case ACL_USER_OBJ:
  173. case ACL_GROUP_OBJ:
  174. case ACL_MASK:
  175. case ACL_OTHER:
  176. dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. }
  182. return new_size;
  183. }
  184. int
  185. xfs_acl_vget(
  186. bhv_vnode_t *vp,
  187. void *acl,
  188. size_t size,
  189. int kind)
  190. {
  191. int error;
  192. xfs_acl_t *xfs_acl = NULL;
  193. posix_acl_xattr_header *ext_acl = acl;
  194. int flags = 0;
  195. VN_HOLD(vp);
  196. if(size) {
  197. if (!(_ACL_ALLOC(xfs_acl))) {
  198. error = ENOMEM;
  199. goto out;
  200. }
  201. memset(xfs_acl, 0, sizeof(xfs_acl_t));
  202. } else
  203. flags = ATTR_KERNOVAL;
  204. xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
  205. if (error)
  206. goto out;
  207. if (!size) {
  208. error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
  209. } else {
  210. if (xfs_acl_invalid(xfs_acl)) {
  211. error = EINVAL;
  212. goto out;
  213. }
  214. if (kind == _ACL_TYPE_ACCESS)
  215. xfs_acl_sync_mode(XFS_I(vp)->i_d.di_mode, xfs_acl);
  216. error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
  217. }
  218. out:
  219. VN_RELE(vp);
  220. if(xfs_acl)
  221. _ACL_FREE(xfs_acl);
  222. return -error;
  223. }
  224. int
  225. xfs_acl_vremove(
  226. bhv_vnode_t *vp,
  227. int kind)
  228. {
  229. int error;
  230. VN_HOLD(vp);
  231. error = xfs_acl_allow_set(vp, kind);
  232. if (!error) {
  233. error = xfs_attr_remove(XFS_I(vp),
  234. kind == _ACL_TYPE_DEFAULT?
  235. SGI_ACL_DEFAULT: SGI_ACL_FILE,
  236. ATTR_ROOT);
  237. if (error == ENOATTR)
  238. error = 0; /* 'scool */
  239. }
  240. VN_RELE(vp);
  241. return -error;
  242. }
  243. int
  244. xfs_acl_vset(
  245. bhv_vnode_t *vp,
  246. void *acl,
  247. size_t size,
  248. int kind)
  249. {
  250. posix_acl_xattr_header *ext_acl = acl;
  251. xfs_acl_t *xfs_acl;
  252. int error;
  253. int basicperms = 0; /* more than std unix perms? */
  254. if (!acl)
  255. return -EINVAL;
  256. if (!(_ACL_ALLOC(xfs_acl)))
  257. return -ENOMEM;
  258. error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
  259. if (error) {
  260. _ACL_FREE(xfs_acl);
  261. return -error;
  262. }
  263. if (!xfs_acl->acl_cnt) {
  264. _ACL_FREE(xfs_acl);
  265. return 0;
  266. }
  267. VN_HOLD(vp);
  268. error = xfs_acl_allow_set(vp, kind);
  269. /* Incoming ACL exists, set file mode based on its value */
  270. if (!error && kind == _ACL_TYPE_ACCESS)
  271. error = xfs_acl_setmode(vp, xfs_acl, &basicperms);
  272. if (error)
  273. goto out;
  274. /*
  275. * If we have more than std unix permissions, set up the actual attr.
  276. * Otherwise, delete any existing attr. This prevents us from
  277. * having actual attrs for permissions that can be stored in the
  278. * standard permission bits.
  279. */
  280. if (!basicperms) {
  281. xfs_acl_set_attr(vp, xfs_acl, kind, &error);
  282. } else {
  283. error = -xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
  284. }
  285. out:
  286. VN_RELE(vp);
  287. _ACL_FREE(xfs_acl);
  288. return -error;
  289. }
  290. int
  291. xfs_acl_iaccess(
  292. xfs_inode_t *ip,
  293. mode_t mode,
  294. cred_t *cr)
  295. {
  296. xfs_acl_t *acl;
  297. int rval;
  298. struct xfs_name acl_name = {SGI_ACL_FILE, SGI_ACL_FILE_SIZE};
  299. if (!(_ACL_ALLOC(acl)))
  300. return -1;
  301. /* If the file has no ACL return -1. */
  302. rval = sizeof(xfs_acl_t);
  303. if (xfs_attr_fetch(ip, &acl_name, (char *)acl, &rval, ATTR_ROOT)) {
  304. _ACL_FREE(acl);
  305. return -1;
  306. }
  307. xfs_acl_get_endian(acl);
  308. /* If the file has an empty ACL return -1. */
  309. if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
  310. _ACL_FREE(acl);
  311. return -1;
  312. }
  313. /* Synchronize ACL with mode bits */
  314. xfs_acl_sync_mode(ip->i_d.di_mode, acl);
  315. rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
  316. _ACL_FREE(acl);
  317. return rval;
  318. }
  319. STATIC int
  320. xfs_acl_allow_set(
  321. bhv_vnode_t *vp,
  322. int kind)
  323. {
  324. if (vp->i_flags & (S_IMMUTABLE|S_APPEND))
  325. return EPERM;
  326. if (kind == _ACL_TYPE_DEFAULT && !S_ISDIR(vp->i_mode))
  327. return ENOTDIR;
  328. if (vp->i_sb->s_flags & MS_RDONLY)
  329. return EROFS;
  330. if (XFS_I(vp)->i_d.di_uid != current->fsuid && !capable(CAP_FOWNER))
  331. return EPERM;
  332. return 0;
  333. }
  334. /*
  335. * Note: cr is only used here for the capability check if the ACL test fails.
  336. * It is not used to find out the credentials uid or groups etc, as was
  337. * done in IRIX. It is assumed that the uid and groups for the current
  338. * thread are taken from "current" instead of the cr parameter.
  339. */
  340. STATIC int
  341. xfs_acl_access(
  342. uid_t fuid,
  343. gid_t fgid,
  344. xfs_acl_t *fap,
  345. mode_t md,
  346. cred_t *cr)
  347. {
  348. xfs_acl_entry_t matched;
  349. int i, allows;
  350. int maskallows = -1; /* true, but not 1, either */
  351. int seen_userobj = 0;
  352. matched.ae_tag = 0; /* Invalid type */
  353. matched.ae_perm = 0;
  354. for (i = 0; i < fap->acl_cnt; i++) {
  355. /*
  356. * Break out if we've got a user_obj entry or
  357. * a user entry and the mask (and have processed USER_OBJ)
  358. */
  359. if (matched.ae_tag == ACL_USER_OBJ)
  360. break;
  361. if (matched.ae_tag == ACL_USER) {
  362. if (maskallows != -1 && seen_userobj)
  363. break;
  364. if (fap->acl_entry[i].ae_tag != ACL_MASK &&
  365. fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
  366. continue;
  367. }
  368. /* True if this entry allows the requested access */
  369. allows = ((fap->acl_entry[i].ae_perm & md) == md);
  370. switch (fap->acl_entry[i].ae_tag) {
  371. case ACL_USER_OBJ:
  372. seen_userobj = 1;
  373. if (fuid != current->fsuid)
  374. continue;
  375. matched.ae_tag = ACL_USER_OBJ;
  376. matched.ae_perm = allows;
  377. break;
  378. case ACL_USER:
  379. if (fap->acl_entry[i].ae_id != current->fsuid)
  380. continue;
  381. matched.ae_tag = ACL_USER;
  382. matched.ae_perm = allows;
  383. break;
  384. case ACL_GROUP_OBJ:
  385. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  386. matched.ae_tag == ACL_GROUP) && !allows)
  387. continue;
  388. if (!in_group_p(fgid))
  389. continue;
  390. matched.ae_tag = ACL_GROUP_OBJ;
  391. matched.ae_perm = allows;
  392. break;
  393. case ACL_GROUP:
  394. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  395. matched.ae_tag == ACL_GROUP) && !allows)
  396. continue;
  397. if (!in_group_p(fap->acl_entry[i].ae_id))
  398. continue;
  399. matched.ae_tag = ACL_GROUP;
  400. matched.ae_perm = allows;
  401. break;
  402. case ACL_MASK:
  403. maskallows = allows;
  404. break;
  405. case ACL_OTHER:
  406. if (matched.ae_tag != 0)
  407. continue;
  408. matched.ae_tag = ACL_OTHER;
  409. matched.ae_perm = allows;
  410. break;
  411. }
  412. }
  413. /*
  414. * First possibility is that no matched entry allows access.
  415. * The capability to override DAC may exist, so check for it.
  416. */
  417. switch (matched.ae_tag) {
  418. case ACL_OTHER:
  419. case ACL_USER_OBJ:
  420. if (matched.ae_perm)
  421. return 0;
  422. break;
  423. case ACL_USER:
  424. case ACL_GROUP_OBJ:
  425. case ACL_GROUP:
  426. if (maskallows && matched.ae_perm)
  427. return 0;
  428. break;
  429. case 0:
  430. break;
  431. }
  432. /* EACCES tells generic_permission to check for capability overrides */
  433. return EACCES;
  434. }
  435. /*
  436. * ACL validity checker.
  437. * This acl validation routine checks each ACL entry read in makes sense.
  438. */
  439. STATIC int
  440. xfs_acl_invalid(
  441. xfs_acl_t *aclp)
  442. {
  443. xfs_acl_entry_t *entry, *e;
  444. int user = 0, group = 0, other = 0, mask = 0;
  445. int mask_required = 0;
  446. int i, j;
  447. if (!aclp)
  448. goto acl_invalid;
  449. if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
  450. goto acl_invalid;
  451. for (i = 0; i < aclp->acl_cnt; i++) {
  452. entry = &aclp->acl_entry[i];
  453. switch (entry->ae_tag) {
  454. case ACL_USER_OBJ:
  455. if (user++)
  456. goto acl_invalid;
  457. break;
  458. case ACL_GROUP_OBJ:
  459. if (group++)
  460. goto acl_invalid;
  461. break;
  462. case ACL_OTHER:
  463. if (other++)
  464. goto acl_invalid;
  465. break;
  466. case ACL_USER:
  467. case ACL_GROUP:
  468. for (j = i + 1; j < aclp->acl_cnt; j++) {
  469. e = &aclp->acl_entry[j];
  470. if (e->ae_id == entry->ae_id &&
  471. e->ae_tag == entry->ae_tag)
  472. goto acl_invalid;
  473. }
  474. mask_required++;
  475. break;
  476. case ACL_MASK:
  477. if (mask++)
  478. goto acl_invalid;
  479. break;
  480. default:
  481. goto acl_invalid;
  482. }
  483. }
  484. if (!user || !group || !other || (mask_required && !mask))
  485. goto acl_invalid;
  486. else
  487. return 0;
  488. acl_invalid:
  489. return EINVAL;
  490. }
  491. /*
  492. * Do ACL endian conversion.
  493. */
  494. STATIC void
  495. xfs_acl_get_endian(
  496. xfs_acl_t *aclp)
  497. {
  498. xfs_acl_entry_t *ace, *end;
  499. INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  500. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  501. for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
  502. INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  503. INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
  504. INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  505. }
  506. }
  507. /*
  508. * Get the ACL from the EA and do endian conversion.
  509. */
  510. STATIC void
  511. xfs_acl_get_attr(
  512. bhv_vnode_t *vp,
  513. xfs_acl_t *aclp,
  514. int kind,
  515. int flags,
  516. int *error)
  517. {
  518. int len = sizeof(xfs_acl_t);
  519. ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
  520. flags |= ATTR_ROOT;
  521. *error = xfs_attr_get(XFS_I(vp),
  522. kind == _ACL_TYPE_ACCESS ?
  523. SGI_ACL_FILE : SGI_ACL_DEFAULT,
  524. (char *)aclp, &len, flags);
  525. if (*error || (flags & ATTR_KERNOVAL))
  526. return;
  527. xfs_acl_get_endian(aclp);
  528. }
  529. /*
  530. * Set the EA with the ACL and do endian conversion.
  531. */
  532. STATIC void
  533. xfs_acl_set_attr(
  534. bhv_vnode_t *vp,
  535. xfs_acl_t *aclp,
  536. int kind,
  537. int *error)
  538. {
  539. xfs_acl_entry_t *ace, *newace, *end;
  540. xfs_acl_t *newacl;
  541. int len;
  542. if (!(_ACL_ALLOC(newacl))) {
  543. *error = ENOMEM;
  544. return;
  545. }
  546. len = sizeof(xfs_acl_t) -
  547. (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
  548. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  549. for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
  550. ace < end;
  551. ace++, newace++) {
  552. INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  553. INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
  554. INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  555. }
  556. INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  557. *error = xfs_attr_set(XFS_I(vp),
  558. kind == _ACL_TYPE_ACCESS ?
  559. SGI_ACL_FILE: SGI_ACL_DEFAULT,
  560. (char *)newacl, len, ATTR_ROOT);
  561. _ACL_FREE(newacl);
  562. }
  563. int
  564. xfs_acl_vtoacl(
  565. bhv_vnode_t *vp,
  566. xfs_acl_t *access_acl,
  567. xfs_acl_t *default_acl)
  568. {
  569. int error = 0;
  570. if (access_acl) {
  571. /*
  572. * Get the Access ACL and the mode. If either cannot
  573. * be obtained for some reason, invalidate the access ACL.
  574. */
  575. xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
  576. if (error)
  577. access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  578. else /* We have a good ACL and the file mode, synchronize. */
  579. xfs_acl_sync_mode(XFS_I(vp)->i_d.di_mode, access_acl);
  580. }
  581. if (default_acl) {
  582. xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
  583. if (error)
  584. default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  585. }
  586. return error;
  587. }
  588. /*
  589. * This function retrieves the parent directory's acl, processes it
  590. * and lets the child inherit the acl(s) that it should.
  591. */
  592. int
  593. xfs_acl_inherit(
  594. bhv_vnode_t *vp,
  595. mode_t mode,
  596. xfs_acl_t *pdaclp)
  597. {
  598. xfs_acl_t *cacl;
  599. int error = 0;
  600. int basicperms = 0;
  601. /*
  602. * If the parent does not have a default ACL, or it's an
  603. * invalid ACL, we're done.
  604. */
  605. if (!vp)
  606. return 0;
  607. if (!pdaclp || xfs_acl_invalid(pdaclp))
  608. return 0;
  609. /*
  610. * Copy the default ACL of the containing directory to
  611. * the access ACL of the new file and use the mode that
  612. * was passed in to set up the correct initial values for
  613. * the u::,g::[m::], and o:: entries. This is what makes
  614. * umask() "work" with ACL's.
  615. */
  616. if (!(_ACL_ALLOC(cacl)))
  617. return ENOMEM;
  618. memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
  619. xfs_acl_filter_mode(mode, cacl);
  620. error = xfs_acl_setmode(vp, cacl, &basicperms);
  621. if (error)
  622. goto out_error;
  623. /*
  624. * Set the Default and Access ACL on the file. The mode is already
  625. * set on the file, so we don't need to worry about that.
  626. *
  627. * If the new file is a directory, its default ACL is a copy of
  628. * the containing directory's default ACL.
  629. */
  630. if (S_ISDIR(vp->i_mode))
  631. xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
  632. if (!error && !basicperms)
  633. xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
  634. out_error:
  635. _ACL_FREE(cacl);
  636. return error;
  637. }
  638. /*
  639. * Set up the correct mode on the file based on the supplied ACL. This
  640. * makes sure that the mode on the file reflects the state of the
  641. * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
  642. * the ACL is going to get the permissions for these entries, we must
  643. * synchronize the mode whenever we set the ACL on a file.
  644. */
  645. STATIC int
  646. xfs_acl_setmode(
  647. bhv_vnode_t *vp,
  648. xfs_acl_t *acl,
  649. int *basicperms)
  650. {
  651. struct iattr iattr;
  652. xfs_acl_entry_t *ap;
  653. xfs_acl_entry_t *gap = NULL;
  654. int i, nomask = 1;
  655. *basicperms = 1;
  656. if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
  657. return 0;
  658. /*
  659. * Copy the u::, g::, o::, and m:: bits from the ACL into the
  660. * mode. The m:: bits take precedence over the g:: bits.
  661. */
  662. iattr.ia_valid = ATTR_MODE;
  663. iattr.ia_mode = XFS_I(vp)->i_d.di_mode;
  664. iattr.ia_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
  665. ap = acl->acl_entry;
  666. for (i = 0; i < acl->acl_cnt; ++i) {
  667. switch (ap->ae_tag) {
  668. case ACL_USER_OBJ:
  669. iattr.ia_mode |= ap->ae_perm << 6;
  670. break;
  671. case ACL_GROUP_OBJ:
  672. gap = ap;
  673. break;
  674. case ACL_MASK: /* more than just standard modes */
  675. nomask = 0;
  676. iattr.ia_mode |= ap->ae_perm << 3;
  677. *basicperms = 0;
  678. break;
  679. case ACL_OTHER:
  680. iattr.ia_mode |= ap->ae_perm;
  681. break;
  682. default: /* more than just standard modes */
  683. *basicperms = 0;
  684. break;
  685. }
  686. ap++;
  687. }
  688. /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
  689. if (gap && nomask)
  690. iattr.ia_mode |= gap->ae_perm << 3;
  691. return xfs_setattr(XFS_I(vp), &iattr, 0, sys_cred);
  692. }
  693. /*
  694. * The permissions for the special ACL entries (u::, g::[m::], o::) are
  695. * actually stored in the file mode (if there is both a group and a mask,
  696. * the group is stored in the ACL entry and the mask is stored on the file).
  697. * This allows the mode to remain automatically in sync with the ACL without
  698. * the need for a call-back to the ACL system at every point where the mode
  699. * could change. This function takes the permissions from the specified mode
  700. * and places it in the supplied ACL.
  701. *
  702. * This implementation draws its validity from the fact that, when the ACL
  703. * was assigned, the mode was copied from the ACL.
  704. * If the mode did not change, therefore, the mode remains exactly what was
  705. * taken from the special ACL entries at assignment.
  706. * If a subsequent chmod() was done, the POSIX spec says that the change in
  707. * mode must cause an update to the ACL seen at user level and used for
  708. * access checks. Before and after a mode change, therefore, the file mode
  709. * most accurately reflects what the special ACL entries should permit/deny.
  710. *
  711. * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
  712. * the existing mode bits will override whatever is in the
  713. * ACL. Similarly, if there is a pre-existing ACL that was
  714. * never in sync with its mode (owing to a bug in 6.5 and
  715. * before), it will now magically (or mystically) be
  716. * synchronized. This could cause slight astonishment, but
  717. * it is better than inconsistent permissions.
  718. *
  719. * The supplied ACL is a template that may contain any combination
  720. * of special entries. These are treated as place holders when we fill
  721. * out the ACL. This routine does not add or remove special entries, it
  722. * simply unites each special entry with its associated set of permissions.
  723. */
  724. STATIC void
  725. xfs_acl_sync_mode(
  726. mode_t mode,
  727. xfs_acl_t *acl)
  728. {
  729. int i, nomask = 1;
  730. xfs_acl_entry_t *ap;
  731. xfs_acl_entry_t *gap = NULL;
  732. /*
  733. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  734. * be set instead of the GROUP entry, if there is a MASK.
  735. */
  736. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  737. switch (ap->ae_tag) {
  738. case ACL_USER_OBJ:
  739. ap->ae_perm = (mode >> 6) & 0x7;
  740. break;
  741. case ACL_GROUP_OBJ:
  742. gap = ap;
  743. break;
  744. case ACL_MASK:
  745. nomask = 0;
  746. ap->ae_perm = (mode >> 3) & 0x7;
  747. break;
  748. case ACL_OTHER:
  749. ap->ae_perm = mode & 0x7;
  750. break;
  751. default:
  752. break;
  753. }
  754. }
  755. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  756. if (gap && nomask)
  757. gap->ae_perm = (mode >> 3) & 0x7;
  758. }
  759. /*
  760. * When inheriting an Access ACL from a directory Default ACL,
  761. * the ACL bits are set to the intersection of the ACL default
  762. * permission bits and the file permission bits in mode. If there
  763. * are no permission bits on the file then we must not give them
  764. * the ACL. This is what what makes umask() work with ACLs.
  765. */
  766. STATIC void
  767. xfs_acl_filter_mode(
  768. mode_t mode,
  769. xfs_acl_t *acl)
  770. {
  771. int i, nomask = 1;
  772. xfs_acl_entry_t *ap;
  773. xfs_acl_entry_t *gap = NULL;
  774. /*
  775. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  776. * be merged with GROUP entry, if there is a MASK.
  777. */
  778. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  779. switch (ap->ae_tag) {
  780. case ACL_USER_OBJ:
  781. ap->ae_perm &= (mode >> 6) & 0x7;
  782. break;
  783. case ACL_GROUP_OBJ:
  784. gap = ap;
  785. break;
  786. case ACL_MASK:
  787. nomask = 0;
  788. ap->ae_perm &= (mode >> 3) & 0x7;
  789. break;
  790. case ACL_OTHER:
  791. ap->ae_perm &= mode & 0x7;
  792. break;
  793. default:
  794. break;
  795. }
  796. }
  797. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  798. if (gap && nomask)
  799. gap->ae_perm &= (mode >> 3) & 0x7;
  800. }