inode.c 13 KB

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