nfsacl.c 7.2 KB

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