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