devtmpfs.c 10 KB

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