Skip to content

Commit 2780f1f

Browse files
author
Al Viro
committed
switch devtmpfs object creation/removal to separate kernel thread
... and give it a namespace where devtmpfs would be mounted on root, thus avoiding abuses of vfs_path_lookup() (it was never intended to be used with LOOKUP_PARENT). Games with credentials are also gone. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 6657719 commit 2780f1f

1 file changed

Lines changed: 149 additions & 73 deletions

File tree

‎drivers/base/devtmpfs.c‎

Lines changed: 149 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@
2121
#include <linux/fs.h>
2222
#include <linux/shmem_fs.h>
2323
#include <linux/ramfs.h>
24-
#include <linux/cred.h>
2524
#include <linux/sched.h>
26-
#include <linux/init_task.h>
2725
#include <linux/slab.h>
26+
#include <linux/kthread.h>
2827

29-
static struct vfsmount *dev_mnt;
28+
static struct task_struct *thread;
3029

3130
#if defined CONFIG_DEVTMPFS_MOUNT
3231
static int mount_dev = 1;
3332
#else
3433
static int mount_dev;
3534
#endif
3635

37-
static DEFINE_MUTEX(dirlock);
36+
static DEFINE_SPINLOCK(req_lock);
37+
38+
static struct req {
39+
struct req *next;
40+
struct completion done;
41+
int err;
42+
const char *name;
43+
mode_t mode; /* 0 => delete */
44+
struct device *dev;
45+
} *requests;
3846

3947
static int __init mount_param(char *str)
4048
{
@@ -68,14 +76,79 @@ static inline int is_blockdev(struct device *dev)
6876
static inline int is_blockdev(struct device *dev) { return 0; }
6977
#endif
7078

79+
int devtmpfs_create_node(struct device *dev)
80+
{
81+
const char *tmp = NULL;
82+
struct req req;
83+
84+
if (!thread)
85+
return 0;
86+
87+
req.mode = 0;
88+
req.name = device_get_devnode(dev, &req.mode, &tmp);
89+
if (!req.name)
90+
return -ENOMEM;
91+
92+
if (req.mode == 0)
93+
req.mode = 0600;
94+
if (is_blockdev(dev))
95+
req.mode |= S_IFBLK;
96+
else
97+
req.mode |= S_IFCHR;
98+
99+
req.dev = dev;
100+
101+
init_completion(&req.done);
102+
103+
spin_lock(&req_lock);
104+
req.next = requests;
105+
requests = &req;
106+
spin_unlock(&req_lock);
107+
108+
wake_up_process(thread);
109+
wait_for_completion(&req.done);
110+
111+
kfree(tmp);
112+
113+
return req.err;
114+
}
115+
116+
int devtmpfs_delete_node(struct device *dev)
117+
{
118+
const char *tmp = NULL;
119+
struct req req;
120+
121+
if (!thread)
122+
return 0;
123+
124+
req.name = device_get_devnode(dev, NULL, &tmp);
125+
if (!req.name)
126+
return -ENOMEM;
127+
128+
req.mode = 0;
129+
req.dev = dev;
130+
131+
init_completion(&req.done);
132+
133+
spin_lock(&req_lock);
134+
req.next = requests;
135+
requests = &req;
136+
spin_unlock(&req_lock);
137+
138+
wake_up_process(thread);
139+
wait_for_completion(&req.done);
140+
141+
kfree(tmp);
142+
return req.err;
143+
}
144+
71145
static int dev_mkdir(const char *name, mode_t mode)
72146
{
73147
struct nameidata nd;
74148
struct dentry *dentry;
75149
int err;
76150

77-
err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
78-
name, LOOKUP_PARENT, &nd);
151+
err = kern_path_parent(name, &nd);
79152
if (err)
80153
return err;
81154

@@ -84,7 +157,7 @@ static int dev_mkdir(const char *name, mode_t mode)
84157
err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
85158
if (!err)
86159
/* mark as kernel-created inode */
87-
dentry->d_inode->i_private = &dev_mnt;
160+
dentry->d_inode->i_private = &thread;
88161
dput(dentry);
89162
} else {
90163
err = PTR_ERR(dentry);
@@ -99,7 +172,6 @@ static int create_path(const char *nodepath)
99172
{
100173
int err;
101174

102-
mutex_lock(&dirlock);
103175
err = dev_mkdir(nodepath, 0755);
104176
if (err == -ENOENT) {
105177
char *path;
@@ -126,45 +198,22 @@ static int create_path(const char *nodepath)
126198
kfree(path);
127199
}
128200
out:
129-
mutex_unlock(&dirlock);
130201
return err;
131202
}
132203

133-
int devtmpfs_create_node(struct device *dev)
204+
static int handle_create(const char *nodename, mode_t mode, struct device *dev)
134205
{
135-
const char *tmp = NULL;
136-
const char *nodename;
137-
const struct cred *curr_cred;
138-
mode_t mode = 0;
139206
struct nameidata nd;
140207
struct dentry *dentry;
141208
int err;
142209

143-
if (!dev_mnt)
144-
return 0;
145-
146-
nodename = device_get_devnode(dev, &mode, &tmp);
147-
if (!nodename)
148-
return -ENOMEM;
149-
150-
if (mode == 0)
151-
mode = 0600;
152-
if (is_blockdev(dev))
153-
mode |= S_IFBLK;
154-
else
155-
mode |= S_IFCHR;
156-
157-
curr_cred = override_creds(&init_cred);
158-
159-
err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
160-
nodename, LOOKUP_PARENT, &nd);
210+
err = kern_path_parent(nodename, &nd);
161211
if (err == -ENOENT) {
162212
create_path(nodename);
163-
err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
164-
nodename, LOOKUP_PARENT, &nd);
213+
err = kern_path_parent(nodename, &nd);
165214
}
166215
if (err)
167-
goto out;
216+
return err;
168217

