nfsacl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /**
  67. * nfsacl_encode - Encode an NFSv3 ACL
  68. *
  69. * @buf: destination xdr_buf to contain XDR encoded ACL
  70. * @base: byte offset in xdr_buf where XDR'd ACL begins
  71. * @inode: inode of file whose ACL this is
  72. * @acl: posix_acl to encode
  73. * @encode_entries: whether to encode ACEs as well
  74. * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
  75. *
  76. * Returns size of encoded ACL in bytes or a negative errno value.
  77. */
  78. int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
  79. struct posix_acl *acl, int encode_entries, int typeflag)
  80. {
  81. int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
  82. struct nfsacl_encode_desc nfsacl_desc = {
  83. .desc = {
  84. .elem_size = 12,
  85. .array_len = encode_entries ? entries : 0,
  86. .xcode = xdr_nfsace_encode,
  87. },
  88. .acl = acl,
  89. .typeflag = typeflag,
  90. .uid = inode->i_uid,
  91. .gid = inode->i_gid,
  92. };
  93. int err;
  94. struct posix_acl *acl2 = NULL;
  95. if (entries > NFS_ACL_MAX_ENTRIES ||
  96. xdr_encode_word(buf, base, entries))
  97. return -EINVAL;
  98. if (encode_entries && acl && acl->a_count == 3) {
  99. /* Fake up an ACL_MASK entry. */
  100. acl2 = posix_acl_alloc(4, GFP_KERNEL);
  101. if (!acl2)
  102. return -ENOMEM;
  103. /* Insert entries in canonical order: other orders seem
  104. to confuse Solaris VxFS. */
  105. acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */
  106. acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */
  107. acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */
  108. acl2->a_entries[2].e_tag = ACL_MASK;
  109. acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */
  110. nfsacl_desc.acl = acl2;
  111. }
  112. err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
  113. if (acl2)
  114. posix_acl_release(acl2);
  115. if (!err)
  116. err = 8 + nfsacl_desc.desc.elem_size *
  117. nfsacl_desc.desc.array_len;
  118. return err;
  119. }
  120. struct nfsacl_decode_desc {
  121. struct xdr_array2_desc desc;
  122. unsigned int count;
  123. struct posix_acl *acl;
  124. };
  125. static int
  126. xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
  127. {
  128. struct nfsacl_decode_desc *nfsacl_desc =
  129. (struct nfsacl_decode_desc *) desc;
  130. __be32 *p = elem;
  131. struct posix_acl_entry *entry;
  132. if (!nfsacl_desc->acl) {
  133. if (desc->array_len > NFS_ACL_MAX_ENTRIES)
  134. return -EINVAL;
  135. nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
  136. if (!nfsacl_desc->acl)
  137. return -ENOMEM;
  138. nfsacl_desc->count = 0;
  139. }
  140. entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  141. entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
  142. entry->e_id = ntohl(*p++);
  143. entry->e_perm = ntohl(*p++);
  144. switch(entry->e_tag) {
  145. case ACL_USER_OBJ:
  146. case ACL_USER:
  147. case ACL_GROUP_OBJ:
  148. case ACL_GROUP:
  149. case ACL_OTHER:
  150. if (entry->e_perm & ~S_IRWXO)
  151. return -EINVAL;
  152. break;
  153. case ACL_MASK:
  154. /* Solaris sometimes sets additonal bits in the mask */
  155. entry->e_perm &= S_IRWXO;
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static int
  163. cmp_acl_entry(const void *x, const void *y)
  164. {
  165. const struct posix_acl_entry *a = x, *b = y;
  166. if (a->e_tag != b->e_tag)
  167. return a->e_tag - b->e_tag;
  168. else if (a->e_id > b->e_id)
  169. return 1;
  170. else if (a->e_id < b->e_id)
  171. return -1;
  172. else
  173. return 0;
  174. }
  175. /*
  176. * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
  177. */
  178. static int
  179. posix_acl_from_nfsacl(struct posix_acl *acl)
  180. {
  181. struct posix_acl_entry *pa, *pe,
  182. *group_obj = NULL, *mask = NULL;
  183. if (!acl)
  184. return 0;
  185. sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
  186. cmp_acl_entry, NULL);
  187. /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
  188. and ACL_MASK entries. */
  189. FOREACH_ACL_ENTRY(pa, acl, pe) {
  190. switch(pa->e_tag) {
  191. case ACL_USER_OBJ:
  192. pa->e_id = ACL_UNDEFINED_ID;
  193. break;
  194. case ACL_GROUP_OBJ:
  195. pa->e_id = ACL_UNDEFINED_ID;
  196. group_obj = pa;
  197. break;
  198. case ACL_MASK:
  199. mask = pa;
  200. /* fall through */
  201. case ACL_OTHER:
  202. pa->e_id = ACL_UNDEFINED_ID;
  203. break;
  204. }
  205. }
  206. if (acl->a_count == 4 && group_obj && mask &&
  207. mask->e_perm == group_obj->e_perm) {
  208. /* remove bogus ACL_MASK entry */
  209. memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
  210. sizeof(struct posix_acl_entry));
  211. acl->a_count = 3;
  212. }
  213. return 0;
  214. }
  215. /**
  216. * nfsacl_decode - Decode an NFSv3 ACL
  217. *
  218. * @buf: xdr_buf containing XDR'd ACL data to decode
  219. * @base: byte offset in xdr_buf where XDR'd ACL begins
  220. * @aclcnt: count of ACEs in decoded posix_acl
  221. * @pacl: buffer in which to place decoded posix_acl
  222. *
  223. * Returns the length of the decoded ACL in bytes, or a negative errno value.
  224. */
  225. int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
  226. struct posix_acl **pacl)
  227. {
  228. struct nfsacl_decode_desc nfsacl_desc = {
  229. .desc = {
  230. .elem_size = 12,
  231. .xcode = pacl ? xdr_nfsace_decode : NULL,
  232. },
  233. };
  234. u32 entries;
  235. int err;
  236. if (xdr_decode_word(buf, base, &entries) ||
  237. entries > NFS_ACL_MAX_ENTRIES)
  238. return -EINVAL;
  239. nfsacl_desc.desc.array_maxlen = entries;
  240. err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
  241. if (err)
  242. return err;
  243. if (pacl) {
  244. if (entries != nfsacl_desc.desc.array_len ||
  245. posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
  246. posix_acl_release(nfsacl_desc.acl);
  247. return -EINVAL;
  248. }
  249. *pacl = nfsacl_desc.acl;
  250. }
  251. if (aclcnt)
  252. *aclcnt = entries;
  253. return 8 + nfsacl_desc.desc.elem_size *
  254. nfsacl_desc.desc.array_len;
  255. }