nfs4acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 "acl.h"
  39. /* mode bit translations: */
  40. #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
  41. #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
  42. #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
  43. #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
  44. #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
  45. /* We don't support these bits; insist they be neither allowed nor denied */
  46. #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
  47. | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
  48. /* flags used to simulate posix default ACLs */
  49. #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
  50. | NFS4_ACE_DIRECTORY_INHERIT_ACE)
  51. #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
  52. | NFS4_ACE_INHERIT_ONLY_ACE \
  53. | NFS4_ACE_IDENTIFIER_GROUP)
  54. #define MASK_EQUAL(mask1, mask2) \
  55. ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
  56. static u32
  57. mask_from_posix(unsigned short perm, unsigned int flags)
  58. {
  59. int mask = NFS4_ANYONE_MODE;
  60. if (flags & NFS4_ACL_OWNER)
  61. mask |= NFS4_OWNER_MODE;
  62. if (perm & ACL_READ)
  63. mask |= NFS4_READ_MODE;
  64. if (perm & ACL_WRITE)
  65. mask |= NFS4_WRITE_MODE;
  66. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  67. mask |= NFS4_ACE_DELETE_CHILD;
  68. if (perm & ACL_EXECUTE)
  69. mask |= NFS4_EXECUTE_MODE;
  70. return mask;
  71. }
  72. static u32
  73. deny_mask_from_posix(unsigned short perm, u32 flags)
  74. {
  75. u32 mask = 0;
  76. if (perm & ACL_READ)
  77. mask |= NFS4_READ_MODE;
  78. if (perm & ACL_WRITE)
  79. mask |= NFS4_WRITE_MODE;
  80. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  81. mask |= NFS4_ACE_DELETE_CHILD;
  82. if (perm & ACL_EXECUTE)
  83. mask |= NFS4_EXECUTE_MODE;
  84. return mask;
  85. }
  86. /* XXX: modify functions to return NFS errors; they're only ever
  87. * used by nfs code, after all.... */
  88. /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
  89. * side of being more restrictive, so the mode bit mapping below is
  90. * pessimistic. An optimistic version would be needed to handle DENY's,
  91. * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
  92. * bits. */
  93. static void
  94. low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
  95. {
  96. u32 write_mode = NFS4_WRITE_MODE;
  97. if (flags & NFS4_ACL_DIR)
  98. write_mode |= NFS4_ACE_DELETE_CHILD;
  99. *mode = 0;
  100. if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
  101. *mode |= ACL_READ;
  102. if ((perm & write_mode) == write_mode)
  103. *mode |= ACL_WRITE;
  104. if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
  105. *mode |= ACL_EXECUTE;
  106. }
  107. struct ace_container {
  108. struct nfs4_ace *ace;
  109. struct list_head ace_l;
  110. };
  111. static short ace2type(struct nfs4_ace *);
  112. static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
  113. unsigned int);
  114. struct nfs4_acl *
  115. nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
  116. unsigned int flags)
  117. {
  118. struct nfs4_acl *acl;
  119. int size = 0;
  120. if (pacl) {
  121. if (posix_acl_valid(pacl) < 0)
  122. return ERR_PTR(-EINVAL);
  123. size += 2*pacl->a_count;
  124. }
  125. if (dpacl) {
  126. if (posix_acl_valid(dpacl) < 0)
  127. return ERR_PTR(-EINVAL);
  128. size += 2*dpacl->a_count;
  129. }
  130. /* Allocate for worst case: one (deny, allow) pair each: */
  131. acl = nfs4_acl_new(size);
  132. if (acl == NULL)
  133. return ERR_PTR(-ENOMEM);
  134. if (pacl)
  135. _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
  136. if (dpacl)
  137. _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
  138. return acl;
  139. }
  140. struct posix_acl_summary {
  141. unsigned short owner;
  142. unsigned short users;
  143. unsigned short group;
  144. unsigned short groups;
  145. unsigned short other;
  146. unsigned short mask;
  147. };
  148. static void
  149. summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
  150. {
  151. struct posix_acl_entry *pa, *pe;
  152. /*
  153. * Only pas.users and pas.groups need initialization; previous
  154. * posix_acl_valid() calls ensure that the other fields will be
  155. * initialized in the following loop. But, just to placate gcc:
  156. */
  157. memset(pas, 0, sizeof(*pas));
  158. pas->mask = 07;
  159. pe = acl->a_entries + acl->a_count;
  160. FOREACH_ACL_ENTRY(pa, acl, pe) {
  161. switch (pa->e_tag) {
  162. case ACL_USER_OBJ:
  163. pas->owner = pa->e_perm;
  164. break;
  165. case ACL_GROUP_OBJ:
  166. pas->group = pa->e_perm;
  167. break;
  168. case ACL_USER:
  169. pas->users |= pa->e_perm;
  170. break;
  171. case ACL_GROUP:
  172. pas->groups |= pa->e_perm;
  173. break;
  174. case ACL_OTHER:
  175. pas->other = pa->e_perm;
  176. break;
  177. case ACL_MASK:
  178. pas->mask = pa->e_perm;
  179. break;
  180. }
  181. }
  182. /* We'll only care about effective permissions: */
  183. pas->users &= pas->mask;
  184. pas->group &= pas->mask;
  185. pas->groups &= pas->mask;
  186. }
  187. /* We assume the acl has been verified with posix_acl_valid. */
  188. static void
  189. _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
  190. unsigned int flags)
  191. {
  192. struct posix_acl_entry *pa, *group_owner_entry;
  193. struct nfs4_ace *ace;
  194. struct posix_acl_summary pas;
  195. unsigned short deny;
  196. int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
  197. NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
  198. BUG_ON(pacl->a_count < 3);
  199. summarize_posix_acl(pacl, &pas);
  200. pa = pacl->a_entries;
  201. ace = acl->aces + acl->naces;
  202. /* We could deny everything not granted by the owner: */
  203. deny = ~pas.owner;
  204. /*
  205. * but it is equivalent (and simpler) to deny only what is not
  206. * granted by later entries:
  207. */
  208. deny &= pas.users | pas.group | pas.groups | pas.other;
  209. if (deny) {
  210. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  211. ace->flag = eflag;
  212. ace->access_mask = deny_mask_from_posix(deny, flags);
  213. ace->whotype = NFS4_ACL_WHO_OWNER;
  214. ace++;
  215. acl->naces++;
  216. }
  217. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  218. ace->flag = eflag;
  219. ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
  220. ace->whotype = NFS4_ACL_WHO_OWNER;
  221. ace++;
  222. acl->naces++;
  223. pa++;
  224. while (pa->e_tag == ACL_USER) {
  225. deny = ~(pa->e_perm & pas.mask);
  226. deny &= pas.groups | pas.group | pas.other;
  227. if (deny) {
  228. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  229. ace->flag = eflag;
  230. ace->access_mask = deny_mask_from_posix(deny, flags);
  231. ace->whotype = NFS4_ACL_WHO_NAMED;
  232. ace->who = pa->e_id;
  233. ace++;
  234. acl->naces++;
  235. }
  236. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  237. ace->flag = eflag;
  238. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  239. flags);
  240. ace->whotype = NFS4_ACL_WHO_NAMED;
  241. ace->who = pa->e_id;
  242. ace++;
  243. acl->naces++;
  244. pa++;
  245. }
  246. /* In the case of groups, we apply allow ACEs first, then deny ACEs,
  247. * since a user can be in more than one group. */
  248. /* allow ACEs */
  249. group_owner_entry = pa;
  250. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  251. ace->flag = eflag;
  252. ace->access_mask = mask_from_posix(pas.group, flags);
  253. ace->whotype = NFS4_ACL_WHO_GROUP;
  254. ace++;
  255. acl->naces++;
  256. pa++;
  257. while (pa->e_tag == ACL_GROUP) {
  258. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  259. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  260. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  261. flags);
  262. ace->whotype = NFS4_ACL_WHO_NAMED;
  263. ace->who = pa->e_id;
  264. ace++;
  265. acl->naces++;
  266. pa++;
  267. }
  268. /* deny ACEs */
  269. pa = group_owner_entry;
  270. deny = ~pas.group & pas.other;
  271. if (deny) {
  272. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  273. ace->flag = eflag;
  274. ace->access_mask = deny_mask_from_posix(deny, flags);
  275. ace->whotype = NFS4_ACL_WHO_GROUP;
  276. ace++;
  277. acl->naces++;
  278. }
  279. pa++;
  280. while (pa->e_tag == ACL_GROUP) {
  281. deny = ~(pa->e_perm & pas.mask);
  282. deny &= pas.other;
  283. if (deny) {
  284. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  285. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  286. ace->access_mask = deny_mask_from_posix(deny, flags);
  287. ace->whotype = NFS4_ACL_WHO_NAMED;
  288. ace->who = pa->e_id;
  289. ace++;
  290. acl->naces++;
  291. }
  292. pa++;
  293. }
  294. if (pa->e_tag == ACL_MASK)
  295. pa++;
  296. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  297. ace->flag = eflag;
  298. ace->access_mask = mask_from_posix(pa->e_perm, flags);
  299. ace->whotype = NFS4_ACL_WHO_EVERYONE;
  300. acl->naces++;
  301. }
  302. static void
  303. sort_pacl_range(struct posix_acl *pacl, int start, int end) {
  304. int sorted = 0, i;
  305. struct posix_acl_entry tmp;
  306. /* We just do a bubble sort; easy to do in place, and we're not
  307. * expecting acl's to be long enough to justify anything more. */
  308. while (!sorted) {
  309. sorted = 1;
  310. for (i = start; i < end; i++) {
  311. if (pacl->a_entries[i].e_id
  312. > pacl->a_entries[i+1].e_id) {
  313. sorted = 0;
  314. tmp = pacl->a_entries[i];
  315. pacl->a_entries[i] = pacl->a_entries[i+1];
  316. pacl->a_entries[i+1] = tmp;
  317. }
  318. }
  319. }
  320. }
  321. static void
  322. sort_pacl(struct posix_acl *pacl)
  323. {
  324. /* posix_acl_valid requires that users and groups be in order
  325. * by uid/gid. */
  326. int i, j;
  327. if (pacl->a_count <= 4)
  328. return; /* no users or groups */
  329. i = 1;
  330. while (pacl->a_entries[i].e_tag == ACL_USER)
  331. i++;
  332. sort_pacl_range(pacl, 1, i-1);
  333. BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
  334. j = ++i;
  335. while (pacl->a_entries[j].e_tag == ACL_GROUP)
  336. j++;
  337. sort_pacl_range(pacl, i, j-1);
  338. return;
  339. }
  340. /*
  341. * While processing the NFSv4 ACE, this maintains bitmasks representing
  342. * which permission bits have been allowed and which denied to a given
  343. * entity: */
  344. struct posix_ace_state {
  345. u32 allow;
  346. u32 deny;
  347. };
  348. struct posix_user_ace_state {
  349. uid_t uid;
  350. struct posix_ace_state perms;
  351. };
  352. struct posix_ace_state_array {
  353. int n;
  354. struct posix_user_ace_state aces[];
  355. };
  356. /*
  357. * While processing the NFSv4 ACE, this maintains the partial permissions
  358. * calculated so far: */
  359. struct posix_acl_state {
  360. int empty;
  361. struct posix_ace_state owner;
  362. struct posix_ace_state group;
  363. struct posix_ace_state other;
  364. struct posix_ace_state everyone;
  365. struct posix_ace_state mask; /* Deny unused in this case */
  366. struct posix_ace_state_array *users;
  367. struct posix_ace_state_array *groups;
  368. };
  369. static int
  370. init_state(struct posix_acl_state *state, int cnt)
  371. {
  372. int alloc;
  373. memset(state, 0, sizeof(struct posix_acl_state));
  374. state->empty = 1;
  375. /*
  376. * In the worst case, each individual acl could be for a distinct
  377. * named user or group, but we don't no which, so we allocate
  378. * enough space for either:
  379. */
  380. alloc = sizeof(struct posix_ace_state_array)
  381. + cnt*sizeof(struct posix_user_ace_state);
  382. state->users = kzalloc(alloc, GFP_KERNEL);
  383. if (!state->users)
  384. return -ENOMEM;
  385. state->groups = kzalloc(alloc, GFP_KERNEL);
  386. if (!state->groups) {
  387. kfree(state->users);
  388. return -ENOMEM;
  389. }
  390. return 0;
  391. }
  392. static void
  393. free_state(struct posix_acl_state *state) {
  394. kfree(state->users);
  395. kfree(state->groups);
  396. }
  397. static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
  398. {
  399. state->mask.allow |= astate->allow;
  400. }
  401. /*
  402. * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
  403. * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
  404. * to traditional read/write/execute permissions.
  405. *
  406. * It's problematic to reject acls that use certain mode bits, because it
  407. * places the burden on users to learn the rules about which bits one
  408. * particular server sets, without giving the user a lot of help--we return an
  409. * error that could mean any number of different things. To make matters
  410. * worse, the problematic bits might be introduced by some application that's
  411. * automatically mapping from some other acl model.
  412. *
  413. * So wherever possible we accept anything, possibly erring on the side of
  414. * denying more permissions than necessary.
  415. *
  416. * However we do reject *explicit* DENY's of a few bits representing
  417. * permissions we could never deny:
  418. */
  419. static inline int check_deny(u32 mask, int isowner)
  420. {
  421. if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
  422. return -EINVAL;
  423. if (!isowner)
  424. return 0;
  425. if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
  426. return -EINVAL;
  427. return 0;
  428. }
  429. static struct posix_acl *
  430. posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
  431. {
  432. struct posix_acl_entry *pace;
  433. struct posix_acl *pacl;
  434. int nace;
  435. int i, error = 0;
  436. /*
  437. * ACLs with no ACEs are treated differently in the inheritable
  438. * and effective cases: when there are no inheritable ACEs, we
  439. * set a zero-length default posix acl:
  440. */
  441. if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
  442. pacl = posix_acl_alloc(0, GFP_KERNEL);
  443. return pacl ? pacl : ERR_PTR(-ENOMEM);
  444. }
  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);