inode.c 13 KB

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