0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/kernel.h>
0017 #include <linux/syscalls.h>
0018 #include <linux/mount.h>
0019 #include <linux/device.h>
0020 #include <linux/blkdev.h>
0021 #include <linux/namei.h>
0022 #include <linux/fs.h>
0023 #include <linux/shmem_fs.h>
0024 #include <linux/ramfs.h>
0025 #include <linux/sched.h>
0026 #include <linux/slab.h>
0027 #include <linux/kthread.h>
0028 #include <linux/init_syscalls.h>
0029 #include <uapi/linux/mount.h>
0030 #include "base.h"
0031
0032 #ifdef CONFIG_DEVTMPFS_SAFE
0033 #define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
0034 #else
0035 #define DEVTMPFS_MFLAGS (MS_SILENT)
0036 #endif
0037
0038 static struct task_struct *thread;
0039
0040 static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
0041
0042 static DEFINE_SPINLOCK(req_lock);
0043
0044 static struct req {
0045 struct req *next;
0046 struct completion done;
0047 int err;
0048 const char *name;
0049 umode_t mode;
0050 kuid_t uid;
0051 kgid_t gid;
0052 struct device *dev;
0053 } *requests;
0054
0055 static int __init mount_param(char *str)
0056 {
0057 mount_dev = simple_strtoul(str, NULL, 0);
0058 return 1;
0059 }
0060 __setup("devtmpfs.mount=", mount_param);
0061
0062 static struct vfsmount *mnt;
0063
0064 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
0065 const char *dev_name, void *data)
0066 {
0067 struct super_block *s = mnt->mnt_sb;
0068 int err;
0069
0070 atomic_inc(&s->s_active);
0071 down_write(&s->s_umount);
0072 err = reconfigure_single(s, flags, data);
0073 if (err < 0) {
0074 deactivate_locked_super(s);
0075 return ERR_PTR(err);
0076 }
0077 return dget(s->s_root);
0078 }
0079
0080 static struct file_system_type internal_fs_type = {
0081 .name = "devtmpfs",
0082 #ifdef CONFIG_TMPFS
0083 .init_fs_context = shmem_init_fs_context,
0084 #else
0085 .init_fs_context = ramfs_init_fs_context,
0086 #endif
0087 .kill_sb = kill_litter_super,
0088 };
0089
0090 static struct file_system_type dev_fs_type = {
0091 .name = "devtmpfs",
0092 .mount = public_dev_mount,
0093 };
0094
0095 #ifdef CONFIG_BLOCK
0096 static inline int is_blockdev(struct device *dev)
0097 {
0098 return dev->class == &block_class;
0099 }
0100 #else
0101 static inline int is_blockdev(struct device *dev) { return 0; }
0102 #endif
0103
0104 static int devtmpfs_submit_req(struct req *req, const char *tmp)
0105 {
0106 init_completion(&req->done);
0107
0108 spin_lock(&req_lock);
0109 req->next = requests;
0110 requests = req;
0111 spin_unlock(&req_lock);
0112
0113 wake_up_process(thread);
0114 wait_for_completion(&req->done);
0115
0116 kfree(tmp);
0117
0118 return req->err;
0119 }
0120
0121 int devtmpfs_create_node(struct device *dev)
0122 {
0123 const char *tmp = NULL;
0124 struct req req;
0125
0126 if (!thread)
0127 return 0;
0128
0129 req.mode = 0;
0130 req.uid = GLOBAL_ROOT_UID;
0131 req.gid = GLOBAL_ROOT_GID;
0132 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
0133 if (!req.name)
0134 return -ENOMEM;
0135
0136 if (req.mode == 0)
0137 req.mode = 0600;
0138 if (is_blockdev(dev))
0139 req.mode |= S_IFBLK;
0140 else
0141 req.mode |= S_IFCHR;
0142
0143 req.dev = dev;
0144
0145 return devtmpfs_submit_req(&req, tmp);
0146 }
0147
0148 int devtmpfs_delete_node(struct device *dev)
0149 {
0150 const char *tmp = NULL;
0151 struct req req;
0152
0153 if (!thread)
0154 return 0;
0155
0156 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
0157 if (!req.name)
0158 return -ENOMEM;
0159
0160 req.mode = 0;
0161 req.dev = dev;
0162
0163 return devtmpfs_submit_req(&req, tmp);
0164 }
0165
0166 static int dev_mkdir(const char *name, umode_t mode)
0167 {
0168 struct dentry *dentry;
0169 struct path path;
0170 int err;
0171
0172 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
0173 if (IS_ERR(dentry))
0174 return PTR_ERR(dentry);
0175
0176 err = vfs_mkdir(&init_user_ns, d_inode(path.dentry), dentry, mode);
0177 if (!err)
0178
0179 d_inode(dentry)->i_private = &thread;
0180 done_path_create(&path, dentry);
0181 return err;
0182 }
0183
0184 static int create_path(const char *nodepath)
0185 {
0186 char *path;
0187 char *s;
0188 int err = 0;
0189
0190
0191 path = kstrdup(nodepath, GFP_KERNEL);
0192 if (!path)
0193 return -ENOMEM;
0194
0195 s = path;
0196 for (;;) {
0197 s = strchr(s, '/');
0198 if (!s)
0199 break;
0200 s[0] = '\0';
0201 err = dev_mkdir(path, 0755);
0202 if (err && err != -EEXIST)
0203 break;
0204 s[0] = '/';
0205 s++;
0206 }
0207 kfree(path);
0208 return err;
0209 }
0210
0211 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
0212 kgid_t gid, struct device *dev)
0213 {
0214 struct dentry *dentry;
0215 struct path path;
0216 int err;
0217
0218 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
0219 if (dentry == ERR_PTR(-ENOENT)) {
0220 create_path(nodename);
0221 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
0222 }
0223 if (IS_ERR(dentry))
0224 return PTR_ERR(dentry);
0225
0226 err = vfs_mknod(&init_user_ns, d_inode(path.dentry), dentry, mode,
0227 dev->devt);
0228 if (!err) {
0229 struct iattr newattrs;
0230
0231 newattrs.ia_mode = mode;
0232 newattrs.ia_uid = uid;
0233 newattrs.ia_gid = gid;
0234 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
0235 inode_lock(d_inode(dentry));
0236 notify_change(&init_user_ns, dentry, &newattrs, NULL);
0237 inode_unlock(d_inode(dentry));
0238
0239
0240 d_inode(dentry)->i_private = &thread;
0241 }
0242 done_path_create(&path, dentry);
0243 return err;
0244 }
0245
0246 static int dev_rmdir(const char *name)
0247 {
0248 struct path parent;
0249 struct dentry *dentry;
0250 int err;
0251
0252 dentry = kern_path_locked(name, &parent);
0253 if (IS_ERR(dentry))
0254 return PTR_ERR(dentry);
0255 if (d_really_is_positive(dentry)) {
0256 if (d_inode(dentry)->i_private == &thread)
0257 err = vfs_rmdir(&init_user_ns, d_inode(parent.dentry),
0258 dentry);
0259 else
0260 err = -EPERM;
0261 } else {
0262 err = -ENOENT;
0263 }
0264 dput(dentry);
0265 inode_unlock(d_inode(parent.dentry));
0266 path_put(&parent);
0267 return err;
0268 }
0269
0270 static int delete_path(const char *nodepath)
0271 {
0272 char *path;
0273 int err = 0;
0274
0275 path = kstrdup(nodepath, GFP_KERNEL);
0276 if (!path)
0277 return -ENOMEM;
0278
0279 for (;;) {
0280 char *base;
0281
0282 base = strrchr(path, '/');
0283 if (!base)
0284 break;
0285 base[0] = '\0';
0286 err = dev_rmdir(path);
0287 if (err)
0288 break;
0289 }
0290
0291 kfree(path);
0292 return err;
0293 }
0294
0295 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
0296 {
0297
0298 if (inode->i_private != &thread)
0299 return 0;
0300
0301
0302 if (is_blockdev(dev)) {
0303 if (!S_ISBLK(stat->mode))
0304 return 0;
0305 } else {
0306 if (!S_ISCHR(stat->mode))
0307 return 0;
0308 }
0309 if (stat->rdev != dev->devt)
0310 return 0;
0311
0312
0313 return 1;
0314 }
0315
0316 static int handle_remove(const char *nodename, struct device *dev)
0317 {
0318 struct path parent;
0319 struct dentry *dentry;
0320 int deleted = 0;
0321 int err;
0322
0323 dentry = kern_path_locked(nodename, &parent);
0324 if (IS_ERR(dentry))
0325 return PTR_ERR(dentry);
0326
0327 if (d_really_is_positive(dentry)) {
0328 struct kstat stat;
0329 struct path p = {.mnt = parent.mnt, .dentry = dentry};
0330 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
0331 AT_STATX_SYNC_AS_STAT);
0332 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
0333 struct iattr newattrs;
0334
0335
0336
0337
0338 newattrs.ia_uid = GLOBAL_ROOT_UID;
0339 newattrs.ia_gid = GLOBAL_ROOT_GID;
0340 newattrs.ia_mode = stat.mode & ~0777;
0341 newattrs.ia_valid =
0342 ATTR_UID|ATTR_GID|ATTR_MODE;
0343 inode_lock(d_inode(dentry));
0344 notify_change(&init_user_ns, dentry, &newattrs, NULL);
0345 inode_unlock(d_inode(dentry));
0346 err = vfs_unlink(&init_user_ns, d_inode(parent.dentry),
0347 dentry, NULL);
0348 if (!err || err == -ENOENT)
0349 deleted = 1;
0350 }
0351 } else {
0352 err = -ENOENT;
0353 }
0354 dput(dentry);
0355 inode_unlock(d_inode(parent.dentry));
0356
0357 path_put(&parent);
0358 if (deleted && strchr(nodename, '/'))
0359 delete_path(nodename);
0360 return err;
0361 }
0362
0363
0364
0365
0366
0367 int __init devtmpfs_mount(void)
0368 {
0369 int err;
0370
0371 if (!mount_dev)
0372 return 0;
0373
0374 if (!thread)
0375 return 0;
0376
0377 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
0378 if (err)
0379 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
0380 else
0381 printk(KERN_INFO "devtmpfs: mounted\n");
0382 return err;
0383 }
0384
0385 static __initdata DECLARE_COMPLETION(setup_done);
0386
0387 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
0388 struct device *dev)
0389 {
0390 if (mode)
0391 return handle_create(name, mode, uid, gid, dev);
0392 else
0393 return handle_remove(name, dev);
0394 }
0395
0396 static void __noreturn devtmpfs_work_loop(void)
0397 {
0398 while (1) {
0399 spin_lock(&req_lock);
0400 while (requests) {
0401 struct req *req = requests;
0402 requests = NULL;
0403 spin_unlock(&req_lock);
0404 while (req) {
0405 struct req *next = req->next;
0406 req->err = handle(req->name, req->mode,
0407 req->uid, req->gid, req->dev);
0408 complete(&req->done);
0409 req = next;
0410 }
0411 spin_lock(&req_lock);
0412 }
0413 __set_current_state(TASK_INTERRUPTIBLE);
0414 spin_unlock(&req_lock);
0415 schedule();
0416 }
0417 }
0418
0419 static noinline int __init devtmpfs_setup(void *p)
0420 {
0421 int err;
0422
0423 err = ksys_unshare(CLONE_NEWNS);
0424 if (err)
0425 goto out;
0426 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
0427 if (err)
0428 goto out;
0429 init_chdir("/..");
0430 init_chroot(".");
0431 out:
0432 *(int *)p = err;
0433 return err;
0434 }
0435
0436
0437
0438
0439
0440
0441 static int __ref devtmpfsd(void *p)
0442 {
0443 int err = devtmpfs_setup(p);
0444
0445 complete(&setup_done);
0446 if (err)
0447 return err;
0448 devtmpfs_work_loop();
0449 return 0;
0450 }
0451
0452
0453
0454
0455
0456 int __init devtmpfs_init(void)
0457 {
0458 char opts[] = "mode=0755";
0459 int err;
0460
0461 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
0462 if (IS_ERR(mnt)) {
0463 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
0464 PTR_ERR(mnt));
0465 return PTR_ERR(mnt);
0466 }
0467 err = register_filesystem(&dev_fs_type);
0468 if (err) {
0469 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
0470 "type %i\n", err);
0471 return err;
0472 }
0473
0474 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
0475 if (!IS_ERR(thread)) {
0476 wait_for_completion(&setup_done);
0477 } else {
0478 err = PTR_ERR(thread);
0479 thread = NULL;
0480 }
0481
0482 if (err) {
0483 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
0484 unregister_filesystem(&dev_fs_type);
0485 thread = NULL;
0486 return err;
0487 }
0488
0489 printk(KERN_INFO "devtmpfs: initialized\n");
0490 return 0;
0491 }