nfsacl.c 6.3 KB

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