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