nfsacl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * fs/nfs_common/nfsacl.c
  3. *
  4. * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
  5. */
  6. /*
  7. * The Solaris nfsacl protocol represents some ACLs slightly differently
  8. * than POSIX 1003.1e draft 17 does (and we do):
  9. *
  10. * - Minimal ACLs always have an ACL_MASK entry, so they have
  11. * four instead of three entries.
  12. * - The ACL_MASK entry in such minimal ACLs always has the same
  13. * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
  14. * the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
  15. * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
  16. * entries contain the identifiers of the owner and owning group.
  17. * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
  18. * - ACL entries in the kernel are kept sorted in ascending order
  19. * of (e_tag, e_id). Solaris ACLs are unsorted.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/fs.h>
  23. #include <linux/gfp.h>
  24. #include <linux/sunrpc/xdr.h>
  25. #include <linux/nfsacl.h>
  26. #include <linux/nfs3.h>
  27. #include <linux/sort.h>
  28. MODULE_LICENSE("GPL");
  29. EXPORT_SYMBOL_GPL(nfsacl_encode);
  30. EXPORT_SYMBOL_GPL(nfsacl_decode);
  31. struct nfsacl_encode_desc {
  32. struct xdr_array2_desc desc;
  33. unsigned int count;
  34. struct posix_acl *acl;
  35. int typeflag;
  36. uid_t uid;
  37. gid_t gid;
  38. };
  39. static int
  40. xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
  41. {
  42. struct nfsacl_encode_desc *nfsacl_desc =
  43. (struct nfsacl_encode_desc *) desc;
  44. __be32 *p = elem;
  45. struct posix_acl_entry *entry =
  46. &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  47. *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
  48. switch(entry->e_tag) {
  49. case ACL_USER_OBJ:
  50. *p++ = htonl(nfsacl_desc->uid);
  51. break;
  52. case ACL_GROUP_OBJ:
  53. *p++ = htonl(nfsacl_desc->gid);
  54. break;
  55. case ACL_USER:
  56. case ACL_GROUP:
  57. *p++ = htonl(entry->e_id);
  58. break;
  59. default: /* Solaris depends on that! */
  60. *p++ = 0;
  61. break;
  62. }
  63. *p++ = htonl(entry->e_perm & S_IRWXO);
  64. return 0;
  65. }
  66. unsigned int
  67. nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
  68. struct posix_acl *acl, int encode_entries, int typeflag)
  69. {
  70. int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
  71. struct nfsacl_encode_desc nfsacl_desc = {
  72. .desc = {
  73. .elem_size = 12,
  74. .array_len = encode_entries ? entries : 0,
  75. .xcode = xdr_nfsace_encode,
  76. },
  77. .acl = acl,
  78. .typeflag = typeflag,
  79. .uid = inode->i_uid,
  80. .gid = inode->i_gid,
  81. };
  82. int err;
  83. struct posix_acl *acl2 = NULL;
  84. if (entries > NFS_ACL_MAX_ENTRIES ||
  85. xdr_encode_word(buf, base, entries))
  86. return -EINVAL;
  87. if (encode_entries && acl && acl->a_count == 3) {
  88. /* Fake up an ACL_MASK entry. */
  89. acl2 = posix_acl_alloc(4, GFP_KERNEL);
  90. if (!acl2)
  91. return -ENOMEM;
  92. /* Insert entries in canonical order: other orders seem
  93. to confuse Solaris VxFS. */
  94. acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
  95. acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
  96. acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
  97. acl2->a_entries[2].e_tag = ACL_MASK;
  98. acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
  99. nfsacl_desc.acl = acl2;
  100. }
  101. err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
  102. if (acl2)
  103. posix_acl_release(acl2);
  104. if (!err)
  105. err = 8 + nfsacl_desc.desc.elem_size *
  106. nfsacl_desc.desc.array_len;
  107. return err;
  108. }
  109. struct nfsacl_decode_desc {
  110. struct xdr_array2_desc desc;
  111. unsigned int count;
  112. struct posix_acl *acl;
  113. };
  114. static int
  115. xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
  116. {
  117. struct nfsacl_decode_desc *nfsacl_desc =
  118. (struct nfsacl_decode_desc *) desc;
  119. __be32 *p = elem;
  120. struct posix_acl_entry *entry;
  121. if (!nfsacl_desc->acl) {
  122. if (desc->array_len > NFS_ACL_MAX_ENTRIES)
  123. return -EINVAL;
  124. nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
  125. if (!nfsacl_desc->acl)
  126. return -ENOMEM;
  127. nfsacl_desc->count = 0;
  128. }
  129. entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  130. entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
  131. entry->e_id = ntohl(*p++);
  132. entry->e_perm = ntohl(*p++);
  133. switch(entry->e_tag) {
  134. case ACL_USER_OBJ:
  135. case ACL_USER:
  136. case ACL_GROUP_OBJ:
  137. case ACL_GROUP:
  138. case ACL_OTHER:
  139. if (entry->e_perm & ~S_IRWXO)
  140. return -EINVAL;
  141. break;
  142. case ACL_MASK:
  143. /* Solaris sometimes sets additonal bits in the mask */
  144. entry->e_perm &= S_IRWXO;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. return 0;
  150. }
  151. static int
  152. cmp_acl_entry(const void *x, const void *y)
  153. {
  154. const struct posix_acl_entry *a = x, *b = y;
  155. if (a->e_tag != b->e_tag)
  156. return a->e_tag - b->e_tag;
  157. else if (a->e_id > b->e_id)
  158. return 1;
  159. else if (a->e_id < b->e_id)
  160. return -1;
  161. else
  162. return 0;
  163. }
  164. /*
  165. * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
  166. */
  167. static int
  168. posix_acl_from_nfsacl(struct posix_acl *acl)
  169. {
  170. struct posix_acl_entry *pa, *pe,
  171. *group_obj = NULL, *mask = NULL;
  172. if (!acl)
  173. return 0;
  174. sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
  175. cmp_acl_entry, NULL);
  176. /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
  177. and ACL_MASK entries. */
  178. FOREACH_ACL_ENTRY(pa, acl, pe) {
  179. switch(pa->e_tag) {
  180. case ACL_USER_OBJ:
  181. pa->e_id = ACL_UNDEFINED_ID;
  182. break;
  183. case ACL_GROUP_OBJ:
  184. pa->e_id = ACL_UNDEFINED_ID;
  185. group_obj = pa;
  186. break;
  187. case ACL_MASK:
  188. mask = pa;
  189. /* fall through */
  190. case ACL_OTHER:
  191. pa->e_id = ACL_UNDEFINED_ID;
  192. break;
  193. }
  194. }
  195. if (acl->a_count == 4 && group_obj && mask &&
  196. mask->e_perm == group_obj->e_perm) {
  197. /* remove bogus ACL_MASK entry */
  198. memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
  199. sizeof(struct posix_acl_entry));
  200. acl->a_count = 3;
  201. }
  202. return 0;
  203. }
  204. unsigned int
  205. nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
  206. struct posix_acl **pacl)
  207. {
  208. struct nfsacl_decode_desc nfsacl_desc = {
  209. .desc = {
  210. .elem_size = 12,
  211. .xcode = pacl ? xdr_nfsace_decode : NULL,
  212. },
  213. };
  214. u32 entries;
  215. int err;
  216. if (xdr_decode_word(buf, base, &entries) ||
  217. entries > NFS_ACL_MAX_ENTRIES)
  218. return -EINVAL;
  219. nfsacl_desc.desc.array_maxlen = entries;
  220. err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
  221. if (err)
  222. return err;
  223. if (pacl) {
  224. if (entries != nfsacl_desc.desc.array_len ||
  225. posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
  226. posix_acl_release(nfsacl_desc.acl);
  227. return -EINVAL;
  228. }
  229. *pacl = nfsacl_desc.acl;
  230. }
  231. if (aclcnt)
  232. *aclcnt = entries;
  233. return 8 + nfsacl_desc.desc.elem_size *
  234. nfsacl_desc.desc.array_len;
  235. }