LCOV - code coverage report
Current view: top level - fs/kernfs - symlink.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 62 68 91.2 %
Date: 2021-04-22 12:43:58 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * fs/kernfs/symlink.c - kernfs symlink 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/fs.h>
      11             : #include <linux/gfp.h>
      12             : #include <linux/namei.h>
      13             : 
      14             : #include "kernfs-internal.h"
      15             : 
      16             : /**
      17             :  * kernfs_create_link - create a symlink
      18             :  * @parent: directory to create the symlink in
      19             :  * @name: name of the symlink
      20             :  * @target: target node for the symlink to point to
      21             :  *
      22             :  * Returns the created node on success, ERR_PTR() value on error.
      23             :  * Ownership of the link matches ownership of the target.
      24             :  */
      25         449 : struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
      26             :                                        const char *name,
      27             :                                        struct kernfs_node *target)
      28             : {
      29         449 :         struct kernfs_node *kn;
      30         449 :         int error;
      31         449 :         kuid_t uid = GLOBAL_ROOT_UID;
      32         449 :         kgid_t gid = GLOBAL_ROOT_GID;
      33             : 
      34         449 :         if (target->iattr) {
      35           0 :                 uid = target->iattr->ia_uid;
      36           0 :                 gid = target->iattr->ia_gid;
      37             :         }
      38             : 
      39         449 :         kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
      40             :                              KERNFS_LINK);
      41         449 :         if (!kn)
      42         449 :                 return ERR_PTR(-ENOMEM);
      43             : 
      44         449 :         if (kernfs_ns_enabled(parent))
      45           2 :                 kn->ns = target->ns;
      46         449 :         kn->symlink.target_kn = target;
      47         449 :         kernfs_get(target);     /* ref owned by symlink */
      48             : 
      49         449 :         error = kernfs_add_one(kn);
      50         449 :         if (!error)
      51             :                 return kn;
      52             : 
      53           0 :         kernfs_put(kn);
      54           0 :         return ERR_PTR(error);
      55             : }
      56             : 
      57         915 : static int kernfs_get_target_path(struct kernfs_node *parent,
      58             :                                   struct kernfs_node *target, char *path)
      59             : {
      60         915 :         struct kernfs_node *base, *kn;
      61         915 :         char *s = path;
      62         915 :         int len = 0;
      63             : 
      64             :         /* go up to the root, stop at the base */
      65         915 :         base = parent;
      66        3760 :         while (base->parent) {
      67        2845 :                 kn = target->parent;
      68       10655 :                 while (kn->parent && base != kn)
      69             :                         kn = kn->parent;
      70             : 
      71        2845 :                 if (base == kn)
      72             :                         break;
      73             : 
      74        2845 :                 if ((s - path) + 3 >= PATH_MAX)
      75             :                         return -ENAMETOOLONG;
      76             : 
      77        2845 :                 strcpy(s, "../");
      78        2845 :                 s += 3;
      79        2845 :                 base = base->parent;
      80             :         }
      81             : 
      82             :         /* determine end of target string for reverse fillup */
      83             :         kn = target;
      84        5212 :         while (kn->parent && kn != base) {
      85        4297 :                 len += strlen(kn->name) + 1;
      86        4297 :                 kn = kn->parent;
      87             :         }
      88             : 
      89             :         /* check limits */
      90         915 :         if (len < 2)
      91             :                 return -EINVAL;
      92         915 :         len--;
      93         915 :         if ((s - path) + len >= PATH_MAX)
      94             :                 return -ENAMETOOLONG;
      95             : 
      96             :         /* reverse fillup of target string from target to base */
      97             :         kn = target;
      98        5212 :         while (kn->parent && kn != base) {
      99        4297 :                 int slen = strlen(kn->name);
     100             : 
     101        4297 :                 len -= slen;
     102        4297 :                 memcpy(s + len, kn->name, slen);
     103        4297 :                 if (len)
     104        3382 :                         s[--len] = '/';
     105             : 
     106        4297 :                 kn = kn->parent;
     107             :         }
     108             : 
     109             :         return 0;
     110             : }
     111             : 
     112         915 : static int kernfs_getlink(struct inode *inode, char *path)
     113             : {
     114         915 :         struct kernfs_node *kn = inode->i_private;
     115         915 :         struct kernfs_node *parent = kn->parent;
     116         915 :         struct kernfs_node *target = kn->symlink.target_kn;
     117         915 :         int error;
     118             : 
     119         915 :         mutex_lock(&kernfs_mutex);
     120         915 :         error = kernfs_get_target_path(parent, target, path);
     121         915 :         mutex_unlock(&kernfs_mutex);
     122             : 
     123         915 :         return error;
     124             : }
     125             : 
     126         915 : static const char *kernfs_iop_get_link(struct dentry *dentry,
     127             :                                        struct inode *inode,
     128             :                                        struct delayed_call *done)
     129             : {
     130         915 :         char *body;
     131         915 :         int error;
     132             : 
     133         915 :         if (!dentry)
     134         915 :                 return ERR_PTR(-ECHILD);
     135         915 :         body = kzalloc(PAGE_SIZE, GFP_KERNEL);
     136         915 :         if (!body)
     137         915 :                 return ERR_PTR(-ENOMEM);
     138         915 :         error = kernfs_getlink(inode, body);
     139         915 :         if (unlikely(error < 0)) {
     140           0 :                 kfree(body);
     141           0 :                 return ERR_PTR(error);
     142             :         }
     143         915 :         set_delayed_call(done, kfree_link, body);
     144         915 :         return body;
     145             : }
     146             : 
     147             : const struct inode_operations kernfs_symlink_iops = {
     148             :         .listxattr      = kernfs_iop_listxattr,
     149             :         .get_link       = kernfs_iop_get_link,
     150             :         .setattr        = kernfs_iop_setattr,
     151             :         .getattr        = kernfs_iop_getattr,
     152             :         .permission     = kernfs_iop_permission,
     153             : };

Generated by: LCOV version 1.14