nfsacl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(nfsacl_encode);
  29. EXPORT_SYMBOL(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. u32 *p = (u32 *) elem;
  44. if (nfsacl_desc->count < nfsacl_desc->acl->a_count) {
  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. } else {
  65. const struct posix_acl_entry *pa, *pe;
  66. int group_obj_perm = ACL_READ|ACL_WRITE|ACL_EXECUTE;
  67. FOREACH_ACL_ENTRY(pa, nfsacl_desc->acl, pe) {
  68. if (pa->e_tag == ACL_GROUP_OBJ) {
  69. group_obj_perm = pa->e_perm & S_IRWXO;
  70. break;
  71. }
  72. }
  73. /* fake up ACL_MASK entry */
  74. *p++ = htonl(ACL_MASK | nfsacl_desc->typeflag);
  75. *p++ = htonl(0);
  76. *p++ = htonl(group_obj_perm);
  77. }
  78. return 0;
  79. }
  80. unsigned int
  81. nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
  82. struct posix_acl *acl, int encode_entries, int typeflag)
  83. {
  84. int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
  85. struct nfsacl_encode_desc nfsacl_desc = {
  86. .desc = {
  87. .elem_size = 12,
  88. .array_len = encode_entries ? entries : 0,
  89. .xcode = xdr_nfsace_encode,
  90. },
  91. .acl = acl,
  92. .typeflag = typeflag,
  93. .uid = inode->i_uid,
  94. .gid = inode->i_gid,
  95. };
  96. int err;
  97. if (entries > NFS_ACL_MAX_ENTRIES ||
  98. xdr_encode_word(buf, base, entries))
  99. return -EINVAL;
  100. err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
  101. if (!err)
  102. err = 8 + nfsacl_desc.desc.elem_size *
  103. nfsacl_desc.desc.array_len;
  104. return err;
  105. }
  106. struct nfsacl_decode_desc {
  107. struct xdr_array2_desc desc;
  108. unsigned int count;
  109. struct posix_acl *acl;
  110. };
  111. static int
  112. xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
  113. {
  114. struct nfsacl_decode_desc *nfsacl_desc =
  115. (struct nfsacl_decode_desc *) desc;
  116. u32 *p = (u32 *) elem;
  117. struct posix_acl_entry *entry;
  118. if (!nfsacl_desc->acl) {
  119. if (desc->array_len > NFS_ACL_MAX_ENTRIES)
  120. return -EINVAL;
  121. nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
  122. if (!nfsacl_desc->acl)
  123. return -ENOMEM;
  124. nfsacl_desc->count = 0;
  125. }
  126. entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  127. entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
  128. entry->e_id = ntohl(*p++);
  129. entry->e_perm = ntohl(*p++);
  130. switch(entry->e_tag) {
  131. case ACL_USER_OBJ:
  132. case ACL_USER:
  133. case ACL_GROUP_OBJ:
  134. case ACL_GROUP:
  135. case ACL_OTHER:
  136. if (entry->e_perm & ~S_IRWXO)
  137. return -EINVAL;
  138. break;
  139. case ACL_MASK:
  140. /* Solaris sometimes sets additonal bits in the mask */
  141. entry->e_perm &= S_IRWXO;
  142. break;
  143. default:
  144. return -EINVAL;
  145. }
  146. return 0;
  147. }
  148. static int
  149. cmp_acl_entry(const void *x, const void *y)
  150. {
  151. const struct posix_acl_entry *a = x, *b = y;
  152. if (a->e_tag != b->e_tag)
  153. return a->e_tag - b->e_tag;
  154. else if (a->e_id > b->e_id)
  155. return 1;
  156. else if (a->e_id < b->e_id)
  157. return -1;
  158. else
  159. return 0;
  160. }
  161. /*
  162. * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
  163. */
  164. static int
  165. posix_acl_from_nfsacl(struct posix_acl *acl)
  166. {
  167. struct posix_acl_entry *pa, *pe,
  168. *group_obj = NULL, *mask = NULL;
  169. if (!acl)
  170. return 0;
  171. sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
  172. cmp_acl_entry, NULL);
  173. /* Clear undefined identifier fields and find the ACL_GROUP_OBJ
  174. and ACL_MASK entries. */
  175. FOREACH_ACL_ENTRY(pa, acl, pe) {
  176. switch(pa->e_tag) {
  177. case ACL_USER_OBJ:
  178. pa->e_id = ACL_UNDEFINED_ID;
  179. break;
  180. case ACL_GROUP_OBJ:
  181. pa->e_id = ACL_UNDEFINED_ID;
  182. group_obj = pa;
  183. break;
  184. case ACL_MASK:
  185. mask = pa;
  186. /* fall through */
  187. case ACL_OTHER:
  188. pa->e_id = ACL_UNDEFINED_ID;
  189. break;
  190. }
  191. }
  192. if (acl->a_count == 4 && group_obj && mask &&
  193. mask->e_perm == group_obj->e_perm) {
  194. /* remove bogus ACL_MASK entry */
  195. memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
  196. sizeof(struct posix_acl_entry));
  197. acl->a_count = 3;
  198. }
  199. return 0;
  200. }
  201. unsigned int
  202. nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
  203. struct posix_acl **pacl)
  204. {
  205. struct nfsacl_decode_desc nfsacl_desc = {
  206. .desc = {
  207. .elem_size = 12,
  208. .xcode = pacl ? xdr_nfsace_decode : NULL,
  209. },
  210. };
  211. u32 entries;
  212. int err;
  213. if (xdr_decode_word(buf, base, &entries) ||
  214. entries > NFS_ACL_MAX_ENTRIES)
  215. return -EINVAL;
  216. err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
  217. if (err)
  218. return err;
  219. if (pacl) {
  220. if (entries != nfsacl_desc.desc.array_len ||
  221. posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
  222. posix_acl_release(nfsacl_desc.acl);
  223. return -EINVAL;
  224. }
  225. *pacl = nfsacl_desc.acl;
  226. }
  227. if (aclcnt)
  228. *aclcnt = entries;
  229. return 8 + nfsacl_desc.desc.elem_size *
  230. nfsacl_desc.desc.array_len;
  231. }