Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * fs/f2fs/acl.c
0004  *
0005  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
0006  *             http://www.samsung.com/
0007  *
0008  * Portions of this code from linux/fs/ext2/acl.c
0009  *
0010  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
0011  */
0012 #include <linux/f2fs_fs.h>
0013 #include "f2fs.h"
0014 #include "xattr.h"
0015 #include "acl.h"
0016 
0017 static inline size_t f2fs_acl_size(int count)
0018 {
0019     if (count <= 4) {
0020         return sizeof(struct f2fs_acl_header) +
0021             count * sizeof(struct f2fs_acl_entry_short);
0022     } else {
0023         return sizeof(struct f2fs_acl_header) +
0024             4 * sizeof(struct f2fs_acl_entry_short) +
0025             (count - 4) * sizeof(struct f2fs_acl_entry);
0026     }
0027 }
0028 
0029 static inline int f2fs_acl_count(size_t size)
0030 {
0031     ssize_t s;
0032 
0033     size -= sizeof(struct f2fs_acl_header);
0034     s = size - 4 * sizeof(struct f2fs_acl_entry_short);
0035     if (s < 0) {
0036         if (size % sizeof(struct f2fs_acl_entry_short))
0037             return -1;
0038         return size / sizeof(struct f2fs_acl_entry_short);
0039     } else {
0040         if (s % sizeof(struct f2fs_acl_entry))
0041             return -1;
0042         return s / sizeof(struct f2fs_acl_entry) + 4;
0043     }
0044 }
0045 
0046 static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
0047 {
0048     int i, count;
0049     struct posix_acl *acl;
0050     struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
0051     struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
0052     const char *end = value + size;
0053 
0054     if (size < sizeof(struct f2fs_acl_header))
0055         return ERR_PTR(-EINVAL);
0056 
0057     if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
0058         return ERR_PTR(-EINVAL);
0059 
0060     count = f2fs_acl_count(size);
0061     if (count < 0)
0062         return ERR_PTR(-EINVAL);
0063     if (count == 0)
0064         return NULL;
0065 
0066     acl = posix_acl_alloc(count, GFP_NOFS);
0067     if (!acl)
0068         return ERR_PTR(-ENOMEM);
0069 
0070     for (i = 0; i < count; i++) {
0071 
0072         if ((char *)entry > end)
0073             goto fail;
0074 
0075         acl->a_entries[i].e_tag  = le16_to_cpu(entry->e_tag);
0076         acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
0077 
0078         switch (acl->a_entries[i].e_tag) {
0079         case ACL_USER_OBJ:
0080         case ACL_GROUP_OBJ:
0081         case ACL_MASK:
0082         case ACL_OTHER:
0083             entry = (struct f2fs_acl_entry *)((char *)entry +
0084                     sizeof(struct f2fs_acl_entry_short));
0085             break;
0086 
0087         case ACL_USER:
0088             acl->a_entries[i].e_uid =
0089                 make_kuid(&init_user_ns,
0090                         le32_to_cpu(entry->e_id));
0091             entry = (struct f2fs_acl_entry *)((char *)entry +
0092                     sizeof(struct f2fs_acl_entry));
0093             break;
0094         case ACL_GROUP:
0095             acl->a_entries[i].e_gid =
0096                 make_kgid(&init_user_ns,
0097                         le32_to_cpu(entry->e_id));
0098             entry = (struct f2fs_acl_entry *)((char *)entry +
0099                     sizeof(struct f2fs_acl_entry));
0100             break;
0101         default:
0102             goto fail;
0103         }
0104     }
0105     if ((char *)entry != end)
0106         goto fail;
0107     return acl;
0108 fail:
0109     posix_acl_release(acl);
0110     return ERR_PTR(-EINVAL);
0111 }
0112 
0113 static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
0114                 const struct posix_acl *acl, size_t *size)
0115 {
0116     struct f2fs_acl_header *f2fs_acl;
0117     struct f2fs_acl_entry *entry;
0118     int i;
0119 
0120     f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
0121             acl->a_count * sizeof(struct f2fs_acl_entry),
0122             GFP_NOFS);
0123     if (!f2fs_acl)
0124         return ERR_PTR(-ENOMEM);
0125 
0126     f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
0127     entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
0128 
0129     for (i = 0; i < acl->a_count; i++) {
0130 
0131         entry->e_tag  = cpu_to_le16(acl->a_entries[i].e_tag);
0132         entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
0133 
0134         switch (acl->a_entries[i].e_tag) {
0135         case ACL_USER:
0136             entry->e_id = cpu_to_le32(
0137                     from_kuid(&init_user_ns,
0138                         acl->a_entries[i].e_uid));
0139             entry = (struct f2fs_acl_entry *)((char *)entry +
0140                     sizeof(struct f2fs_acl_entry));
0141             break;
0142         case ACL_GROUP:
0143             entry->e_id = cpu_to_le32(
0144                     from_kgid(&init_user_ns,
0145                         acl->a_entries[i].e_gid));
0146             entry = (struct f2fs_acl_entry *)((char *)entry +
0147                     sizeof(struct f2fs_acl_entry));
0148             break;
0149         case ACL_USER_OBJ:
0150         case ACL_GROUP_OBJ:
0151         case ACL_MASK:
0152         case ACL_OTHER:
0153             entry = (struct f2fs_acl_entry *)((char *)entry +
0154                     sizeof(struct f2fs_acl_entry_short));
0155             break;
0156         default:
0157             goto fail;
0158         }
0159     }
0160     *size = f2fs_acl_size(acl->a_count);
0161     return (void *)f2fs_acl;
0162 
0163 fail:
0164     kfree(f2fs_acl);
0165     return ERR_PTR(-EINVAL);
0166 }
0167 
0168 static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
0169                         struct page *dpage)
0170 {
0171     int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
0172     void *value = NULL;
0173     struct posix_acl *acl;
0174     int retval;
0175 
0176     if (type == ACL_TYPE_ACCESS)
0177         name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
0178 
0179     retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
0180     if (retval > 0) {
0181         value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
0182         if (!value)
0183             return ERR_PTR(-ENOMEM);
0184         retval = f2fs_getxattr(inode, name_index, "", value,
0185                             retval, dpage);
0186     }
0187 
0188     if (retval > 0)
0189         acl = f2fs_acl_from_disk(value, retval);
0190     else if (retval == -ENODATA)
0191         acl = NULL;
0192     else
0193         acl = ERR_PTR(retval);
0194     kfree(value);
0195 
0196     return acl;
0197 }
0198 
0199 struct posix_acl *f2fs_get_acl(struct inode *inode, int type, bool rcu)
0200 {
0201     if (rcu)
0202         return ERR_PTR(-ECHILD);
0203 
0204     return __f2fs_get_acl(inode, type, NULL);
0205 }
0206 
0207 static int f2fs_acl_update_mode(struct user_namespace *mnt_userns,
0208                 struct inode *inode, umode_t *mode_p,
0209                 struct posix_acl **acl)
0210 {
0211     umode_t mode = inode->i_mode;
0212     int error;
0213 
0214     if (is_inode_flag_set(inode, FI_ACL_MODE))
0215         mode = F2FS_I(inode)->i_acl_mode;
0216 
0217     error = posix_acl_equiv_mode(*acl, &mode);
0218     if (error < 0)
0219         return error;
0220     if (error == 0)
0221         *acl = NULL;
0222     if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) &&
0223         !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
0224         mode &= ~S_ISGID;
0225     *mode_p = mode;
0226     return 0;
0227 }
0228 
0229 static int __f2fs_set_acl(struct user_namespace *mnt_userns,
0230             struct inode *inode, int type,
0231             struct posix_acl *acl, struct page *ipage)
0232 {
0233     int name_index;
0234     void *value = NULL;
0235     size_t size = 0;
0236     int error;
0237     umode_t mode = inode->i_mode;
0238 
0239     switch (type) {
0240     case ACL_TYPE_ACCESS:
0241         name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
0242         if (acl && !ipage) {
0243             error = f2fs_acl_update_mode(mnt_userns, inode,
0244                                 &mode, &acl);
0245             if (error)
0246                 return error;
0247             set_acl_inode(inode, mode);
0248         }
0249         break;
0250 
0251     case ACL_TYPE_DEFAULT:
0252         name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
0253         if (!S_ISDIR(inode->i_mode))
0254             return acl ? -EACCES : 0;
0255         break;
0256 
0257     default:
0258         return -EINVAL;
0259     }
0260 
0261     if (acl) {
0262         value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
0263         if (IS_ERR(value)) {
0264             clear_inode_flag(inode, FI_ACL_MODE);
0265             return PTR_ERR(value);
0266         }
0267     }
0268 
0269     error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
0270 
0271     kfree(value);
0272     if (!error)
0273         set_cached_acl(inode, type, acl);
0274 
0275     clear_inode_flag(inode, FI_ACL_MODE);
0276     return error;
0277 }
0278 
0279 int f2fs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0280          struct posix_acl *acl, int type)
0281 {
0282     if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
0283         return -EIO;
0284 
0285     return __f2fs_set_acl(mnt_userns, inode, type, acl, NULL);
0286 }
0287 
0288 /*
0289  * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
0290  * are copied from posix_acl.c
0291  */
0292 static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
0293                             gfp_t flags)
0294 {
0295     struct posix_acl *clone = NULL;
0296 
0297     if (acl) {
0298         int size = sizeof(struct posix_acl) + acl->a_count *
0299                 sizeof(struct posix_acl_entry);
0300         clone = kmemdup(acl, size, flags);
0301         if (clone)
0302             refcount_set(&clone->a_refcount, 1);
0303     }
0304     return clone;
0305 }
0306 
0307 static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
0308 {
0309     struct posix_acl_entry *pa, *pe;
0310     struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
0311     umode_t mode = *mode_p;
0312     int not_equiv = 0;
0313 
0314     /* assert(atomic_read(acl->a_refcount) == 1); */
0315 
0316     FOREACH_ACL_ENTRY(pa, acl, pe) {
0317         switch (pa->e_tag) {
0318         case ACL_USER_OBJ:
0319             pa->e_perm &= (mode >> 6) | ~S_IRWXO;
0320             mode &= (pa->e_perm << 6) | ~S_IRWXU;
0321             break;
0322 
0323         case ACL_USER:
0324         case ACL_GROUP:
0325             not_equiv = 1;
0326             break;
0327 
0328         case ACL_GROUP_OBJ:
0329             group_obj = pa;
0330             break;
0331 
0332         case ACL_OTHER:
0333             pa->e_perm &= mode | ~S_IRWXO;
0334             mode &= pa->e_perm | ~S_IRWXO;
0335             break;
0336 
0337         case ACL_MASK:
0338             mask_obj = pa;
0339             not_equiv = 1;
0340             break;
0341 
0342         default:
0343             return -EIO;
0344         }
0345     }
0346 
0347     if (mask_obj) {
0348         mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
0349         mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
0350     } else {
0351         if (!group_obj)
0352             return -EIO;
0353         group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
0354         mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
0355     }
0356 
0357     *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
0358     return not_equiv;
0359 }
0360 
0361 static int f2fs_acl_create(struct inode *dir, umode_t *mode,
0362         struct posix_acl **default_acl, struct posix_acl **acl,
0363         struct page *dpage)
0364 {
0365     struct posix_acl *p;
0366     struct posix_acl *clone;
0367     int ret;
0368 
0369     *acl = NULL;
0370     *default_acl = NULL;
0371 
0372     if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
0373         return 0;
0374 
0375     p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
0376     if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
0377         *mode &= ~current_umask();
0378         return 0;
0379     }
0380     if (IS_ERR(p))
0381         return PTR_ERR(p);
0382 
0383     clone = f2fs_acl_clone(p, GFP_NOFS);
0384     if (!clone) {
0385         ret = -ENOMEM;
0386         goto release_acl;
0387     }
0388 
0389     ret = f2fs_acl_create_masq(clone, mode);
0390     if (ret < 0)
0391         goto release_clone;
0392 
0393     if (ret == 0)
0394         posix_acl_release(clone);
0395     else
0396         *acl = clone;
0397 
0398     if (!S_ISDIR(*mode))
0399         posix_acl_release(p);
0400     else
0401         *default_acl = p;
0402 
0403     return 0;
0404 
0405 release_clone:
0406     posix_acl_release(clone);
0407 release_acl:
0408     posix_acl_release(p);
0409     return ret;
0410 }
0411 
0412 int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
0413                             struct page *dpage)
0414 {
0415     struct posix_acl *default_acl = NULL, *acl = NULL;
0416     int error;
0417 
0418     error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
0419     if (error)
0420         return error;
0421 
0422     f2fs_mark_inode_dirty_sync(inode, true);
0423 
0424     if (default_acl) {
0425         error = __f2fs_set_acl(NULL, inode, ACL_TYPE_DEFAULT, default_acl,
0426                        ipage);
0427         posix_acl_release(default_acl);
0428     } else {
0429         inode->i_default_acl = NULL;
0430     }
0431     if (acl) {
0432         if (!error)
0433             error = __f2fs_set_acl(NULL, inode, ACL_TYPE_ACCESS, acl,
0434                            ipage);
0435         posix_acl_release(acl);
0436     } else {
0437         inode->i_acl = NULL;
0438     }
0439 
0440     return error;
0441 }