blob: fb4cace9ea412ecaf31d225b677f1be4e52aaca5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/proc/inode.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Alexey Dobriyanefb1a572018-02-06 15:37:24 -08008#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/time.h>
10#include <linux/proc_fs.h>
11#include <linux/kernel.h>
Vasiliy Kulikov97412952012-01-10 15:11:27 -080012#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/string.h>
15#include <linux/stat.h>
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070016#include <linux/completion.h>
Alexey Dobriyandd23aae2007-09-11 15:23:55 -070017#include <linux/poll.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080018#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/file.h>
20#include <linux/limits.h>
21#include <linux/init.h>
22#include <linux/module.h>
Al Viro9043476f2008-07-15 08:54:06 -040023#include <linux/sysctl.h>
Vasiliy Kulikov97412952012-01-10 15:11:27 -080024#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Vasiliy Kulikov97412952012-01-10 15:11:27 -080026#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Adrian Bunkfee781e2006-01-08 01:04:16 -080030#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Al Viro82679522010-06-04 22:17:56 -040032static void proc_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct proc_dir_entry *de;
Al Virodfef6dcd32011-03-08 01:25:28 -050035 struct ctl_table_header *head;
Eric W. Biederman71448012020-02-20 11:17:28 -060036 struct proc_inode *ei = PROC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Johannes Weiner91b0abe2014-04-03 14:47:49 -070038 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +020039 clear_inode(inode);
Mark Fashehfef26652005-09-09 13:01:31 -070040
Eric W. Biederman99f89552006-06-26 00:25:55 -070041 /* Stop tracking associated processes */
Eric W. Biederman71448012020-02-20 11:17:28 -060042 if (ei->pid) {
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -060043 proc_pid_evict_inode(ei);
Eric W. Biederman71448012020-02-20 11:17:28 -060044 ei->pid = NULL;
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 /* Let go of any associated proc directory entry */
Eric W. Biederman71448012020-02-20 11:17:28 -060048 de = ei->pde;
49 if (de) {
Alexey Dobriyan135d5652009-12-15 16:45:39 -080050 pde_put(de);
Eric W. Biederman71448012020-02-20 11:17:28 -060051 ei->pde = NULL;
52 }
Konstantin Khlebnikovd6cffbb2017-02-10 10:35:02 +030053
Eric W. Biederman71448012020-02-20 11:17:28 -060054 head = ei->sysctl;
Al Virodfef6dcd32011-03-08 01:25:28 -050055 if (head) {
Eric W. Biederman71448012020-02-20 11:17:28 -060056 RCU_INIT_POINTER(ei->sysctl, NULL);
Konstantin Khlebnikovd6cffbb2017-02-10 10:35:02 +030057 proc_sys_evict_inode(inode, head);
Al Virodfef6dcd32011-03-08 01:25:28 -050058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Alexey Dobriyanefb1a572018-02-06 15:37:24 -080061static struct kmem_cache *proc_inode_cachep __ro_after_init;
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -070062static struct kmem_cache *pde_opener_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static struct inode *proc_alloc_inode(struct super_block *sb)
65{
66 struct proc_inode *ei;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tobin C. Hardingf245e1c172017-05-08 15:54:55 -070068 ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!ei)
70 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070071 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070072 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ei->op.proc_get_link = NULL;
74 ei->pde = NULL;
Al Viro9043476f2008-07-15 08:54:06 -040075 ei->sysctl = NULL;
76 ei->sysctl_entry = NULL;
Eric W. Biederman0afa5ca2020-02-19 17:17:34 -060077 INIT_HLIST_NODE(&ei->sibling_inodes);
Al Viro3d3d35b2014-11-01 11:10:28 -040078 ei->ns_ops = NULL;
Alexey Dobriyan230f72e2019-01-03 15:26:05 -080079 return &ei->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Al Viro4aa6b552019-04-15 20:28:38 -040082static void proc_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110083{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110084 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
85}
86
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070087static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct proc_inode *ei = (struct proc_inode *) foo;
90
Christoph Lametera35afb82007-05-16 22:10:57 -070091 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Paul Mundt20c2df82007-07-20 10:11:58 +090093
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -070094void __init proc_init_kmemcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
97 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -080098 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -080099 SLAB_MEM_SPREAD|SLAB_ACCOUNT|
100 SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900101 init_once);
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -0700102 pde_opener_cache =
103 kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
Alexey Dobriyan2acddbe2018-04-10 16:31:12 -0700104 SLAB_ACCOUNT|SLAB_PANIC, NULL);
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700105 proc_dir_entry_cache = kmem_cache_create_usercopy(
Alexey Dobriyan2d6e4e82018-08-21 21:54:09 -0700106 "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
107 offsetof(struct proc_dir_entry, inline_name),
108 SIZEOF_PDE_INLINE_NAME, NULL);
109 BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Eric W. Biedermanf90f3ca2020-02-21 08:43:23 -0600112void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600113{
114 struct inode *inode;
115 struct proc_inode *ei;
116 struct hlist_node *node;
Eric W. Biederman080f6272020-02-21 08:33:57 -0600117 struct super_block *old_sb = NULL;
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600118
119 rcu_read_lock();
120 for (;;) {
Eric W. Biederman080f6272020-02-21 08:33:57 -0600121 struct super_block *sb;
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600122 node = hlist_first_rcu(inodes);
123 if (!node)
124 break;
125 ei = hlist_entry(node, struct proc_inode, sibling_inodes);
126 spin_lock(lock);
127 hlist_del_init_rcu(&ei->sibling_inodes);
128 spin_unlock(lock);
129
130 inode = &ei->vfs_inode;
131 sb = inode->i_sb;
Eric W. Biederman080f6272020-02-21 08:33:57 -0600132 if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600133 continue;
134 inode = igrab(inode);
135 rcu_read_unlock();
Eric W. Biederman080f6272020-02-21 08:33:57 -0600136 if (sb != old_sb) {
137 if (old_sb)
138 deactivate_super(old_sb);
139 old_sb = sb;
140 }
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600141 if (unlikely(!inode)) {
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600142 rcu_read_lock();
143 continue;
144 }
145
Eric W. Biedermanf90f3ca2020-02-21 08:43:23 -0600146 if (S_ISDIR(inode->i_mode)) {
147 struct dentry *dir = d_find_any_alias(inode);
148 if (dir) {
149 d_invalidate(dir);
150 dput(dir);
151 }
152 } else {
153 struct dentry *dentry;
154 while ((dentry = d_find_alias(inode))) {
155 d_invalidate(dentry);
156 dput(dentry);
157 }
158 }
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600159 iput(inode);
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600160
161 rcu_read_lock();
162 }
163 rcu_read_unlock();
Eric W. Biederman080f6272020-02-21 08:33:57 -0600164 if (old_sb)
165 deactivate_super(old_sb);
Eric W. Biederman26dbc602020-02-20 08:34:44 -0600166}
167
Vasiliy Kulikov97412952012-01-10 15:11:27 -0800168static int proc_show_options(struct seq_file *seq, struct dentry *root)
169{
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800170 struct super_block *sb = root->d_sb;
171 struct pid_namespace *pid = sb->s_fs_info;
172
Eric W. Biedermandcb0f222012-02-09 08:48:21 -0800173 if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
174 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800175 if (pid->hide_pid != HIDEPID_OFF)
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800176 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
177
Vasiliy Kulikov97412952012-01-10 15:11:27 -0800178 return 0;
179}
180
David Howells60a3c3a2018-11-01 23:07:25 +0000181const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .alloc_inode = proc_alloc_inode,
Al Viro4aa6b552019-04-15 20:28:38 -0400183 .free_inode = proc_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .drop_inode = generic_delete_inode,
Al Viro82679522010-06-04 22:17:56 -0400185 .evict_inode = proc_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .statfs = simple_statfs,
Vasiliy Kulikov97412952012-01-10 15:11:27 -0800187 .show_options = proc_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Al Viro866ad9a72013-04-03 19:07:30 -0400190enum {BIAS = -1U<<31};
191
192static inline int use_pde(struct proc_dir_entry *pde)
193{
Alexey Dobriyan15b158b2018-02-06 15:37:28 -0800194 return likely(atomic_inc_unless_negative(&pde->in_use));
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700195}
196
Al Viro866ad9a72013-04-03 19:07:30 -0400197static void unuse_pde(struct proc_dir_entry *pde)
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700198{
Alexey Dobriyan15b158b2018-02-06 15:37:28 -0800199 if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
Al Viro05c0ae22013-04-04 16:28:47 -0400200 complete(pde->pde_unload_completion);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700201}
202
Alexey Dobriyan2f897422018-04-10 16:30:54 -0700203/* pde is locked on entry, unlocked on exit */
Al Viroca469f32013-04-03 19:57:00 -0400204static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
Jules Irenge904f3942020-04-06 20:08:55 -0700205 __releases(&pde->pde_unload_lock)
Al Viroca469f32013-04-03 19:57:00 -0400206{
Alexey Dobriyan492b2da2016-12-12 16:45:22 -0800207 /*
208 * close() (proc_reg_release()) can't delete an entry and proceed:
209 * ->release hook needs to be available at the right moment.
210 *
211 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
212 * "struct file" needs to be available at the right moment.
213 *
214 * Therefore, first process to enter this function does ->release() and
215 * signals its completion to the other process which does nothing.
216 */
Al Viro05c0ae22013-04-04 16:28:47 -0400217 if (pdeo->closing) {
Al Viroca469f32013-04-03 19:57:00 -0400218 /* somebody else is doing that, just wait */
Al Viro05c0ae22013-04-04 16:28:47 -0400219 DECLARE_COMPLETION_ONSTACK(c);
220 pdeo->c = &c;
Al Viroca469f32013-04-03 19:57:00 -0400221 spin_unlock(&pde->pde_unload_lock);
Al Viro05c0ae22013-04-04 16:28:47 -0400222 wait_for_completion(&c);
Al Viroca469f32013-04-03 19:57:00 -0400223 } else {
224 struct file *file;
Alexey Dobriyan2f897422018-04-10 16:30:54 -0700225 struct completion *c;
226
Alexey Dobriyanf5887c712016-12-12 16:45:17 -0800227 pdeo->closing = true;
Al Viroca469f32013-04-03 19:57:00 -0400228 spin_unlock(&pde->pde_unload_lock);
229 file = pdeo->file;
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800230 pde->proc_ops->proc_release(file_inode(file), file);
Al Viroca469f32013-04-03 19:57:00 -0400231 spin_lock(&pde->pde_unload_lock);
Alexey Dobriyan492b2da2016-12-12 16:45:22 -0800232 /* After ->release. */
Alexey Dobriyan06a0c412016-12-12 16:45:14 -0800233 list_del(&pdeo->lh);
Alexey Dobriyan2f897422018-04-10 16:30:54 -0700234 c = pdeo->c;
235 spin_unlock(&pde->pde_unload_lock);
236 if (unlikely(c))
237 complete(c);
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -0700238 kmem_cache_free(pde_opener_cache, pdeo);
Al Viro05c0ae22013-04-04 16:28:47 -0400239 }
Al Viroca469f32013-04-03 19:57:00 -0400240}
241
Al Viro866ad9a72013-04-03 19:07:30 -0400242void proc_entry_rundown(struct proc_dir_entry *de)
243{
Al Viro05c0ae22013-04-04 16:28:47 -0400244 DECLARE_COMPLETION_ONSTACK(c);
Al Viro866ad9a72013-04-03 19:07:30 -0400245 /* Wait until all existing callers into module are done. */
Al Viro05c0ae22013-04-04 16:28:47 -0400246 de->pde_unload_completion = &c;
247 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
248 wait_for_completion(&c);
Al Viro866ad9a72013-04-03 19:07:30 -0400249
Alexey Dobriyan492b2da2016-12-12 16:45:22 -0800250 /* ->pde_openers list can't grow from now on. */
251
Al Viro05c0ae22013-04-04 16:28:47 -0400252 spin_lock(&de->pde_unload_lock);
Al Viro866ad9a72013-04-03 19:07:30 -0400253 while (!list_empty(&de->pde_openers)) {
254 struct pde_opener *pdeo;
Al Viro866ad9a72013-04-03 19:07:30 -0400255 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
Al Viroca469f32013-04-03 19:57:00 -0400256 close_pdeo(de, pdeo);
Alexey Dobriyan2f897422018-04-10 16:30:54 -0700257 spin_lock(&de->pde_unload_lock);
Al Viro866ad9a72013-04-03 19:07:30 -0400258 }
259 spin_unlock(&de->pde_unload_lock);
260}
261
Alexey Dobriyand919b332020-04-06 20:09:01 -0700262static loff_t pde_lseek(struct proc_dir_entry *pde, struct file *file, loff_t offset, int whence)
263{
264 typeof_member(struct proc_ops, proc_lseek) lseek;
265
266 lseek = pde->proc_ops->proc_lseek;
267 if (!lseek)
268 lseek = default_llseek;
269 return lseek(file, offset, whence);
270}
271
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700272static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
273{
Al Viro496ad9a2013-01-23 17:07:38 -0500274 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700275 loff_t rv = -EINVAL;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700276
Alexey Dobriyand919b332020-04-06 20:09:01 -0700277 if (pde_is_permanent(pde)) {
278 return pde_lseek(pde, file, offset, whence);
279 } else if (use_pde(pde)) {
280 rv = pde_lseek(pde, file, offset, whence);
Al Viro866ad9a72013-04-03 19:07:30 -0400281 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700282 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700283 return rv;
284}
285
Alexey Dobriyand919b332020-04-06 20:09:01 -0700286static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __user *buf, size_t count, loff_t *ppos)
287{
288 typeof_member(struct proc_ops, proc_read) read;
289
290 read = pde->proc_ops->proc_read;
291 if (read)
292 return read(file, buf, count, ppos);
293 return -EIO;
294}
295
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700296static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
297{
Al Viro496ad9a2013-01-23 17:07:38 -0500298 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700299 ssize_t rv = -EIO;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700300
Alexey Dobriyand919b332020-04-06 20:09:01 -0700301 if (pde_is_permanent(pde)) {
302 return pde_read(pde, file, buf, count, ppos);
303 } else if (use_pde(pde)) {
304 rv = pde_read(pde, file, buf, count, ppos);
Al Viro866ad9a72013-04-03 19:07:30 -0400305 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700306 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700307 return rv;
308}
309
Alexey Dobriyand919b332020-04-06 20:09:01 -0700310static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const char __user *buf, size_t count, loff_t *ppos)
311{
312 typeof_member(struct proc_ops, proc_write) write;
313
314 write = pde->proc_ops->proc_write;
315 if (write)
316 return write(file, buf, count, ppos);
317 return -EIO;
318}
319
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700320static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
321{
Al Viro496ad9a2013-01-23 17:07:38 -0500322 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700323 ssize_t rv = -EIO;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700324
Alexey Dobriyand919b332020-04-06 20:09:01 -0700325 if (pde_is_permanent(pde)) {
326 return pde_write(pde, file, buf, count, ppos);
327 } else if (use_pde(pde)) {
328 rv = pde_write(pde, file, buf, count, ppos);
Al Viro866ad9a72013-04-03 19:07:30 -0400329 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700330 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700331 return rv;
332}
333
Alexey Dobriyand919b332020-04-06 20:09:01 -0700334static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts)
335{
336 typeof_member(struct proc_ops, proc_poll) poll;
337
338 poll = pde->proc_ops->proc_poll;
339 if (poll)
340 return poll(file, pts);
341 return DEFAULT_POLLMASK;
342}
343
Al Viro076ccb72017-07-03 01:02:18 -0400344static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700345{
Al Viro496ad9a2013-01-23 17:07:38 -0500346 struct proc_dir_entry *pde = PDE(file_inode(file));
Al Viroe6c8adc2017-07-03 22:25:56 -0400347 __poll_t rv = DEFAULT_POLLMASK;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700348
Alexey Dobriyand919b332020-04-06 20:09:01 -0700349 if (pde_is_permanent(pde)) {
350 return pde_poll(pde, file, pts);
351 } else if (use_pde(pde)) {
352 rv = pde_poll(pde, file, pts);
Al Viro866ad9a72013-04-03 19:07:30 -0400353 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700354 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700355 return rv;
356}
357
Alexey Dobriyand919b332020-04-06 20:09:01 -0700358static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
359{
360 typeof_member(struct proc_ops, proc_ioctl) ioctl;
361
362 ioctl = pde->proc_ops->proc_ioctl;
363 if (ioctl)
364 return ioctl(file, cmd, arg);
365 return -ENOTTY;
366}
367
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700368static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
369{
Al Viro496ad9a2013-01-23 17:07:38 -0500370 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700371 long rv = -ENOTTY;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700372
Alexey Dobriyand919b332020-04-06 20:09:01 -0700373 if (pde_is_permanent(pde)) {
374 return pde_ioctl(pde, file, cmd, arg);
375 } else if (use_pde(pde)) {
376 rv = pde_ioctl(pde, file, cmd, arg);
Al Viro866ad9a72013-04-03 19:07:30 -0400377 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700378 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700379 return rv;
380}
381
382#ifdef CONFIG_COMPAT
Alexey Dobriyand919b332020-04-06 20:09:01 -0700383static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
384{
385 typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
386
387 compat_ioctl = pde->proc_ops->proc_compat_ioctl;
388 if (compat_ioctl)
389 return compat_ioctl(file, cmd, arg);
390 return -ENOTTY;
391}
392
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700393static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
394{
Al Viro496ad9a2013-01-23 17:07:38 -0500395 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700396 long rv = -ENOTTY;
Alexey Dobriyand919b332020-04-06 20:09:01 -0700397 if (pde_is_permanent(pde)) {
398 return pde_compat_ioctl(pde, file, cmd, arg);
399 } else if (use_pde(pde)) {
400 rv = pde_compat_ioctl(pde, file, cmd, arg);
Al Viro866ad9a72013-04-03 19:07:30 -0400401 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700402 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700403 return rv;
404}
405#endif
406
Alexey Dobriyand919b332020-04-06 20:09:01 -0700407static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma)
408{
409 typeof_member(struct proc_ops, proc_mmap) mmap;
410
411 mmap = pde->proc_ops->proc_mmap;
412 if (mmap)
413 return mmap(file, vma);
414 return -EIO;
415}
416
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700417static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
418{
Al Viro496ad9a2013-01-23 17:07:38 -0500419 struct proc_dir_entry *pde = PDE(file_inode(file));
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700420 int rv = -EIO;
Alexey Dobriyan9af27b22019-07-16 16:26:45 -0700421
Alexey Dobriyand919b332020-04-06 20:09:01 -0700422 if (pde_is_permanent(pde)) {
423 return pde_mmap(pde, file, vma);
424 } else if (use_pde(pde)) {
425 rv = pde_mmap(pde, file, vma);
Al Viro866ad9a72013-04-03 19:07:30 -0400426 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700427 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700428 return rv;
429}
430
HATAYAMA Daisuke5721cf82013-11-12 15:11:15 -0800431static unsigned long
Alexey Dobriyand919b332020-04-06 20:09:01 -0700432pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned long orig_addr,
433 unsigned long len, unsigned long pgoff,
434 unsigned long flags)
435{
436 typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
437
438 get_area = pde->proc_ops->proc_get_unmapped_area;
439#ifdef CONFIG_MMU
440 if (!get_area)
441 get_area = current->mm->get_unmapped_area;
442#endif
443 if (get_area)
444 return get_area(file, orig_addr, len, pgoff, flags);
445 return orig_addr;
446}
447
448static unsigned long
HATAYAMA Daisuke5721cf82013-11-12 15:11:15 -0800449proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
450 unsigned long len, unsigned long pgoff,
451 unsigned long flags)
Alexey Dobriyanc4fe2442013-08-20 22:17:24 +0300452{
453 struct proc_dir_entry *pde = PDE(file_inode(file));
HATAYAMA Daisuke2cbe3b02013-10-16 13:47:04 -0700454 unsigned long rv = -EIO;
Jan Beulichae5758a2013-12-12 17:12:22 -0800455
Alexey Dobriyand919b332020-04-06 20:09:01 -0700456 if (pde_is_permanent(pde)) {
457 return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
458 } else if (use_pde(pde)) {
459 rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
Alexey Dobriyanc4fe2442013-08-20 22:17:24 +0300460 unuse_pde(pde);
461 }
462 return rv;
463}
464
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700465static int proc_reg_open(struct inode *inode, struct file *file)
466{
467 struct proc_dir_entry *pde = PDE(inode);
468 int rv = 0;
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800469 typeof_member(struct proc_ops, proc_open) open;
470 typeof_member(struct proc_ops, proc_release) release;
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700471 struct pde_opener *pdeo;
472
Alexey Dobriyand919b332020-04-06 20:09:01 -0700473 if (pde_is_permanent(pde)) {
474 open = pde->proc_ops->proc_open;
475 if (open)
476 rv = open(inode, file);
477 return rv;
478 }
479
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700480 /*
Alexey Dobriyan492b2da2016-12-12 16:45:22 -0800481 * Ensure that
482 * 1) PDE's ->release hook will be called no matter what
483 * either normally by close()/->release, or forcefully by
484 * rmmod/remove_proc_entry.
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700485 *
Alexey Dobriyan492b2da2016-12-12 16:45:22 -0800486 * 2) rmmod isn't blocked by opening file in /proc and sitting on
487 * the descriptor (including "rmmod foo </proc/foo" scenario).
488 *
489 * Save every "struct file" with custom ->release hook.
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700490 */
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700491 if (!use_pde(pde))
Daisuke Oginod2857e72011-07-26 16:08:37 -0700492 return -ENOENT;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700493
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800494 release = pde->proc_ops->proc_release;
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700495 if (release) {
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -0700496 pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700497 if (!pdeo) {
498 rv = -ENOMEM;
499 goto out_unuse;
500 }
501 }
502
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800503 open = pde->proc_ops->proc_open;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700504 if (open)
505 rv = open(inode, file);
506
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700507 if (release) {
508 if (rv == 0) {
509 /* To know what to release. */
510 pdeo->file = file;
511 pdeo->closing = false;
512 pdeo->c = NULL;
513 spin_lock(&pde->pde_unload_lock);
514 list_add(&pdeo->lh, &pde->pde_openers);
515 spin_unlock(&pde->pde_unload_lock);
516 } else
Alexey Dobriyan195b8cf2018-04-10 16:31:09 -0700517 kmem_cache_free(pde_opener_cache, pdeo);
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700518 }
Al Viro05c0ae22013-04-04 16:28:47 -0400519
Alexey Dobriyane7a6e292018-04-10 16:31:01 -0700520out_unuse:
Al Viro05c0ae22013-04-04 16:28:47 -0400521 unuse_pde(pde);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700522 return rv;
523}
524
525static int proc_reg_release(struct inode *inode, struct file *file)
526{
527 struct proc_dir_entry *pde = PDE(inode);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700528 struct pde_opener *pdeo;
Alexey Dobriyand919b332020-04-06 20:09:01 -0700529
530 if (pde_is_permanent(pde)) {
531 typeof_member(struct proc_ops, proc_release) release;
532
533 release = pde->proc_ops->proc_release;
534 if (release) {
535 return release(inode, file);
536 }
537 return 0;
538 }
539
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700540 spin_lock(&pde->pde_unload_lock);
Al Viroca469f32013-04-03 19:57:00 -0400541 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
542 if (pdeo->file == file) {
543 close_pdeo(pde, pdeo);
Alexey Dobriyan2f897422018-04-10 16:30:54 -0700544 return 0;
Al Viroca469f32013-04-03 19:57:00 -0400545 }
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700546 }
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700547 spin_unlock(&pde->pde_unload_lock);
Al Viroca469f32013-04-03 19:57:00 -0400548 return 0;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700549}
550
551static const struct file_operations proc_reg_file_ops = {
552 .llseek = proc_reg_llseek,
553 .read = proc_reg_read,
554 .write = proc_reg_write,
555 .poll = proc_reg_poll,
556 .unlocked_ioctl = proc_reg_unlocked_ioctl,
557#ifdef CONFIG_COMPAT
558 .compat_ioctl = proc_reg_compat_ioctl,
559#endif
560 .mmap = proc_reg_mmap,
Alexey Dobriyanc4fe2442013-08-20 22:17:24 +0300561 .get_unmapped_area = proc_reg_get_unmapped_area,
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700562 .open = proc_reg_open,
563 .release = proc_reg_release,
564};
565
David Miller778f3dd2007-07-27 22:58:37 -0700566#ifdef CONFIG_COMPAT
567static const struct file_operations proc_reg_file_ops_no_compat = {
568 .llseek = proc_reg_llseek,
569 .read = proc_reg_read,
570 .write = proc_reg_write,
571 .poll = proc_reg_poll,
572 .unlocked_ioctl = proc_reg_unlocked_ioctl,
573 .mmap = proc_reg_mmap,
Alexey Dobriyanc4fe2442013-08-20 22:17:24 +0300574 .get_unmapped_area = proc_reg_get_unmapped_area,
David Miller778f3dd2007-07-27 22:58:37 -0700575 .open = proc_reg_open,
576 .release = proc_reg_release,
577};
578#endif
579
Al Virofceef392015-12-29 15:58:39 -0500580static void proc_put_link(void *p)
581{
582 unuse_pde(p);
583}
584
Al Viro6b255392015-11-17 10:20:54 -0500585static const char *proc_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500586 struct inode *inode,
587 struct delayed_call *done)
Al Viro7e0e9532015-02-21 22:16:11 -0500588{
Al Viro6b255392015-11-17 10:20:54 -0500589 struct proc_dir_entry *pde = PDE(inode);
Alexey Dobriyan15b158b2018-02-06 15:37:28 -0800590 if (!use_pde(pde))
Al Viro7e0e9532015-02-21 22:16:11 -0500591 return ERR_PTR(-EINVAL);
Al Virofceef392015-12-29 15:58:39 -0500592 set_delayed_call(done, proc_put_link, pde);
Al Viro680baac2015-05-02 13:32:22 -0400593 return pde->data;
Al Viro7e0e9532015-02-21 22:16:11 -0500594}
595
Al Viro7e0e9532015-02-21 22:16:11 -0500596const struct inode_operations proc_link_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -0500597 .get_link = proc_get_link,
Al Viro7e0e9532015-02-21 22:16:11 -0500598};
599
Alexey Dobriyan6d1b6e42011-01-12 17:00:33 -0800600struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds51f08852013-03-22 11:44:04 -0700602 struct inode *inode = new_inode_pseudo(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds51f08852013-03-22 11:44:04 -0700604 if (inode) {
605 inode->i_ino = de->low_ino;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700606 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
David Howellsa1d4aeb2008-02-07 00:15:45 -0800607 PROC_I(inode)->pde = de;
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700608
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500609 if (is_empty_pde(de)) {
610 make_empty_dir_inode(inode);
611 return inode;
612 }
Alexey Dobriyan5e971dc2008-04-29 01:01:41 -0700613 if (de->mode) {
614 inode->i_mode = de->mode;
615 inode->i_uid = de->uid;
616 inode->i_gid = de->gid;
617 }
618 if (de->size)
619 inode->i_size = de->size;
620 if (de->nlink)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200621 set_nlink(inode, de->nlink);
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800622
623 if (S_ISREG(inode->i_mode)) {
624 inode->i_op = de->proc_iops;
625 inode->i_fop = &proc_reg_file_ops;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800626#ifdef CONFIG_COMPAT
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800627 if (!de->proc_ops->proc_compat_ioctl) {
628 inode->i_fop = &proc_reg_file_ops_no_compat;
David Howellsa1d4aeb2008-02-07 00:15:45 -0800629 }
Alexey Dobriyand56c0d42020-02-03 17:37:14 -0800630#endif
631 } else if (S_ISDIR(inode->i_mode)) {
632 inode->i_op = de->proc_iops;
633 inode->i_fop = de->proc_dir_ops;
634 } else if (S_ISLNK(inode->i_mode)) {
635 inode->i_op = de->proc_iops;
636 inode->i_fop = NULL;
637 } else
638 BUG();
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300639 } else
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800640 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return inode;
Al Virod3d009c2013-01-25 20:11:22 -0500642}