symlink.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * symlink.c - operations for configfs symlinks.
  4. *
  5. * Based on sysfs:
  6. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  7. *
  8. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/module.h>
  12. #include <linux/namei.h>
  13. #include <linux/slab.h>
  14. #include <linux/configfs.h>
  15. #include "configfs_internal.h"
  16. /* Protects attachments of new symlinks */
  17. DEFINE_MUTEX(configfs_symlink_mutex);
  18. static int item_depth(struct config_item * item)
  19. {
  20. struct config_item * p = item;
  21. int depth = 0;
  22. do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
  23. return depth;
  24. }
  25. static int item_path_length(struct config_item * item)
  26. {
  27. struct config_item * p = item;
  28. int length = 1;
  29. do {
  30. length += strlen(config_item_name(p)) + 1;
  31. p = p->ci_parent;
  32. } while (p && !configfs_is_root(p));
  33. return length;
  34. }
  35. static void fill_item_path(struct config_item * item, char * buffer, int length)
  36. {
  37. struct config_item * p;
  38. --length;
  39. for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
  40. int cur = strlen(config_item_name(p));
  41. /* back up enough to print this bus id with '/' */
  42. length -= cur;
  43. memcpy(buffer + length, config_item_name(p), cur);
  44. *(buffer + --length) = '/';
  45. }
  46. }
  47. static int configfs_get_target_path(struct config_item *item,
  48. struct config_item *target, char *path)
  49. {
  50. int depth, size;
  51. char *s;
  52. depth = item_depth(item);
  53. size = item_path_length(target) + depth * 3 - 1;
  54. if (size > PATH_MAX)
  55. return -ENAMETOOLONG;
  56. pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
  57. for (s = path; depth--; s += 3)
  58. strcpy(s,"../");
  59. fill_item_path(target, path, size);
  60. pr_debug("%s: path = '%s'\n", __func__, path);
  61. return 0;
  62. }
  63. static int create_link(struct config_item *parent_item,
  64. struct config_item *item,
  65. struct dentry *dentry)
  66. {
  67. struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
  68. char *body;
  69. int ret;
  70. if (!configfs_dirent_is_ready(target_sd))
  71. return -ENOENT;
  72. body = kzalloc(PAGE_SIZE, GFP_KERNEL);
  73. if (!body)
  74. return -ENOMEM;
  75. configfs_get(target_sd);
  76. spin_lock(&configfs_dirent_lock);
  77. if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
  78. spin_unlock(&configfs_dirent_lock);
  79. configfs_put(target_sd);
  80. kfree(body);
  81. return -ENOENT;
  82. }
  83. target_sd->s_links++;
  84. spin_unlock(&configfs_dirent_lock);
  85. ret = configfs_get_target_path(parent_item, item, body);
  86. if (!ret)
  87. ret = configfs_create_link(target_sd, parent_item->ci_dentry,
  88. dentry, body);
  89. if (ret) {
  90. spin_lock(&configfs_dirent_lock);
  91. target_sd->s_links--;
  92. spin_unlock(&configfs_dirent_lock);
  93. configfs_put(target_sd);
  94. kfree(body);
  95. }
  96. return ret;
  97. }
  98. static int get_target(const char *symname, struct config_item **target,
  99. struct super_block *sb)
  100. {
  101. struct path path __free(path_put) = {};
  102. int ret;
  103. ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path);
  104. if (ret)
  105. return ret;
  106. if (path.dentry->d_sb != sb)
  107. return -EPERM;
  108. *target = configfs_get_config_item(path.dentry);
  109. if (!*target)
  110. return -ENOENT;
  111. return 0;
  112. }
  113. int configfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  114. struct dentry *dentry, const char *symname)
  115. {
  116. int ret;
  117. struct configfs_dirent *sd;
  118. struct config_item *parent_item;
  119. struct config_item *target_item = NULL;
  120. const struct config_item_type *type;
  121. sd = dentry->d_parent->d_fsdata;
  122. /*
  123. * Fake invisibility if dir belongs to a group/default groups hierarchy
  124. * being attached
  125. */
  126. if (!configfs_dirent_is_ready(sd))
  127. return -ENOENT;
  128. parent_item = configfs_get_config_item(dentry->d_parent);
  129. type = parent_item->ci_type;
  130. ret = -EPERM;
  131. if (!type || !type->ct_item_ops ||
  132. !type->ct_item_ops->allow_link)
  133. goto out_put;
  134. /*
  135. * This is really sick. What they wanted was a hybrid of
  136. * link(2) and symlink(2) - they wanted the target resolved
  137. * at syscall time (as link(2) would've done), be a directory
  138. * (which link(2) would've refused to do) *AND* be a deep
  139. * fucking magic, making the target busy from rmdir POV.
  140. * symlink(2) is nothing of that sort, and the locking it
  141. * gets matches the normal symlink(2) semantics. Without
  142. * attempts to resolve the target (which might very well
  143. * not even exist yet) done prior to locking the parent
  144. * directory. This perversion, OTOH, needs to resolve
  145. * the target, which would lead to obvious deadlocks if
  146. * attempted with any directories locked.
  147. *
  148. * Unfortunately, that garbage is userland ABI and we should've
  149. * said "no" back in 2005. Too late now, so we get to
  150. * play very ugly games with locking.
  151. *
  152. * Try *ANYTHING* of that sort in new code, and you will
  153. * really regret it. Just ask yourself - what could a BOFH
  154. * do to me and do I want to find it out first-hand?
  155. *
  156. * AV, a thoroughly annoyed bastard.
  157. */
  158. inode_unlock(dir);
  159. ret = get_target(symname, &target_item, dentry->d_sb);
  160. inode_lock(dir);
  161. if (ret)
  162. goto out_put;
  163. if (dentry->d_inode || d_unhashed(dentry))
  164. ret = -EEXIST;
  165. else
  166. ret = inode_permission(&nop_mnt_idmap, dir,
  167. MAY_WRITE | MAY_EXEC);
  168. if (!ret)
  169. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  170. if (!ret) {
  171. mutex_lock(&configfs_symlink_mutex);
  172. ret = create_link(parent_item, target_item, dentry);
  173. mutex_unlock(&configfs_symlink_mutex);
  174. if (ret && type->ct_item_ops->drop_link)
  175. type->ct_item_ops->drop_link(parent_item,
  176. target_item);
  177. }
  178. config_item_put(target_item);
  179. out_put:
  180. config_item_put(parent_item);
  181. return ret;
  182. }
  183. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  184. {
  185. struct configfs_dirent *sd = dentry->d_fsdata, *target_sd;
  186. struct config_item *parent_item;
  187. const struct config_item_type *type;
  188. int ret;
  189. ret = -EPERM; /* What lack-of-symlink returns */
  190. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  191. goto out;
  192. target_sd = sd->s_element;
  193. parent_item = configfs_get_config_item(dentry->d_parent);
  194. type = parent_item->ci_type;
  195. spin_lock(&configfs_dirent_lock);
  196. list_del_init(&sd->s_sibling);
  197. spin_unlock(&configfs_dirent_lock);
  198. configfs_drop_dentry(sd, dentry->d_parent);
  199. dput(dentry);
  200. configfs_put(sd);
  201. /*
  202. * drop_link() must be called before
  203. * decrementing target's ->s_links, so that the order of
  204. * drop_link(this, target) and drop_item(target) is preserved.
  205. */
  206. if (type && type->ct_item_ops &&
  207. type->ct_item_ops->drop_link)
  208. type->ct_item_ops->drop_link(parent_item,
  209. target_sd->s_element);
  210. spin_lock(&configfs_dirent_lock);
  211. target_sd->s_links--;
  212. spin_unlock(&configfs_dirent_lock);
  213. configfs_put(target_sd);
  214. config_item_put(parent_item);
  215. ret = 0;
  216. out:
  217. return ret;
  218. }
  219. const struct inode_operations configfs_symlink_inode_operations = {
  220. .get_link = simple_get_link,
  221. .setattr = configfs_setattr,
  222. };