nfsacl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. struct nfsacl_encode_desc {
  30. struct xdr_array2_desc desc;
  31. unsigned int count;
  32. struct posix_acl *acl;
  33. int typeflag;
  34. kuid_t uid;
  35. kgid_t gid;
  36. };
  37. struct nfsacl_simple_acl {
  38. struct posix_acl acl;
  39. struct posix_acl_entry ace[4];
  40. };
  41. static int
  42. xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
  43. {
  44. struct nfsacl_encode_desc *nfsacl_desc =
  45. (struct nfsacl_encode_desc *) desc;
  46. __be32 *p = elem;
  47. struct posix_acl_entry *entry =
  48. &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  49. *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
  50. switch(entry->e_tag) {
  51. case ACL_USER_OBJ:
  52. *p++ = htonl(from_kuid(&init_user_ns, nfsacl_desc->uid));
  53. break;
  54. case ACL_GROUP_OBJ:
  55. *p++ = htonl(from_kgid(&init_user_ns, nfsacl_desc->gid));
  56. break;
  57. case ACL_USER:
  58. *p++ = htonl(from_kuid(&init_user_ns, entry->e_uid));
  59. break;
  60. case ACL_GROUP:
  61. *p++ = htonl(from_kgid(&init_user_ns, entry->e_gid));
  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. EXPORT_SYMBOL_GPL(nfsacl_encode);
  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. unsigned int id;
  138. if (!nfsacl_desc->acl) {
  139. if (desc->array_len > NFS_ACL_MAX_ENTRIES)
  140. return -EINVAL;
  141. nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
  142. if (!nfsacl_desc->acl)
  143. return -ENOMEM;
  144. nfsacl_desc->count = 0;
  145. }
  146. entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  147. entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
  148. id = ntohl(*p++);
  149. entry->e_perm = ntohl(*p++);
  150. switch(entry->e_tag) {
  151. case ACL_USER:
  152. entry->e_uid = make_kuid(&init_user_ns, id);
  153. if (!uid_valid(entry->e_uid))
  154. return -EINVAL;
  155. break;
  156. case ACL_GROUP:
  157. entry->e_gid = make_kgid(&init_user_ns, id);
  158. if (!gid_valid(entry->e_gid))
  159. return -EINVAL;
  160. break;
  161. case ACL_USER_OBJ:
  162. case ACL_GROUP_OBJ:
  163. case ACL_OTHER:
  164. if (entry->e_perm & ~S_IRWXO)
  165. return -EINVAL;
  166. break;
  167. case ACL_MASK:
  168. /* Solaris sometimes sets additional bits in the mask */
  169. entry->e_perm &= S_IRWXO;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. return 0;
  175. }
  176. static int
  177. cmp_acl_entry(const void *x, const void *y)
  178. {
  179. const struct posix_acl_entry *a = x, *b = y;
  180. if (a->e_tag != b->e_tag)
  181. return a->e_tag - b->e_tag;
  182. else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid))
  183. return 1;
  184. else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid))
  185. return -1;
  186. else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid))
  187. return 1;
  188. else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid))
  189. return -1;
  190. else
  191. return 0;
  192. }
  193. /*
  194. * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
  195. */
  196. static int
  197. posix_acl_from_nfsacl(struct posix_acl *acl)
  198. {
  199. struct posix_acl_entry *pa, *pe,
  200. *group_obj = NULL, *mask = NULL;
  201. if (!acl)
  202. return 0;
  203. sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
  204. cmp_acl_entry, NULL);
  205. /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
  206. FOREACH_ACL_ENTRY(pa, acl, pe) {
  207. switch(pa->e_tag) {
  208. case ACL_USER_OBJ:
  209. break;
  210. case ACL_GROUP_OBJ:
  211. group_obj = pa;
  212. break;
  213. case ACL_MASK:
  214. mask = pa;
  215. /* fall through */
  216. case ACL_OTHER:
  217. break;
  218. }
  219. }
  220. if (acl->a_count == 4 && group_obj && mask &&
  221. mask->e_perm == group_obj->e_perm) {
  222. /* remove bogus ACL_MASK entry */
  223. memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
  224. sizeof(struct posix_acl_entry));
  225. acl->a_count = 3;
  226. }
  227. return 0;
  228. }
  229. /**
  230. * nfsacl_decode - Decode an NFSv3 ACL
  231. *
  232. * @buf: xdr_buf containing XDR'd ACL data to decode
  233. * @base: byte offset in xdr_buf where XDR'd ACL begins
  234. * @aclcnt: count of ACEs in decoded posix_acl
  235. * @pacl: buffer in which to place decoded posix_acl
  236. *
  237. * Returns the length of the decoded ACL in bytes, or a negative errno value.
  238. */
  239. int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
  240. struct posix_acl **pacl)
  241. {
  242. struct nfsacl_decode_desc nfsacl_desc = {
  243. .desc = {
  244. .elem_size = 12,
  245. .xcode = pacl ? xdr_nfsace_decode : NULL,
  246. },
  247. };
  248. u32 entries;
  249. int err;
  250. if (xdr_decode_word(buf, base, &entries) ||
  251. entries > NFS_ACL_MAX_ENTRIES)
  252. return -EINVAL;
  253. nfsacl_desc.desc.array_maxlen = entries;
  254. err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
  255. if (err)
  256. return err;
  257. if (pacl) {
  258. if (entries != nfsacl_desc.desc.array_len ||
  259. posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
  260. posix_acl_release(nfsacl_desc.acl);
  261. return -EINVAL;
  262. }
  263. *pacl = nfsacl_desc.acl;
  264. }
  265. if (aclcnt)
  266. *aclcnt = entries;
  267. return 8 + nfsacl_desc.desc.elem_size *
  268. nfsacl_desc.desc.array_len;
  269. }
  270. EXPORT_SYMBOL_GPL(nfsacl_decode);