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