169218
dentry = lookup_create(&nd, 0);
170219
if (!IS_ERR(dentry)) {
@@ -181,7 +230,7 @@ int devtmpfs_create_node(struct device *dev)
181230
mutex_unlock(&dentry->d_inode->i_mutex);
182231

183232
/* mark as kernel-created inode */
184-
dentry->d_inode->i_private = &dev_mnt;
233+
dentry->d_inode->i_private = &thread;
185234
}
186235
dput(dentry);
187236
} else {
@@ -190,9 +239,6 @@ int devtmpfs_create_node(struct device *dev)
190239

191240
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
192241
path_put(&nd.path);
193-
out:
194-
kfree(tmp);
195-
revert_creds(curr_cred);
196242
return err;
197243
}
198244

@@ -202,16 +248,15 @@ static int dev_rmdir(const char *name)
202248
struct dentry *dentry;
203249
int err;
204250

205-
err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
206-
name, LOOKUP_PARENT, &nd);
251+
err = kern_path_parent(name, &nd);
207252
if (err)
208253
return err;
209254

210255
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
211256
dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
212257
if (!IS_ERR(dentry)) {
213258
if (dentry->d_inode) {
214-
if (dentry->d_inode->i_private == &dev_mnt)
259+
if (dentry->d_inode->i_private == &thread)
215260
err = vfs_rmdir(nd.path.dentry->d_inode,
216261
dentry);
217262
else
@@ -238,7 +283,6 @@ static int delete_path(const char *nodepath)
238283
if (!path)
239284
return -ENOMEM;
240285

241-
mutex_lock(&dirlock);
242286
for (;;) {
243287
char *base;
244288

@@ -250,7 +294,6 @@ static int delete_path(const char *nodepath)
250294
if (err)
251295
break;
252296
}
253-
mutex_unlock(&dirlock);
254297

255298
kfree(path);
256299
return err;
@@ -259,7 +302,7 @@ static int delete_path(const char *nodepath)
259302
static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
260303
{
261304
/* did we create it */
262-
if (inode->i_private != &dev_mnt)
305+
if (inode->i_private != &thread)
263306
return 0;
264307

265308
/* does the dev_t match */
@@ -277,29 +320,17 @@ static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *sta
277320
return 1;
278321
}
279322

280-
int devtmpfs_delete_node(struct device *dev)
323+
static int handle_remove(const char *nodename, struct device *dev)
281324
{
282-
const char *tmp = NULL;
283-
const char *nodename;
284-
const struct cred *curr_cred;
285325
struct nameidata nd;
286326
struct dentry *dentry;
287327
struct kstat stat;
288328
int deleted = 1;
289329
int err;
290330

291-
if (!dev_mnt)
292-
return 0;
293-
294-
nodename = device_get_devnode(dev, NULL, &tmp);
295-
if (!nodename)
296-
return -ENOMEM;
297-
298-
curr_cred = override_creds(&init_cred);
299-
err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
300-
nodename, LOOKUP_PARENT, &nd);
331+
err = kern_path_parent(nodename, &nd);
301332
if (err)
302-
goto out;
333+
return err;
303334

304335
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
305336
dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
@@ -337,9 +368,6 @@ int devtmpfs_delete_node(struct device *dev)
337368
path_put(&nd.path);
338369
if (deleted && strchr(nodename, '/'))
339370
delete_path(nodename);
340-
out:
341-
kfree(tmp);
342-
revert_creds(curr_cred);
343371
return err;
344372
}
345373

@@ -354,7 +382,7 @@ int devtmpfs_mount(const char *mntdir)
354382
if (!mount_dev)
355383
return 0;
356384

357-
if (!dev_mnt)
385+
if (!thread)
358386
return 0;
359387

360388
err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
@@ -365,31 +393,79 @@ int devtmpfs_mount(const char *mntdir)
365393
return err;
366394
}
367395

