inode.c 13 KB

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