nfs4acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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_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_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. int empty;
  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. state->empty = 1;
  401. /*
  402. * In the worst case, each individual acl could be for a distinct
  403. * named user or group, but we don't know which, so we allocate
  404. * enough space for either:
  405. */
  406. alloc = sizeof(struct posix_ace_state_array)
  407. + cnt*sizeof(struct posix_user_ace_state);
  408. state->users = kzalloc(alloc, GFP_KERNEL);
  409. if (!state->users)
  410. return -ENOMEM;
  411. state->groups = kzalloc(alloc, GFP_KERNEL);
  412. if (!state->groups) {
  413. kfree(state->users);
  414. return -ENOMEM;
  415. }
  416. return 0;
  417. }
  418. static void
  419. free_state(struct posix_acl_state *state) {
  420. kfree(state->users);
  421. kfree(state->groups);
  422. }
  423. static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
  424. {
  425. state->mask.allow |= astate->allow;
  426. }
  427. static struct posix_acl *
  428. posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
  429. {
  430. struct posix_acl_entry *pace;
  431. struct posix_acl *pacl;
  432. int nace;
  433. int i;
  434. /*
  435. * ACLs with no ACEs are treated differently in the inheritable
  436. * and effective cases: when there are no inheritable ACEs,
  437. * calls ->set_acl with a NULL ACL structure.
  438. */
  439. if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT))
  440. return NULL;
  441. /*
  442. * When there are no effective ACEs, the following will end
  443. * up setting a 3-element effective posix ACL with all
  444. * permissions zero.
  445. */
  446. if (!state->users->n && !state->groups->n)
  447. nace = 3;
  448. else /* Note we also include a MASK ACE in this case: */
  449. nace = 4 + state->users->n + state->groups->n;
  450. pacl = posix_acl_alloc(nace, GFP_KERNEL);
  451. if (!pacl)
  452. return ERR_PTR(-ENOMEM);
  453. pace = pacl->a_entries;
  454. pace->e_tag = ACL_USER_OBJ;
  455. low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
  456. for (i=0; i < state->users->n; i++) {
  457. pace++;
  458. pace->e_tag = ACL_USER;
  459. low_mode_from_nfs4(state->users->aces[i].perms.allow,
  460. &pace->e_perm, flags);
  461. pace->e_uid = state->users->aces[i].uid;
  462. add_to_mask(state, &state->users->aces[i].perms);
  463. }
  464. pace++;
  465. pace->e_tag = ACL_GROUP_OBJ;
  466. low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
  467. add_to_mask(state, &state->group);
  468. for (i=0; i < state->groups->n; i++) {
  469. pace++;
  470. pace->e_tag = ACL_GROUP;
  471. low_mode_from_nfs4(state->groups->aces[i].perms.allow,
  472. &pace->e_perm, flags);
  473. pace->e_gid = state->groups->aces[i].gid;
  474. add_to_mask(state, &state->groups->aces[i].perms);
  475. }
  476. if (state->users->n || state->groups->n) {
  477. pace++;
  478. pace->e_tag = ACL_MASK;
  479. low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
  480. }
  481. pace++;
  482. pace->e_tag = ACL_OTHER;
  483. low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
  484. return pacl;
  485. }
  486. static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
  487. {
  488. /* Allow all bits in the mask not already denied: */
  489. astate->allow |= mask & ~astate->deny;
  490. }
  491. static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
  492. {
  493. /* Deny all bits in the mask not already allowed: */
  494. astate->deny |= mask & ~astate->allow;
  495. }
  496. static int find_uid(struct posix_acl_state *state, kuid_t uid)
  497. {
  498. struct posix_ace_state_array *a = state->users;
  499. int i;
  500. for (i = 0; i < a->n; i++)
  501. if (uid_eq(a->aces[i].uid, uid))
  502. return i;
  503. /* Not found: */
  504. a->n++;
  505. a->aces[i].uid = uid;
  506. a->aces[i].perms.allow = state->everyone.allow;
  507. a->aces[i].perms.deny = state->everyone.deny;
  508. return i;
  509. }
  510. static int find_gid(struct posix_acl_state *state, kgid_t gid)
  511. {
  512. struct posix_ace_state_array *a = state->groups;
  513. int i;
  514. for (i = 0; i < a->n; i++)
  515. if (gid_eq(a->aces[i].gid, gid))
  516. return i;
  517. /* Not found: */
  518. a->n++;
  519. a->aces[i].gid = gid;
  520. a->aces[i].perms.allow = state->everyone.allow;
  521. a->aces[i].perms.deny = state->everyone.deny;
  522. return i;
  523. }
  524. static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
  525. {
  526. int i;
  527. for (i=0; i < a->n; i++)
  528. deny_bits(&a->aces[i].perms, mask);
  529. }
  530. static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
  531. {
  532. int i;
  533. for (i=0; i < a->n; i++)
  534. allow_bits(&a->aces[i].perms, mask);
  535. }
  536. static void process_one_v4_ace(struct posix_acl_state *state,
  537. struct nfs4_ace *ace)
  538. {
  539. u32 mask = ace->access_mask;
  540. int i;
  541. state->empty = 0;
  542. switch (ace2type(ace)) {
  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. *pacl = posix_state_to_acl(&effective_acl_state, flags);
  640. if (IS_ERR(*pacl)) {
  641. ret = PTR_ERR(*pacl);
  642. *pacl = NULL;
  643. goto out_dstate;
  644. }
  645. *dpacl = posix_state_to_acl(&default_acl_state,
  646. flags | NFS4_ACL_TYPE_DEFAULT);
  647. if (IS_ERR(*dpacl)) {
  648. ret = PTR_ERR(*dpacl);
  649. *dpacl = NULL;
  650. posix_acl_release(*pacl);
  651. *pacl = NULL;
  652. goto out_dstate;
  653. }
  654. sort_pacl(*pacl);
  655. sort_pacl(*dpacl);
  656. ret = 0;
  657. out_dstate:
  658. free_state(&default_acl_state);
  659. out_estate:
  660. free_state(&effective_acl_state);
  661. return ret;
  662. }
  663. __be32
  664. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  665. struct nfs4_acl *acl)
  666. {
  667. __be32 error;
  668. int host_error;
  669. struct dentry *dentry;
  670. struct inode *inode;
  671. struct posix_acl *pacl = NULL, *dpacl = NULL;
  672. unsigned int flags = 0;
  673. /* Get inode */
  674. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
  675. if (error)
  676. return error;
  677. dentry = fhp->fh_dentry;
  678. inode = d_inode(dentry);
  679. if (S_ISDIR(inode->i_mode))
  680. flags = NFS4_ACL_DIR;
  681. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  682. if (host_error == -EINVAL)
  683. return nfserr_attrnotsupp;
  684. if (host_error < 0)
  685. goto out_nfserr;
  686. fh_lock(fhp);
  687. host_error = set_posix_acl(inode, ACL_TYPE_ACCESS, pacl);
  688. if (host_error < 0)
  689. goto out_drop_lock;
  690. if (S_ISDIR(inode->i_mode)) {
  691. host_error = set_posix_acl(inode, ACL_TYPE_DEFAULT, dpacl);
  692. }
  693. out_drop_lock:
  694. fh_unlock(fhp);
  695. posix_acl_release(pacl);
  696. posix_acl_release(dpacl);
  697. out_nfserr:
  698. if (host_error == -EOPNOTSUPP)
  699. return nfserr_attrnotsupp;
  700. else
  701. return nfserrno(host_error);
  702. }
  703. static short
  704. ace2type(struct nfs4_ace *ace)
  705. {
  706. switch (ace->whotype) {
  707. case NFS4_ACL_WHO_NAMED:
  708. return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
  709. ACL_GROUP : ACL_USER);
  710. case NFS4_ACL_WHO_OWNER:
  711. return ACL_USER_OBJ;
  712. case NFS4_ACL_WHO_GROUP:
  713. return ACL_GROUP_OBJ;
  714. case NFS4_ACL_WHO_EVERYONE:
  715. return ACL_OTHER;
  716. }
  717. BUG();
  718. return -1;
  719. }
  720. /*
  721. * return the size of the struct nfs4_acl required to represent an acl
  722. * with @entries entries.
  723. */
  724. int nfs4_acl_bytes(int entries)
  725. {
  726. return sizeof(struct nfs4_acl) + entries * sizeof(struct nfs4_ace);
  727. }
  728. static struct {
  729. char *string;
  730. int stringlen;
  731. int type;
  732. } s2t_map[] = {
  733. {
  734. .string = "OWNER@",
  735. .stringlen = sizeof("OWNER@") - 1,
  736. .type = NFS4_ACL_WHO_OWNER,
  737. },
  738. {
  739. .string = "GROUP@",
  740. .stringlen = sizeof("GROUP@") - 1,
  741. .type = NFS4_ACL_WHO_GROUP,
  742. },
  743. {
  744. .string = "EVERYONE@",
  745. .stringlen = sizeof("EVERYONE@") - 1,
  746. .type = NFS4_ACL_WHO_EVERYONE,
  747. },
  748. };
  749. int
  750. nfs4_acl_get_whotype(char *p, u32 len)
  751. {
  752. int i;
  753. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  754. if (s2t_map[i].stringlen == len &&
  755. 0 == memcmp(s2t_map[i].string, p, len))
  756. return s2t_map[i].type;
  757. }
  758. return NFS4_ACL_WHO_NAMED;
  759. }
  760. __be32 nfs4_acl_write_who(struct xdr_stream *xdr, int who)
  761. {
  762. __be32 *p;
  763. int i;
  764. for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
  765. if (s2t_map[i].type != who)
  766. continue;
  767. p = xdr_reserve_space(xdr, s2t_map[i].stringlen + 4);
  768. if (!p)
  769. return nfserr_resource;
  770. p = xdr_encode_opaque(p, s2t_map[i].string,
  771. s2t_map[i].stringlen);
  772. return 0;
  773. }
  774. WARN_ON_ONCE(1);
  775. return nfserr_serverfault;
  776. }