xfs_acl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 (!VN_ISDIR(vp))
  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. bhv_vattr_t va;
  216. va.va_mask = XFS_AT_MODE;
  217. error = xfs_getattr(xfs_vtoi(vp), &va, 0);
  218. if (error)
  219. goto out;
  220. xfs_acl_sync_mode(va.va_mode, xfs_acl);
  221. }
  222. error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
  223. }
  224. out:
  225. VN_RELE(vp);
  226. if(xfs_acl)
  227. _ACL_FREE(xfs_acl);
  228. return -error;
  229. }
  230. int
  231. xfs_acl_vremove(
  232. bhv_vnode_t *vp,
  233. int kind)
  234. {
  235. int error;
  236. VN_HOLD(vp);
  237. error = xfs_acl_allow_set(vp, kind);
  238. if (!error) {
  239. error = xfs_attr_remove(xfs_vtoi(vp),
  240. kind == _ACL_TYPE_DEFAULT?
  241. SGI_ACL_DEFAULT: SGI_ACL_FILE,
  242. ATTR_ROOT);
  243. if (error == ENOATTR)
  244. error = 0; /* 'scool */
  245. }
  246. VN_RELE(vp);
  247. return -error;
  248. }
  249. int
  250. xfs_acl_vset(
  251. bhv_vnode_t *vp,
  252. void *acl,
  253. size_t size,
  254. int kind)
  255. {
  256. posix_acl_xattr_header *ext_acl = acl;
  257. xfs_acl_t *xfs_acl;
  258. int error;
  259. int basicperms = 0; /* more than std unix perms? */
  260. if (!acl)
  261. return -EINVAL;
  262. if (!(_ACL_ALLOC(xfs_acl)))
  263. return -ENOMEM;
  264. error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
  265. if (error) {
  266. _ACL_FREE(xfs_acl);
  267. return -error;
  268. }
  269. if (!xfs_acl->acl_cnt) {
  270. _ACL_FREE(xfs_acl);
  271. return 0;
  272. }
  273. VN_HOLD(vp);
  274. error = xfs_acl_allow_set(vp, kind);
  275. /* Incoming ACL exists, set file mode based on its value */
  276. if (!error && kind == _ACL_TYPE_ACCESS)
  277. error = xfs_acl_setmode(vp, xfs_acl, &basicperms);
  278. if (error)
  279. goto out;
  280. /*
  281. * If we have more than std unix permissions, set up the actual attr.
  282. * Otherwise, delete any existing attr. This prevents us from
  283. * having actual attrs for permissions that can be stored in the
  284. * standard permission bits.
  285. */
  286. if (!basicperms) {
  287. xfs_acl_set_attr(vp, xfs_acl, kind, &error);
  288. } else {
  289. error = -xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
  290. }
  291. out:
  292. VN_RELE(vp);
  293. _ACL_FREE(xfs_acl);
  294. return -error;
  295. }
  296. int
  297. xfs_acl_iaccess(
  298. xfs_inode_t *ip,
  299. mode_t mode,
  300. cred_t *cr)
  301. {
  302. xfs_acl_t *acl;
  303. int rval;
  304. if (!(_ACL_ALLOC(acl)))
  305. return -1;
  306. /* If the file has no ACL return -1. */
  307. rval = sizeof(xfs_acl_t);
  308. if (xfs_attr_fetch(ip, SGI_ACL_FILE, SGI_ACL_FILE_SIZE,
  309. (char *)acl, &rval, ATTR_ROOT | ATTR_KERNACCESS, cr)) {
  310. _ACL_FREE(acl);
  311. return -1;
  312. }
  313. xfs_acl_get_endian(acl);
  314. /* If the file has an empty ACL return -1. */
  315. if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
  316. _ACL_FREE(acl);
  317. return -1;
  318. }
  319. /* Synchronize ACL with mode bits */
  320. xfs_acl_sync_mode(ip->i_d.di_mode, acl);
  321. rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
  322. _ACL_FREE(acl);
  323. return rval;
  324. }
  325. STATIC int
  326. xfs_acl_allow_set(
  327. bhv_vnode_t *vp,
  328. int kind)
  329. {
  330. xfs_inode_t *ip = xfs_vtoi(vp);
  331. bhv_vattr_t va;
  332. int error;
  333. if (vp->i_flags & (S_IMMUTABLE|S_APPEND))
  334. return EPERM;
  335. if (kind == _ACL_TYPE_DEFAULT && !VN_ISDIR(vp))
  336. return ENOTDIR;
  337. if (vp->i_sb->s_flags & MS_RDONLY)
  338. return EROFS;
  339. va.va_mask = XFS_AT_UID;
  340. error = xfs_getattr(ip, &va, 0);
  341. if (error)
  342. return error;
  343. if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
  344. return EPERM;
  345. return error;
  346. }
  347. /*
  348. * Note: cr is only used here for the capability check if the ACL test fails.
  349. * It is not used to find out the credentials uid or groups etc, as was
  350. * done in IRIX. It is assumed that the uid and groups for the current
  351. * thread are taken from "current" instead of the cr parameter.
  352. */
  353. STATIC int
  354. xfs_acl_access(
  355. uid_t fuid,
  356. gid_t fgid,
  357. xfs_acl_t *fap,
  358. mode_t md,
  359. cred_t *cr)
  360. {
  361. xfs_acl_entry_t matched;
  362. int i, allows;
  363. int maskallows = -1; /* true, but not 1, either */
  364. int seen_userobj = 0;
  365. matched.ae_tag = 0; /* Invalid type */
  366. matched.ae_perm = 0;
  367. for (i = 0; i < fap->acl_cnt; i++) {
  368. /*
  369. * Break out if we've got a user_obj entry or
  370. * a user entry and the mask (and have processed USER_OBJ)
  371. */
  372. if (matched.ae_tag == ACL_USER_OBJ)
  373. break;
  374. if (matched.ae_tag == ACL_USER) {
  375. if (maskallows != -1 && seen_userobj)
  376. break;
  377. if (fap->acl_entry[i].ae_tag != ACL_MASK &&
  378. fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
  379. continue;
  380. }
  381. /* True if this entry allows the requested access */
  382. allows = ((fap->acl_entry[i].ae_perm & md) == md);
  383. switch (fap->acl_entry[i].ae_tag) {
  384. case ACL_USER_OBJ:
  385. seen_userobj = 1;
  386. if (fuid != current->fsuid)
  387. continue;
  388. matched.ae_tag = ACL_USER_OBJ;
  389. matched.ae_perm = allows;
  390. break;
  391. case ACL_USER:
  392. if (fap->acl_entry[i].ae_id != current->fsuid)
  393. continue;
  394. matched.ae_tag = ACL_USER;
  395. matched.ae_perm = allows;
  396. break;
  397. case ACL_GROUP_OBJ:
  398. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  399. matched.ae_tag == ACL_GROUP) && !allows)
  400. continue;
  401. if (!in_group_p(fgid))
  402. continue;
  403. matched.ae_tag = ACL_GROUP_OBJ;
  404. matched.ae_perm = allows;
  405. break;
  406. case ACL_GROUP:
  407. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  408. matched.ae_tag == ACL_GROUP) && !allows)
  409. continue;
  410. if (!in_group_p(fap->acl_entry[i].ae_id))
  411. continue;
  412. matched.ae_tag = ACL_GROUP;
  413. matched.ae_perm = allows;
  414. break;
  415. case ACL_MASK:
  416. maskallows = allows;
  417. break;
  418. case ACL_OTHER:
  419. if (matched.ae_tag != 0)
  420. continue;
  421. matched.ae_tag = ACL_OTHER;
  422. matched.ae_perm = allows;
  423. break;
  424. }
  425. }
  426. /*
  427. * First possibility is that no matched entry allows access.
  428. * The capability to override DAC may exist, so check for it.
  429. */
  430. switch (matched.ae_tag) {
  431. case ACL_OTHER:
  432. case ACL_USER_OBJ:
  433. if (matched.ae_perm)
  434. return 0;
  435. break;
  436. case ACL_USER:
  437. case ACL_GROUP_OBJ:
  438. case ACL_GROUP:
  439. if (maskallows && matched.ae_perm)
  440. return 0;
  441. break;
  442. case 0:
  443. break;
  444. }
  445. /* EACCES tells generic_permission to check for capability overrides */
  446. return EACCES;
  447. }
  448. /*
  449. * ACL validity checker.
  450. * This acl validation routine checks each ACL entry read in makes sense.
  451. */
  452. STATIC int
  453. xfs_acl_invalid(
  454. xfs_acl_t *aclp)
  455. {
  456. xfs_acl_entry_t *entry, *e;
  457. int user = 0, group = 0, other = 0, mask = 0;
  458. int mask_required = 0;
  459. int i, j;
  460. if (!aclp)
  461. goto acl_invalid;
  462. if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
  463. goto acl_invalid;
  464. for (i = 0; i < aclp->acl_cnt; i++) {
  465. entry = &aclp->acl_entry[i];
  466. switch (entry->ae_tag) {
  467. case ACL_USER_OBJ:
  468. if (user++)
  469. goto acl_invalid;
  470. break;
  471. case ACL_GROUP_OBJ:
  472. if (group++)
  473. goto acl_invalid;
  474. break;
  475. case ACL_OTHER:
  476. if (other++)
  477. goto acl_invalid;
  478. break;
  479. case ACL_USER:
  480. case ACL_GROUP:
  481. for (j = i + 1; j < aclp->acl_cnt; j++) {
  482. e = &aclp->acl_entry[j];
  483. if (e->ae_id == entry->ae_id &&
  484. e->ae_tag == entry->ae_tag)
  485. goto acl_invalid;
  486. }
  487. mask_required++;
  488. break;
  489. case ACL_MASK:
  490. if (mask++)
  491. goto acl_invalid;
  492. break;
  493. default:
  494. goto acl_invalid;
  495. }
  496. }
  497. if (!user || !group || !other || (mask_required && !mask))
  498. goto acl_invalid;
  499. else
  500. return 0;
  501. acl_invalid:
  502. return EINVAL;
  503. }
  504. /*
  505. * Do ACL endian conversion.
  506. */
  507. STATIC void
  508. xfs_acl_get_endian(
  509. xfs_acl_t *aclp)
  510. {
  511. xfs_acl_entry_t *ace, *end;
  512. INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  513. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  514. for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
  515. INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  516. INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
  517. INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  518. }
  519. }
  520. /*
  521. * Get the ACL from the EA and do endian conversion.
  522. */
  523. STATIC void
  524. xfs_acl_get_attr(
  525. bhv_vnode_t *vp,
  526. xfs_acl_t *aclp,
  527. int kind,
  528. int flags,
  529. int *error)
  530. {
  531. int len = sizeof(xfs_acl_t);
  532. ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
  533. flags |= ATTR_ROOT;
  534. *error = xfs_attr_get(xfs_vtoi(vp),
  535. kind == _ACL_TYPE_ACCESS ?
  536. SGI_ACL_FILE : SGI_ACL_DEFAULT,
  537. (char *)aclp, &len, flags, sys_cred);
  538. if (*error || (flags & ATTR_KERNOVAL))
  539. return;
  540. xfs_acl_get_endian(aclp);
  541. }
  542. /*
  543. * Set the EA with the ACL and do endian conversion.
  544. */
  545. STATIC void
  546. xfs_acl_set_attr(
  547. bhv_vnode_t *vp,
  548. xfs_acl_t *aclp,
  549. int kind,
  550. int *error)
  551. {
  552. xfs_acl_entry_t *ace, *newace, *end;
  553. xfs_acl_t *newacl;
  554. int len;
  555. if (!(_ACL_ALLOC(newacl))) {
  556. *error = ENOMEM;
  557. return;
  558. }
  559. len = sizeof(xfs_acl_t) -
  560. (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
  561. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  562. for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
  563. ace < end;
  564. ace++, newace++) {
  565. INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  566. INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
  567. INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  568. }
  569. INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  570. *error = xfs_attr_set(xfs_vtoi(vp),
  571. kind == _ACL_TYPE_ACCESS ?
  572. SGI_ACL_FILE: SGI_ACL_DEFAULT,
  573. (char *)newacl, len, ATTR_ROOT);
  574. _ACL_FREE(newacl);
  575. }
  576. int
  577. xfs_acl_vtoacl(
  578. bhv_vnode_t *vp,
  579. xfs_acl_t *access_acl,
  580. xfs_acl_t *default_acl)
  581. {
  582. bhv_vattr_t va;
  583. int error = 0;
  584. if (access_acl) {
  585. /*
  586. * Get the Access ACL and the mode. If either cannot
  587. * be obtained for some reason, invalidate the access ACL.
  588. */
  589. xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
  590. if (!error) {
  591. /* Got the ACL, need the mode... */
  592. va.va_mask = XFS_AT_MODE;
  593. error = xfs_getattr(xfs_vtoi(vp), &va, 0);
  594. }
  595. if (error)
  596. access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  597. else /* We have a good ACL and the file mode, synchronize. */
  598. xfs_acl_sync_mode(va.va_mode, access_acl);
  599. }
  600. if (default_acl) {
  601. xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
  602. if (error)
  603. default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  604. }
  605. return error;
  606. }
  607. /*
  608. * This function retrieves the parent directory's acl, processes it
  609. * and lets the child inherit the acl(s) that it should.
  610. */
  611. int
  612. xfs_acl_inherit(
  613. bhv_vnode_t *vp,
  614. mode_t mode,
  615. xfs_acl_t *pdaclp)
  616. {
  617. xfs_acl_t *cacl;
  618. int error = 0;
  619. int basicperms = 0;
  620. /*
  621. * If the parent does not have a default ACL, or it's an
  622. * invalid ACL, we're done.
  623. */
  624. if (!vp)
  625. return 0;
  626. if (!pdaclp || xfs_acl_invalid(pdaclp))
  627. return 0;
  628. /*
  629. * Copy the default ACL of the containing directory to
  630. * the access ACL of the new file and use the mode that
  631. * was passed in to set up the correct initial values for
  632. * the u::,g::[m::], and o:: entries. This is what makes
  633. * umask() "work" with ACL's.
  634. */
  635. if (!(_ACL_ALLOC(cacl)))
  636. return ENOMEM;
  637. memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
  638. xfs_acl_filter_mode(mode, cacl);
  639. error = xfs_acl_setmode(vp, cacl, &basicperms);
  640. if (error)
  641. goto out_error;
  642. /*
  643. * Set the Default and Access ACL on the file. The mode is already
  644. * set on the file, so we don't need to worry about that.
  645. *
  646. * If the new file is a directory, its default ACL is a copy of
  647. * the containing directory's default ACL.
  648. */
  649. if (VN_ISDIR(vp))
  650. xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
  651. if (!error && !basicperms)
  652. xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
  653. out_error:
  654. _ACL_FREE(cacl);
  655. return error;
  656. }
  657. /*
  658. * Set up the correct mode on the file based on the supplied ACL. This
  659. * makes sure that the mode on the file reflects the state of the
  660. * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
  661. * the ACL is going to get the permissions for these entries, we must
  662. * synchronize the mode whenever we set the ACL on a file.
  663. */
  664. STATIC int
  665. xfs_acl_setmode(
  666. bhv_vnode_t *vp,
  667. xfs_acl_t *acl,
  668. int *basicperms)
  669. {
  670. bhv_vattr_t va;
  671. xfs_acl_entry_t *ap;
  672. xfs_acl_entry_t *gap = NULL;
  673. int i, error, nomask = 1;
  674. *basicperms = 1;
  675. if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
  676. return 0;
  677. /*
  678. * Copy the u::, g::, o::, and m:: bits from the ACL into the
  679. * mode. The m:: bits take precedence over the g:: bits.
  680. */
  681. va.va_mask = XFS_AT_MODE;
  682. error = xfs_getattr(xfs_vtoi(vp), &va, 0);
  683. if (error)
  684. return error;
  685. va.va_mask = XFS_AT_MODE;
  686. va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
  687. ap = acl->acl_entry;
  688. for (i = 0; i < acl->acl_cnt; ++i) {
  689. switch (ap->ae_tag) {
  690. case ACL_USER_OBJ:
  691. va.va_mode |= ap->ae_perm << 6;
  692. break;
  693. case ACL_GROUP_OBJ:
  694. gap = ap;
  695. break;
  696. case ACL_MASK: /* more than just standard modes */
  697. nomask = 0;
  698. va.va_mode |= ap->ae_perm << 3;
  699. *basicperms = 0;
  700. break;
  701. case ACL_OTHER:
  702. va.va_mode |= ap->ae_perm;
  703. break;
  704. default: /* more than just standard modes */
  705. *basicperms = 0;
  706. break;
  707. }
  708. ap++;
  709. }
  710. /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
  711. if (gap && nomask)
  712. va.va_mode |= gap->ae_perm << 3;
  713. return xfs_setattr(xfs_vtoi(vp), &va, 0, sys_cred);
  714. }
  715. /*
  716. * The permissions for the special ACL entries (u::, g::[m::], o::) are
  717. * actually stored in the file mode (if there is both a group and a mask,
  718. * the group is stored in the ACL entry and the mask is stored on the file).
  719. * This allows the mode to remain automatically in sync with the ACL without
  720. * the need for a call-back to the ACL system at every point where the mode
  721. * could change. This function takes the permissions from the specified mode
  722. * and places it in the supplied ACL.
  723. *
  724. * This implementation draws its validity from the fact that, when the ACL
  725. * was assigned, the mode was copied from the ACL.
  726. * If the mode did not change, therefore, the mode remains exactly what was
  727. * taken from the special ACL entries at assignment.
  728. * If a subsequent chmod() was done, the POSIX spec says that the change in
  729. * mode must cause an update to the ACL seen at user level and used for
  730. * access checks. Before and after a mode change, therefore, the file mode
  731. * most accurately reflects what the special ACL entries should permit/deny.
  732. *
  733. * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
  734. * the existing mode bits will override whatever is in the
  735. * ACL. Similarly, if there is a pre-existing ACL that was
  736. * never in sync with its mode (owing to a bug in 6.5 and
  737. * before), it will now magically (or mystically) be
  738. * synchronized. This could cause slight astonishment, but
  739. * it is better than inconsistent permissions.
  740. *
  741. * The supplied ACL is a template that may contain any combination
  742. * of special entries. These are treated as place holders when we fill
  743. * out the ACL. This routine does not add or remove special entries, it
  744. * simply unites each special entry with its associated set of permissions.
  745. */
  746. STATIC void
  747. xfs_acl_sync_mode(
  748. mode_t mode,
  749. xfs_acl_t *acl)
  750. {
  751. int i, nomask = 1;
  752. xfs_acl_entry_t *ap;
  753. xfs_acl_entry_t *gap = NULL;
  754. /*
  755. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  756. * be set instead of the GROUP entry, if there is a MASK.
  757. */
  758. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  759. switch (ap->ae_tag) {
  760. case ACL_USER_OBJ:
  761. ap->ae_perm = (mode >> 6) & 0x7;
  762. break;
  763. case ACL_GROUP_OBJ:
  764. gap = ap;
  765. break;
  766. case ACL_MASK:
  767. nomask = 0;
  768. ap->ae_perm = (mode >> 3) & 0x7;
  769. break;
  770. case ACL_OTHER:
  771. ap->ae_perm = mode & 0x7;
  772. break;
  773. default:
  774. break;
  775. }
  776. }
  777. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  778. if (gap && nomask)
  779. gap->ae_perm = (mode >> 3) & 0x7;
  780. }
  781. /*
  782. * When inheriting an Access ACL from a directory Default ACL,
  783. * the ACL bits are set to the intersection of the ACL default
  784. * permission bits and the file permission bits in mode. If there
  785. * are no permission bits on the file then we must not give them
  786. * the ACL. This is what what makes umask() work with ACLs.
  787. */
  788. STATIC void
  789. xfs_acl_filter_mode(
  790. mode_t mode,
  791. xfs_acl_t *acl)
  792. {
  793. int i, nomask = 1;
  794. xfs_acl_entry_t *ap;
  795. xfs_acl_entry_t *gap = NULL;
  796. /*
  797. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  798. * be merged with GROUP entry, if there is a MASK.
  799. */
  800. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  801. switch (ap->ae_tag) {
  802. case ACL_USER_OBJ:
  803. ap->ae_perm &= (mode >> 6) & 0x7;
  804. break;
  805. case ACL_GROUP_OBJ:
  806. gap = ap;
  807. break;
  808. case ACL_MASK:
  809. nomask = 0;
  810. ap->ae_perm &= (mode >> 3) & 0x7;
  811. break;
  812. case ACL_OTHER:
  813. ap->ae_perm &= mode & 0x7;
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  820. if (gap && nomask)
  821. gap->ae_perm &= (mode >> 3) & 0x7;
  822. }