inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/proc/inode.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/time.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pid_namespace.h>
  11. #include <linux/mm.h>
  12. #include <linux/string.h>
  13. #include <linux/stat.h>
  14. #include <linux/completion.h>
  15. #include <linux/poll.h>
  16. #include <linux/printk.h>
  17. #include <linux/file.h>
  18. #include <linux/limits.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/mount.h>
  25. #include <linux/magic.h>
  26. #include <linux/uaccess.h>
  27. #include "internal.h"
  28. static void proc_evict_inode(struct inode *inode)
  29. {
  30. struct proc_dir_entry *de;
  31. struct ctl_table_header *head;
  32. truncate_inode_pages_final(&inode->i_data);
  33. clear_inode(inode);
  34. /* Stop tracking associated processes */
  35. put_pid(PROC_I(inode)->pid);
  36. /* Let go of any associated proc directory entry */
  37. de = PDE(inode);
  38. if (de)
  39. pde_put(de);
  40. head = PROC_I(inode)->sysctl;
  41. if (head) {
  42. RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
  43. proc_sys_evict_inode(inode, head);
  44. }
  45. }
  46. static struct kmem_cache * proc_inode_cachep;
  47. static struct inode *proc_alloc_inode(struct super_block *sb)
  48. {
  49. struct proc_inode *ei;
  50. struct inode *inode;
  51. ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  52. if (!ei)
  53. return NULL;
  54. ei->pid = NULL;
  55. ei->fd = 0;
  56. ei->op.proc_get_link = NULL;
  57. ei->pde = NULL;
  58. ei->sysctl = NULL;
  59. ei->sysctl_entry = NULL;
  60. ei->ns_ops = NULL;
  61. inode = &ei->vfs_inode;
  62. return inode;
  63. }
  64. static void proc_i_callback(struct rcu_head *head)
  65. {
  66. struct inode *inode = container_of(head, struct inode, i_rcu);
  67. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  68. }
  69. static void proc_destroy_inode(struct inode *inode)
  70. {
  71. call_rcu(&inode->i_rcu, proc_i_callback);
  72. }
  73. static void init_once(void *foo)
  74. {
  75. struct proc_inode *ei = (struct proc_inode *) foo;
  76. inode_init_once(&ei->vfs_inode);
  77. }
  78. void __init proc_init_inodecache(void)
  79. {
  80. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  81. sizeof(struct proc_inode),
  82. 0, (SLAB_RECLAIM_ACCOUNT|
  83. SLAB_MEM_SPREAD|SLAB_ACCOUNT|
  84. SLAB_PANIC),
  85. init_once);
  86. }
  87. static int proc_show_options(struct seq_file *seq, struct dentry *root)
  88. {
  89. struct super_block *sb = root->d_sb;
  90. struct pid_namespace *pid = sb->s_fs_info;
  91. if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
  92. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
  93. if (pid->hide_pid != HIDEPID_OFF)
  94. seq_printf(seq, ",hidepid=%u", pid->hide_pid);
  95. return 0;
  96. }
  97. static const struct super_operations proc_sops = {
  98. .alloc_inode = proc_alloc_inode,
  99. .destroy_inode = proc_destroy_inode,
  100. .drop_inode = generic_delete_inode,
  101. .evict_inode = proc_evict_inode,
  102. .statfs = simple_statfs,
  103. .remount_fs = proc_remount,
  104. .show_options = proc_show_options,
  105. };
  106. enum {BIAS = -1U<<31};
  107. static inline int use_pde(struct proc_dir_entry *pde)
  108. {
  109. return atomic_inc_unless_negative(&pde->in_use);
  110. }
  111. static void unuse_pde(struct proc_dir_entry *pde)
  112. {
  113. if (atomic_dec_return(&pde->in_use) == BIAS)
  114. complete(pde->pde_unload_completion);
  115. }
  116. /* pde is locked */
  117. static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
  118. {
  119. /*
  120. * close() (proc_reg_release()) can't delete an entry and proceed:
  121. * ->release hook needs to be available at the right moment.
  122. *
  123. * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
  124. * "struct file" needs to be available at the right moment.
  125. *
  126. * Therefore, first process to enter this function does ->release() and
  127. * signals its completion to the other process which does nothing.
  128. */
  129. if (pdeo->closing) {
  130. /* somebody else is doing that, just wait */
  131. DECLARE_COMPLETION_ONSTACK(c);
  132. pdeo->c = &c;
  133. spin_unlock(&pde->pde_unload_lock);
  134. wait_for_completion(&c);
  135. spin_lock(&pde->pde_unload_lock);
  136. } else {
  137. struct file *file;
  138. pdeo->closing = true;
  139. spin_unlock(&pde->pde_unload_lock);
  140. file = pdeo->file;
  141. pde->proc_fops->release(file_inode(file), file);
  142. spin_lock(&pde->pde_unload_lock);
  143. /* After ->release. */
  144. list_del(&pdeo->lh);
  145. if (pdeo->c)
  146. complete(pdeo->c);
  147. kfree(pdeo);
  148. }
  149. }
  150. void proc_entry_rundown(struct proc_dir_entry *de)
  151. {
  152. DECLARE_COMPLETION_ONSTACK(c);
  153. /* Wait until all existing callers into module are done. */
  154. de->pde_unload_completion = &c;
  155. if (atomic_add_return(BIAS, &de->in_use) != BIAS)
  156. wait_for_completion(&c);
  157. /* ->pde_openers list can't grow from now on. */
  158. spin_lock(&de->pde_unload_lock);
  159. while (!list_empty(&de->pde_openers)) {
  160. struct pde_opener *pdeo;
  161. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  162. close_pdeo(de, pdeo);
  163. }
  164. spin_unlock(&de->pde_unload_lock);
  165. }
  166. static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
  167. {
  168. struct proc_dir_entry *pde = PDE(file_inode(file));
  169. loff_t rv = -EINVAL;
  170. if (use_pde(pde)) {
  171. loff_t (*llseek)(struct file *, loff_t, int);
  172. llseek = pde->proc_fops->llseek;
  173. if (!llseek)
  174. llseek = default_llseek;
  175. rv = llseek(file, offset, whence);
  176. unuse_pde(pde);
  177. }
  178. return rv;
  179. }
  180. static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  181. {
  182. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  183. struct proc_dir_entry *pde = PDE(file_inode(file));
  184. ssize_t rv = -EIO;
  185. if (use_pde(pde)) {
  186. read = pde->proc_fops->read;
  187. if (read)
  188. rv = read(file, buf, count, ppos);
  189. unuse_pde(pde);
  190. }
  191. return rv;
  192. }
  193. static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  194. {
  195. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  196. struct proc_dir_entry *pde = PDE(file_inode(file));
  197. ssize_t rv = -EIO;
  198. if (use_pde(pde)) {
  199. write = pde->proc_fops->write;
  200. if (write)
  201. rv = write(file, buf, count, ppos);
  202. unuse_pde(pde);
  203. }
  204. return rv;
  205. }
  206. static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
  207. {
  208. struct proc_dir_entry *pde = PDE(file_inode(file));
  209. unsigned int rv = DEFAULT_POLLMASK;
  210. unsigned int (*poll)(struct file *, struct poll_table_struct *);
  211. if (use_pde(pde)) {
  212. poll = pde->proc_fops->poll;
  213. if (poll)
  214. rv = poll(file, pts);
  215. unuse_pde(pde);
  216. }
  217. return rv;
  218. }
  219. static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  220. {
  221. struct proc_dir_entry *pde = PDE(file_inode(file));
  222. long rv = -ENOTTY;
  223. long (*ioctl)(struct file *, unsigned int, unsigned long);
  224. if (use_pde(pde)) {
  225. ioctl = pde->proc_fops->unlocked_ioctl;
  226. if (ioctl)
  227. rv = ioctl(file, cmd, arg);
  228. unuse_pde(pde);
  229. }
  230. return rv;
  231. }
  232. #ifdef CONFIG_COMPAT
  233. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  234. {
  235. struct proc_dir_entry *pde = PDE(file_inode(file));
  236. long rv = -ENOTTY;
  237. long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
  238. if (use_pde(pde)) {
  239. compat_ioctl = pde->proc_fops->compat_ioctl;
  240. if (compat_ioctl)
  241. rv = compat_ioctl(file, cmd, arg);
  242. unuse_pde(pde);
  243. }
  244. return rv;
  245. }
  246. #endif
  247. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  248. {
  249. struct proc_dir_entry *pde = PDE(file_inode(file));
  250. int rv = -EIO;
  251. int (*mmap)(struct file *, struct vm_area_struct *);
  252. if (use_pde(pde)) {
  253. mmap = pde->proc_fops->mmap;
  254. if (mmap)
  255. rv = mmap(file, vma);
  256. unuse_pde(pde);
  257. }
  258. return rv;
  259. }
  260. static unsigned long
  261. proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
  262. unsigned long len, unsigned long pgoff,
  263. unsigned long flags)
  264. {
  265. struct proc_dir_entry *pde = PDE(file_inode(file));
  266. unsigned long rv = -EIO;
  267. if (use_pde(pde)) {
  268. typeof(proc_reg_get_unmapped_area) *get_area;
  269. get_area = pde->proc_fops->get_unmapped_area;
  270. #ifdef CONFIG_MMU
  271. if (!get_area)
  272. get_area = current->mm->get_unmapped_area;
  273. #endif
  274. if (get_area)
  275. rv = get_area(file, orig_addr, len, pgoff, flags);
  276. else
  277. rv = orig_addr;
  278. unuse_pde(pde);
  279. }
  280. return rv;
  281. }
  282. static int proc_reg_open(struct inode *inode, struct file *file)
  283. {
  284. struct proc_dir_entry *pde = PDE(inode);
  285. int rv = 0;
  286. int (*open)(struct inode *, struct file *);
  287. int (*release)(struct inode *, struct file *);
  288. struct pde_opener *pdeo;
  289. /*
  290. * Ensure that
  291. * 1) PDE's ->release hook will be called no matter what
  292. * either normally by close()/->release, or forcefully by
  293. * rmmod/remove_proc_entry.
  294. *
  295. * 2) rmmod isn't blocked by opening file in /proc and sitting on
  296. * the descriptor (including "rmmod foo </proc/foo" scenario).
  297. *
  298. * Save every "struct file" with custom ->release hook.
  299. */
  300. pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
  301. if (!pdeo)
  302. return -ENOMEM;
  303. if (!use_pde(pde)) {
  304. kfree(pdeo);
  305. return -ENOENT;
  306. }
  307. open = pde->proc_fops->open;
  308. release = pde->proc_fops->release;
  309. if (open)
  310. rv = open(inode, file);
  311. if (rv == 0 && release) {
  312. /* To know what to release. */
  313. pdeo->file = file;
  314. pdeo->closing = false;
  315. pdeo->c = NULL;
  316. spin_lock(&pde->pde_unload_lock);
  317. list_add(&pdeo->lh, &pde->pde_openers);
  318. spin_unlock(&pde->pde_unload_lock);
  319. } else
  320. kfree(pdeo);
  321. unuse_pde(pde);
  322. return rv;
  323. }
  324. static int proc_reg_release(struct inode *inode, struct file *file)
  325. {
  326. struct proc_dir_entry *pde = PDE(inode);
  327. struct pde_opener *pdeo;
  328. spin_lock(&pde->pde_unload_lock);
  329. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  330. if (pdeo->file == file) {
  331. close_pdeo(pde, pdeo);
  332. break;
  333. }
  334. }
  335. spin_unlock(&pde->pde_unload_lock);
  336. return 0;
  337. }
  338. static const struct file_operations proc_reg_file_ops = {
  339. .llseek = proc_reg_llseek,
  340. .read = proc_reg_read,
  341. .write = proc_reg_write,
  342. .poll = proc_reg_poll,
  343. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  344. #ifdef CONFIG_COMPAT
  345. .compat_ioctl = proc_reg_compat_ioctl,
  346. #endif
  347. .mmap = proc_reg_mmap,
  348. .get_unmapped_area = proc_reg_get_unmapped_area,
  349. .open = proc_reg_open,
  350. .release = proc_reg_release,
  351. };
  352. #ifdef CONFIG_COMPAT
  353. static const struct file_operations proc_reg_file_ops_no_compat = {
  354. .llseek = proc_reg_llseek,
  355. .read = proc_reg_read,
  356. .write = proc_reg_write,
  357. .poll = proc_reg_poll,
  358. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  359. .mmap = proc_reg_mmap,
  360. .get_unmapped_area = proc_reg_get_unmapped_area,
  361. .open = proc_reg_open,
  362. .release = proc_reg_release,
  363. };
  364. #endif
  365. static void proc_put_link(void *p)
  366. {
  367. unuse_pde(p);
  368. }
  369. static const char *proc_get_link(struct dentry *dentry,
  370. struct inode *inode,
  371. struct delayed_call *done)
  372. {
  373. struct proc_dir_entry *pde = PDE(inode);
  374. if (unlikely(!use_pde(pde)))
  375. return ERR_PTR(-EINVAL);
  376. set_delayed_call(done, proc_put_link, pde);
  377. return pde->data;
  378. }
  379. const struct inode_operations proc_link_inode_operations = {
  380. .get_link = proc_get_link,
  381. };
  382. struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
  383. {
  384. struct inode *inode = new_inode_pseudo(sb);
  385. if (inode) {
  386. inode->i_ino = de->low_ino;
  387. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  388. PROC_I(inode)->pde = de;
  389. if (is_empty_pde(de)) {
  390. make_empty_dir_inode(inode);
  391. return inode;
  392. }
  393. if (de->mode) {
  394. inode->i_mode = de->mode;
  395. inode->i_uid = de->uid;
  396. inode->i_gid = de->gid;
  397. }
  398. if (de->size)
  399. inode->i_size = de->size;
  400. if (de->nlink)
  401. set_nlink(inode, de->nlink);
  402. WARN_ON(!de->proc_iops);
  403. inode->i_op = de->proc_iops;
  404. if (de->proc_fops) {
  405. if (S_ISREG(inode->i_mode)) {
  406. #ifdef CONFIG_COMPAT
  407. if (!de->proc_fops->compat_ioctl)
  408. inode->i_fop =
  409. &proc_reg_file_ops_no_compat;
  410. else
  411. #endif
  412. inode->i_fop = &proc_reg_file_ops;
  413. } else {
  414. inode->i_fop = de->proc_fops;
  415. }
  416. }
  417. } else
  418. pde_put(de);
  419. return inode;
  420. }
  421. int proc_fill_super(struct super_block *s, void *data, int silent)
  422. {
  423. struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
  424. struct inode *root_inode;
  425. int ret;
  426. if (!proc_parse_options(data, ns))
  427. return -EINVAL;
  428. /* User space would break if executables or devices appear on proc */
  429. s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
  430. s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
  431. s->s_blocksize = 1024;
  432. s->s_blocksize_bits = 10;
  433. s->s_magic = PROC_SUPER_MAGIC;
  434. s->s_op = &proc_sops;
  435. s->s_time_gran = 1;
  436. /*
  437. * procfs isn't actually a stacking filesystem; however, there is
  438. * too much magic going on inside it to permit stacking things on
  439. * top of it
  440. */
  441. s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  442. pde_get(&proc_root);
  443. root_inode = proc_get_inode(s, &proc_root);
  444. if (!root_inode) {
  445. pr_err("proc_fill_super: get root inode failed\n");
  446. return -ENOMEM;
  447. }
  448. s->s_root = d_make_root(root_inode);
  449. if (!s->s_root) {
  450. pr_err("proc_fill_super: allocate dentry failed\n");
  451. return -ENOMEM;
  452. }
  453. ret = proc_setup_self(s);
  454. if (ret) {
  455. return ret;
  456. }
  457. return proc_setup_thread_self(s);
  458. }