sysfs: Don't allow the creation of symlinks we can't remove
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / sysfs / symlink.c
blob44bca5f49cd5837e695d71d6a9983940e1522ce2
1 /*
2 * fs/sysfs/symlink.c - sysfs symlink implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
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>
22 #include "sysfs.h"
24 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
25 const char *name, int warn)
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;
34 BUG_ON(!name);
36 if (!kobj)
37 parent_sd = &sysfs_root;
38 else
39 parent_sd = kobj->sd;
41 error = -EFAULT;
42 if (!parent_sd)
43 goto out_put;
45 /* target->sd can go away beneath us but is protected with
46 * sysfs_assoc_lock. Fetch target_sd from it.
48 spin_lock(&sysfs_assoc_lock);
49 if (target->sd)
50 target_sd = sysfs_get(target->sd);
51 spin_unlock(&sysfs_assoc_lock);
53 error = -ENOENT;
54 if (!target_sd)
55 goto out_put;
57 error = -ENOMEM;
58 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
59 if (!sd)
60 goto out_put;
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 */
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);
84 sysfs_addrm_finish(&acxt);
86 if (error)
87 goto out_put;
89 return 0;
91 out_put:
92 sysfs_put(target_sd);
93 sysfs_put(sd);
94 return error;
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.
103 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
104 const char *name)
106 return sysfs_do_create_link(kobj, target, name, 1);
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.
115 * This function does the same as sysf_create_link(), but it
116 * doesn't warn if the link already exists.
118 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
119 const char *name)
121 return sysfs_do_create_link(kobj, target, name, 0);
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.
130 * Unlike sysfs_remove_link sysfs_delete_link has enough information
131 * to successfully delete symlinks in tagged directories.
133 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
134 const char *name)
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);
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.
150 void sysfs_remove_link(struct kobject * kobj, const char * name)
152 struct sysfs_dirent *parent_sd = NULL;
154 if (!kobj)
155 parent_sd = &sysfs_root;
156 else
157 parent_sd = kobj->sd;
159 sysfs_hash_and_remove(parent_sd, NULL, name);
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.
169 * A helper function for the common rename symlink idiom.
171 int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
172 const char *old, const char *new)
174 struct sysfs_dirent *parent_sd, *sd = NULL;
175 const void *old_ns = NULL, *new_ns = NULL;
176 int result;
178 if (!kobj)
179 parent_sd = &sysfs_root;
180 else
181 parent_sd = kobj->sd;
183 if (targ->sd)
184 old_ns = targ->sd->s_ns;
186 result = -ENOENT;
187 sd = sysfs_get_dirent(parent_sd, old_ns, old);
188 if (!sd)
189 goto out;
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;
197 if (sysfs_ns_type(parent_sd))
198 new_ns = targ->ktype->namespace(targ);
200 result = sysfs_rename(sd, parent_sd, new_ns, new);
202 out:
203 sysfs_put(sd);
204 return result;
207 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
208 struct sysfs_dirent *target_sd, char *path)
210 struct sysfs_dirent *base, *sd;
211 char *s = path;
212 int len = 0;
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;
221 if (base == sd)
222 break;
224 strcpy(s, "../");
225 s += 3;
226 base = base->s_parent;
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;
236 /* check limits */
237 if (len < 2)
238 return -EINVAL;
239 len--;
240 if ((s - path) + len > PATH_MAX)
241 return -ENAMETOOLONG;
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);
248 len -= slen;
249 strncpy(s + len, sd->s_name, slen);
250 if (len)
251 s[--len] = '/';
253 sd = sd->s_parent;
256 return 0;
259 static int sysfs_getlink(struct dentry *dentry, char * path)
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;
266 mutex_lock(&sysfs_mutex);
267 error = sysfs_get_target_path(parent_sd, target_sd, path);
268 mutex_unlock(&sysfs_mutex);
270 return error;
273 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
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);
282 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
283 return NULL;
286 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
288 char *page = nd_get_link(nd);
289 if (!IS_ERR(page))
290 free_page((unsigned long)page);
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,
304 EXPORT_SYMBOL_GPL(sysfs_create_link);
305 EXPORT_SYMBOL_GPL(sysfs_remove_link);
306 EXPORT_SYMBOL_GPL(sysfs_rename_link);