xref: /linux-6.15/drivers/base/devtmpfs.c (revision c54b3869)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * devtmpfs - kernel-maintained tmpfs-based /dev
4  *
5  * Copyright (C) 2009, Kay Sievers <[email protected]>
6  *
7  * During bootup, before any driver core device is registered,
8  * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
9  * device which requests a device node, will add a node in this
10  * filesystem.
11  * By default, all devices are named after the name of the device,
12  * owned by root and have a default mode of 0600. Subsystems can
13  * overwrite the default setting if needed.
14  */
15 
16 #define pr_fmt(fmt) "devtmpfs: " fmt
17 
18 #include <linux/kernel.h>
19 #include <linux/syscalls.h>
20 #include <linux/mount.h>
21 #include <linux/device.h>
22 #include <linux/blkdev.h>
23 #include <linux/namei.h>
24 #include <linux/fs.h>
25 #include <linux/shmem_fs.h>
26 #include <linux/ramfs.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/kthread.h>
30 #include <linux/init_syscalls.h>
31 #include <uapi/linux/mount.h>
32 #include "base.h"
33 
34 #ifdef CONFIG_DEVTMPFS_SAFE
35 #define DEVTMPFS_MFLAGS       (MS_SILENT | MS_NOEXEC | MS_NOSUID)
36 #else
37 #define DEVTMPFS_MFLAGS       (MS_SILENT)
38 #endif
39 
40 static struct task_struct *thread;
41 
42 static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
43 
44 static DEFINE_SPINLOCK(req_lock);
45 
46 static struct req {
47 	struct req *next;
48 	struct completion done;
49 	int err;
50 	const char *name;
51 	umode_t mode;	/* 0 => delete */
52 	kuid_t uid;
53 	kgid_t gid;
54 	struct device *dev;
55 } *requests;
56 
57 static int __init mount_param(char *str)
58 {
59 	mount_dev = simple_strtoul(str, NULL, 0);
60 	return 1;
61 }
62 __setup("devtmpfs.mount=", mount_param);
63 
64 static struct vfsmount *mnt;
65 
66 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
67 		      const char *dev_name, void *data)
68 {
69 	struct super_block *s = mnt->mnt_sb;
70 	int err;
71 
72 	atomic_inc(&s->s_active);
73 	down_write(&s->s_umount);
74 	err = reconfigure_single(s, flags, data);
75 	if (err < 0) {
76 		deactivate_locked_super(s);
77 		return ERR_PTR(err);
78 	}
79 	return dget(s->s_root);
80 }
81 
82 static struct file_system_type internal_fs_type = {
83 	.name = "devtmpfs",
84 #ifdef CONFIG_TMPFS
85 	.init_fs_context = shmem_init_fs_context,
86 #else
87 	.init_fs_context = ramfs_init_fs_context,
88 #endif
89 	.kill_sb = kill_litter_super,
90 };
91 
92 static struct file_system_type dev_fs_type = {
93 	.name = "devtmpfs",
94 	.mount = public_dev_mount,
95 };
96 
97 static int devtmpfs_submit_req(struct req *req, const char *tmp)
98 {
99 	init_completion(&req->done);
100 
101 	spin_lock(&req_lock);
102 	req->next = requests;
103 	requests = req;
104 	spin_unlock(&req_lock);
105 
106 	wake_up_process(thread);
107 	wait_for_completion(&req->done);
108 
109 	kfree(tmp);
110 
111 	return req->err;
112 }
113 
114 int devtmpfs_create_node(struct device *dev)
115 {
116 	const char *tmp = NULL;
117 	struct req req;
118 
119 	if (!thread)
120 		return 0;
121 
122 	req.mode = 0;
123 	req.uid = GLOBAL_ROOT_UID;
124 	req.gid = GLOBAL_ROOT_GID;
125 	req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
126 	if (!req.name)
127 		return -ENOMEM;
128 
129 	if (req.mode == 0)
130 		req.mode = 0600;
131 	if (is_blockdev(dev))
132 		req.mode |= S_IFBLK;
133 	else
134 		req.mode |= S_IFCHR;
135 
136 	req.dev = dev;
137 
138 	return devtmpfs_submit_req(&req, tmp);
139 }
140 
141 int devtmpfs_delete_node(struct device *dev)
142 {
143 	const char *tmp = NULL;
144 	struct req req;
145 
146 	if (!thread)
147 		return 0;
148 
149 	req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
150 	if (!req.name)
151 		return -ENOMEM;
152 
153 	req.mode = 0;
154 	req.dev = dev;
155 
156 	return devtmpfs_submit_req(&req, tmp);
157 }
158 
159 static int dev_mkdir(const char *name, umode_t mode)
160 {
161 	struct dentry *dentry;
162 	struct path path;
163 
164 	dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
165 	if (IS_ERR(dentry))
166 		return PTR_ERR(dentry);
167 
168 	dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode);
169 	if (!IS_ERR(dentry))
170 		/* mark as kernel-created inode */
171 		d_inode(dentry)->i_private = &thread;
172 	done_path_create(&path, dentry);
173 	return PTR_ERR_OR_ZERO(dentry);
174 }
175 
176 static int create_path(const char *nodepath)
177 {
178 	char *path;
179 	char *s;
180 	int err = 0;
181 
182 	/* parent directories do not exist, create them */
183 	path = kstrdup(nodepath, GFP_KERNEL);
184 	if (!path)
185 		return -ENOMEM;
186 
187 	s = path;
188 	for (;;) {
189 		s = strchr(s, '/');
190 		if (!s)
191 			break;
192 		s[0] = '\0';
193 		err = dev_mkdir(path, 0755);
194 		if (err && err != -EEXIST)
195 			break;
196 		s[0] = '/';
197 		s++;
198 	}
199 	kfree(path);
200 	return err;
201 }
202 
203 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
204 			 kgid_t gid, struct device *dev)
205 {
206 	struct dentry *dentry;
207 	struct path path;
208 	int err;
209 
210 	dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
211 	if (dentry == ERR_PTR(-ENOENT)) {
212 		create_path(nodename);
213 		dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
214 	}
215 	if (IS_ERR(dentry))
216 		return PTR_ERR(dentry);
217 
218 	err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
219 			dev->devt);
220 	if (!err) {
221 		struct iattr newattrs;
222 
223 		newattrs.ia_mode = mode;
224 		newattrs.ia_uid = uid;
225 		newattrs.ia_gid = gid;
226 		newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
227 		inode_lock(d_inode(dentry));
228 		notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
229 		inode_unlock(d_inode(dentry));
230 
231 		/* mark as kernel-created inode */
232 		d_inode(dentry)->i_private = &thread;
233 	}
234 	done_path_create(&path, dentry);
235 	return err;
236 }
237 
238 static int dev_rmdir(const char *name)
239 {
240 	struct path parent;
241 	struct dentry *dentry;
242 	int err;
243 
244 	dentry = kern_path_locked(name, &parent);
245 	if (IS_ERR(dentry))
246 		return PTR_ERR(dentry);
247 	if (d_inode(dentry)->i_private == &thread)
248 		err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
249 				dentry);
250 	else
251 		err = -EPERM;
252 
253 	dput(dentry);
254 	inode_unlock(d_inode(parent.dentry));
255 	path_put(&parent);
256 	return err;
257 }
258 
259 static int delete_path(const char *nodepath)
260 {
261 	char *path;
262 	int err = 0;
263 
264 	path = kstrdup(nodepath, GFP_KERNEL);
265 	if (!path)
266 		return -ENOMEM;
267 
268 	for (;;) {
269 		char *base;
270 
271 		base = strrchr(path, '/');
272 		if (!base)
273 			break;
274 		base[0] = '\0';
275 		err = dev_rmdir(path);
276 		if (err)
277 			break;
278 	}
279 
280 	kfree(path);
281 	return err;
282 }
283 
284 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
285 {
286 	/* did we create it */
287 	if (inode->i_private != &thread)
288 		return 0;
289 
290 	/* does the dev_t match */
291 	if (is_blockdev(dev)) {
292 		if (!S_ISBLK(stat->mode))
293 			return 0;
294 	} else {
295 		if (!S_ISCHR(stat->mode))
296 			return 0;
297 	}
298 	if (stat->rdev != dev->devt)
299 		return 0;
300 
301 	/* ours */
302 	return 1;
303 }
304 
305 static int handle_remove(const char *nodename, struct device *dev)
306 {
307 	struct path parent;
308 	struct dentry *dentry;
309 	struct kstat stat;
310 	struct path p;
311 	int deleted = 0;
312 	int err;
313 
314 	dentry = kern_path_locked(nodename, &parent);
315 	if (IS_ERR(dentry))
316 		return PTR_ERR(dentry);
317 
318 	p.mnt = parent.mnt;
319 	p.dentry = dentry;
320 	err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
321 			  AT_STATX_SYNC_AS_STAT);
322 	if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
323 		struct iattr newattrs;
324 		/*
325 		 * before unlinking this node, reset permissions
326 		 * of possible references like hardlinks
327 		 */
328 		newattrs.ia_uid = GLOBAL_ROOT_UID;
329 		newattrs.ia_gid = GLOBAL_ROOT_GID;
330 		newattrs.ia_mode = stat.mode & ~0777;
331 		newattrs.ia_valid =
332 			ATTR_UID|ATTR_GID|ATTR_MODE;
333 		inode_lock(d_inode(dentry));
334 		notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
335 		inode_unlock(d_inode(dentry));
336 		err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
337 				 dentry, NULL);
338 		if (!err || err == -ENOENT)
339 			deleted = 1;
340 	}
341 	dput(dentry);
342 	inode_unlock(d_inode(parent.dentry));
343 
344 	path_put(&parent);
345 	if (deleted && strchr(nodename, '/'))
346 		delete_path(nodename);
347 	return err;
348 }
349 
350 /*
351  * If configured, or requested by the commandline, devtmpfs will be
352  * auto-mounted after the kernel mounted the root filesystem.
353  */
354 int __init devtmpfs_mount(void)
355 {
356 	int err;
357 
358 	if (!mount_dev)
359 		return 0;
360 
361 	if (!thread)
362 		return 0;
363 
364 	err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
365 	if (err)
366 		pr_info("error mounting %d\n", err);
367 	else
368 		pr_info("mounted\n");
369 	return err;
370 }
371 
372 static __initdata DECLARE_COMPLETION(setup_done);
373 
374 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
375 		  struct device *dev)
376 {
377 	if (mode)
378 		return handle_create(name, mode, uid, gid, dev);
379 	else
380 		return handle_remove(name, dev);
381 }
382 
383 static void __noreturn devtmpfs_work_loop(void)
384 {
385 	while (1) {
386 		spin_lock(&req_lock);
387 		while (requests) {
388 			struct req *req = requests;
389 			requests = NULL;
390 			spin_unlock(&req_lock);
391 			while (req) {
392 				struct req *next = req->next;
393 				req->err = handle(req->name, req->mode,
394 						  req->uid, req->gid, req->dev);
395 				complete(&req->done);
396 				req = next;
397 			}
398 			spin_lock(&req_lock);
399 		}
400 		__set_current_state(TASK_INTERRUPTIBLE);
401 		spin_unlock(&req_lock);
402 		schedule();
403 	}
404 }
405 
406 static noinline int __init devtmpfs_setup(void *p)
407 {
408 	int err;
409 
410 	err = ksys_unshare(CLONE_NEWNS);
411 	if (err)
412 		goto out;
413 	err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
414 	if (err)
415 		goto out;
416 	init_chdir("/.."); /* will traverse into overmounted root */
417 	init_chroot(".");
418 out:
419 	*(int *)p = err;
420 	return err;
421 }
422 
423 /*
424  * The __ref is because devtmpfs_setup needs to be __init for the routines it
425  * calls.  That call is done while devtmpfs_init, which is marked __init,
426  * synchronously waits for it to complete.
427  */
428 static int __ref devtmpfsd(void *p)
429 {
430 	int err = devtmpfs_setup(p);
431 
432 	complete(&setup_done);
433 	if (err)
434 		return err;
435 	devtmpfs_work_loop();
436 	return 0;
437 }
438 
439 /*
440  * Create devtmpfs instance, driver-core devices will add their device
441  * nodes here.
442  */
443 int __init devtmpfs_init(void)
444 {
445 	char opts[] = "mode=0755";
446 	int err;
447 
448 	mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
449 	if (IS_ERR(mnt)) {
450 		pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
451 		return PTR_ERR(mnt);
452 	}
453 	err = register_filesystem(&dev_fs_type);
454 	if (err) {
455 		pr_err("unable to register devtmpfs type %d\n", err);
456 		return err;
457 	}
458 
459 	thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
460 	if (!IS_ERR(thread)) {
461 		wait_for_completion(&setup_done);
462 	} else {
463 		err = PTR_ERR(thread);
464 		thread = NULL;
465 	}
466 
467 	if (err) {
468 		pr_err("unable to create devtmpfs %d\n", err);
469 		unregister_filesystem(&dev_fs_type);
470 		thread = NULL;
471 		return err;
472 	}
473 
474 	pr_info("initialized\n");
475 	return 0;
476 }
477