devtmpfs.c 9.9 KB

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