symlink.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 path *path,
  99. struct config_item **target, struct super_block *sb)
  100. {
  101. int ret;
  102. ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
  103. if (!ret) {
  104. if (path->dentry->d_sb == sb) {
  105. *target = configfs_get_config_item(path->dentry);
  106. if (!*target) {
  107. ret = -ENOENT;
  108. path_put(path);
  109. }
  110. } else {
  111. ret = -EPERM;
  112. path_put(path);
  113. }
  114. }
  115. return ret;
  116. }
  117. int configfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
  118. struct dentry *dentry, const char *symname)
  119. {
  120. int ret;
  121. struct path path;
  122. struct configfs_dirent *sd;
  123. struct config_item *parent_item;
  124. struct config_item *target_item = NULL;
  125. const struct config_item_type *type;
  126. sd = dentry->d_parent->d_fsdata;
  127. /*
  128. * Fake invisibility if dir belongs to a group/default groups hierarchy
  129. * being attached
  130. */
  131. if (!configfs_dirent_is_ready(sd))
  132. return -ENOENT;
  133. parent_item = configfs_get_config_item(dentry->d_parent);
  134. type = parent_item->ci_type;
  135. ret = -EPERM;
  136. if (!type || !type->ct_item_ops ||
  137. !type->ct_item_ops->allow_link)
  138. goto out_put;
  139. /*
  140. * This is really sick. What they wanted was a hybrid of
  141. * link(2) and symlink(2) - they wanted the target resolved
  142. * at syscall time (as link(2) would've done), be a directory
  143. * (which link(2) would've refused to do) *AND* be a deep
  144. * fucking magic, making the target busy from rmdir POV.
  145. * symlink(2) is nothing of that sort, and the locking it
  146. * gets matches the normal symlink(2) semantics. Without
  147. * attempts to resolve the target (which might very well
  148. * not even exist yet) done prior to locking the parent
  149. * directory. This perversion, OTOH, needs to resolve
  150. * the target, which would lead to obvious deadlocks if
  151. * attempted with any directories locked.
  152. *
  153. * Unfortunately, that garbage is userland ABI and we should've
  154. * said "no" back in 2005. Too late now, so we get to
  155. * play very ugly games with locking.
  156. *
  157. * Try *ANYTHING* of that sort in new code, and you will
  158. * really regret it. Just ask yourself - what could a BOFH
  159. * do to me and do I want to find it out first-hand?
  160. *
  161. * AV, a thoroughly annoyed bastard.
  162. */
  163. inode_unlock(dir);
  164. ret = get_target(symname, &path, &target_item, dentry->d_sb);
  165. inode_lock(dir);
  166. if (ret)
  167. goto out_put;
  168. if (dentry->d_inode || d_unhashed(dentry))
  169. ret = -EEXIST;
  170. else
  171. ret = inode_permission(&init_user_ns, dir,
  172. MAY_WRITE | MAY_EXEC);
  173. if (!ret)
  174. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  175. if (!ret) {
  176. mutex_lock(&configfs_symlink_mutex);
  177. ret = create_link(parent_item, target_item, dentry);
  178. mutex_unlock(&configfs_symlink_mutex);
  179. if (ret && type->ct_item_ops->drop_link)
  180. type->ct_item_ops->drop_link(parent_item,
  181. target_item);
  182. }
  183. config_item_put(target_item);
  184. path_put(&path);
  185. out_put:
  186. config_item_put(parent_item);
  187. return ret;
  188. }
  189. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  190. {
  191. struct configfs_dirent *sd = dentry->d_fsdata, *target_sd;
  192. struct config_item *parent_item;
  193. const struct config_item_type *type;
  194. int ret;
  195. ret = -EPERM; /* What lack-of-symlink returns */
  196. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  197. goto out;
  198. target_sd = sd->s_element;
  199. parent_item = configfs_get_config_item(dentry->d_parent);
  200. type = parent_item->ci_type;
  201. spin_lock(&configfs_dirent_lock);
  202. list_del_init(&sd->s_sibling);
  203. spin_unlock(&configfs_dirent_lock);
  204. configfs_drop_dentry(sd, dentry->d_parent);
  205. dput(dentry);
  206. configfs_put(sd);
  207. /*
  208. * drop_link() must be called before
  209. * decrementing target's ->s_links, so that the order of
  210. * drop_link(this, target) and drop_item(target) is preserved.
  211. */
  212. if (type && type->ct_item_ops &&
  213. type->ct_item_ops->drop_link)
  214. type->ct_item_ops->drop_link(parent_item,
  215. target_sd->s_element);
  216. spin_lock(&configfs_dirent_lock);
  217. target_sd->s_links--;
  218. spin_unlock(&configfs_dirent_lock);
  219. configfs_put(target_sd);
  220. config_item_put(parent_item);
  221. ret = 0;
  222. out:
  223. return ret;
  224. }
  225. const struct inode_operations configfs_symlink_inode_operations = {
  226. .get_link = simple_get_link,
  227. .setattr = configfs_setattr,
  228. };