396+
static __initdata DECLARE_COMPLETION(setup_done);
397+
398+
static int handle(const char *name, mode_t mode, struct device *dev)
399+
{
400+
if (mode)
401+
return handle_create(name, mode, dev);
402+
else
403+
return handle_remove(name, dev);
404+
}
405+
406+
static int devtmpfsd(void *p)
407+
{
408+
char options[] = "mode=0755";
409+
int *err = p;
410+
*err = sys_unshare(CLONE_NEWNS);
411+
if (*err)
412+
goto out;
413+
*err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
414+
if (*err)
415+
goto out;
416+
sys_chdir("/.."); /* will traverse into overmounted root */
417+
sys_chroot(".");
418+
complete(&setup_done);
419+
while (1) {
420+
spin_lock(&req_lock);
421+
while (requests) {
422+
struct req *req = requests;
423+
requests = NULL;
424+
spin_unlock(&req_lock);
425+
while (req) {
426+
req->err = handle(req->name, req->mode, req->dev);
427+
complete(&req->done);
428+
req = req->next;
429+
}
430+
spin_lock(&req_lock);
431+
}
432+
set_current_state(TASK_INTERRUPTIBLE);
433+
spin_unlock(&req_lock);
434+
schedule();
435+
__set_current_state(TASK_RUNNING);
436+
}
437+
return 0;
438+
out:
439+
complete(&setup_done);
440+
return *err;
441+
}
442+
368443
/*
369444
* Create devtmpfs instance, driver-core devices will add their device
370445
* nodes here.
371446
*/
372447
int __init devtmpfs_init(void)
373448
{
374-
int err;
375-
struct vfsmount *mnt;
376-
char options[] = "mode=0755";
377-
378-
err = register_filesystem(&dev_fs_type);
449+
int err = register_filesystem(&dev_fs_type);
379450
if (err) {
380451
printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
381452
"type %i\n", err);
382453
return err;
383454
}
384455

385-
mnt = kern_mount_data(&dev_fs_type, options);
386-
if (IS_ERR(mnt)) {
387-
err = PTR_ERR(mnt);
456+
thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
457+
if (!IS_ERR(thread)) {
458+
wait_for_completion(&setup_done);
459+
} else {
460+
err = PTR_ERR(thread);
461+
thread = NULL;
462+
}
463+
464+
if (err) {
388465
printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
389466
unregister_filesystem(&dev_fs_type);
390467
return err;
391468
}
392-
dev_mnt = mnt;
393469

394470
printk(KERN_INFO "devtmpfs: initialized\n");
395471
return 0;

0 commit comments

Comments
 (0)