xfs_acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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_dir.h"
  25. #include "xfs_dir2.h"
  26. #include "xfs_bmap_btree.h"
  27. #include "xfs_alloc_btree.h"
  28. #include "xfs_ialloc_btree.h"
  29. #include "xfs_dir_sf.h"
  30. #include "xfs_dir2_sf.h"
  31. #include "xfs_attr_sf.h"
  32. #include "xfs_dinode.h"
  33. #include "xfs_inode.h"
  34. #include "xfs_btree.h"
  35. #include "xfs_acl.h"
  36. #include "xfs_mac.h"
  37. #include "xfs_attr.h"
  38. #include <linux/posix_acl_xattr.h>
  39. STATIC int xfs_acl_setmode(vnode_t *, xfs_acl_t *, int *);
  40. STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
  41. STATIC void xfs_acl_get_endian(xfs_acl_t *);
  42. STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
  43. STATIC int xfs_acl_invalid(xfs_acl_t *);
  44. STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
  45. STATIC void xfs_acl_get_attr(vnode_t *, xfs_acl_t *, int, int, int *);
  46. STATIC void xfs_acl_set_attr(vnode_t *, xfs_acl_t *, int, int *);
  47. STATIC int xfs_acl_allow_set(vnode_t *, int);
  48. kmem_zone_t *xfs_acl_zone;
  49. /*
  50. * Test for existence of access ACL attribute as efficiently as possible.
  51. */
  52. int
  53. xfs_acl_vhasacl_access(
  54. vnode_t *vp)
  55. {
  56. int error;
  57. xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
  58. return (error == 0);
  59. }
  60. /*
  61. * Test for existence of default ACL attribute as efficiently as possible.
  62. */
  63. int
  64. xfs_acl_vhasacl_default(
  65. vnode_t *vp)
  66. {
  67. int error;
  68. if (!VN_ISDIR(vp))
  69. return 0;
  70. xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
  71. return (error == 0);
  72. }
  73. /*
  74. * Convert from extended attribute representation to in-memory for XFS.
  75. */
  76. STATIC int
  77. posix_acl_xattr_to_xfs(
  78. posix_acl_xattr_header *src,
  79. size_t size,
  80. xfs_acl_t *dest)
  81. {
  82. posix_acl_xattr_entry *src_entry;
  83. xfs_acl_entry_t *dest_entry;
  84. int n;
  85. if (!src || !dest)
  86. return EINVAL;
  87. if (size < sizeof(posix_acl_xattr_header))
  88. return EINVAL;
  89. if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  90. return EOPNOTSUPP;
  91. memset(dest, 0, sizeof(xfs_acl_t));
  92. dest->acl_cnt = posix_acl_xattr_count(size);
  93. if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
  94. return EINVAL;
  95. /*
  96. * acl_set_file(3) may request that we set default ACLs with
  97. * zero length -- defend (gracefully) against that here.
  98. */
  99. if (!dest->acl_cnt)
  100. return 0;
  101. src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
  102. dest_entry = &dest->acl_entry[0];
  103. for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
  104. dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
  105. if (_ACL_PERM_INVALID(dest_entry->ae_perm))
  106. return EINVAL;
  107. dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
  108. switch(dest_entry->ae_tag) {
  109. case ACL_USER:
  110. case ACL_GROUP:
  111. dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
  112. break;
  113. case ACL_USER_OBJ:
  114. case ACL_GROUP_OBJ:
  115. case ACL_MASK:
  116. case ACL_OTHER:
  117. dest_entry->ae_id = ACL_UNDEFINED_ID;
  118. break;
  119. default:
  120. return EINVAL;
  121. }
  122. }
  123. if (xfs_acl_invalid(dest))
  124. return EINVAL;
  125. return 0;
  126. }
  127. /*
  128. * Comparison function called from xfs_sort().
  129. * Primary key is ae_tag, secondary key is ae_id.
  130. */
  131. STATIC int
  132. xfs_acl_entry_compare(
  133. const void *va,
  134. const void *vb)
  135. {
  136. xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
  137. *b = (xfs_acl_entry_t *)vb;
  138. if (a->ae_tag == b->ae_tag)
  139. return (a->ae_id - b->ae_id);
  140. return (a->ae_tag - b->ae_tag);
  141. }
  142. /*
  143. * Convert from in-memory XFS to extended attribute representation.
  144. */
  145. STATIC int
  146. posix_acl_xfs_to_xattr(
  147. xfs_acl_t *src,
  148. posix_acl_xattr_header *dest,
  149. size_t size)
  150. {
  151. int n;
  152. size_t new_size = posix_acl_xattr_size(src->acl_cnt);
  153. posix_acl_xattr_entry *dest_entry;
  154. xfs_acl_entry_t *src_entry;
  155. if (size < new_size)
  156. return -ERANGE;
  157. /* Need to sort src XFS ACL by <ae_tag,ae_id> */
  158. xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
  159. xfs_acl_entry_compare);
  160. dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  161. dest_entry = &dest->a_entries[0];
  162. src_entry = &src->acl_entry[0];
  163. for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
  164. dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
  165. if (_ACL_PERM_INVALID(src_entry->ae_perm))
  166. return -EINVAL;
  167. dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
  168. switch (src_entry->ae_tag) {
  169. case ACL_USER:
  170. case ACL_GROUP:
  171. dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
  172. break;
  173. case ACL_USER_OBJ:
  174. case ACL_GROUP_OBJ:
  175. case ACL_MASK:
  176. case ACL_OTHER:
  177. dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. }
  183. return new_size;
  184. }
  185. int
  186. xfs_acl_vget(
  187. vnode_t *vp,
  188. void *acl,
  189. size_t size,
  190. int kind)
  191. {
  192. int error;
  193. xfs_acl_t *xfs_acl = NULL;
  194. posix_acl_xattr_header *ext_acl = acl;
  195. int flags = 0;
  196. VN_HOLD(vp);
  197. if(size) {
  198. if (!(_ACL_ALLOC(xfs_acl))) {
  199. error = ENOMEM;
  200. goto out;
  201. }
  202. memset(xfs_acl, 0, sizeof(xfs_acl_t));
  203. } else
  204. flags = ATTR_KERNOVAL;
  205. xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
  206. if (error)
  207. goto out;
  208. if (!size) {
  209. error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
  210. } else {
  211. if (xfs_acl_invalid(xfs_acl)) {
  212. error = EINVAL;
  213. goto out;
  214. }
  215. if (kind == _ACL_TYPE_ACCESS) {
  216. vattr_t va;
  217. va.va_mask = XFS_AT_MODE;
  218. VOP_GETATTR(vp, &va, 0, sys_cred, error);
  219. if (error)
  220. goto out;
  221. xfs_acl_sync_mode(va.va_mode, xfs_acl);
  222. }
  223. error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
  224. }
  225. out:
  226. VN_RELE(vp);
  227. if(xfs_acl)
  228. _ACL_FREE(xfs_acl);
  229. return -error;
  230. }
  231. int
  232. xfs_acl_vremove(
  233. vnode_t *vp,
  234. int kind)
  235. {
  236. int error;
  237. VN_HOLD(vp);
  238. error = xfs_acl_allow_set(vp, kind);
  239. if (!error) {
  240. VOP_ATTR_REMOVE(vp, kind == _ACL_TYPE_DEFAULT?
  241. SGI_ACL_DEFAULT: SGI_ACL_FILE,
  242. ATTR_ROOT, sys_cred, error);
  243. if (error == ENOATTR)
  244. error = 0; /* 'scool */
  245. }
  246. VN_RELE(vp);
  247. return -error;
  248. }
  249. int
  250. xfs_acl_vset(
  251. 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. if (error)
  276. goto out;
  277. /* Incoming ACL exists, set file mode based on its value */
  278. if (kind == _ACL_TYPE_ACCESS)
  279. xfs_acl_setmode(vp, xfs_acl, &basicperms);
  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. 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. vnode_t *vp,
  328. int kind)
  329. {
  330. vattr_t va;
  331. int error;
  332. if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
  333. return EPERM;
  334. if (kind == _ACL_TYPE_DEFAULT && !VN_ISDIR(vp))
  335. return ENOTDIR;
  336. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  337. return EROFS;
  338. va.va_mask = XFS_AT_UID;
  339. VOP_GETATTR(vp, &va, 0, NULL, error);
  340. if (error)
  341. return error;
  342. if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
  343. return EPERM;
  344. return error;
  345. }
  346. /*
  347. * The access control process to determine the access permission:
  348. * if uid == file owner id, use the file owner bits.
  349. * if gid == file owner group id, use the file group bits.
  350. * scan ACL for a maching user or group, and use matched entry
  351. * permission. Use total permissions of all matching group entries,
  352. * until all acl entries are exhausted. The final permission produced
  353. * by matching acl entry or entries needs to be & with group permission.
  354. * if not owner, owning group, or matching entry in ACL, use file
  355. * other bits.
  356. */
  357. STATIC int
  358. xfs_acl_capability_check(
  359. mode_t mode,
  360. cred_t *cr)
  361. {
  362. if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
  363. return EACCES;
  364. if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
  365. return EACCES;
  366. if ((mode & ACL_EXECUTE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
  367. return EACCES;
  368. return 0;
  369. }
  370. /*
  371. * Note: cr is only used here for the capability check if the ACL test fails.
  372. * It is not used to find out the credentials uid or groups etc, as was
  373. * done in IRIX. It is assumed that the uid and groups for the current
  374. * thread are taken from "current" instead of the cr parameter.
  375. */
  376. STATIC int
  377. xfs_acl_access(
  378. uid_t fuid,
  379. gid_t fgid,
  380. xfs_acl_t *fap,
  381. mode_t md,
  382. cred_t *cr)
  383. {
  384. xfs_acl_entry_t matched;
  385. int i, allows;
  386. int maskallows = -1; /* true, but not 1, either */
  387. int seen_userobj = 0;
  388. matched.ae_tag = 0; /* Invalid type */
  389. matched.ae_perm = 0;
  390. md >>= 6; /* Normalize the bits for comparison */
  391. for (i = 0; i < fap->acl_cnt; i++) {
  392. /*
  393. * Break out if we've got a user_obj entry or
  394. * a user entry and the mask (and have processed USER_OBJ)
  395. */
  396. if (matched.ae_tag == ACL_USER_OBJ)
  397. break;
  398. if (matched.ae_tag == ACL_USER) {
  399. if (maskallows != -1 && seen_userobj)
  400. break;
  401. if (fap->acl_entry[i].ae_tag != ACL_MASK &&
  402. fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
  403. continue;
  404. }
  405. /* True if this entry allows the requested access */
  406. allows = ((fap->acl_entry[i].ae_perm & md) == md);
  407. switch (fap->acl_entry[i].ae_tag) {
  408. case ACL_USER_OBJ:
  409. seen_userobj = 1;
  410. if (fuid != current->fsuid)
  411. continue;
  412. matched.ae_tag = ACL_USER_OBJ;
  413. matched.ae_perm = allows;
  414. break;
  415. case ACL_USER:
  416. if (fap->acl_entry[i].ae_id != current->fsuid)
  417. continue;
  418. matched.ae_tag = ACL_USER;
  419. matched.ae_perm = allows;
  420. break;
  421. case ACL_GROUP_OBJ:
  422. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  423. matched.ae_tag == ACL_GROUP) && !allows)
  424. continue;
  425. if (!in_group_p(fgid))
  426. continue;
  427. matched.ae_tag = ACL_GROUP_OBJ;
  428. matched.ae_perm = allows;
  429. break;
  430. case ACL_GROUP:
  431. if ((matched.ae_tag == ACL_GROUP_OBJ ||
  432. matched.ae_tag == ACL_GROUP) && !allows)
  433. continue;
  434. if (!in_group_p(fap->acl_entry[i].ae_id))
  435. continue;
  436. matched.ae_tag = ACL_GROUP;
  437. matched.ae_perm = allows;
  438. break;
  439. case ACL_MASK:
  440. maskallows = allows;
  441. break;
  442. case ACL_OTHER:
  443. if (matched.ae_tag != 0)
  444. continue;
  445. matched.ae_tag = ACL_OTHER;
  446. matched.ae_perm = allows;
  447. break;
  448. }
  449. }
  450. /*
  451. * First possibility is that no matched entry allows access.
  452. * The capability to override DAC may exist, so check for it.
  453. */
  454. switch (matched.ae_tag) {
  455. case ACL_OTHER:
  456. case ACL_USER_OBJ:
  457. if (matched.ae_perm)
  458. return 0;
  459. break;
  460. case ACL_USER:
  461. case ACL_GROUP_OBJ:
  462. case ACL_GROUP:
  463. if (maskallows && matched.ae_perm)
  464. return 0;
  465. break;
  466. case 0:
  467. break;
  468. }
  469. return xfs_acl_capability_check(md, cr);
  470. }
  471. /*
  472. * ACL validity checker.
  473. * This acl validation routine checks each ACL entry read in makes sense.
  474. */
  475. STATIC int
  476. xfs_acl_invalid(
  477. xfs_acl_t *aclp)
  478. {
  479. xfs_acl_entry_t *entry, *e;
  480. int user = 0, group = 0, other = 0, mask = 0;
  481. int mask_required = 0;
  482. int i, j;
  483. if (!aclp)
  484. goto acl_invalid;
  485. if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
  486. goto acl_invalid;
  487. for (i = 0; i < aclp->acl_cnt; i++) {
  488. entry = &aclp->acl_entry[i];
  489. switch (entry->ae_tag) {
  490. case ACL_USER_OBJ:
  491. if (user++)
  492. goto acl_invalid;
  493. break;
  494. case ACL_GROUP_OBJ:
  495. if (group++)
  496. goto acl_invalid;
  497. break;
  498. case ACL_OTHER:
  499. if (other++)
  500. goto acl_invalid;
  501. break;
  502. case ACL_USER:
  503. case ACL_GROUP:
  504. for (j = i + 1; j < aclp->acl_cnt; j++) {
  505. e = &aclp->acl_entry[j];
  506. if (e->ae_id == entry->ae_id &&
  507. e->ae_tag == entry->ae_tag)
  508. goto acl_invalid;
  509. }
  510. mask_required++;
  511. break;
  512. case ACL_MASK:
  513. if (mask++)
  514. goto acl_invalid;
  515. break;
  516. default:
  517. goto acl_invalid;
  518. }
  519. }
  520. if (!user || !group || !other || (mask_required && !mask))
  521. goto acl_invalid;
  522. else
  523. return 0;
  524. acl_invalid:
  525. return EINVAL;
  526. }
  527. /*
  528. * Do ACL endian conversion.
  529. */
  530. STATIC void
  531. xfs_acl_get_endian(
  532. xfs_acl_t *aclp)
  533. {
  534. xfs_acl_entry_t *ace, *end;
  535. INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  536. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  537. for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
  538. INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  539. INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
  540. INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  541. }
  542. }
  543. /*
  544. * Get the ACL from the EA and do endian conversion.
  545. */
  546. STATIC void
  547. xfs_acl_get_attr(
  548. vnode_t *vp,
  549. xfs_acl_t *aclp,
  550. int kind,
  551. int flags,
  552. int *error)
  553. {
  554. int len = sizeof(xfs_acl_t);
  555. ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
  556. flags |= ATTR_ROOT;
  557. VOP_ATTR_GET(vp,
  558. kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE : SGI_ACL_DEFAULT,
  559. (char *)aclp, &len, flags, sys_cred, *error);
  560. if (*error || (flags & ATTR_KERNOVAL))
  561. return;
  562. xfs_acl_get_endian(aclp);
  563. }
  564. /*
  565. * Set the EA with the ACL and do endian conversion.
  566. */
  567. STATIC void
  568. xfs_acl_set_attr(
  569. vnode_t *vp,
  570. xfs_acl_t *aclp,
  571. int kind,
  572. int *error)
  573. {
  574. xfs_acl_entry_t *ace, *newace, *end;
  575. xfs_acl_t *newacl;
  576. int len;
  577. if (!(_ACL_ALLOC(newacl))) {
  578. *error = ENOMEM;
  579. return;
  580. }
  581. len = sizeof(xfs_acl_t) -
  582. (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
  583. end = &aclp->acl_entry[0]+aclp->acl_cnt;
  584. for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
  585. ace < end;
  586. ace++, newace++) {
  587. INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
  588. INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
  589. INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
  590. }
  591. INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
  592. VOP_ATTR_SET(vp,
  593. kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE: SGI_ACL_DEFAULT,
  594. (char *)newacl, len, ATTR_ROOT, sys_cred, *error);
  595. _ACL_FREE(newacl);
  596. }
  597. int
  598. xfs_acl_vtoacl(
  599. vnode_t *vp,
  600. xfs_acl_t *access_acl,
  601. xfs_acl_t *default_acl)
  602. {
  603. vattr_t va;
  604. int error = 0;
  605. if (access_acl) {
  606. /*
  607. * Get the Access ACL and the mode. If either cannot
  608. * be obtained for some reason, invalidate the access ACL.
  609. */
  610. xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
  611. if (!error) {
  612. /* Got the ACL, need the mode... */
  613. va.va_mask = XFS_AT_MODE;
  614. VOP_GETATTR(vp, &va, 0, sys_cred, error);
  615. }
  616. if (error)
  617. access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  618. else /* We have a good ACL and the file mode, synchronize. */
  619. xfs_acl_sync_mode(va.va_mode, access_acl);
  620. }
  621. if (default_acl) {
  622. xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
  623. if (error)
  624. default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
  625. }
  626. return error;
  627. }
  628. /*
  629. * This function retrieves the parent directory's acl, processes it
  630. * and lets the child inherit the acl(s) that it should.
  631. */
  632. int
  633. xfs_acl_inherit(
  634. vnode_t *vp,
  635. vattr_t *vap,
  636. xfs_acl_t *pdaclp)
  637. {
  638. xfs_acl_t *cacl;
  639. int error = 0;
  640. int basicperms = 0;
  641. /*
  642. * If the parent does not have a default ACL, or it's an
  643. * invalid ACL, we're done.
  644. */
  645. if (!vp)
  646. return 0;
  647. if (!pdaclp || xfs_acl_invalid(pdaclp))
  648. return 0;
  649. /*
  650. * Copy the default ACL of the containing directory to
  651. * the access ACL of the new file and use the mode that
  652. * was passed in to set up the correct initial values for
  653. * the u::,g::[m::], and o:: entries. This is what makes
  654. * umask() "work" with ACL's.
  655. */
  656. if (!(_ACL_ALLOC(cacl)))
  657. return ENOMEM;
  658. memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
  659. xfs_acl_filter_mode(vap->va_mode, cacl);
  660. xfs_acl_setmode(vp, cacl, &basicperms);
  661. /*
  662. * Set the Default and Access ACL on the file. The mode is already
  663. * set on the file, so we don't need to worry about that.
  664. *
  665. * If the new file is a directory, its default ACL is a copy of
  666. * the containing directory's default ACL.
  667. */
  668. if (VN_ISDIR(vp))
  669. xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
  670. if (!error && !basicperms)
  671. xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
  672. _ACL_FREE(cacl);
  673. return error;
  674. }
  675. /*
  676. * Set up the correct mode on the file based on the supplied ACL. This
  677. * makes sure that the mode on the file reflects the state of the
  678. * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
  679. * the ACL is going to get the permissions for these entries, we must
  680. * synchronize the mode whenever we set the ACL on a file.
  681. */
  682. STATIC int
  683. xfs_acl_setmode(
  684. vnode_t *vp,
  685. xfs_acl_t *acl,
  686. int *basicperms)
  687. {
  688. vattr_t va;
  689. xfs_acl_entry_t *ap;
  690. xfs_acl_entry_t *gap = NULL;
  691. int i, error, nomask = 1;
  692. *basicperms = 1;
  693. if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
  694. return 0;
  695. /*
  696. * Copy the u::, g::, o::, and m:: bits from the ACL into the
  697. * mode. The m:: bits take precedence over the g:: bits.
  698. */
  699. va.va_mask = XFS_AT_MODE;
  700. VOP_GETATTR(vp, &va, 0, sys_cred, error);
  701. if (error)
  702. return error;
  703. va.va_mask = XFS_AT_MODE;
  704. va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
  705. ap = acl->acl_entry;
  706. for (i = 0; i < acl->acl_cnt; ++i) {
  707. switch (ap->ae_tag) {
  708. case ACL_USER_OBJ:
  709. va.va_mode |= ap->ae_perm << 6;
  710. break;
  711. case ACL_GROUP_OBJ:
  712. gap = ap;
  713. break;
  714. case ACL_MASK: /* more than just standard modes */
  715. nomask = 0;
  716. va.va_mode |= ap->ae_perm << 3;
  717. *basicperms = 0;
  718. break;
  719. case ACL_OTHER:
  720. va.va_mode |= ap->ae_perm;
  721. break;
  722. default: /* more than just standard modes */
  723. *basicperms = 0;
  724. break;
  725. }
  726. ap++;
  727. }
  728. /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
  729. if (gap && nomask)
  730. va.va_mode |= gap->ae_perm << 3;
  731. VOP_SETATTR(vp, &va, 0, sys_cred, error);
  732. return error;
  733. }
  734. /*
  735. * The permissions for the special ACL entries (u::, g::[m::], o::) are
  736. * actually stored in the file mode (if there is both a group and a mask,
  737. * the group is stored in the ACL entry and the mask is stored on the file).
  738. * This allows the mode to remain automatically in sync with the ACL without
  739. * the need for a call-back to the ACL system at every point where the mode
  740. * could change. This function takes the permissions from the specified mode
  741. * and places it in the supplied ACL.
  742. *
  743. * This implementation draws its validity from the fact that, when the ACL
  744. * was assigned, the mode was copied from the ACL.
  745. * If the mode did not change, therefore, the mode remains exactly what was
  746. * taken from the special ACL entries at assignment.
  747. * If a subsequent chmod() was done, the POSIX spec says that the change in
  748. * mode must cause an update to the ACL seen at user level and used for
  749. * access checks. Before and after a mode change, therefore, the file mode
  750. * most accurately reflects what the special ACL entries should permit/deny.
  751. *
  752. * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
  753. * the existing mode bits will override whatever is in the
  754. * ACL. Similarly, if there is a pre-existing ACL that was
  755. * never in sync with its mode (owing to a bug in 6.5 and
  756. * before), it will now magically (or mystically) be
  757. * synchronized. This could cause slight astonishment, but
  758. * it is better than inconsistent permissions.
  759. *
  760. * The supplied ACL is a template that may contain any combination
  761. * of special entries. These are treated as place holders when we fill
  762. * out the ACL. This routine does not add or remove special entries, it
  763. * simply unites each special entry with its associated set of permissions.
  764. */
  765. STATIC void
  766. xfs_acl_sync_mode(
  767. mode_t mode,
  768. xfs_acl_t *acl)
  769. {
  770. int i, nomask = 1;
  771. xfs_acl_entry_t *ap;
  772. xfs_acl_entry_t *gap = NULL;
  773. /*
  774. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  775. * be set instead of the GROUP entry, if there is a MASK.
  776. */
  777. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  778. switch (ap->ae_tag) {
  779. case ACL_USER_OBJ:
  780. ap->ae_perm = (mode >> 6) & 0x7;
  781. break;
  782. case ACL_GROUP_OBJ:
  783. gap = ap;
  784. break;
  785. case ACL_MASK:
  786. nomask = 0;
  787. ap->ae_perm = (mode >> 3) & 0x7;
  788. break;
  789. case ACL_OTHER:
  790. ap->ae_perm = mode & 0x7;
  791. break;
  792. default:
  793. break;
  794. }
  795. }
  796. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  797. if (gap && nomask)
  798. gap->ae_perm = (mode >> 3) & 0x7;
  799. }
  800. /*
  801. * When inheriting an Access ACL from a directory Default ACL,
  802. * the ACL bits are set to the intersection of the ACL default
  803. * permission bits and the file permission bits in mode. If there
  804. * are no permission bits on the file then we must not give them
  805. * the ACL. This is what what makes umask() work with ACLs.
  806. */
  807. STATIC void
  808. xfs_acl_filter_mode(
  809. mode_t mode,
  810. xfs_acl_t *acl)
  811. {
  812. int i, nomask = 1;
  813. xfs_acl_entry_t *ap;
  814. xfs_acl_entry_t *gap = NULL;
  815. /*
  816. * Set ACL entries. POSIX1003.1eD16 requires that the MASK
  817. * be merged with GROUP entry, if there is a MASK.
  818. */
  819. for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
  820. switch (ap->ae_tag) {
  821. case ACL_USER_OBJ:
  822. ap->ae_perm &= (mode >> 6) & 0x7;
  823. break;
  824. case ACL_GROUP_OBJ:
  825. gap = ap;
  826. break;
  827. case ACL_MASK:
  828. nomask = 0;
  829. ap->ae_perm &= (mode >> 3) & 0x7;
  830. break;
  831. case ACL_OTHER:
  832. ap->ae_perm &= mode & 0x7;
  833. break;
  834. default:
  835. break;
  836. }
  837. }
  838. /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
  839. if (gap && nomask)
  840. gap->ae_perm &= (mode >> 3) & 0x7;
  841. }