LCOV - code coverage report
Current view: top level - fs/kernfs - inode.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 140 199 70.4 %
Date: 2021-04-22 12:43:58 Functions: 14 19 73.7 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * fs/kernfs/inode.c - kernfs inode implementation
       4             :  *
       5             :  * Copyright (c) 2001-3 Patrick Mochel
       6             :  * Copyright (c) 2007 SUSE Linux Products GmbH
       7             :  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
       8             :  */
       9             : 
      10             : #include <linux/pagemap.h>
      11             : #include <linux/backing-dev.h>
      12             : #include <linux/capability.h>
      13             : #include <linux/errno.h>
      14             : #include <linux/slab.h>
      15             : #include <linux/xattr.h>
      16             : #include <linux/security.h>
      17             : 
      18             : #include "kernfs-internal.h"
      19             : 
      20             : static const struct address_space_operations kernfs_aops = {
      21             :         .readpage       = simple_readpage,
      22             :         .write_begin    = simple_write_begin,
      23             :         .write_end      = simple_write_end,
      24             : };
      25             : 
      26             : static const struct inode_operations kernfs_iops = {
      27             :         .permission     = kernfs_iop_permission,
      28             :         .setattr        = kernfs_iop_setattr,
      29             :         .getattr        = kernfs_iop_getattr,
      30             :         .listxattr      = kernfs_iop_listxattr,
      31             : };
      32             : 
      33          69 : static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
      34             : {
      35          69 :         static DEFINE_MUTEX(iattr_mutex);
      36          69 :         struct kernfs_iattrs *ret;
      37             : 
      38          69 :         mutex_lock(&iattr_mutex);
      39             : 
      40          69 :         if (kn->iattr || !alloc)
      41          15 :                 goto out_unlock;
      42             : 
      43          54 :         kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
      44          54 :         if (!kn->iattr)
      45           0 :                 goto out_unlock;
      46             : 
      47             :         /* assign default attributes */
      48          54 :         kn->iattr->ia_uid = GLOBAL_ROOT_UID;
      49          54 :         kn->iattr->ia_gid = GLOBAL_ROOT_GID;
      50             : 
      51          54 :         ktime_get_real_ts64(&kn->iattr->ia_atime);
      52          54 :         kn->iattr->ia_mtime = kn->iattr->ia_atime;
      53          54 :         kn->iattr->ia_ctime = kn->iattr->ia_atime;
      54             : 
      55          54 :         simple_xattrs_init(&kn->iattr->xattrs);
      56          54 :         atomic_set(&kn->iattr->nr_user_xattrs, 0);
      57          54 :         atomic_set(&kn->iattr->user_xattr_size, 0);
      58          69 : out_unlock:
      59          69 :         ret = kn->iattr;
      60          69 :         mutex_unlock(&iattr_mutex);
      61          69 :         return ret;
      62             : }
      63             : 
      64          63 : static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
      65             : {
      66          63 :         return __kernfs_iattrs(kn, 1);
      67             : }
      68             : 
      69           6 : static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
      70             : {
      71           6 :         return __kernfs_iattrs(kn, 0);
      72             : }
      73             : 
      74          16 : int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
      75             : {
      76          16 :         struct kernfs_iattrs *attrs;
      77          16 :         unsigned int ia_valid = iattr->ia_valid;
      78             : 
      79          16 :         attrs = kernfs_iattrs(kn);
      80          16 :         if (!attrs)
      81             :                 return -ENOMEM;
      82             : 
      83          16 :         if (ia_valid & ATTR_UID)
      84           8 :                 attrs->ia_uid = iattr->ia_uid;
      85          16 :         if (ia_valid & ATTR_GID)
      86           8 :                 attrs->ia_gid = iattr->ia_gid;
      87          16 :         if (ia_valid & ATTR_ATIME)
      88           0 :                 attrs->ia_atime = iattr->ia_atime;
      89          16 :         if (ia_valid & ATTR_MTIME)
      90           0 :                 attrs->ia_mtime = iattr->ia_mtime;
      91          16 :         if (ia_valid & ATTR_CTIME)
      92          16 :                 attrs->ia_ctime = iattr->ia_ctime;
      93          16 :         if (ia_valid & ATTR_MODE)
      94           8 :                 kn->mode = iattr->ia_mode;
      95             :         return 0;
      96             : }
      97             : 
      98             : /**
      99             :  * kernfs_setattr - set iattr on a node
     100             :  * @kn: target node
     101             :  * @iattr: iattr to set
     102             :  *
     103             :  * Returns 0 on success, -errno on failure.
     104             :  */
     105           0 : int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
     106             : {
     107           0 :         int ret;
     108             : 
     109           0 :         mutex_lock(&kernfs_mutex);
     110           0 :         ret = __kernfs_setattr(kn, iattr);
     111           0 :         mutex_unlock(&kernfs_mutex);
     112           0 :         return ret;
     113             : }
     114             : 
     115          16 : int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
     116             :                        struct iattr *iattr)
     117             : {
     118          16 :         struct inode *inode = d_inode(dentry);
     119          16 :         struct kernfs_node *kn = inode->i_private;
     120          16 :         int error;
     121             : 
     122          16 :         if (!kn)
     123             :                 return -EINVAL;
     124             : 
     125          16 :         mutex_lock(&kernfs_mutex);
     126          16 :         error = setattr_prepare(&init_user_ns, dentry, iattr);
     127          16 :         if (error)
     128           0 :                 goto out;
     129             : 
     130          16 :         error = __kernfs_setattr(kn, iattr);
     131          16 :         if (error)
     132           0 :                 goto out;
     133             : 
     134             :         /* this ignores size changes */
     135          16 :         setattr_copy(&init_user_ns, inode, iattr);
     136             : 
     137          16 : out:
     138          16 :         mutex_unlock(&kernfs_mutex);
     139          16 :         return error;
     140             : }
     141             : 
     142           0 : ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
     143             : {
     144           0 :         struct kernfs_node *kn = kernfs_dentry_node(dentry);
     145           0 :         struct kernfs_iattrs *attrs;
     146             : 
     147           0 :         attrs = kernfs_iattrs(kn);
     148           0 :         if (!attrs)
     149             :                 return -ENOMEM;
     150             : 
     151           0 :         return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
     152             : }
     153             : 
     154        1359 : static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
     155             : {
     156        1359 :         inode->i_mode = mode;
     157        2718 :         inode->i_atime = inode->i_mtime =
     158        1359 :                 inode->i_ctime = current_time(inode);
     159        1359 : }
     160             : 
     161         857 : static inline void set_inode_attr(struct inode *inode,
     162             :                                   struct kernfs_iattrs *attrs)
     163             : {
     164         857 :         inode->i_uid = attrs->ia_uid;
     165         857 :         inode->i_gid = attrs->ia_gid;
     166         857 :         inode->i_atime = attrs->ia_atime;
     167         857 :         inode->i_mtime = attrs->ia_mtime;
     168         857 :         inode->i_ctime = attrs->ia_ctime;
     169         857 : }
     170             : 
     171       34330 : static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
     172             : {
     173       34330 :         struct kernfs_iattrs *attrs = kn->iattr;
     174             : 
     175       34330 :         inode->i_mode = kn->mode;
     176       34330 :         if (attrs)
     177             :                 /*
     178             :                  * kernfs_node has non-default attributes get them from
     179             :                  * persistent copy in kernfs_node.
     180             :                  */
     181         857 :                 set_inode_attr(inode, attrs);
     182             : 
     183       34330 :         if (kernfs_type(kn) == KERNFS_DIR)
     184       29945 :                 set_nlink(inode, kn->dir.subdirs + 2);
     185       34330 : }
     186             : 
     187        6751 : int kernfs_iop_getattr(struct user_namespace *mnt_userns,
     188             :                        const struct path *path, struct kstat *stat,
     189             :                        u32 request_mask, unsigned int query_flags)
     190             : {
     191        6751 :         struct inode *inode = d_inode(path->dentry);
     192        6751 :         struct kernfs_node *kn = inode->i_private;
     193             : 
     194        6751 :         mutex_lock(&kernfs_mutex);
     195        6752 :         kernfs_refresh_inode(kn, inode);
     196        6752 :         mutex_unlock(&kernfs_mutex);
     197             : 
     198        6752 :         generic_fillattr(&init_user_ns, inode, stat);
     199        6752 :         return 0;
     200             : }
     201             : 
     202        1359 : static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
     203             : {
     204        1359 :         kernfs_get(kn);
     205        1359 :         inode->i_private = kn;
     206        1359 :         inode->i_mapping->a_ops = &kernfs_aops;
     207        1359 :         inode->i_op = &kernfs_iops;
     208        1359 :         inode->i_generation = kernfs_gen(kn);
     209             : 
     210        1359 :         set_default_inode_attr(inode, kn->mode);
     211        1359 :         kernfs_refresh_inode(kn, inode);
     212             : 
     213             :         /* initialize inode according to type */
     214        1359 :         switch (kernfs_type(kn)) {
     215         373 :         case KERNFS_DIR:
     216         373 :                 inode->i_op = &kernfs_dir_iops;
     217         373 :                 inode->i_fop = &kernfs_dir_fops;
     218         373 :                 if (kn->flags & KERNFS_EMPTY_DIR)
     219           3 :                         make_empty_dir_inode(inode);
     220             :                 break;
     221         675 :         case KERNFS_FILE:
     222         675 :                 inode->i_size = kn->attr.size;
     223         675 :                 inode->i_fop = &kernfs_file_fops;
     224         675 :                 break;
     225         311 :         case KERNFS_LINK:
     226         311 :                 inode->i_op = &kernfs_symlink_iops;
     227         311 :                 break;
     228           0 :         default:
     229           0 :                 BUG();
     230             :         }
     231             : 
     232        1359 :         unlock_new_inode(inode);
     233        1359 : }
     234             : 
     235             : /**
     236             :  *      kernfs_get_inode - get inode for kernfs_node
     237             :  *      @sb: super block
     238             :  *      @kn: kernfs_node to allocate inode for
     239             :  *
     240             :  *      Get inode for @kn.  If such inode doesn't exist, a new inode is
     241             :  *      allocated and basics are initialized.  New inode is returned
     242             :  *      locked.
     243             :  *
     244             :  *      LOCKING:
     245             :  *      Kernel thread context (may sleep).
     246             :  *
     247             :  *      RETURNS:
     248             :  *      Pointer to allocated inode on success, NULL on failure.
     249             :  */
     250        1459 : struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
     251             : {
     252        1459 :         struct inode *inode;
     253             : 
     254        1459 :         inode = iget_locked(sb, kernfs_ino(kn));
     255        1459 :         if (inode && (inode->i_state & I_NEW))
     256        1359 :                 kernfs_init_inode(kn, inode);
     257             : 
     258        1459 :         return inode;
     259             : }
     260             : 
     261             : /*
     262             :  * The kernfs_node serves as both an inode and a directory entry for
     263             :  * kernfs.  To prevent the kernfs inode numbers from being freed
     264             :  * prematurely we take a reference to kernfs_node from the kernfs inode.  A
     265             :  * super_operations.evict_inode() implementation is needed to drop that
     266             :  * reference upon inode destruction.
     267             :  */
     268         460 : void kernfs_evict_inode(struct inode *inode)
     269             : {
     270         460 :         struct kernfs_node *kn = inode->i_private;
     271             : 
     272         460 :         truncate_inode_pages_final(&inode->i_data);
     273         460 :         clear_inode(inode);
     274         460 :         kernfs_put(kn);
     275         460 : }
     276             : 
     277       35806 : int kernfs_iop_permission(struct user_namespace *mnt_userns,
     278             :                           struct inode *inode, int mask)
     279             : {
     280       35806 :         struct kernfs_node *kn;
     281             : 
     282       35806 :         if (mask & MAY_NOT_BLOCK)
     283             :                 return -ECHILD;
     284             : 
     285       26219 :         kn = inode->i_private;
     286             : 
     287       26219 :         mutex_lock(&kernfs_mutex);
     288       26219 :         kernfs_refresh_inode(kn, inode);
     289       26219 :         mutex_unlock(&kernfs_mutex);
     290             : 
     291       26219 :         return generic_permission(&init_user_ns, inode, mask);
     292             : }
     293             : 
     294           6 : int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
     295             :                      void *value, size_t size)
     296             : {
     297           6 :         struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
     298           6 :         if (!attrs)
     299             :                 return -ENODATA;
     300             : 
     301           6 :         return simple_xattr_get(&attrs->xattrs, name, value, size);
     302             : }
     303             : 
     304          47 : int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
     305             :                      const void *value, size_t size, int flags)
     306             : {
     307          47 :         struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
     308          47 :         if (!attrs)
     309             :                 return -ENOMEM;
     310             : 
     311          47 :         return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
     312             : }
     313             : 
     314           6 : static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
     315             :                                 struct dentry *unused, struct inode *inode,
     316             :                                 const char *suffix, void *value, size_t size)
     317             : {
     318           6 :         const char *name = xattr_full_name(handler, suffix);
     319           6 :         struct kernfs_node *kn = inode->i_private;
     320             : 
     321           6 :         return kernfs_xattr_get(kn, name, value, size);
     322             : }
     323             : 
     324          47 : static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
     325             :                                 struct user_namespace *mnt_userns,
     326             :                                 struct dentry *unused, struct inode *inode,
     327             :                                 const char *suffix, const void *value,
     328             :                                 size_t size, int flags)
     329             : {
     330          47 :         const char *name = xattr_full_name(handler, suffix);
     331          47 :         struct kernfs_node *kn = inode->i_private;
     332             : 
     333          47 :         return kernfs_xattr_set(kn, name, value, size, flags);
     334             : }
     335             : 
     336           0 : static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
     337             :                                      const char *full_name,
     338             :                                      struct simple_xattrs *xattrs,
     339             :                                      const void *value, size_t size, int flags)
     340             : {
     341           0 :         atomic_t *sz = &kn->iattr->user_xattr_size;
     342           0 :         atomic_t *nr = &kn->iattr->nr_user_xattrs;
     343           0 :         ssize_t removed_size;
     344           0 :         int ret;
     345             : 
     346           0 :         if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
     347           0 :                 ret = -ENOSPC;
     348           0 :                 goto dec_count_out;
     349             :         }
     350             : 
     351           0 :         if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
     352           0 :                 ret = -ENOSPC;
     353           0 :                 goto dec_size_out;
     354             :         }
     355             : 
     356           0 :         ret = simple_xattr_set(xattrs, full_name, value, size, flags,
     357             :                                &removed_size);
     358             : 
     359           0 :         if (!ret && removed_size >= 0)
     360           0 :                 size = removed_size;
     361           0 :         else if (!ret)
     362             :                 return 0;
     363           0 : dec_size_out:
     364           0 :         atomic_sub(size, sz);
     365           0 : dec_count_out:
     366           0 :         atomic_dec(nr);
     367           0 :         return ret;
     368             : }
     369             : 
     370           0 : static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
     371             :                                     const char *full_name,
     372             :                                     struct simple_xattrs *xattrs,
     373             :                                     const void *value, size_t size, int flags)
     374             : {
     375           0 :         atomic_t *sz = &kn->iattr->user_xattr_size;
     376           0 :         atomic_t *nr = &kn->iattr->nr_user_xattrs;
     377           0 :         ssize_t removed_size;
     378           0 :         int ret;
     379             : 
     380           0 :         ret = simple_xattr_set(xattrs, full_name, value, size, flags,
     381             :                                &removed_size);
     382             : 
     383           0 :         if (removed_size >= 0) {
     384           0 :                 atomic_sub(removed_size, sz);
     385           0 :                 atomic_dec(nr);
     386             :         }
     387             : 
     388           0 :         return ret;
     389             : }
     390             : 
     391           0 : static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
     392             :                                      struct user_namespace *mnt_userns,
     393             :                                      struct dentry *unused, struct inode *inode,
     394             :                                      const char *suffix, const void *value,
     395             :                                      size_t size, int flags)
     396             : {
     397           0 :         const char *full_name = xattr_full_name(handler, suffix);
     398           0 :         struct kernfs_node *kn = inode->i_private;
     399           0 :         struct kernfs_iattrs *attrs;
     400             : 
     401           0 :         if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
     402             :                 return -EOPNOTSUPP;
     403             : 
     404           0 :         attrs = kernfs_iattrs(kn);
     405           0 :         if (!attrs)
     406             :                 return -ENOMEM;
     407             : 
     408           0 :         if (value)
     409           0 :                 return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
     410             :                                                  value, size, flags);
     411             :         else
     412           0 :                 return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
     413             :                                                 value, size, flags);
     414             : 
     415             : }
     416             : 
     417             : static const struct xattr_handler kernfs_trusted_xattr_handler = {
     418             :         .prefix = XATTR_TRUSTED_PREFIX,
     419             :         .get = kernfs_vfs_xattr_get,
     420             :         .set = kernfs_vfs_xattr_set,
     421             : };
     422             : 
     423             : static const struct xattr_handler kernfs_security_xattr_handler = {
     424             :         .prefix = XATTR_SECURITY_PREFIX,
     425             :         .get = kernfs_vfs_xattr_get,
     426             :         .set = kernfs_vfs_xattr_set,
     427             : };
     428             : 
     429             : static const struct xattr_handler kernfs_user_xattr_handler = {
     430             :         .prefix = XATTR_USER_PREFIX,
     431             :         .get = kernfs_vfs_xattr_get,
     432             :         .set = kernfs_vfs_user_xattr_set,
     433             : };
     434             : 
     435             : const struct xattr_handler *kernfs_xattr_handlers[] = {
     436             :         &kernfs_trusted_xattr_handler,
     437             :         &kernfs_security_xattr_handler,
     438             :         &kernfs_user_xattr_handler,
     439             :         NULL
     440             : };

Generated by: LCOV version 1.14