nfsacl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/nfs_common/nfsacl.c
  4. *
  5. * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
  6. */
  7. /*
  8. * The Solaris nfsacl protocol represents some ACLs slightly differently
  9. * than POSIX 1003.1e draft 17 does (and we do):
  10. *
  11. * - Minimal ACLs always have an ACL_MASK entry, so they have
  12. * four instead of three entries.
  13. * - The ACL_MASK entry in such minimal ACLs always has the same
  14. * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
  15. * the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
  16. * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
  17. * entries contain the identifiers of the owner and owning group.
  18. * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
  19. * - ACL entries in the kernel are kept sorted in ascending order
  20. * of (e_tag, e_id). Solaris ACLs are unsorted.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/fs.h>
  24. #include <linux/gfp.h>
  25. #include <linux/sunrpc/xdr.h>
  26. #include <linux/nfsacl.h>
  27. #include <linux/nfs3.h>
  28. #include <linux/sort.h>
  29. MODULE_LICENSE("GPL");
  30. struct nfsacl_encode_desc {
  31. struct xdr_array2_desc desc;
  32. unsigned int count;
  33. struct posix_acl *acl;
  34. int typeflag;
  35. kuid_t uid;
  36. kgid_t gid;
  37. };
  38. struct nfsacl_simple_acl {
  39. struct posix_acl acl;
  40. struct posix_acl_entry ace[4];
  41. };
  42. static int
  43. xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
  44. {
  45. struct nfsacl_encode_desc *nfsacl_desc =
  46. (struct nfsacl_encode_desc *) desc;
  47. __be32 *p = elem;
  48. struct posix_acl_entry *entry =
  49. &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  50. *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
  51. switch(entry->e_tag) {
  52. case ACL_USER_OBJ:
  53. *p++ = htonl(from_kuid(&init_user_ns, nfsacl_desc->uid));
  54. break;
  55. case ACL_GROUP_OBJ:
  56. *p++ = htonl(from_kgid(&init_user_ns, nfsacl_desc->gid));
  57. break;
  58. case ACL_USER:
  59. *p++ = htonl(from_kuid(&init_user_ns, entry->e_uid));
  60. break;
  61. case ACL_GROUP:
  62. *p++ = htonl(from_kgid(&init_user_ns, entry->e_gid));
  63. break;
  64. default: /* Solaris depends on that! */
  65. *p++ = 0;
  66. break;
  67. }
  68. *p++ = htonl(entry->e_perm & S_IRWXO);
  69. return 0;
  70. }
  71. /**
  72. * nfsacl_encode - Encode an NFSv3 ACL
  73. *
  74. * @buf: destination xdr_buf to contain XDR encoded ACL
  75. * @base: byte offset in xdr_buf where XDR'd ACL begins
  76. * @inode: inode of file whose ACL this is
  77. * @acl: posix_acl to encode
  78. * @encode_entries: whether to encode ACEs as well
  79. * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
  80. *
  81. * Returns size of encoded ACL in bytes or a negative errno value.
  82. */
  83. int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
  84. struct posix_acl *acl, int encode_entries, int typeflag)
  85. {
  86. int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
  87. struct nfsacl_encode_desc nfsacl_desc = {
  88. .desc = {
  89. .elem_size = 12,
  90. .array_len = encode_entries ? entries : 0,
  91. .xcode = xdr_nfsace_encode,
  92. },
  93. .acl = acl,
  94. .typeflag = typeflag,
  95. .uid = inode->i_uid,
  96. .gid = inode->i_gid,
  97. };
  98. struct nfsacl_simple_acl aclbuf;
  99. int err;
  100. if (entries > NFS_ACL_MAX_ENTRIES ||
  101. xdr_encode_word(buf, base, entries))
  102. return -EINVAL;
  103. if (encode_entries && acl && acl->a_count == 3) {
  104. struct posix_acl *acl2 = &aclbuf.acl;
  105. /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
  106. * invoked in contexts where a memory allocation failure is
  107. * fatal. Fortunately this fake ACL is small enough to
  108. * construct on the stack. */
  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. EXPORT_SYMBOL_GPL(nfsacl_encode);
  126. struct nfsacl_decode_desc {
  127. struct xdr_array2_desc desc;
  128. unsigned int count;
  129. struct posix_acl *acl;
  130. };
  131. static int
  132. xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
  133. {
  134. struct nfsacl_decode_desc *nfsacl_desc =
  135. (struct nfsacl_decode_desc *) desc;
  136. __be32 *p = elem;
  137. struct posix_acl_entry *entry;
  138. unsigned int id;
  139. if (!nfsacl_desc->acl) {
  140. if (desc->array_len > NFS_ACL_MAX_ENTRIES)
  141. return -EINVAL;
  142. nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
  143. if (!nfsacl_desc->acl)
  144. return -ENOMEM;
  145. nfsacl_desc->count = 0;
  146. }
  147. entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
  148. entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
  149. id = ntohl(*p++);
  150. entry->e_perm = ntohl(*p++);
  151. switch(entry->e_tag) {
  152. case ACL_USER:
  153. entry->e_uid = make_kuid(&init_user_ns, id);
  154. if (!uid_valid(entry->e_uid))
  155. return -EINVAL;
  156. break;
  157. case ACL_GROUP:
  158. entry->e_gid = make_kgid(&init_user_ns, id);
  159. if (!gid_valid(entry->e_gid))
  160. return -EINVAL;
  161. break;
  162. case ACL_USER_OBJ:
  163. case ACL_GROUP_OBJ:
  164. case ACL_OTHER:
  165. if (entry->e_perm & ~S_IRWXO)
  166. return -EINVAL;
  167. break;
  168. case ACL_MASK:
  169. /* Solaris sometimes sets additional bits in the mask */
  170. entry->e_perm &= S_IRWXO;
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. return 0;
  176. }
  177. static int
  178. cmp_acl_entry(const void *x, const void *y)
  179. {
  180. const struct posix_acl_entry *a = x, *b = y;
  181. if (a->e_tag != b->e_tag)
  182. return a->e_tag - b->e_tag;
  183. else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid))
  184. return 1;
  185. else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid))
  186. return -1;
  187. else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid))
  188. return 1;
  189. else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid))
  190. return -1;
  191. else
  192. return 0;
  193. }
  194. /*
  195. * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
  196. */
  197. static int
  198. posix_acl_from_nfsacl(struct posix_acl *acl)
  199. {
  200. struct posix_acl_entry *pa, *pe,
  201. *group_obj = NULL, *mask = NULL;
  202. if (!acl)
  203. return 0;
  204. sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
  205. cmp_acl_entry, NULL);
  206. /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
  207. FOREACH_ACL_ENTRY(pa, acl, pe) {
  208. switch(pa->e_tag) {
  209. case ACL_USER_OBJ:
  210. break;
  211. case ACL_GROUP_OBJ:
  212. group_obj = pa;
  213. break;
  214. case ACL_MASK:
  215. mask = pa;
  216. fallthrough;
  217. case ACL_OTHER:
  218. break;
  219. }
  220. }
  221. if (acl->a_count == 4 && group_obj && mask &&
  222. mask->e_perm == group_obj->e_perm) {
  223. /* remove bogus ACL_MASK entry */
  224. memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
  225. sizeof(struct posix_acl_entry));
  226. acl->a_count = 3;
  227. }
  228. return 0;
  229. }
  230. /**
  231. * nfsacl_decode - Decode an NFSv3 ACL
  232. *
  233. * @buf: xdr_buf containing XDR'd ACL data to decode
  234. * @base: byte offset in xdr_buf where XDR'd ACL begins
  235. * @aclcnt: count of ACEs in decoded posix_acl
  236. * @pacl: buffer in which to place decoded posix_acl
  237. *
  238. * Returns the length of the decoded ACL in bytes, or a negative errno value.
  239. */
  240. int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
  241. struct posix_acl **pacl)
  242. {
  243. struct nfsacl_decode_desc nfsacl_desc = {
  244. .desc = {
  245. .elem_size = 12,
  246. .xcode = pacl ? xdr_nfsace_decode : NULL,
  247. },
  248. };
  249. u32 entries;
  250. int err;
  251. if (xdr_decode_word(buf, base, &entries) ||
  252. entries > NFS_ACL_MAX_ENTRIES)
  253. return -EINVAL;
  254. nfsacl_desc.desc.array_maxlen = entries;
  255. err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
  256. if (err)
  257. return err;
  258. if (pacl) {
  259. if (entries != nfsacl_desc.desc.array_len ||
  260. posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
  261. posix_acl_release(nfsacl_desc.acl);
  262. return -EINVAL;
  263. }
  264. *pacl = nfsacl_desc.acl;
  265. }
  266. if (aclcnt)
  267. *aclcnt = entries;
  268. return 8 + nfsacl_desc.desc.elem_size *
  269. nfsacl_desc.desc.array_len;
  270. }
  271. EXPORT_SYMBOL_GPL(nfsacl_decode);