]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/configfs/symlink.c
configfs: fix a deadlock in configfs_symlink()
[linux.git] / fs / configfs / symlink.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3  * vim: noexpandtab sw=8 ts=8 sts=0:
4  *
5  * symlink.c - operations for configfs symlinks.
6  *
7  * Based on sysfs:
8  *      sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9  *
10  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
11  */
12
13 #include <linux/fs.h>
14 #include <linux/module.h>
15 #include <linux/namei.h>
16 #include <linux/slab.h>
17
18 #include <linux/configfs.h>
19 #include "configfs_internal.h"
20
21 /* Protects attachments of new symlinks */
22 DEFINE_MUTEX(configfs_symlink_mutex);
23
24 static int item_depth(struct config_item * item)
25 {
26         struct config_item * p = item;
27         int depth = 0;
28         do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
29         return depth;
30 }
31
32 static int item_path_length(struct config_item * item)
33 {
34         struct config_item * p = item;
35         int length = 1;
36         do {
37                 length += strlen(config_item_name(p)) + 1;
38                 p = p->ci_parent;
39         } while (p && !configfs_is_root(p));
40         return length;
41 }
42
43 static void fill_item_path(struct config_item * item, char * buffer, int length)
44 {
45         struct config_item * p;
46
47         --length;
48         for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
49                 int cur = strlen(config_item_name(p));
50
51                 /* back up enough to print this bus id with '/' */
52                 length -= cur;
53                 memcpy(buffer + length, config_item_name(p), cur);
54                 *(buffer + --length) = '/';
55         }
56 }
57
58 static int create_link(struct config_item *parent_item,
59                        struct config_item *item,
60                        struct dentry *dentry)
61 {
62         struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
63         struct configfs_symlink *sl;
64         int ret;
65
66         ret = -ENOENT;
67         if (!configfs_dirent_is_ready(target_sd))
68                 goto out;
69         ret = -ENOMEM;
70         sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
71         if (sl) {
72                 spin_lock(&configfs_dirent_lock);
73                 if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
74                         spin_unlock(&configfs_dirent_lock);
75                         kfree(sl);
76                         return -ENOENT;
77                 }
78                 sl->sl_target = config_item_get(item);
79                 list_add(&sl->sl_list, &target_sd->s_links);
80                 spin_unlock(&configfs_dirent_lock);
81                 ret = configfs_create_link(sl, parent_item->ci_dentry,
82                                            dentry);
83                 if (ret) {
84                         spin_lock(&configfs_dirent_lock);
85                         list_del_init(&sl->sl_list);
86                         spin_unlock(&configfs_dirent_lock);
87                         config_item_put(item);
88                         kfree(sl);
89                 }
90         }
91
92 out:
93         return ret;
94 }
95
96
97 static int get_target(const char *symname, struct path *path,
98                       struct config_item **target, struct super_block *sb)
99 {
100         int ret;
101
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
116         return ret;
117 }
118
119
120 int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
121 {
122         int ret;
123         struct path path;
124         struct configfs_dirent *sd;
125         struct config_item *parent_item;
126         struct config_item *target_item = NULL;
127         const struct config_item_type *type;
128
129         sd = dentry->d_parent->d_fsdata;
130         /*
131          * Fake invisibility if dir belongs to a group/default groups hierarchy
132          * being attached
133          */
134         ret = -ENOENT;
135         if (!configfs_dirent_is_ready(sd))
136                 goto out;
137
138         parent_item = configfs_get_config_item(dentry->d_parent);
139         type = parent_item->ci_type;
140
141         ret = -EPERM;
142         if (!type || !type->ct_item_ops ||
143             !type->ct_item_ops->allow_link)
144                 goto out_put;
145
146         /*
147          * This is really sick.  What they wanted was a hybrid of
148          * link(2) and symlink(2) - they wanted the target resolved
149          * at syscall time (as link(2) would've done), be a directory
150          * (which link(2) would've refused to do) *AND* be a deep
151          * fucking magic, making the target busy from rmdir POV.
152          * symlink(2) is nothing of that sort, and the locking it
153          * gets matches the normal symlink(2) semantics.  Without
154          * attempts to resolve the target (which might very well
155          * not even exist yet) done prior to locking the parent
156          * directory.  This perversion, OTOH, needs to resolve
157          * the target, which would lead to obvious deadlocks if
158          * attempted with any directories locked.
159          *
160          * Unfortunately, that garbage is userland ABI and we should've
161          * said "no" back in 2005.  Too late now, so we get to
162          * play very ugly games with locking.
163          *
164          * Try *ANYTHING* of that sort in new code, and you will
165          * really regret it.  Just ask yourself - what could a BOFH
166          * do to me and do I want to find it out first-hand?
167          *
168          *  AV, a thoroughly annoyed bastard.
169          */
170         inode_unlock(dir);
171         ret = get_target(symname, &path, &target_item, dentry->d_sb);
172         inode_lock(dir);
173         if (ret)
174                 goto out_put;
175
176         if (dentry->d_inode || d_unhashed(dentry))
177                 ret = -EEXIST;
178         else
179                 ret = inode_permission(dir, MAY_WRITE | MAY_EXEC);
180         if (!ret)
181                 ret = type->ct_item_ops->allow_link(parent_item, target_item);
182         if (!ret) {
183                 mutex_lock(&configfs_symlink_mutex);
184                 ret = create_link(parent_item, target_item, dentry);
185                 mutex_unlock(&configfs_symlink_mutex);
186                 if (ret && type->ct_item_ops->drop_link)
187                         type->ct_item_ops->drop_link(parent_item,
188                                                      target_item);
189         }
190
191         config_item_put(target_item);
192         path_put(&path);
193
194 out_put:
195         config_item_put(parent_item);
196
197 out:
198         return ret;
199 }
200
201 int configfs_unlink(struct inode *dir, struct dentry *dentry)
202 {
203         struct configfs_dirent *sd = dentry->d_fsdata;
204         struct configfs_symlink *sl;
205         struct config_item *parent_item;
206         const struct config_item_type *type;
207         int ret;
208
209         ret = -EPERM;  /* What lack-of-symlink returns */
210         if (!(sd->s_type & CONFIGFS_ITEM_LINK))
211                 goto out;
212
213         sl = sd->s_element;
214
215         parent_item = configfs_get_config_item(dentry->d_parent);
216         type = parent_item->ci_type;
217
218         spin_lock(&configfs_dirent_lock);
219         list_del_init(&sd->s_sibling);
220         spin_unlock(&configfs_dirent_lock);
221         configfs_drop_dentry(sd, dentry->d_parent);
222         dput(dentry);
223         configfs_put(sd);
224
225         /*
226          * drop_link() must be called before
227          * list_del_init(&sl->sl_list), so that the order of
228          * drop_link(this, target) and drop_item(target) is preserved.
229          */
230         if (type && type->ct_item_ops &&
231             type->ct_item_ops->drop_link)
232                 type->ct_item_ops->drop_link(parent_item,
233                                                sl->sl_target);
234
235         spin_lock(&configfs_dirent_lock);
236         list_del_init(&sl->sl_list);
237         spin_unlock(&configfs_dirent_lock);
238
239         /* Put reference from create_link() */
240         config_item_put(sl->sl_target);
241         kfree(sl);
242
243         config_item_put(parent_item);
244
245         ret = 0;
246
247 out:
248         return ret;
249 }
250
251 static int configfs_get_target_path(struct config_item * item, struct config_item * target,
252                                    char *path)
253 {
254         char * s;
255         int depth, size;
256
257         depth = item_depth(item);
258         size = item_path_length(target) + depth * 3 - 1;
259         if (size > PATH_MAX)
260                 return -ENAMETOOLONG;
261
262         pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
263
264         for (s = path; depth--; s += 3)
265                 strcpy(s,"../");
266
267         fill_item_path(target, path, size);
268         pr_debug("%s: path = '%s'\n", __func__, path);
269
270         return 0;
271 }
272
273 static int configfs_getlink(struct dentry *dentry, char * path)
274 {
275         struct config_item *item, *target_item;
276         int error = 0;
277
278         item = configfs_get_config_item(dentry->d_parent);
279         if (!item)
280                 return -EINVAL;
281
282         target_item = configfs_get_config_item(dentry);
283         if (!target_item) {
284                 config_item_put(item);
285                 return -EINVAL;
286         }
287
288         down_read(&configfs_rename_sem);
289         error = configfs_get_target_path(item, target_item, path);
290         up_read(&configfs_rename_sem);
291
292         config_item_put(item);
293         config_item_put(target_item);
294         return error;
295
296 }
297
298 static const char *configfs_get_link(struct dentry *dentry,
299                                      struct inode *inode,
300                                      struct delayed_call *done)
301 {
302         char *body;
303         int error;
304
305         if (!dentry)
306                 return ERR_PTR(-ECHILD);
307
308         body = kzalloc(PAGE_SIZE, GFP_KERNEL);
309         if (!body)
310                 return ERR_PTR(-ENOMEM);
311
312         error = configfs_getlink(dentry, body);
313         if (!error) {
314                 set_delayed_call(done, kfree_link, body);
315                 return body;
316         }
317
318         kfree(body);
319         return ERR_PTR(error);
320 }
321
322 const struct inode_operations configfs_symlink_inode_operations = {
323         .get_link = configfs_get_link,
324         .setattr = configfs_setattr,
325 };
326