]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/sysfs/symlink.c
sysfs: Don't allow the creation of symlinks we can't remove
[net-next-2.6.git] / fs / sysfs / symlink.c
1 /*
2  * fs/sysfs/symlink.c - sysfs symlink implementation
3  *
4  * Copyright (c) 2001-3 Patrick Mochel
5  * Copyright (c) 2007 SUSE Linux Products GmbH
6  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7  *
8  * This file is released under the GPLv2.
9  *
10  * Please see Documentation/filesystems/sysfs.txt for more information.
11  */
12
13 #include <linux/fs.h>
14 #include <linux/gfp.h>
15 #include <linux/mount.h>
16 #include <linux/module.h>
17 #include <linux/kobject.h>
18 #include <linux/namei.h>
19 #include <linux/mutex.h>
20 #include <linux/security.h>
21
22 #include "sysfs.h"
23
24 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
25                                 const char *name, int warn)
26 {
27         struct sysfs_dirent *parent_sd = NULL;
28         struct sysfs_dirent *target_sd = NULL;
29         struct sysfs_dirent *sd = NULL;
30         struct sysfs_addrm_cxt acxt;
31         enum kobj_ns_type ns_type;
32         int error;
33
34         BUG_ON(!name);
35
36         if (!kobj)
37                 parent_sd = &sysfs_root;
38         else
39                 parent_sd = kobj->sd;
40
41         error = -EFAULT;
42         if (!parent_sd)
43                 goto out_put;
44
45         /* target->sd can go away beneath us but is protected with
46          * sysfs_assoc_lock.  Fetch target_sd from it.
47          */
48         spin_lock(&sysfs_assoc_lock);
49         if (target->sd)
50                 target_sd = sysfs_get(target->sd);
51         spin_unlock(&sysfs_assoc_lock);
52
53         error = -ENOENT;
54         if (!target_sd)
55                 goto out_put;
56
57         error = -ENOMEM;
58         sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
59         if (!sd)
60                 goto out_put;
61
62         ns_type = sysfs_ns_type(parent_sd);
63         if (ns_type)
64                 sd->s_ns = target->ktype->namespace(target);
65         sd->s_symlink.target_sd = target_sd;
66         target_sd = NULL;       /* reference is now owned by the symlink */
67
68         sysfs_addrm_start(&acxt, parent_sd);
69         /* Symlinks must be between directories with the same ns_type */
70         if (ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent)) {
71                 if (warn)
72                         error = sysfs_add_one(&acxt, sd);
73                 else
74                         error = __sysfs_add_one(&acxt, sd);
75         } else {
76                 error = -EINVAL;
77                 WARN(1, KERN_WARNING
78                         "sysfs: symlink across ns_types %s/%s -> %s/%s\n",
79                         parent_sd->s_name,
80                         sd->s_name,
81                         sd->s_symlink.target_sd->s_parent->s_name,
82                         sd->s_symlink.target_sd->s_name);
83         }
84         sysfs_addrm_finish(&acxt);
85
86         if (error)
87                 goto out_put;
88
89         return 0;
90
91  out_put:
92         sysfs_put(target_sd);
93         sysfs_put(sd);
94         return error;
95 }
96
97 /**
98  *      sysfs_create_link - create symlink between two objects.
99  *      @kobj:  object whose directory we're creating the link in.
100  *      @target:        object we're pointing to.
101  *      @name:          name of the symlink.
102  */
103 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
104                       const char *name)
105 {
106         return sysfs_do_create_link(kobj, target, name, 1);
107 }
108
109 /**
110  *      sysfs_create_link_nowarn - create symlink between two objects.
111  *      @kobj:  object whose directory we're creating the link in.
112  *      @target:        object we're pointing to.
113  *      @name:          name of the symlink.
114  *
115  *      This function does the same as sysf_create_link(), but it
116  *      doesn't warn if the link already exists.
117  */
118 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
119                              const char *name)
120 {
121         return sysfs_do_create_link(kobj, target, name, 0);
122 }
123
124 /**
125  *      sysfs_delete_link - remove symlink in object's directory.
126  *      @kobj:  object we're acting for.
127  *      @targ:  object we're pointing to.
128  *      @name:  name of the symlink to remove.
129  *
130  *      Unlike sysfs_remove_link sysfs_delete_link has enough information
131  *      to successfully delete symlinks in tagged directories.
132  */
133 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
134                         const char *name)
135 {
136         const void *ns = NULL;
137         spin_lock(&sysfs_assoc_lock);
138         if (targ->sd)
139                 ns = targ->sd->s_ns;
140         spin_unlock(&sysfs_assoc_lock);
141         sysfs_hash_and_remove(kobj->sd, ns, name);
142 }
143
144 /**
145  *      sysfs_remove_link - remove symlink in object's directory.
146  *      @kobj:  object we're acting for.
147  *      @name:  name of the symlink to remove.
148  */
149
150 void sysfs_remove_link(struct kobject * kobj, const char * name)
151 {
152         struct sysfs_dirent *parent_sd = NULL;
153
154         if (!kobj)
155                 parent_sd = &sysfs_root;
156         else
157                 parent_sd = kobj->sd;
158
159         sysfs_hash_and_remove(parent_sd, NULL, name);
160 }
161
162 /**
163  *      sysfs_rename_link - rename symlink in object's directory.
164  *      @kobj:  object we're acting for.
165  *      @targ:  object we're pointing to.
166  *      @old:   previous name of the symlink.
167  *      @new:   new name of the symlink.
168  *
169  *      A helper function for the common rename symlink idiom.
170  */
171 int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
172                         const char *old, const char *new)
173 {
174         struct sysfs_dirent *parent_sd, *sd = NULL;
175         const void *old_ns = NULL, *new_ns = NULL;
176         int result;
177
178         if (!kobj)
179                 parent_sd = &sysfs_root;
180         else
181                 parent_sd = kobj->sd;
182
183         if (targ->sd)
184                 old_ns = targ->sd->s_ns;
185
186         result = -ENOENT;
187         sd = sysfs_get_dirent(parent_sd, old_ns, old);
188         if (!sd)
189                 goto out;
190
191         result = -EINVAL;
192         if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
193                 goto out;
194         if (sd->s_symlink.target_sd->s_dir.kobj != targ)
195                 goto out;
196
197         if (sysfs_ns_type(parent_sd))
198                 new_ns = targ->ktype->namespace(targ);
199
200         result = sysfs_rename(sd, parent_sd, new_ns, new);
201
202 out:
203         sysfs_put(sd);
204         return result;
205 }
206
207 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
208                                  struct sysfs_dirent *target_sd, char *path)
209 {
210         struct sysfs_dirent *base, *sd;
211         char *s = path;
212         int len = 0;
213
214         /* go up to the root, stop at the base */
215         base = parent_sd;
216         while (base->s_parent) {
217                 sd = target_sd->s_parent;
218                 while (sd->s_parent && base != sd)
219                         sd = sd->s_parent;
220
221                 if (base == sd)
222                         break;
223
224                 strcpy(s, "../");
225                 s += 3;
226                 base = base->s_parent;
227         }
228
229         /* determine end of target string for reverse fillup */
230         sd = target_sd;
231         while (sd->s_parent && sd != base) {
232                 len += strlen(sd->s_name) + 1;
233                 sd = sd->s_parent;
234         }
235
236         /* check limits */
237         if (len < 2)
238                 return -EINVAL;
239         len--;
240         if ((s - path) + len > PATH_MAX)
241                 return -ENAMETOOLONG;
242
243         /* reverse fillup of target string from target to base */
244         sd = target_sd;
245         while (sd->s_parent && sd != base) {
246                 int slen = strlen(sd->s_name);
247
248                 len -= slen;
249                 strncpy(s + len, sd->s_name, slen);
250                 if (len)
251                         s[--len] = '/';
252
253                 sd = sd->s_parent;
254         }
255
256         return 0;
257 }
258
259 static int sysfs_getlink(struct dentry *dentry, char * path)
260 {
261         struct sysfs_dirent *sd = dentry->d_fsdata;
262         struct sysfs_dirent *parent_sd = sd->s_parent;
263         struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
264         int error;
265
266         mutex_lock(&sysfs_mutex);
267         error = sysfs_get_target_path(parent_sd, target_sd, path);
268         mutex_unlock(&sysfs_mutex);
269
270         return error;
271 }
272
273 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
274 {
275         int error = -ENOMEM;
276         unsigned long page = get_zeroed_page(GFP_KERNEL);
277         if (page) {
278                 error = sysfs_getlink(dentry, (char *) page); 
279                 if (error < 0)
280                         free_page((unsigned long)page);
281         }
282         nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
283         return NULL;
284 }
285
286 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
287 {
288         char *page = nd_get_link(nd);
289         if (!IS_ERR(page))
290                 free_page((unsigned long)page);
291 }
292
293 const struct inode_operations sysfs_symlink_inode_operations = {
294         .setxattr       = sysfs_setxattr,
295         .readlink       = generic_readlink,
296         .follow_link    = sysfs_follow_link,
297         .put_link       = sysfs_put_link,
298         .setattr        = sysfs_setattr,
299         .getattr        = sysfs_getattr,
300         .permission     = sysfs_permission,
301 };
302
303
304 EXPORT_SYMBOL_GPL(sysfs_create_link);
305 EXPORT_SYMBOL_GPL(sysfs_remove_link);
306 EXPORT_SYMBOL_GPL(sysfs_rename_link);