inode.c 13 KB

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