nfs4acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 "nfsfh.h"
  39. #include "nfsd.h"
  40. #include "acl.h"
  41. #include "vfs.h"
  42. #define NFS4_ACL_TYPE_DEFAULT 0x01
  43. #define NFS4_ACL_DIR 0x02
  44. #define NFS4_ACL_OWNER 0x04
  45. /* mode bit translations: */
  46. #define NFS4_READ_MODE (NFS4_ACE_READ_DATA)
  47. #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA)
  48. #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
  49. #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
  50. #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
  51. /* flags used to simulate posix default ACLs */
  52. #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
  53. | NFS4_ACE_DIRECTORY_INHERIT_ACE)
  54. #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
  55. | NFS4_ACE_INHERIT_ONLY_ACE \
  56. | NFS4_ACE_IDENTIFIER_GROUP)
  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. static short ace2type(struct nfs4_ace *);
  109. static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
  110. unsigned int);
  111. int
  112. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry,
  113. struct nfs4_acl **acl)
  114. {
  115. struct inode *inode = d_inode(dentry);
  116. int error = 0;
  117. struct posix_acl *pacl = NULL, *dpacl = NULL;
  118. unsigned int flags = 0;
  119. int size = 0;
  120. pacl = get_acl(inode, ACL_TYPE_ACCESS);
  121. if (!pacl)
  122. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  123. if (IS_ERR(pacl))
  124. return PTR_ERR(pacl);
  125. /* allocate for worst case: one (deny, allow) pair each: */
  126. size += 2 * pacl->a_count;
  127. if (S_ISDIR(inode->i_mode)) {
  128. flags = NFS4_ACL_DIR;
  129. dpacl = get_acl(inode, ACL_TYPE_DEFAULT);
  130. if (IS_ERR(dpacl)) {
  131. error = PTR_ERR(dpacl);
  132. goto rel_pacl;
  133. }
  134. if (dpacl)
  135. size += 2 * dpacl->a_count;
  136. }
  137. *acl = kmalloc(nfs4_acl_bytes(size), GFP_KERNEL);
  138. if (*acl == NULL) {
  139. error = -ENOMEM;
  140. goto out;
  141. }
  142. (*acl)->naces = 0;
  143. _posix_to_nfsv4_one(pacl, *acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
  144. if (dpacl)
  145. _posix_to_nfsv4_one(dpacl, *acl, flags | NFS4_ACL_TYPE_DEFAULT);
  146. out:
  147. posix_acl_release(dpacl);
  148. rel_pacl:
  149. posix_acl_release(pacl);
  150. return error;
  151. }
  152. struct posix_acl_summary {
  153. unsigned short owner;
  154. unsigned short users;
  155. unsigned short group;
  156. unsigned short groups;
  157. unsigned short other;
  158. unsigned short mask;
  159. };
  160. static void
  161. summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
  162. {
  163. struct posix_acl_entry *pa, *pe;
  164. /*
  165. * Only pas.users and pas.groups need initialization; previous
  166. * posix_acl_valid() calls ensure that the other fields will be
  167. * initialized in the following loop. But, just to placate gcc:
  168. */
  169. memset(pas, 0, sizeof(*pas));
  170. pas->mask = 07;
  171. pe = acl->a_entries + acl->a_count;
  172. FOREACH_ACL_ENTRY(pa, acl, pe) {
  173. switch (pa->e_tag) {
  174. case ACL_USER_OBJ:
  175. pas->owner = pa->e_perm;
  176. break;
  177. case ACL_GROUP_OBJ:
  178. pas->group = pa->e_perm;
  179. break;
  180. case ACL_USER:
  181. pas->users |= pa->e_perm;
  182. break;
  183. case ACL_GROUP:
  184. pas->groups |= pa->e_perm;
  185. break;
  186. case ACL_OTHER:
  187. pas->other = pa->e_perm;
  188. break;
  189. case ACL_MASK:
  190. pas->mask = pa->e_perm;
  191. break;
  192. }
  193. }
  194. /* We'll only care about effective permissions: */
  195. pas->users &= pas->mask;
  196. pas->group &= pas->mask;
  197. pas->groups &= pas->mask;
  198. }
  199. /* We assume the acl has been verified with posix_acl_valid. */
  200. static void
  201. _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
  202. unsigned int flags)
  203. {
  204. struct posix_acl_entry *pa, *group_owner_entry;
  205. struct nfs4_ace *ace;
  206. struct posix_acl_summary pas;
  207. unsigned short deny;
  208. int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
  209. NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
  210. BUG_ON(pacl->a_count < 3);
  211. summarize_posix_acl(pacl, &pas);
  212. pa = pacl->a_entries;
  213. ace = acl->aces + acl->naces;
  214. /* We could deny everything not granted by the owner: */
  215. deny = ~pas.owner;
  216. /*
  217. * but it is equivalent (and simpler) to deny only what is not
  218. * granted by later entries:
  219. */
  220. deny &= pas.users | pas.group | pas.groups | pas.other;
  221. if (deny) {
  222. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  223. ace->flag = eflag;
  224. ace->access_mask = deny_mask_from_posix(deny, flags);
  225. ace->whotype = NFS4_ACL_WHO_OWNER;
  226. ace++;
  227. acl->naces++;
  228. }
  229. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  230. ace->flag = eflag;
  231. ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
  232. ace->whotype = NFS4_ACL_WHO_OWNER;
  233. ace++;
  234. acl->naces++;
  235. pa++;
  236. while (pa->e_tag == ACL_USER) {
  237. deny = ~(pa->e_perm & pas.mask);
  238. deny &= pas.groups | pas.group | pas.other;
  239. if (deny) {
  240. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  241. ace->flag = eflag;
  242. ace->access_mask = deny_mask_from_posix(deny, flags);
  243. ace->whotype = NFS4_ACL_WHO_NAMED;
  244. ace->who_uid = pa->e_uid;
  245. ace++;
  246. acl->naces++;
  247. }
  248. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  249. ace->flag = eflag;
  250. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  251. flags);
  252. ace->whotype = NFS4_ACL_WHO_NAMED;
  253. ace->who_uid = pa->e_uid;
  254. ace++;
  255. acl->naces++;
  256. pa++;
  257. }
  258. /* In the case of groups, we apply allow ACEs first, then deny ACEs,
  259. * since a user can be in more than one group. */
  260. /* allow ACEs */
  261. group_owner_entry = pa;
  262. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  263. ace->flag = eflag;
  264. ace->access_mask = mask_from_posix(pas.group, flags);
  265. ace->whotype = NFS4_ACL_WHO_GROUP;
  266. ace++;
  267. acl->naces++;
  268. pa++;
  269. while (pa->e_tag == ACL_GROUP) {
  270. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  271. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  272. ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
  273. flags);
  274. ace->whotype = NFS4_ACL_WHO_NAMED;
  275. ace->who_gid = pa->e_gid;
  276. ace++;
  277. acl->naces++;
  278. pa++;
  279. }
  280. /* deny ACEs */
  281. pa = group_owner_entry;
  282. deny = ~pas.group & pas.other;
  283. if (deny) {
  284. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  285. ace->flag = eflag;
  286. ace->access_mask = deny_mask_from_posix(deny, flags);
  287. ace->whotype = NFS4_ACL_WHO_GROUP;
  288. ace++;
  289. acl->naces++;
  290. }
  291. pa++;
  292. while (pa->e_tag == ACL_GROUP) {
  293. deny = ~(pa->e_perm & pas.mask);
  294. deny &= pas.other;
  295. if (deny) {
  296. ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
  297. ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
  298. ace->access_mask = deny_mask_from_posix(deny, flags);
  299. ace->whotype = NFS4_ACL_WHO_NAMED;
  300. ace->who_gid = pa->e_gid;
  301. ace++;
  302. acl->naces++;
  303. }
  304. pa++;
  305. }
  306. if (pa->e_tag == ACL_MASK)
  307. pa++;
  308. ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
  309. ace->flag = eflag;
  310. ace->access_mask = mask_from_posix(pa->e_perm, flags);
  311. ace->whotype = NFS4_ACL_WHO_EVERYONE;
  312. acl->naces++;
  313. }
  314. static bool
  315. pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2)
  316. {
  317. if (pace1->e_tag != pace2->e_tag)
  318. return pace1->e_tag > pace2->e_tag;
  319. if (pace1->e_tag == ACL_USER)
  320. return uid_gt(pace1->e_uid, pace2->e_uid);
  321. if (pace1->e_tag == ACL_GROUP)
  322. return gid_gt(pace1->e_gid, pace2->e_gid);
  323. return false;
  324. }
  325. static void
  326. sort_pacl_range(struct posix_acl *pacl, int start, int end) {
  327. int sorted = 0, i;
  328. /* We just do a bubble sort; easy to do in place, and we're not
  329. * expecting acl's to be long enough to justify anything more. */
  330. while (!sorted) {
  331. sorted = 1;
  332. for (i = start; i < end; i++) {
  333. if (pace_gt(&pacl->a_entries[i],
  334. &pacl->a_entries[i+1])) {
  335. sorted = 0;
  336. swap(pacl->a_entries[i],
  337. pacl->a_entries[i + 1]);
  338. }
  339. }
  340. }
  341. }
  342. static void
  343. sort_pacl(struct posix_acl *pacl)
  344. {
  345. /* posix_acl_valid requires that users and groups be in order
  346. * by uid/gid. */
  347. int i, j;
  348. /* no users or groups */
  349. if (!pacl || pacl->a_count <= 4)
  350. return;
  351. i = 1;
  352. while (pacl->a_entries[i].e_tag == ACL_USER)
  353. i++;
  354. sort_pacl_range(pacl, 1, i-1);
  355. BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
  356. j = ++i;
  357. while (pacl->a_entries[j].e_tag == ACL_GROUP)
  358. j++;
  359. sort_pacl_range(pacl, i, j-1);
  360. return;
  361. }
  362. /*
  363. * While processing the NFSv4 ACE, this maintains bitmasks representing
  364. * which permission bits have been allowed and which denied to a given
  365. * entity: */
  366. struct posix_ace_state {
  367. u32 allow;
  368. u32 deny;
  369. };
  370. struct posix_user_ace_state {
  371. union {
  372. kuid_t uid;
  373. kgid_t gid;
  374. };
  375. struct posix_ace_state perms;
  376. };
  377. struct posix_ace_state_array {
  378. int n;
  379. struct posix_user_ace_state aces[];
  380. };
  381. /*
  382. * While processing the NFSv4 ACE, this maintains the partial permissions
  383. * calculated so far: */
  384. struct posix_acl_state {
  385. int empty;
  386. struct posix_ace_state owner;
  387. struct posix_ace_state group;
  388. struct posix_ace_state other;
  389. struct posix_ace_state everyone;
  390. struct posix_ace_state mask; /* Deny unused in this case */
  391. struct posix_ace_state_array *users;
  392. struct posix_ace_state_array *groups;
  393. };
  394. static int
  395. init_state(struct posix_acl_state *state, int cnt)
  396. {
  397. int alloc;
  398. memset(state, 0, sizeof(struct posix_acl_state));
  399. state->empty = 1;
  400. /*
  401. * In the worst case, each individual acl could be for a distinct
  402. * named user or group, but we don't no 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->empty && (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. int i;
  540. state->empty = 0;
  541. switch (ace2type(ace)) {
  542. case ACL_USER_OBJ:
  543. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  544. allow_bits(&state->owner, mask);
  545. } else {
  546. deny_bits(&state->owner, mask);
  547. }
  548. break;
  549. case ACL_USER:
  550. i = find_uid(state, ace->who_uid);
  551. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  552. allow_bits(&state->users->aces[i].perms, mask);
  553. } else {
  554. deny_bits(&state->users->aces[i].perms, mask);
  555. mask = state->users->aces[i].perms.deny;
  556. deny_bits(&state->owner, mask);
  557. }
  558. break;
  559. case ACL_GROUP_OBJ:
  560. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  561. allow_bits(&state->group, mask);
  562. } else {
  563. deny_bits(&state->group, mask);
  564. mask = state->group.deny;
  565. deny_bits(&state->owner, mask);
  566. deny_bits(&state->everyone, mask);
  567. deny_bits_array(state->users, mask);
  568. deny_bits_array(state->groups, mask);
  569. }
  570. break;
  571. case ACL_GROUP:
  572. i = find_gid(state, ace->who_gid);
  573. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  574. allow_bits(&state->groups->aces[i].perms, mask);
  575. } else {
  576. deny_bits(&state->groups->aces[i].perms, mask);
  577. mask = state->groups->aces[i].perms.deny;
  578. deny_bits(&state->owner, mask);
  579. deny_bits(&state->group, mask);
  580. deny_bits(&state->everyone, mask);
  581. deny_bits_array(state->users, mask);
  582. deny_bits_array(state->groups, mask);
  583. }
  584. break;
  585. case ACL_OTHER:
  586. if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
  587. allow_bits(&state->owner, mask);
  588. allow_bits(&state->group, mask);
  589. allow_bits(&state->other, mask);
  590. allow_bits(&state->everyone, mask);
  591. allow_bits_array(state->users, mask);
  592. allow_bits_array(state->groups, mask);
  593. } else {
  594. deny_bits(&state->owner, mask);
  595. deny_bits(&state->group, mask);
  596. deny_bits(&state->other, mask);
  597. deny_bits(&state->everyone, mask);
  598. deny_bits_array(state->users, mask);
  599. deny_bits_array(state->groups, mask);
  600. }
  601. }
  602. }
  603. static int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl,
  604. struct posix_acl **pacl, struct posix_acl **dpacl,
  605. unsigned int flags)
  606. {
  607. struct posix_acl_state effective_acl_state, default_acl_state;
  608. struct nfs4_ace *ace;
  609. int ret;
  610. ret = init_state(&effective_acl_state, acl->naces);
  611. if (ret)
  612. return ret;
  613. ret = init_state(&default_acl_state, acl->naces);
  614. if (ret)
  615. goto out_estate;
  616. ret = -EINVAL;
  617. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  618. if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
  619. ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
  620. goto out_dstate;
  621. if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
  622. goto out_dstate;
  623. if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
  624. process_one_v4_ace(&effective_acl_state, ace);
  625. continue;
  626. }
  627. if (!(flags & NFS4_ACL_DIR))
  628. goto out_dstate;
  629. /*
  630. * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
  631. * is set, we're effectively turning on the other. That's OK,
  632. * according to rfc 3530.
  633. */
  634. process_one_v4_ace(&default_acl_state, ace);
  635. if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
  636. process_one_v4_ace(&effective_acl_state, ace);
  637. }
  638. *pacl = posix_state_to_acl(&effective_acl_state, flags);
  639. if (IS_ERR(*pacl)) {
  640. ret = PTR_ERR(*pacl);
  641. *pacl = NULL;
  642. goto out_dstate;
  643. }
  644. *dpacl = posix_state_to_acl(&default_acl_state,
  645. flags | NFS4_ACL_TYPE_DEFAULT);
  646. if (IS_ERR(*dpacl)) {
  647. ret = PTR_ERR(*dpacl);
  648. *dpacl = NULL;
  649. posix_acl_release(*pacl);
  650. *pacl = NULL;
  651. goto out_dstate;
  652. }
  653. sort_pacl(*pacl);
  654. sort_pacl(*dpacl);
  655. ret = 0;
  656. out_dstate:
  657. free_state(&default_acl_state);
  658. out_estate:
  659. free_state(&effective_acl_state);
  660. return ret;
  661. }
  662. __be32
  663. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  664. struct nfs4_acl *acl)
  665. {
  666. __be32 error;
  667. int host_error;
  668. struct dentry *dentry;
  669. struct inode *inode;
  670. struct posix_acl *pacl = NULL, *dpacl = NULL;
  671. unsigned int flags = 0;
  672. /* Get inode */
  673. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
  674. if (error)
  675. return error;
  676. dentry = fhp->fh_dentry;
  677. inode = d_inode(dentry);
  678. if (!inode->i_op->set_acl || !IS_POSIXACL(inode))
  679. return nfserr_attrnotsupp;
  680. if (S_ISDIR(inode->i_mode))
  681. flags = NFS4_ACL_DIR;
  682. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  683. if (host_error == -EINVAL)
  684. return nfserr_attrnotsupp;
  685. if (host_error < 0)
  686. goto out_nfserr;
  687. host_error = inode->i_op->set_acl(inode, pacl, ACL_TYPE_ACCESS);
  688. if (host_error < 0)
  689. goto out_release;
  690. if (S_ISDIR(inode->i_mode)) {
  691. host_error = inode->i_op->set_acl(inode, dpacl,
  692. ACL_TYPE_DEFAULT);
  693. }
  694. out_release:
  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. }