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 /* 9 * The Solaris nfsacl protocol represents some ACLs slightly differently 10 * than POSIX 1003.1e draft 17 does (and we do): 11 * 12 * - Minimal ACLs always have an ACL_MASK entry, so they have 13 * four instead of three entries. 14 * - The ACL_MASK entry in such minimal ACLs always has the same 15 * permissions as the ACL_GROUP_OBJ entry. (In extended ACLs 16 * the ACL_MASK and ACL_GROUP_OBJ entries may differ.) 17 * - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ 18 * entries contain the identifiers of the owner and owning group. 19 * (In POSIX ACLs we always set them to ACL_UNDEFINED_ID). 20 * - ACL entries in the kernel are kept sorted in ascending order 21 * of (e_tag, e_id). Solaris ACLs are unsorted. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/fs.h> 26 #include <linux/gfp.h> 27 #include <linux/sunrpc/xdr.h> 28 #include <linux/nfsacl.h> 29 #include <linux/nfs3.h> 30 #include <linux/sort.h> 31 32 MODULE_DESCRIPTION("NFS ACL support"); 33 MODULE_LICENSE("GPL"); 34 35 struct nfsacl_encode_desc { 36 struct xdr_array2_desc desc; 37 unsigned int count; 38 struct posix_acl *acl; 39 int typeflag; 40 kuid_t uid; 41 kgid_t gid; 42 }; 43 44 struct nfsacl_simple_acl { 45 struct posix_acl_hdr acl; 46 struct posix_acl_entry ace[4]; 47 }; 48 49 static int 50 xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem) 51 { 52 struct nfsacl_encode_desc *nfsacl_desc = 53 (struct nfsacl_encode_desc *) desc; 54 __be32 *p = elem; 55 56 struct posix_acl_entry *entry = 57 &nfsacl_desc->acl->a_entries[nfsacl_desc->count++]; 58 59 *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag); 60 switch(entry->e_tag) { 61 case ACL_USER_OBJ: 62 *p++ = htonl(from_kuid(&init_user_ns, nfsacl_desc->uid)); 63 break; 64 case ACL_GROUP_OBJ: 65 *p++ = htonl(from_kgid(&init_user_ns, nfsacl_desc->gid)); 66 break; 67 case ACL_USER: 68 *p++ = htonl(from_kuid(&init_user_ns, entry->e_uid)); 69 break; 70 case ACL_GROUP: 71 *p++ = htonl(from_kgid(&init_user_ns, entry->e_gid)); 72 break; 73 default: /* Solaris depends on that! */ 74 *p++ = 0; 75 break; 76 } 77 *p++ = htonl(entry->e_perm & S_IRWXO); 78 return 0; 79 } 80 81 /** 82 * nfsacl_encode - Encode an NFSv3 ACL 83 * 84 * @buf: destination xdr_buf to contain XDR encoded ACL 85 * @base: byte offset in xdr_buf where XDR'd ACL begins 86 * @inode: inode of file whose ACL this is 87 * @acl: posix_acl to encode 88 * @encode_entries: whether to encode ACEs as well 89 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero 90 * 91 * Returns size of encoded ACL in bytes or a negative errno value. 92 */ 93 int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode, 94 struct posix_acl *acl, int encode_entries, int typeflag) 95 { 96 int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0; 97 struct nfsacl_encode_desc nfsacl_desc = { 98 .desc = { 99 .elem_size = 12, 100 .array_len = encode_entries ? entries : 0, 101 .xcode = xdr_nfsace_encode, 102 }, 103 .acl = acl, 104 .typeflag = typeflag, 105 .uid = inode->i_uid, 106 .gid = inode->i_gid, 107 }; 108 struct nfsacl_simple_acl aclbuf; 109 int err; 110 111 if (entries > NFS_ACL_MAX_ENTRIES || 112 xdr_encode_word(buf, base, entries)) 113 return -EINVAL; 114 if (encode_entries && acl && acl->a_count == 3) { 115 struct posix_acl *acl2 = 116 container_of(&aclbuf.acl, struct posix_acl, hdr); 117 118 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is 119 * invoked in contexts where a memory allocation failure is 120 * fatal. Fortunately this fake ACL is small enough to 121 * construct on the stack. */ 122 posix_acl_init(acl2, 4); 123 124 /* Insert entries in canonical order: other orders seem 125 to confuse Solaris VxFS. */ 126 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */ 127 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */ 128 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */ 129 acl2->a_entries[2].e_tag = ACL_MASK; 130 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */ 131 nfsacl_desc.acl = acl2; 132 } 133 err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc); 134 if (!err) 135 err = 8 + nfsacl_desc.desc.elem_size * 136 nfsacl_desc.desc.array_len; 137 return err; 138 } 139 EXPORT_SYMBOL_GPL(nfsacl_encode); 140 141 /** 142 * nfs_stream_encode_acl - Encode an NFSv3 ACL 143 * 144 * @xdr: an xdr_stream positioned to receive an encoded ACL 145 * @inode: inode of file whose ACL this is 146 * @acl: posix_acl to encode 147 * @encode_entries: whether to encode ACEs as well 148 * @typeflag: ACL type: NFS_ACL_DEFAULT or zero 149 * 150 * Return values: 151 * %false: The ACL could not be encoded 152 * %true: @xdr is advanced to the next available position 153 */ 154 bool nfs_stream_encode_acl(struct xdr_stream *xdr, struct inode *inode, 155 struct posix_acl *acl, int encode_entries, 156 int typeflag) 157 { 158 const size_t elem_size = XDR_UNIT * 3; 159 u32 entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0; 160 struct nfsacl_encode_desc nfsacl_desc = { 161 .desc = { 162 .elem_size = elem_size, 163 .array_len = encode_entries ? entries : 0, 164 .xcode = xdr_nfsace_encode, 165 }, 166 .acl = acl, 167 .typeflag = typeflag, 168 .uid = inode->i_uid, 169 .gid = inode->i_gid, 170 }; 171 struct nfsacl_simple_acl aclbuf; 172 unsigned int base; 173 int err; 174 175 if (entries > NFS_ACL_MAX_ENTRIES) 176 return false; 177 if (xdr_stream_encode_u32(xdr, entries) < 0) 178 return false; 179 180 if (encode_entries && acl && acl->a_count == 3) { 181 struct posix_acl *acl2 = 182 container_of(&aclbuf.acl, struct posix_acl, hdr); 183 184 /* Avoid the use of posix_acl_alloc(). nfsacl_encode() is 185 * invoked in contexts where a memory allocation failure is 186 * fatal. Fortunately this fake ACL is small enough to 187 * construct on the stack. */ 188 posix_acl_init(acl2, 4); 189 190 /* Insert entries in canonical order: other orders seem 191 to confuse Solaris VxFS. */ 192 acl2->a_entries[0] = acl->a_entries[0]; /* ACL_USER_OBJ */ 193 acl2->a_entries[1] = acl->a_entries[1]; /* ACL_GROUP_OBJ */ 194 acl2->a_entries[2] = acl->a_entries[1]; /* ACL_MASK */ 195 acl2->a_entries[2].e_tag = ACL_MASK; 196 acl2->a_entries[3] = acl->a_entries[2]; /* ACL_OTHER */ 197 nfsacl_desc.acl = acl2; 198 } 199 200 base = xdr_stream_pos(xdr); 201 if (!xdr_reserve_space(xdr, XDR_UNIT + 202 elem_size * nfsacl_desc.desc.array_len)) 203 return false; 204 err = xdr_encode_array2(xdr->buf, base, &nfsacl_desc.desc); 205 if (err) 206 return false; 207 208 return true; 209 } 210 EXPORT_SYMBOL_GPL(nfs_stream_encode_acl); 211 212 213 struct nfsacl_decode_desc { 214 struct xdr_array2_desc desc; 215 unsigned int count; 216 struct posix_acl *acl; 217 }; 218 219 static int 220 xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem) 221 { 222 struct nfsacl_decode_desc *nfsacl_desc = 223 (struct nfsacl_decode_desc *) desc; 224 __be32 *p = elem; 225 struct posix_acl_entry *entry; 226 unsigned int id; 227 228 if (!nfsacl_desc->acl) { 229 if (desc->array_len > NFS_ACL_MAX_ENTRIES) 230 return -EINVAL; 231 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL); 232 if (!nfsacl_desc->acl) 233 return -ENOMEM; 234 nfsacl_desc->count = 0; 235 } 236 237 entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++]; 238 entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT; 239 id = ntohl(*p++); 240 entry->e_perm = ntohl(*p++); 241 242 switch(entry->e_tag) { 243 case ACL_USER: 244 entry->e_uid = make_kuid(&init_user_ns, id); 245 if (!uid_valid(entry->e_uid)) 246 return -EINVAL; 247 break; 248 case ACL_GROUP: 249 entry->e_gid = make_kgid(&init_user_ns, id); 250 if (!gid_valid(entry->e_gid)) 251 return -EINVAL; 252 break; 253 case ACL_USER_OBJ: 254 case ACL_GROUP_OBJ: 255 case ACL_OTHER: 256 if (entry->e_perm & ~S_IRWXO) 257 return -EINVAL; 258 break; 259 case ACL_MASK: 260 /* Solaris sometimes sets additional bits in the mask */ 261 entry->e_perm &= S_IRWXO; 262 break; 263 default: 264 return -EINVAL; 265 } 266 267 return 0; 268 } 269 270 static int 271 cmp_acl_entry(const void *x, const void *y) 272 { 273 const struct posix_acl_entry *a = x, *b = y; 274 275 if (a->e_tag != b->e_tag) 276 return a->e_tag - b->e_tag; 277 else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid)) 278 return 1; 279 else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid)) 280 return -1; 281 else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid)) 282 return 1; 283 else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid)) 284 return -1; 285 else 286 return 0; 287 } 288 289 /* 290 * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL. 291 */ 292 static int 293 posix_acl_from_nfsacl(struct posix_acl *acl) 294 { 295 struct posix_acl_entry *pa, *pe, 296 *group_obj = NULL, *mask = NULL; 297 298 if (!acl) 299 return 0; 300 301 sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry), 302 cmp_acl_entry, NULL); 303 304 /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */ 305 FOREACH_ACL_ENTRY(pa, acl, pe) { 306 switch(pa->e_tag) { 307 case ACL_USER_OBJ: 308 break; 309 case ACL_GROUP_OBJ: 310 group_obj = pa; 311 break; 312 case ACL_MASK: 313 mask = pa; 314 fallthrough; 315 case ACL_OTHER: 316 break; 317 } 318 } 319 if (acl->a_count == 4 && group_obj && mask && 320 mask->e_perm == group_obj->e_perm) { 321 /* remove bogus ACL_MASK entry */ 322 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) * 323 sizeof(struct posix_acl_entry)); 324 acl->a_count = 3; 325 } 326 return 0; 327 } 328 329 /** 330 * nfsacl_decode - Decode an NFSv3 ACL 331 * 332 * @buf: xdr_buf containing XDR'd ACL data to decode 333 * @base: byte offset in xdr_buf where XDR'd ACL begins 334 * @aclcnt: count of ACEs in decoded posix_acl 335 * @pacl: buffer in which to place decoded posix_acl 336 * 337 * Returns the length of the decoded ACL in bytes, or a negative errno value. 338 */ 339 int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt, 340 struct posix_acl **pacl) 341 { 342 struct nfsacl_decode_desc nfsacl_desc = { 343 .desc = { 344 .elem_size = 12, 345 .xcode = pacl ? xdr_nfsace_decode : NULL, 346 }, 347 }; 348 u32 entries; 349 int err; 350 351 if (xdr_decode_word(buf, base, &entries) || 352 entries > NFS_ACL_MAX_ENTRIES) 353 return -EINVAL; 354 nfsacl_desc.desc.array_maxlen = entries; 355 err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc); 356 if (err) 357 return err; 358 if (pacl) { 359 if (entries != nfsacl_desc.desc.array_len || 360 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) { 361 posix_acl_release(nfsacl_desc.acl); 362 return -EINVAL; 363 } 364 *pacl = nfsacl_desc.acl; 365 } 366 if (aclcnt) 367 *aclcnt = entries; 368 return 8 + nfsacl_desc.desc.elem_size * 369 nfsacl_desc.desc.array_len; 370 } 371 EXPORT_SYMBOL_GPL(nfsacl_decode); 372 373 /** 374 * nfs_stream_decode_acl - Decode an NFSv3 ACL 375 * 376 * @xdr: an xdr_stream positioned at an encoded ACL 377 * @aclcnt: OUT: count of ACEs in decoded posix_acl 378 * @pacl: OUT: a dynamically-allocated buffer containing the decoded posix_acl 379 * 380 * Return values: 381 * %false: The encoded ACL is not valid 382 * %true: @pacl contains a decoded ACL, and @xdr is advanced 383 * 384 * On a successful return, caller must release *pacl using posix_acl_release(). 385 */ 386 bool nfs_stream_decode_acl(struct xdr_stream *xdr, unsigned int *aclcnt, 387 struct posix_acl **pacl) 388 { 389 const size_t elem_size = XDR_UNIT * 3; 390 struct nfsacl_decode_desc nfsacl_desc = { 391 .desc = { 392 .elem_size = elem_size, 393 .xcode = pacl ? xdr_nfsace_decode : NULL, 394 }, 395 }; 396 unsigned int base; 397 u32 entries; 398 399 if (xdr_stream_decode_u32(xdr, &entries) < 0) 400 return false; 401 if (entries > NFS_ACL_MAX_ENTRIES) 402 return false; 403 404 base = xdr_stream_pos(xdr); 405 if (!xdr_inline_decode(xdr, XDR_UNIT + elem_size * entries)) 406 return false; 407 nfsacl_desc.desc.array_maxlen = entries; 408 if (xdr_decode_array2(xdr->buf, base, &nfsacl_desc.desc)) 409 return false; 410 411 if (pacl) { 412 if (entries != nfsacl_desc.desc.array_len || 413 posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) { 414 posix_acl_release(nfsacl_desc.acl); 415 return false; 416 } 417 *pacl = nfsacl_desc.acl; 418 } 419 if (aclcnt) 420 *aclcnt = entries; 421 return true; 422 } 423 EXPORT_SYMBOL_GPL(nfs_stream_decode_acl); 424