nfs4acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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/fs.h>
  37. #include <linux/slab.h>
  38. #include <linux/posix_acl.h>
  39. #include "nfsfh.h"
  40. #include "nfsd.h"
  41. #include "acl.h"
  42. #include "vfs.h"
  43. #define NFS4_ACL_TYPE_DEFAULT 0x01
  44. #define NFS4_ACL_DIR 0x02
  45. #define NFS4_ACL_OWNER 0x04
  46. /* mode bit translations: */
  47. #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
  48. #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
  49. #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
  50. #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
  51. #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
  52. /* flags used to simulate posix default ACLs */
  53. #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
  54. | NFS4_ACE_DIRECTORY_INHERIT_ACE)
  55. #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
  56. | NFS4_ACE_INHERIT_ONLY_ACE \
  57. | NFS4_ACE_IDENTIFIER_GROUP)
  58. static u32
  59. mask_from_posix(unsigned short perm, unsigned int flags)
  60. {
  61. int mask = NFS4_ANYONE_MODE;
  62. if (flags & NFS4_ACL_OWNER)
  63. mask |= NFS4_OWNER_MODE;
  64. if (perm & ACL_READ)
  65. mask |= NFS4_READ_MODE;
  66. if (perm & ACL_WRITE)
  67. mask |= NFS4_WRITE_MODE;
  68. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  69. mask |= NFS4_ACE_DELETE_CHILD;
  70. if (perm & ACL_EXECUTE)
  71. mask |= NFS4_EXECUTE_MODE;
  72. return mask;
  73. }
  74. static u32
  75. deny_mask_from_posix(unsigned short perm, u32 flags)
  76. {
  77. u32 mask = 0;
  78. if (perm & ACL_READ)
  79. mask |= NFS4_READ_MODE;
  80. if (perm & ACL_WRITE)
  81. mask |= NFS4_WRITE_MODE;
  82. if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
  83. mask |= NFS4_ACE_DELETE_CHILD;
  84. if (perm & ACL_EXECUTE)
  85. mask |= NFS4_EXECUTE_MODE;
  86. return mask;
  87. }
  88. /* XXX: modify functions to return NFS errors; they're only ever
  89. * used by nfs code, after all.... */
  90. /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
  91. * side of being more restrictive, so the mode bit mapping below is
  92. * pessimistic. An optimistic version would be needed to handle DENY's,
  93. * but we expect to coalesce all ALLOWs and DENYs before mapping to mode
  94. * bits. */
  95. static void
  96. low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
  97. {
  98. u32 write_mode = NFS4_WRITE_MODE;
  99. if (flags & NFS4_ACL_DIR)
  100. write_mode |= NFS4_ACE_DELETE_CHILD;
  101. *mode = 0;
  102. if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
  103. *mode |= ACL_READ;
  104. if ((perm & write_mode) == write_mode)
  105. *mode |= ACL_WRITE;
  106. if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
  107. *mode |= ACL_EXECUTE;
  108. }
  109. static short ace2type(struct nfs4_ace *);
  110. static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
  111. unsigned int);
  112. int
  113. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
  114. struct nfs4_acl **acl)
  115. {
  116. struct inode *inode = d_inode(dentry);
  117. int error = 0;
  118. struct posix_acl *pacl = NULL, *dpacl = NULL;
  119. unsigned int flags = 0;
  120. int size = 0;
  121. pacl = get_inode_acl(inode, ACL_TYPE_ACCESS);
  122. if (!pacl)
  123. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  124. if (IS_ERR(pacl))
  125. return PTR_ERR(pacl);
  126. /* allocate for worst case: one (deny, allow) pair each: */
  127. size += 2 * pacl->a_count;
  128. if (S_ISDIR(inode->i_mode)) {
  129. flags = NFS4_ACL_DIR;
  130. dpacl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
  131. if (IS_ERR(dpacl)) {
  132. error = PTR_ERR(dpacl);
  133. goto rel_pacl;
  134. }
  135. if (dpacl)
  136. size += 2 * dpacl->a_count;
  137. }
  138. *acl = kmalloc(nfs4_acl_bytes(size), GFP_KERNEL);
  139. if (*acl == NULL) {
  140. error = -ENOMEM;
  141. goto out;
  142. }
  143. (*acl)->naces = 0;
  144. _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
  145. if (dpacl)
  146. _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
  147. out:
  148. posix_acl_release(dpacl);
  149. rel_pacl:
  150. posix_acl_release(pacl);
  151. return error;
  152. }
  153. struct posix_acl_summary {
  154. unsigned short owner;
  155. unsigned short users;
  156. unsigned short group;
  157. unsigned short groups;
  158. unsigned short other;
  159. unsigned short mask;
  160. };
  161. static void
  162. summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
  163. {
  164. struct posix_acl_entry *pa, *pe;
  165. /*
  166. * Only pas.users and pas.groups need initialization; previous
  167. * posix_acl_valid() calls ensure that the other fields will be
  168. * initialized in the following loop. But, just to placate gcc:
  169. */
  170. memset(pas, 0, sizeof(*pas));
  171. pas->mask = 07;
  172. pe = acl->a_entries + acl->a_count;
  173. FOREACH_ACL_ENTRY(pa, acl, pe) {
  174. switch (pa->e_tag) {
  175. case ACL_USER_OBJ:
  176. pas->owner = pa->e_perm;
  177. break;
  178. case ACL_GROUP_OBJ:
  179. pas->group = pa->e_perm;
  180. break;
  181. case ACL_USER:
  182. pas->users |= pa->e_perm;
  183. break;
  184. case ACL_GROUP:
  185. pas->groups |= pa->e_perm;
  186. break;
  187. case ACL_OTHER:
  188. pas->other = pa->e_perm;
  189. break;
  190. case ACL_MASK:
  191. pas->mask = pa->e_perm;
  192. break;
  193. }
  194. }
  195. /* We'll only care about effective permissions: */
  196. pas->users &= pas->mask;
  197. pas->group &= pas->mask;
  198. pas->groups &= pas->mask;
  199. }
  200. /* We assume the acl has been verified with posix_acl_valid. */
  201. static void
  202. _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
  203. unsigned int flags)
  204. {
  205. struct posix_acl_entry *pa, *group_owner_entry;
  206. struct nfs4_ace *ace;
  207. struct posix_acl_summary pas;
  208. unsigned short deny;
  209. int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
  210. NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
  211. BUG_ON(pacl->a_count < 3);
  212. summarize_posix_acl(pacl, &pas);
  213. pa = pacl->a_entries;
  214. ace = acl->aces + acl->naces;
  215. /* We could deny everything not granted by the owner: */
  216. deny = ~pas.owner;
  217. /*
  218. * but it is equivalent (and simpler) to deny only what is not
  219. * granted by later entries:
  220. */
  221. deny &= pas.users | pas.group | pas.groups | pas.other;
  222. if (deny) {
  223. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  224. ace->flag = eflag;
  225. ace->access_mask = deny_mask_from_posix(deny, flags);
  226. ace->whotype = NFS4_ACL_WHO_OWNER;
  227. ace++;
  228. acl->naces++;
  229. }
  230. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  231. ace->flag = eflag;
  232. ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
  233. ace->whotype = NFS4_ACL_WHO_OWNER;
  234. ace++;
  235. acl->naces++;
  236. pa++;
  237. while (pa->e_tag == ACL_USER) {
  238. deny = ~(pa->e_perm & pas.mask);
  239. deny &= pas.groups | pas.group | pas.other;
  240. if (deny) {
  241. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  242. ace->flag = eflag;
  243. ace->access_mask = deny_mask_from_posix(deny, flags);
  244. ace->whotype = NFS4_ACL_WHO_NAMED;
  245. ace->who_uid = pa->e_uid;
  246. ace++;
  247. acl->naces++;
  248. }
  249. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  250. ace->flag = eflag;
  251. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  252. flags);
  253. ace->whotype = NFS4_ACL_WHO_NAMED;
  254. ace->who_uid = pa->e_uid;
  255. ace++;
  256. acl->naces++;
  257. pa++;
  258. }
  259. /* In the case of groups, we apply allow ACEs first, then deny ACEs,
  260. * since a user can be in more than one group. */
  261. /* allow ACEs */
  262. group_owner_entry = pa;
  263. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  264. ace->flag = eflag;
  265. ace->access_mask = mask_from_posix(pas.group, flags);
  266. ace->whotype = NFS4_ACL_WHO_GROUP;
  267. ace++;
  268. acl->naces++;
  269. pa++;
  270. while (pa->e_tag == ACL_GROUP) {
  271. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  272. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  273. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  274. flags);
  275. ace->whotype = NFS4_ACL_WHO_NAMED;
  276. ace->who_gid = pa->e_gid;
  277. ace++;
  278. acl->naces++;
  279. pa++;
  280. }
  281. /* deny ACEs */
  282. pa = group_owner_entry;
  283. deny = ~pas.group & pas.other;
  284. if (deny) {
  285. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  286. ace->flag = eflag;
  287. ace->access_mask = deny_mask_from_posix(deny, flags);
  288. ace->whotype = NFS4_ACL_WHO_GROUP;
  289. ace++;
  290. acl->naces++;
  291. }
  292. pa++;
  293. while (pa->e_tag == ACL_GROUP) {
  294. deny = ~(pa->e_perm & pas.mask);
  295. deny &= pas.other;
  296. if (deny) {
  297. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  298. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  299. ace->access_mask = deny_mask_from_posix(deny, flags);
  300. ace->whotype = NFS4_ACL_WHO_NAMED;
  301. ace->who_gid = pa->e_gid;
  302. ace++;
  303. acl->naces++;
  304. }
  305. pa++;
  306. }
  307. if (pa->e_tag == ACL_MASK)
  308. pa++;
  309. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  310. ace->flag = eflag;
  311. ace->access_mask = mask_from_posix(pa->e_perm, flags);
  312. ace->whotype = NFS4_ACL_WHO_EVERYONE;
  313. acl->naces++;
  314. }
  315. static bool
  316. pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
  317. {
  318. if (pace1->e_tag != pace2->e_tag)
  319. return pace1->e_tag > pace2->e_tag;
  320. if (pace1->e_tag == ACL_USER)
  321. return uid_gt(pace1->e_uid, pace2->e_uid);
  322. if (pace1->e_tag == ACL_GROUP)
  323. return gid_gt(pace1->e_gid, pace2->e_gid);
  324. return false;
  325. }
  326. static void
  327. sort_pacl_range(struct posix_acl *pacl, int start, int end) {
  328. int sorted = 0, i;
  329. /* We just do a bubble sort; easy to do in place, and we're not
  330. * expecting acl's to be long enough to justify anything more. */
  331. while (!sorted) {
  332. sorted = 1;
  333. for (i = start; i < end; i++) {
  334. if (pace_gt(&pacl->a_entries[i],
  335. &pacl->a_entries[i+1])) {
  336. sorted = 0;
  337. swap(pacl->a_entries[i],
  338. pacl->a_entries[i + 1]);
  339. }
  340. }
  341. }
  342. }
  343. static void
  344. sort_pacl(struct posix_acl *pacl)
  345. {
  346. /* posix_acl_valid requires that users and groups be in order
  347. * by uid/gid. */
  348. int i, j;
  349. /* no users or groups */
  350. if (!pacl || pacl->a_count <= 4)
  351. return;
  352. i = 1;
  353. while (pacl->a_entries[i].e_tag == ACL_USER)
  354. i++;
  355. sort_pacl_range(pacl, 1, i-1);
  356. BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
  357. j = ++i;
  358. while (pacl->a_entries[j].e_tag == ACL_GROUP)
  359. j++;
  360. sort_pacl_range(pacl, i, j-1);
  361. return;
  362. }
  363. /*
  364. * While processing the NFSv4 ACE, this maintains bitmasks representing
  365. * which permission bits have been allowed and which denied to a given
  366. * entity: */
  367. struct posix_ace_state {
  368. u32 allow;
  369. u32 deny;
  370. };
  371. struct posix_user_ace_state {
  372. union {
  373. kuid_t uid;
  374. kgid_t gid;
  375. };
  376. struct posix_ace_state perms;
  377. };
  378. struct posix_ace_state_array {
  379. int n;
  380. struct posix_user_ace_state aces[];
  381. };
  382. /*
  383. * While processing the NFSv4 ACE, this maintains the partial permissions
  384. * calculated so far: */
  385. struct posix_acl_state {
  386. unsigned char valid;
  387. struct posix_ace_state owner;
  388. struct posix_ace_state group;
  389. struct posix_ace_state other;
  390. struct posix_ace_state everyone;
  391. struct posix_ace_state mask; /* Deny unused in this case */
  392. struct posix_ace_state_array *users;
  393. struct posix_ace_state_array *groups;
  394. };
  395. static int
  396. init_state(struct posix_acl_state *state, int cnt)
  397. {
  398. int alloc;
  399. memset(state, 0, sizeof(struct posix_acl_state));
  400. /*
  401. * In the worst case, each individual acl could be for a distinct
  402. * named user or group, but we don't know which, so we allocate
  403. * enough space for either:
  404. */
  405. alloc = sizeof(struct posix_ace_state_array)
  406. + cnt*sizeof(struct posix_user_ace_state);
  407. state->users = kzalloc(alloc, GFP_KERNEL);
  408. if (!state->users)
  409. return -ENOMEM;
  410. state->groups = kzalloc(alloc, GFP_KERNEL);
  411. if (!state->groups) {
  412. kfree(state->users);
  413. return -ENOMEM;
  414. }
  415. return 0;
  416. }
  417. static void
  418. free_state(struct posix_acl_state *state) {
  419. kfree(state->users);
  420. kfree(state->groups);
  421. }
  422. static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
  423. {
  424. state->mask.allow |= astate->allow;
  425. }
  426. static struct posix_acl *
  427. posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
  428. {
  429. struct posix_acl_entry *pace;
  430. struct posix_acl *pacl;
  431. int nace;
  432. int i;
  433. /*
  434. * ACLs with no ACEs are treated differently in the inheritable
  435. * and effective cases: when there are no inheritable ACEs,
  436. * calls ->set_acl with a NULL ACL structure.
  437. */
  438. if (!state->valid && (flags & NFS4_ACL_TYPE_DEFAULT))
  439. return NULL;
  440. /*
  441. * When there are no effective ACEs, the following will end
  442. * up setting a 3-element effective posix ACL with all
  443. * permissions zero.
  444. */
  445. if (!state->users->n && !state->groups->n)
  446. nace = 3;
  447. else /* Note we also include a MASK ACE in this case: */
  448. nace = 4 + state->users->n + state->groups->n;
  449. pacl = posix_acl_alloc(nace, GFP_KERNEL);
  450. if (!pacl)
  451. return ERR_PTR(-ENOMEM);
  452. pace = pacl->a_entries;
  453. pace->e_tag = ACL_USER_OBJ;
  454. low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
  455. for (i=0; i < state->users->n; i++) {
  456. pace++;
  457. pace->e_tag = ACL_USER;
  458. low_mode_from_nfs4(state->users->aces[i].perms.allow,
  459. &pace->e_perm, flags);
  460. pace->e_uid = state->users->aces[i].uid;
  461. add_to_mask(state, &state->users->aces[i].perms);
  462. }
  463. pace++;
  464. pace->e_tag = ACL_GROUP_OBJ;
  465. low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
  466. add_to_mask(state, &state->group);
  467. for (i=0; i < state->groups->n; i++) {
  468. pace++;
  469. pace->e_tag = ACL_GROUP;
  470. low_mode_from_nfs4(state->groups->aces[i].perms.allow,
  471. &pace->e_perm, flags);
  472. pace->e_gid = state->groups->aces[i].gid;
  473. add_to_mask(state, &state->groups->aces[i].perms);
  474. }
  475. if (state->users->n || state->groups->n) {
  476. pace++;
  477. pace->e_tag = ACL_MASK;
  478. low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
  479. }
  480. pace++;
  481. pace->e_tag = ACL_OTHER;
  482. low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
  483. return pacl;
  484. }
  485. static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
  486. {
  487. /* Allow all bits in the mask not already denied: */
  488. astate->allow |= mask & ~astate->deny;
  489. }
  490. static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
  491. {
  492. /* Deny all bits in the mask not already allowed: */
  493. astate->deny |= mask & ~astate->allow;
  494. }
  495. static int find_uid(struct posix_acl_state *state, kuid_t uid)
  496. {
  497. struct posix_ace_state_array *a = state->users;
  498. int i;
  499. for (i = 0; i < a->n; i++)
  500. if (uid_eq(a->aces[i].uid, uid))
  501. return i;
  502. /* Not found: */
  503. a->n++;
  504. a->aces[i].uid = uid;
  505. a->aces[i].perms.allow = state->everyone.allow;
  506. a->aces[i].perms.deny = state->everyone.deny;
  507. return i;
  508. }
  509. static int find_gid(struct posix_acl_state *state, kgid_t gid)
  510. {
  511. struct posix_ace_state_array *a = state->groups;
  512. int i;
  513. for (i = 0; i < a->n; i++)
  514. if (gid_eq(a->aces[i].gid, gid))
  515. return i;
  516. /* Not found: */
  517. a->n++;
  518. a->aces[i].gid = gid;
  519. a->aces[i].perms.allow = state->everyone.allow;
  520. a->aces[i].perms.deny = state->everyone.deny;
  521. return i;
  522. }
  523. static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
  524. {
  525. int i;
  526. for (i=0; i < a->n; i++)
  527. deny_bits(&a->aces[i].perms, mask);
  528. }
  529. static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
  530. {
  531. int i;
  532. for (i=0; i < a->n; i++)
  533. allow_bits(&a->aces[i].perms, mask);
  534. }
  535. static void process_one_v4_ace(struct posix_acl_state *state,
  536. struct nfs4_ace *ace)
  537. {
  538. u32 mask = ace->access_mask;
  539. short type = ace2type(ace);
  540. int i;
  541. state->valid |= type;
  542. switch (type) {
  543. case ACL_USER_OBJ:
  544. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  545. allow_bits(&state->owner, mask);
  546. } else {
  547. deny_bits(&state->owner, mask);
  548. }
  549. break;
  550. case ACL_USER:
  551. i = find_uid(state, ace->who_uid);
  552. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  553. allow_bits(&state->users->aces[i].perms, mask);
  554. } else {
  555. deny_bits(&state->users->aces[i].perms, mask);
  556. mask = state->users->aces[i].perms.deny;
  557. deny_bits(&state->owner, mask);
  558. }
  559. break;
  560. case ACL_GROUP_OBJ:
  561. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  562. allow_bits(&state->group, mask);
  563. } else {
  564. deny_bits(&state->group, mask);
  565. mask = state->group.deny;
  566. deny_bits(&state->owner, mask);
  567. deny_bits(&state->everyone, mask);
  568. deny_bits_array(state->users, mask);
  569. deny_bits_array(state->groups, mask);
  570. }
  571. break;
  572. case ACL_GROUP:
  573. i = find_gid(state, ace->who_gid);
  574. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  575. allow_bits(&state->groups->aces[i].perms, mask);
  576. } else {
  577. deny_bits(&state->groups->aces[i].perms, mask);
  578. mask = state->groups->aces[i].perms.deny;
  579. deny_bits(&state->owner, mask);
  580. deny_bits(&state->group, mask);
  581. deny_bits(&state->everyone, mask);
  582. deny_bits_array(state->users, mask);
  583. deny_bits_array(state->groups, mask);
  584. }
  585. break;
  586. case ACL_OTHER:
  587. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  588. allow_bits(&state->owner, mask);
  589. allow_bits(&state->group, mask);
  590. allow_bits(&state->other, mask);
  591. allow_bits(&state->everyone, mask);
  592. allow_bits_array(state->users, mask);
  593. allow_bits_array(state->groups, mask);
  594. } else {
  595. deny_bits(&state->owner, mask);
  596. deny_bits(&state->group, mask);
  597. deny_bits(&state->other, mask);
  598. deny_bits(&state->everyone, mask);
  599. deny_bits_array(state->users, mask);
  600. deny_bits_array(state->groups, mask);
  601. }
  602. }
  603. }
  604. static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
  605. struct posix_acl **pacl, struct posix_acl **dpacl,
  606. unsigned int flags)
  607. {
  608. struct posix_acl_state effective_acl_state, default_acl_state;
  609. struct nfs4_ace *ace;
  610. int ret;
  611. ret = init_state(&effective_acl_state, acl->naces);
  612. if (ret)
  613. return ret;
  614. ret = init_state(&default_acl_state, acl->naces);
  615. if (ret)
  616. goto out_estate;
  617. ret = -EINVAL;
  618. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  619. if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
  620. ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
  621. goto out_dstate;
  622. if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
  623. goto out_dstate;
  624. if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
  625. process_one_v4_ace(&effective_acl_state, ace);
  626. continue;
  627. }
  628. if (!(flags & NFS4_ACL_DIR))
  629. goto out_dstate;
  630. /*
  631. * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
  632. * is set, we're effectively turning on the other. That's OK,
  633. * according to rfc 3530.
  634. */
  635. process_one_v4_ace(&default_acl_state, ace);
  636. if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
  637. process_one_v4_ace(&effective_acl_state, ace);
  638. }
  639. /*
  640. * At this point, the default ACL may have zeroed-out entries for owner,
  641. * group and other. That usually results in a non-sensical resulting ACL
  642. * that denies all access except to any ACE that was explicitly added.
  643. *
  644. * The setfacl command solves a similar problem with this logic:
  645. *
  646. * "If a Default ACL entry is created, and the Default ACL contains
  647. * no owner, owning group, or others entry, a copy of the ACL
  648. * owner, owning group, or others entry is added to the Default ACL."
  649. *
  650. * Copy any missing ACEs from the effective set, if any ACEs were
  651. * explicitly set.
  652. */
  653. if (default_acl_state.valid) {
  654. if (!(default_acl_state.valid & ACL_USER_OBJ))
  655. default_acl_state.owner = effective_acl_state.owner;
  656. if (!(default_acl_state.valid & ACL_GROUP_OBJ))
  657. default_acl_state.group = effective_acl_state.group;
  658. if (!(default_acl_state.valid & ACL_OTHER))
  659. default_acl_state.other = effective_acl_state.other;
  660. }
  661. *pacl = posix_state_to_acl(&effective_acl_state, flags);
  662. if (IS_ERR(*pacl)) {
  663. ret = PTR_ERR(*pacl);
  664. *pacl = NULL;
  665. goto out_dstate;
  666. }
  667. *dpacl = posix_state_to_acl(&default_acl_state,
  668. flags | NFS4_ACL_TYPE_DEFAULT);
  669. if (IS_ERR(*dpacl)) {
  670. ret = PTR_ERR(*dpacl);
  671. *dpacl = NULL;
  672. posix_acl_release(*pacl);
  673. *pacl = NULL;
  674. goto out_dstate;
  675. }
  676. sort_pacl(*pacl);
  677. sort_pacl(*dpacl);
  678. ret = 0;
  679. out_dstate:
  680. free_state(&default_acl_state);
  681. out_estate:
  682. free_state(&effective_acl_state);
  683. return ret;
  684. }
  685. __be32 nfsd4_acl_to_attr(enum nfs_ftype4 type, struct nfs4_acl *acl,
  686. struct nfsd_attrs *attr)
  687. {
  688. int host_error;
  689. unsigned int flags = 0;
  690. if (!acl)
  691. return nfs_ok;
  692. if (type == NF4DIR)
  693. flags = NFS4_ACL_DIR;
  694. host_error = nfs4_acl_nfsv4_to_posix(acl, &attr->na_pacl,
  695. &attr->na_dpacl, flags);
  696. if (host_error == -EINVAL)
  697. return nfserr_attrnotsupp;
  698. else
  699. return nfserrno(host_error);
  700. }
  701. static short
  702. ace2type(struct nfs4_ace *ace)
  703. {
  704. switch (ace->whotype) {
  705. case NFS4_ACL_WHO_NAMED:
  706. return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
  707. ACL_GROUP : ACL_USER);
  708. case NFS4_ACL_WHO_OWNER:
  709. return ACL_USER_OBJ;
  710. case NFS4_ACL_WHO_GROUP:
  711. return ACL_GROUP_OBJ;
  712. case NFS4_ACL_WHO_EVERYONE:
  713. return ACL_OTHER;
  714. }
  715. BUG();
  716. return -1;
  717. }
  718. /*
  719. * return the size of the struct nfs4_acl required to represent an acl
  720. * with @entries entries.
  721. */
  722. int nfs4_acl_bytes(int entries)
  723. {
  724. return sizeof(struct nfs4_acl) + entries * sizeof(struct nfs4_ace);
  725. }
  726. static struct {
  727. char *string;
  728. int stringlen;
  729. int type;
  730. } s2t_map[] = {
  731. {
  732. .string = "OWNER@",
  733. .stringlen = sizeof("OWNER@") - 1,
  734. .type = NFS4_ACL_WHO_OWNER,
  735. },
  736. {
  737. .string = "GROUP@",
  738. .stringlen = sizeof("GROUP@") - 1,
  739. .type = NFS4_ACL_WHO_GROUP,
  740. },
  741. {
  742. .string = "EVERYONE@",
  743. .stringlen = sizeof("EVERYONE@") - 1,
  744. .type = NFS4_ACL_WHO_EVERYONE,
  745. },
  746. };
  747. int
  748. nfs4_acl_get_whotype(char *p, u32 len)
  749. {
  750. int i;
  751. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  752. if (s2t_map[i].stringlen == len &&
  753. 0 == memcmp(s2t_map[i].string, p, len))
  754. return s2t_map[i].type;
  755. }
  756. return NFS4_ACL_WHO_NAMED;
  757. }
  758. __be32 nfs4_acl_write_who(struct xdr_stream *xdr, int who)
  759. {
  760. __be32 *p;
  761. int i;
  762. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  763. if (s2t_map[i].type != who)
  764. continue;
  765. p = xdr_reserve_space(xdr, s2t_map[i].stringlen + 4);
  766. if (!p)
  767. return nfserr_resource;
  768. p = xdr_encode_opaque(p, s2t_map[i].string,
  769. s2t_map[i].stringlen);
  770. return 0;
  771. }
  772. WARN_ON_ONCE(1);
  773. return nfserr_serverfault;
  774. }