nfs4acl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Common NFSv4 ACL handling code.
  3. *
  4. * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Marius Aamodt Eriksen <marius@umich.edu>
  8. * Jeff Sedlak <jsedlak@umich.edu>
  9. * J. Bruce Fields <bfields@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/slab.h>
  37. #include <linux/nfs_fs.h>
  38. #include <linux/export.h>
  39. #include "acl.h"
  40. /* mode bit translations: */
  41. #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
  42. #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
  43. #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
  44. #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
  45. #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
  46. /* We don't support these bits; insist they be neither allowed nor denied */
  47. #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
  48. | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
  49. /* flags used to simulate posix default ACLs */
  50. #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
  51. | NFS4_ACE_DIRECTORY_INHERIT_ACE)
  52. #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
  53. | NFS4_ACE_INHERIT_ONLY_ACE \
  54. | NFS4_ACE_IDENTIFIER_GROUP)
  55. #define MASK_EQUAL(mask1, mask2) \
  56. ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
  57. static u32
  58. mask_from_posix(unsigned short perm, unsigned int flags)
  59. {
  60. int mask = NFS4_ANYONE_MODE;
  61. if (flags & NFS4_ACL_OWNER)
  62. mask |= NFS4_OWNER_MODE;
  63. if (perm & ACL_READ)
  64. mask |= NFS4_READ_MODE;
  65. if (perm & ACL_WRITE)
  66. mask |= NFS4_WRITE_MODE;
  67. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  68. mask |= NFS4_ACE_DELETE_CHILD;
  69. if (perm & ACL_EXECUTE)
  70. mask |= NFS4_EXECUTE_MODE;
  71. return mask;
  72. }
  73. static u32
  74. deny_mask_from_posix(unsigned short perm, u32 flags)
  75. {
  76. u32 mask = 0;
  77. if (perm & ACL_READ)
  78. mask |= NFS4_READ_MODE;
  79. if (perm & ACL_WRITE)
  80. mask |= NFS4_WRITE_MODE;
  81. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  82. mask |= NFS4_ACE_DELETE_CHILD;
  83. if (perm & ACL_EXECUTE)
  84. mask |= NFS4_EXECUTE_MODE;
  85. return mask;
  86. }
  87. /* XXX: modify functions to return NFS errors; they're only ever
  88. * used by nfs code, after all.... */
  89. /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
  90. * side of being more restrictive, so the mode bit mapping below is
  91. * pessimistic. An optimistic version would be needed to handle DENY's,
  92. * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
  93. * bits. */
  94. static void
  95. low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
  96. {
  97. u32 write_mode = NFS4_WRITE_MODE;
  98. if (flags & NFS4_ACL_DIR)
  99. write_mode |= NFS4_ACE_DELETE_CHILD;
  100. *mode = 0;
  101. if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
  102. *mode |= ACL_READ;
  103. if ((perm & write_mode) == write_mode)
  104. *mode |= ACL_WRITE;
  105. if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
  106. *mode |= ACL_EXECUTE;
  107. }
  108. struct ace_container {
  109. struct nfs4_ace *ace;
  110. struct list_head ace_l;
  111. };
  112. static short ace2type(struct nfs4_ace *);
  113. static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
  114. unsigned int);
  115. struct nfs4_acl *
  116. nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
  117. unsigned int flags)
  118. {
  119. struct nfs4_acl *acl;
  120. int size = 0;
  121. if (pacl) {
  122. if (posix_acl_valid(pacl) < 0)
  123. return ERR_PTR(-EINVAL);
  124. size += 2*pacl->a_count;
  125. }
  126. if (dpacl) {
  127. if (posix_acl_valid(dpacl) < 0)
  128. return ERR_PTR(-EINVAL);
  129. size += 2*dpacl->a_count;
  130. }
  131. /* Allocate for worst case: one (deny, allow) pair each: */
  132. acl = nfs4_acl_new(size);
  133. if (acl == NULL)
  134. return ERR_PTR(-ENOMEM);
  135. if (pacl)
  136. _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
  137. if (dpacl)
  138. _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
  139. return acl;
  140. }
  141. struct posix_acl_summary {
  142. unsigned short owner;
  143. unsigned short users;
  144. unsigned short group;
  145. unsigned short groups;
  146. unsigned short other;
  147. unsigned short mask;
  148. };
  149. static void
  150. summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
  151. {
  152. struct posix_acl_entry *pa, *pe;
  153. /*
  154. * Only pas.users and pas.groups need initialization; previous
  155. * posix_acl_valid() calls ensure that the other fields will be
  156. * initialized in the following loop. But, just to placate gcc:
  157. */
  158. memset(pas, 0, sizeof(*pas));
  159. pas->mask = 07;
  160. pe = acl->a_entries + acl->a_count;
  161. FOREACH_ACL_ENTRY(pa, acl, pe) {
  162. switch (pa->e_tag) {
  163. case ACL_USER_OBJ:
  164. pas->owner = pa->e_perm;
  165. break;
  166. case ACL_GROUP_OBJ:
  167. pas->group = pa->e_perm;
  168. break;
  169. case ACL_USER:
  170. pas->users |= pa->e_perm;
  171. break;
  172. case ACL_GROUP:
  173. pas->groups |= pa->e_perm;
  174. break;
  175. case ACL_OTHER:
  176. pas->other = pa->e_perm;
  177. break;
  178. case ACL_MASK:
  179. pas->mask = pa->e_perm;
  180. break;
  181. }
  182. }
  183. /* We'll only care about effective permissions: */
  184. pas->users &= pas->mask;
  185. pas->group &= pas->mask;
  186. pas->groups &= pas->mask;
  187. }
  188. /* We assume the acl has been verified with posix_acl_valid. */
  189. static void
  190. _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
  191. unsigned int flags)
  192. {
  193. struct posix_acl_entry *pa, *group_owner_entry;
  194. struct nfs4_ace *ace;
  195. struct posix_acl_summary pas;
  196. unsigned short deny;
  197. int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
  198. NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
  199. BUG_ON(pacl->a_count < 3);
  200. summarize_posix_acl(pacl, &pas);
  201. pa = pacl->a_entries;
  202. ace = acl->aces + acl->naces;
  203. /* We could deny everything not granted by the owner: */
  204. deny = ~pas.owner;
  205. /*
  206. * but it is equivalent (and simpler) to deny only what is not
  207. * granted by later entries:
  208. */
  209. deny &= pas.users | pas.group | pas.groups | pas.other;
  210. if (deny) {
  211. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  212. ace->flag = eflag;
  213. ace->access_mask = deny_mask_from_posix(deny, flags);
  214. ace->whotype = NFS4_ACL_WHO_OWNER;
  215. ace++;
  216. acl->naces++;
  217. }
  218. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  219. ace->flag = eflag;
  220. ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
  221. ace->whotype = NFS4_ACL_WHO_OWNER;
  222. ace++;
  223. acl->naces++;
  224. pa++;
  225. while (pa->e_tag == ACL_USER) {
  226. deny = ~(pa->e_perm & pas.mask);
  227. deny &= pas.groups | pas.group | pas.other;
  228. if (deny) {
  229. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  230. ace->flag = eflag;
  231. ace->access_mask = deny_mask_from_posix(deny, flags);
  232. ace->whotype = NFS4_ACL_WHO_NAMED;
  233. ace->who = pa->e_id;
  234. ace++;
  235. acl->naces++;
  236. }
  237. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  238. ace->flag = eflag;
  239. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  240. flags);
  241. ace->whotype = NFS4_ACL_WHO_NAMED;
  242. ace->who = pa->e_id;
  243. ace++;
  244. acl->naces++;
  245. pa++;
  246. }
  247. /* In the case of groups, we apply allow ACEs first, then deny ACEs,
  248. * since a user can be in more than one group. */
  249. /* allow ACEs */
  250. group_owner_entry = pa;
  251. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  252. ace->flag = eflag;
  253. ace->access_mask = mask_from_posix(pas.group, flags);
  254. ace->whotype = NFS4_ACL_WHO_GROUP;
  255. ace++;
  256. acl->naces++;
  257. pa++;
  258. while (pa->e_tag == ACL_GROUP) {
  259. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  260. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  261. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  262. flags);
  263. ace->whotype = NFS4_ACL_WHO_NAMED;
  264. ace->who = pa->e_id;
  265. ace++;
  266. acl->naces++;
  267. pa++;
  268. }
  269. /* deny ACEs */
  270. pa = group_owner_entry;
  271. deny = ~pas.group & pas.other;
  272. if (deny) {
  273. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  274. ace->flag = eflag;
  275. ace->access_mask = deny_mask_from_posix(deny, flags);
  276. ace->whotype = NFS4_ACL_WHO_GROUP;
  277. ace++;
  278. acl->naces++;
  279. }
  280. pa++;
  281. while (pa->e_tag == ACL_GROUP) {
  282. deny = ~(pa->e_perm & pas.mask);
  283. deny &= pas.other;
  284. if (deny) {
  285. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  286. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  287. ace->access_mask = deny_mask_from_posix(deny, flags);
  288. ace->whotype = NFS4_ACL_WHO_NAMED;
  289. ace->who = pa->e_id;
  290. ace++;
  291. acl->naces++;
  292. }
  293. pa++;
  294. }
  295. if (pa->e_tag == ACL_MASK)
  296. pa++;
  297. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  298. ace->flag = eflag;
  299. ace->access_mask = mask_from_posix(pa->e_perm, flags);
  300. ace->whotype = NFS4_ACL_WHO_EVERYONE;
  301. acl->naces++;
  302. }
  303. static void
  304. sort_pacl_range(struct posix_acl *pacl, int start, int end) {
  305. int sorted = 0, i;
  306. struct posix_acl_entry tmp;
  307. /* We just do a bubble sort; easy to do in place, and we're not
  308. * expecting acl's to be long enough to justify anything more. */
  309. while (!sorted) {
  310. sorted = 1;
  311. for (i = start; i < end; i++) {
  312. if (pacl->a_entries[i].e_id
  313. > pacl->a_entries[i+1].e_id) {
  314. sorted = 0;
  315. tmp = pacl->a_entries[i];
  316. pacl->a_entries[i] = pacl->a_entries[i+1];
  317. pacl->a_entries[i+1] = tmp;
  318. }
  319. }
  320. }
  321. }
  322. static void
  323. sort_pacl(struct posix_acl *pacl)
  324. {
  325. /* posix_acl_valid requires that users and groups be in order
  326. * by uid/gid. */
  327. int i, j;
  328. /* no users or groups */
  329. if (!pacl || pacl->a_count <= 4)
  330. return;
  331. i = 1;
  332. while (pacl->a_entries[i].e_tag == ACL_USER)
  333. i++;
  334. sort_pacl_range(pacl, 1, i-1);
  335. BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
  336. j = ++i;
  337. while (pacl->a_entries[j].e_tag == ACL_GROUP)
  338. j++;
  339. sort_pacl_range(pacl, i, j-1);
  340. return;
  341. }
  342. /*
  343. * While processing the NFSv4 ACE, this maintains bitmasks representing
  344. * which permission bits have been allowed and which denied to a given
  345. * entity: */
  346. struct posix_ace_state {
  347. u32 allow;
  348. u32 deny;
  349. };
  350. struct posix_user_ace_state {
  351. uid_t uid;
  352. struct posix_ace_state perms;
  353. };
  354. struct posix_ace_state_array {
  355. int n;
  356. struct posix_user_ace_state aces[];
  357. };
  358. /*
  359. * While processing the NFSv4 ACE, this maintains the partial permissions
  360. * calculated so far: */
  361. struct posix_acl_state {
  362. int empty;
  363. struct posix_ace_state owner;
  364. struct posix_ace_state group;
  365. struct posix_ace_state other;
  366. struct posix_ace_state everyone;
  367. struct posix_ace_state mask; /* Deny unused in this case */
  368. struct posix_ace_state_array *users;
  369. struct posix_ace_state_array *groups;
  370. };
  371. static int
  372. init_state(struct posix_acl_state *state, int cnt)
  373. {
  374. int alloc;
  375. memset(state, 0, sizeof(struct posix_acl_state));
  376. state->empty = 1;
  377. /*
  378. * In the worst case, each individual acl could be for a distinct
  379. * named user or group, but we don't no which, so we allocate
  380. * enough space for either:
  381. */
  382. alloc = sizeof(struct posix_ace_state_array)
  383. + cnt*sizeof(struct posix_user_ace_state);
  384. state->users = kzalloc(alloc, GFP_KERNEL);
  385. if (!state->users)
  386. return -ENOMEM;
  387. state->groups = kzalloc(alloc, GFP_KERNEL);
  388. if (!state->groups) {
  389. kfree(state->users);
  390. return -ENOMEM;
  391. }
  392. return 0;
  393. }
  394. static void
  395. free_state(struct posix_acl_state *state) {
  396. kfree(state->users);
  397. kfree(state->groups);
  398. }
  399. static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
  400. {
  401. state->mask.allow |= astate->allow;
  402. }
  403. /*
  404. * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
  405. * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
  406. * to traditional read/write/execute permissions.
  407. *
  408. * It's problematic to reject acls that use certain mode bits, because it
  409. * places the burden on users to learn the rules about which bits one
  410. * particular server sets, without giving the user a lot of help--we return an
  411. * error that could mean any number of different things. To make matters
  412. * worse, the problematic bits might be introduced by some application that's
  413. * automatically mapping from some other acl model.
  414. *
  415. * So wherever possible we accept anything, possibly erring on the side of
  416. * denying more permissions than necessary.
  417. *
  418. * However we do reject *explicit* DENY's of a few bits representing
  419. * permissions we could never deny:
  420. */
  421. static inline int check_deny(u32 mask, int isowner)
  422. {
  423. if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
  424. return -EINVAL;
  425. if (!isowner)
  426. return 0;
  427. if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
  428. return -EINVAL;
  429. return 0;
  430. }
  431. static struct posix_acl *
  432. posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
  433. {
  434. struct posix_acl_entry *pace;
  435. struct posix_acl *pacl;
  436. int nace;
  437. int i, error = 0;
  438. /*
  439. * ACLs with no ACEs are treated differently in the inheritable
  440. * and effective cases: when there are no inheritable ACEs,
  441. * calls ->set_acl with a NULL ACL structure.
  442. */
  443. if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT))
  444. return NULL;
  445. /*
  446. * When there are no effective ACEs, the following will end
  447. * up setting a 3-element effective posix ACL with all
  448. * permissions zero.
  449. */
  450. nace = 4 + state->users->n + state->groups->n;
  451. pacl = posix_acl_alloc(nace, GFP_KERNEL);
  452. if (!pacl)
  453. return ERR_PTR(-ENOMEM);
  454. pace = pacl->a_entries;
  455. pace->e_tag = ACL_USER_OBJ;
  456. error = check_deny(state->owner.deny, 1);
  457. if (error)
  458. goto out_err;
  459. low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
  460. pace->e_id = ACL_UNDEFINED_ID;
  461. for (i=0; i < state->users->n; i++) {
  462. pace++;
  463. pace->e_tag = ACL_USER;
  464. error = check_deny(state->users->aces[i].perms.deny, 0);
  465. if (error)
  466. goto out_err;
  467. low_mode_from_nfs4(state->users->aces[i].perms.allow,
  468. &pace->e_perm, flags);
  469. pace->e_id = state->users->aces[i].uid;
  470. add_to_mask(state, &state->users->aces[i].perms);
  471. }
  472. pace++;
  473. pace->e_tag = ACL_GROUP_OBJ;
  474. error = check_deny(state->group.deny, 0);
  475. if (error)
  476. goto out_err;
  477. low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
  478. pace->e_id = ACL_UNDEFINED_ID;
  479. add_to_mask(state, &state->group);
  480. for (i=0; i < state->groups->n; i++) {
  481. pace++;
  482. pace->e_tag = ACL_GROUP;
  483. error = check_deny(state->groups->aces[i].perms.deny, 0);
  484. if (error)
  485. goto out_err;
  486. low_mode_from_nfs4(state->groups->aces[i].perms.allow,
  487. &pace->e_perm, flags);
  488. pace->e_id = state->groups->aces[i].uid;
  489. add_to_mask(state, &state->groups->aces[i].perms);
  490. }
  491. pace++;
  492. pace->e_tag = ACL_MASK;
  493. low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
  494. pace->e_id = ACL_UNDEFINED_ID;
  495. pace++;
  496. pace->e_tag = ACL_OTHER;
  497. error = check_deny(state->other.deny, 0);
  498. if (error)
  499. goto out_err;
  500. low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
  501. pace->e_id = ACL_UNDEFINED_ID;
  502. return pacl;
  503. out_err:
  504. posix_acl_release(pacl);
  505. return ERR_PTR(error);
  506. }
  507. static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
  508. {
  509. /* Allow all bits in the mask not already denied: */
  510. astate->allow |= mask & ~astate->deny;
  511. }
  512. static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
  513. {
  514. /* Deny all bits in the mask not already allowed: */
  515. astate->deny |= mask & ~astate->allow;
  516. }
  517. static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
  518. {
  519. int i;
  520. for (i = 0; i < a->n; i++)
  521. if (a->aces[i].uid == uid)
  522. return i;
  523. /* Not found: */
  524. a->n++;
  525. a->aces[i].uid = uid;
  526. a->aces[i].perms.allow = state->everyone.allow;
  527. a->aces[i].perms.deny = state->everyone.deny;
  528. return i;
  529. }
  530. static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
  531. {
  532. int i;
  533. for (i=0; i < a->n; i++)
  534. deny_bits(&a->aces[i].perms, mask);
  535. }
  536. static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
  537. {
  538. int i;
  539. for (i=0; i < a->n; i++)
  540. allow_bits(&a->aces[i].perms, mask);
  541. }
  542. static void process_one_v4_ace(struct posix_acl_state *state,
  543. struct nfs4_ace *ace)
  544. {
  545. u32 mask = ace->access_mask;
  546. int i;
  547. state->empty = 0;
  548. switch (ace2type(ace)) {
  549. case ACL_USER_OBJ:
  550. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  551. allow_bits(&state->owner, mask);
  552. } else {
  553. deny_bits(&state->owner, mask);
  554. }
  555. break;
  556. case ACL_USER:
  557. i = find_uid(state, state->users, ace->who);
  558. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  559. allow_bits(&state->users->aces[i].perms, mask);
  560. } else {
  561. deny_bits(&state->users->aces[i].perms, mask);
  562. mask = state->users->aces[i].perms.deny;
  563. deny_bits(&state->owner, mask);
  564. }
  565. break;
  566. case ACL_GROUP_OBJ:
  567. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  568. allow_bits(&state->group, mask);
  569. } else {
  570. deny_bits(&state->group, mask);
  571. mask = state->group.deny;
  572. deny_bits(&state->owner, mask);
  573. deny_bits(&state->everyone, mask);
  574. deny_bits_array(state->users, mask);
  575. deny_bits_array(state->groups, mask);
  576. }
  577. break;
  578. case ACL_GROUP:
  579. i = find_uid(state, state->groups, ace->who);
  580. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  581. allow_bits(&state->groups->aces[i].perms, mask);
  582. } else {
  583. deny_bits(&state->groups->aces[i].perms, mask);
  584. mask = state->groups->aces[i].perms.deny;
  585. deny_bits(&state->owner, mask);
  586. deny_bits(&state->group, mask);
  587. deny_bits(&state->everyone, mask);
  588. deny_bits_array(state->users, mask);
  589. deny_bits_array(state->groups, mask);
  590. }
  591. break;
  592. case ACL_OTHER:
  593. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  594. allow_bits(&state->owner, mask);
  595. allow_bits(&state->group, mask);
  596. allow_bits(&state->other, mask);
  597. allow_bits(&state->everyone, mask);
  598. allow_bits_array(state->users, mask);
  599. allow_bits_array(state->groups, mask);
  600. } else {
  601. deny_bits(&state->owner, mask);
  602. deny_bits(&state->group, mask);
  603. deny_bits(&state->other, mask);
  604. deny_bits(&state->everyone, mask);
  605. deny_bits_array(state->users, mask);
  606. deny_bits_array(state->groups, mask);
  607. }
  608. }
  609. }
  610. int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
  611. struct posix_acl **dpacl, unsigned int flags)
  612. {
  613. struct posix_acl_state effective_acl_state, default_acl_state;
  614. struct nfs4_ace *ace;
  615. int ret;
  616. ret = init_state(&effective_acl_state, acl->naces);
  617. if (ret)
  618. return ret;
  619. ret = init_state(&default_acl_state, acl->naces);
  620. if (ret)
  621. goto out_estate;
  622. ret = -EINVAL;
  623. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  624. if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
  625. ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
  626. goto out_dstate;
  627. if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
  628. goto out_dstate;
  629. if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
  630. process_one_v4_ace(&effective_acl_state, ace);
  631. continue;
  632. }
  633. if (!(flags & NFS4_ACL_DIR))
  634. goto out_dstate;
  635. /*
  636. * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
  637. * is set, we're effectively turning on the other. That's OK,
  638. * according to rfc 3530.
  639. */
  640. process_one_v4_ace(&default_acl_state, ace);
  641. if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
  642. process_one_v4_ace(&effective_acl_state, ace);
  643. }
  644. *pacl = posix_state_to_acl(&effective_acl_state, flags);
  645. if (IS_ERR(*pacl)) {
  646. ret = PTR_ERR(*pacl);
  647. *pacl = NULL;
  648. goto out_dstate;
  649. }
  650. *dpacl = posix_state_to_acl(&default_acl_state,
  651. flags | NFS4_ACL_TYPE_DEFAULT);
  652. if (IS_ERR(*dpacl)) {
  653. ret = PTR_ERR(*dpacl);
  654. *dpacl = NULL;
  655. posix_acl_release(*pacl);
  656. *pacl = NULL;
  657. goto out_dstate;
  658. }
  659. sort_pacl(*pacl);
  660. sort_pacl(*dpacl);
  661. ret = 0;
  662. out_dstate:
  663. free_state(&default_acl_state);
  664. out_estate:
  665. free_state(&effective_acl_state);
  666. return ret;
  667. }
  668. static short
  669. ace2type(struct nfs4_ace *ace)
  670. {
  671. switch (ace->whotype) {
  672. case NFS4_ACL_WHO_NAMED:
  673. return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
  674. ACL_GROUP : ACL_USER);
  675. case NFS4_ACL_WHO_OWNER:
  676. return ACL_USER_OBJ;
  677. case NFS4_ACL_WHO_GROUP:
  678. return ACL_GROUP_OBJ;
  679. case NFS4_ACL_WHO_EVERYONE:
  680. return ACL_OTHER;
  681. }
  682. BUG();
  683. return -1;
  684. }
  685. EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
  686. EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
  687. struct nfs4_acl *
  688. nfs4_acl_new(int n)
  689. {
  690. struct nfs4_acl *acl;
  691. acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
  692. if (acl == NULL)
  693. return NULL;
  694. acl->naces = 0;
  695. return acl;
  696. }
  697. static struct {
  698. char *string;
  699. int stringlen;
  700. int type;
  701. } s2t_map[] = {
  702. {
  703. .string = "OWNER@",
  704. .stringlen = sizeof("OWNER@") - 1,
  705. .type = NFS4_ACL_WHO_OWNER,
  706. },
  707. {
  708. .string = "GROUP@",
  709. .stringlen = sizeof("GROUP@") - 1,
  710. .type = NFS4_ACL_WHO_GROUP,
  711. },
  712. {
  713. .string = "EVERYONE@",
  714. .stringlen = sizeof("EVERYONE@") - 1,
  715. .type = NFS4_ACL_WHO_EVERYONE,
  716. },
  717. };
  718. int
  719. nfs4_acl_get_whotype(char *p, u32 len)
  720. {
  721. int i;
  722. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  723. if (s2t_map[i].stringlen == len &&
  724. 0 == memcmp(s2t_map[i].string, p, len))
  725. return s2t_map[i].type;
  726. }
  727. return NFS4_ACL_WHO_NAMED;
  728. }
  729. int
  730. nfs4_acl_write_who(int who, char *p)
  731. {
  732. int i;
  733. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  734. if (s2t_map[i].type == who) {
  735. memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
  736. return s2t_map[i].stringlen;
  737. }
  738. }
  739. BUG();
  740. return -1;
  741. }
  742. EXPORT_SYMBOL(nfs4_acl_new);
  743. EXPORT_SYMBOL(nfs4_acl_get_whotype);
  744. EXPORT_SYMBOL(nfs4_acl_write_who);