xref: /linux/fs/proc/inode.c (revision 2de4e82318c7f9d34f4b08599a612cd4cd10bf0b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/proc/inode.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 #include <linux/cache.h>
9 #include <linux/time.h>
10 #include <linux/proc_fs.h>
11 #include <linux/kernel.h>
12 #include <linux/pid_namespace.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
16 #include <linux/completion.h>
17 #include <linux/poll.h>
18 #include <linux/printk.h>
19 #include <linux/file.h>
20 #include <linux/limits.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/sysctl.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/mount.h>
27 
28 #include <linux/uaccess.h>
29 
30 #include "internal.h"
31 
32 static void proc_evict_inode(struct inode *inode)
33 {
34 	struct proc_dir_entry *de;
35 	struct ctl_table_header *head;
36 	struct proc_inode *ei = PROC_I(inode);
37 
38 	truncate_inode_pages_final(&inode->i_data);
39 	clear_inode(inode);
40 
41 	/* Stop tracking associated processes */
42 	if (ei->pid) {
43 		proc_pid_evict_inode(ei);
44 		ei->pid = NULL;
45 	}
46 
47 	/* Let go of any associated proc directory entry */
48 	de = ei->pde;
49 	if (de) {
50 		pde_put(de);
51 		ei->pde = NULL;
52 	}
53 
54 	head = ei->sysctl;
55 	if (head) {
56 		RCU_INIT_POINTER(ei->sysctl, NULL);
57 		proc_sys_evict_inode(inode, head);
58 	}
59 }
60 
61 static struct kmem_cache *proc_inode_cachep __ro_after_init;
62 static struct kmem_cache *pde_opener_cache __ro_after_init;
63 
64 static struct inode *proc_alloc_inode(struct super_block *sb)
65 {
66 	struct proc_inode *ei;
67 
68 	ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
69 	if (!ei)
70 		return NULL;
71 	ei->pid = NULL;
72 	ei->fd = 0;
73 	ei->op.proc_get_link = NULL;
74 	ei->pde = NULL;
75 	ei->sysctl = NULL;
76 	ei->sysctl_entry = NULL;
77 	INIT_HLIST_NODE(&ei->sibling_inodes);
78 	ei->ns_ops = NULL;
79 	return &ei->vfs_inode;
80 }
81 
82 static void proc_free_inode(struct inode *inode)
83 {
84 	kmem_cache_free(proc_inode_cachep, PROC_I(inode));
85 }
86 
87 static void init_once(void *foo)
88 {
89 	struct proc_inode *ei = (struct proc_inode *) foo;
90 
91 	inode_init_once(&ei->vfs_inode);
92 }
93 
94 void __init proc_init_kmemcache(void)
95 {
96 	proc_inode_cachep = kmem_cache_create("proc_inode_cache",
97 					     sizeof(struct proc_inode),
98 					     0, (SLAB_RECLAIM_ACCOUNT|
99 						SLAB_MEM_SPREAD|SLAB_ACCOUNT|
100 						SLAB_PANIC),
101 					     init_once);
102 	pde_opener_cache =
103 		kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
104 				  SLAB_ACCOUNT|SLAB_PANIC, NULL);
105 	proc_dir_entry_cache = kmem_cache_create_usercopy(
106 		"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);
110 }
111 
112 void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
113 {
114 	struct inode *inode;
115 	struct proc_inode *ei;
116 	struct hlist_node *node;
117 	struct super_block *old_sb = NULL;
118 
119 	rcu_read_lock();
120 	for (;;) {
121 		struct super_block *sb;
122 		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;
132 		if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
133 			continue;
134 		inode = igrab(inode);
135 		rcu_read_unlock();
136 		if (sb != old_sb) {
137 			if (old_sb)
138 				deactivate_super(old_sb);
139 			old_sb = sb;
140 		}
141 		if (unlikely(!inode)) {
142 			rcu_read_lock();
143 			continue;
144 		}
145 
146 		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 		}
159 		iput(inode);
160 
161 		rcu_read_lock();
162 	}
163 	rcu_read_unlock();
164 	if (old_sb)
165 		deactivate_super(old_sb);
166 }
167 
168 static int proc_show_options(struct seq_file *seq, struct dentry *root)
169 {
170 	struct super_block *sb = root->d_sb;
171 	struct pid_namespace *pid = sb->s_fs_info;
172 
173 	if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
174 		seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
175 	if (pid->hide_pid != HIDEPID_OFF)
176 		seq_printf(seq, ",hidepid=%u", pid->hide_pid);
177 
178 	return 0;
179 }
180 
181 const struct super_operations proc_sops = {
182 	.alloc_inode	= proc_alloc_inode,
183 	.free_inode	= proc_free_inode,
184 	.drop_inode	= generic_delete_inode,
185 	.evict_inode	= proc_evict_inode,
186 	.statfs		= simple_statfs,
187 	.show_options	= proc_show_options,
188 };
189 
190 enum {BIAS = -1U<<31};
191 
192 static inline int use_pde(struct proc_dir_entry *pde)
193 {
194 	return likely(atomic_inc_unless_negative(&pde->in_use));
195 }
196 
197 static void unuse_pde(struct proc_dir_entry *pde)
198 {
199 	if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
200 		complete(pde->pde_unload_completion);
201 }
202 
203 /* pde is locked on entry, unlocked on exit */
204 static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
205 {
206 	/*
207 	 * close() (proc_reg_release()) can't delete an entry and proceed:
208 	 * ->release hook needs to be available at the right moment.
209 	 *
210 	 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
211 	 * "struct file" needs to be available at the right moment.
212 	 *
213 	 * Therefore, first process to enter this function does ->release() and
214 	 * signals its completion to the other process which does nothing.
215 	 */
216 	if (pdeo->closing) {
217 		/* somebody else is doing that, just wait */
218 		DECLARE_COMPLETION_ONSTACK(c);
219 		pdeo->c = &c;
220 		spin_unlock(&pde->pde_unload_lock);
221 		wait_for_completion(&c);
222 	} else {
223 		struct file *file;
224 		struct completion *c;
225 
226 		pdeo->closing = true;
227 		spin_unlock(&pde->pde_unload_lock);
228 		file = pdeo->file;
229 		pde->proc_ops->proc_release(file_inode(file), file);
230 		spin_lock(&pde->pde_unload_lock);
231 		/* After ->release. */
232 		list_del(&pdeo->lh);
233 		c = pdeo->c;
234 		spin_unlock(&pde->pde_unload_lock);
235 		if (unlikely(c))
236 			complete(c);
237 		kmem_cache_free(pde_opener_cache, pdeo);
238 	}
239 }
240 
241 void proc_entry_rundown(struct proc_dir_entry *de)
242 {
243 	DECLARE_COMPLETION_ONSTACK(c);
244 	/* Wait until all existing callers into module are done. */
245 	de->pde_unload_completion = &c;
246 	if (atomic_add_return(BIAS, &de->in_use) != BIAS)
247 		wait_for_completion(&c);
248 
249 	/* ->pde_openers list can't grow from now on. */
250 
251 	spin_lock(&de->pde_unload_lock);
252 	while (!list_empty(&de->pde_openers)) {
253 		struct pde_opener *pdeo;
254 		pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
255 		close_pdeo(de, pdeo);
256 		spin_lock(&de->pde_unload_lock);
257 	}
258 	spin_unlock(&de->pde_unload_lock);
259 }
260 
261 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
262 {
263 	struct proc_dir_entry *pde = PDE(file_inode(file));
264 	loff_t rv = -EINVAL;
265 	if (use_pde(pde)) {
266 		typeof_member(struct proc_ops, proc_lseek) lseek;
267 
268 		lseek = pde->proc_ops->proc_lseek;
269 		if (!lseek)
270 			lseek = default_llseek;
271 		rv = lseek(file, offset, whence);
272 		unuse_pde(pde);
273 	}
274 	return rv;
275 }
276 
277 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
278 {
279 	struct proc_dir_entry *pde = PDE(file_inode(file));
280 	ssize_t rv = -EIO;
281 	if (use_pde(pde)) {
282 		typeof_member(struct proc_ops, proc_read) read;
283 
284 		read = pde->proc_ops->proc_read;
285 		if (read)
286 			rv = read(file, buf, count, ppos);
287 		unuse_pde(pde);
288 	}
289 	return rv;
290 }
291 
292 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
293 {
294 	struct proc_dir_entry *pde = PDE(file_inode(file));
295 	ssize_t rv = -EIO;
296 	if (use_pde(pde)) {
297 		typeof_member(struct proc_ops, proc_write) write;
298 
299 		write = pde->proc_ops->proc_write;
300 		if (write)
301 			rv = write(file, buf, count, ppos);
302 		unuse_pde(pde);
303 	}
304 	return rv;
305 }
306 
307 static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
308 {
309 	struct proc_dir_entry *pde = PDE(file_inode(file));
310 	__poll_t rv = DEFAULT_POLLMASK;
311 	if (use_pde(pde)) {
312 		typeof_member(struct proc_ops, proc_poll) poll;
313 
314 		poll = pde->proc_ops->proc_poll;
315 		if (poll)
316 			rv = poll(file, pts);
317 		unuse_pde(pde);
318 	}
319 	return rv;
320 }
321 
322 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
323 {
324 	struct proc_dir_entry *pde = PDE(file_inode(file));
325 	long rv = -ENOTTY;
326 	if (use_pde(pde)) {
327 		typeof_member(struct proc_ops, proc_ioctl) ioctl;
328 
329 		ioctl = pde->proc_ops->proc_ioctl;
330 		if (ioctl)
331 			rv = ioctl(file, cmd, arg);
332 		unuse_pde(pde);
333 	}
334 	return rv;
335 }
336 
337 #ifdef CONFIG_COMPAT
338 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
339 {
340 	struct proc_dir_entry *pde = PDE(file_inode(file));
341 	long rv = -ENOTTY;
342 	if (use_pde(pde)) {
343 		typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
344 
345 		compat_ioctl = pde->proc_ops->proc_compat_ioctl;
346 		if (compat_ioctl)
347 			rv = compat_ioctl(file, cmd, arg);
348 		unuse_pde(pde);
349 	}
350 	return rv;
351 }
352 #endif
353 
354 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
355 {
356 	struct proc_dir_entry *pde = PDE(file_inode(file));
357 	int rv = -EIO;
358 	if (use_pde(pde)) {
359 		typeof_member(struct proc_ops, proc_mmap) mmap;
360 
361 		mmap = pde->proc_ops->proc_mmap;
362 		if (mmap)
363 			rv = mmap(file, vma);
364 		unuse_pde(pde);
365 	}
366 	return rv;
367 }
368 
369 static unsigned long
370 proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
371 			   unsigned long len, unsigned long pgoff,
372 			   unsigned long flags)
373 {
374 	struct proc_dir_entry *pde = PDE(file_inode(file));
375 	unsigned long rv = -EIO;
376 
377 	if (use_pde(pde)) {
378 		typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
379 
380 		get_area = pde->proc_ops->proc_get_unmapped_area;
381 #ifdef CONFIG_MMU
382 		if (!get_area)
383 			get_area = current->mm->get_unmapped_area;
384 #endif
385 
386 		if (get_area)
387 			rv = get_area(file, orig_addr, len, pgoff, flags);
388 		else
389 			rv = orig_addr;
390 		unuse_pde(pde);
391 	}
392 	return rv;
393 }
394 
395 static int proc_reg_open(struct inode *inode, struct file *file)
396 {
397 	struct proc_dir_entry *pde = PDE(inode);
398 	int rv = 0;
399 	typeof_member(struct proc_ops, proc_open) open;
400 	typeof_member(struct proc_ops, proc_release) release;
401 	struct pde_opener *pdeo;
402 
403 	/*
404 	 * Ensure that
405 	 * 1) PDE's ->release hook will be called no matter what
406 	 *    either normally by close()/->release, or forcefully by
407 	 *    rmmod/remove_proc_entry.
408 	 *
409 	 * 2) rmmod isn't blocked by opening file in /proc and sitting on
410 	 *    the descriptor (including "rmmod foo </proc/foo" scenario).
411 	 *
412 	 * Save every "struct file" with custom ->release hook.
413 	 */
414 	if (!use_pde(pde))
415 		return -ENOENT;
416 
417 	release = pde->proc_ops->proc_release;
418 	if (release) {
419 		pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
420 		if (!pdeo) {
421 			rv = -ENOMEM;
422 			goto out_unuse;
423 		}
424 	}
425 
426 	open = pde->proc_ops->proc_open;
427 	if (open)
428 		rv = open(inode, file);
429 
430 	if (release) {
431 		if (rv == 0) {
432 			/* To know what to release. */
433 			pdeo->file = file;
434 			pdeo->closing = false;
435 			pdeo->c = NULL;
436 			spin_lock(&pde->pde_unload_lock);
437 			list_add(&pdeo->lh, &pde->pde_openers);
438 			spin_unlock(&pde->pde_unload_lock);
439 		} else
440 			kmem_cache_free(pde_opener_cache, pdeo);
441 	}
442 
443 out_unuse:
444 	unuse_pde(pde);
445 	return rv;
446 }
447 
448 static int proc_reg_release(struct inode *inode, struct file *file)
449 {
450 	struct proc_dir_entry *pde = PDE(inode);
451 	struct pde_opener *pdeo;
452 	spin_lock(&pde->pde_unload_lock);
453 	list_for_each_entry(pdeo, &pde->pde_openers, lh) {
454 		if (pdeo->file == file) {
455 			close_pdeo(pde, pdeo);
456 			return 0;
457 		}
458 	}
459 	spin_unlock(&pde->pde_unload_lock);
460 	return 0;
461 }
462 
463 static const struct file_operations proc_reg_file_ops = {
464 	.llseek		= proc_reg_llseek,
465 	.read		= proc_reg_read,
466 	.write		= proc_reg_write,
467 	.poll		= proc_reg_poll,
468 	.unlocked_ioctl	= proc_reg_unlocked_ioctl,
469 #ifdef CONFIG_COMPAT
470 	.compat_ioctl	= proc_reg_compat_ioctl,
471 #endif
472 	.mmap		= proc_reg_mmap,
473 	.get_unmapped_area = proc_reg_get_unmapped_area,
474 	.open		= proc_reg_open,
475 	.release	= proc_reg_release,
476 };
477 
478 #ifdef CONFIG_COMPAT
479 static const struct file_operations proc_reg_file_ops_no_compat = {
480 	.llseek		= proc_reg_llseek,
481 	.read		= proc_reg_read,
482 	.write		= proc_reg_write,
483 	.poll		= proc_reg_poll,
484 	.unlocked_ioctl	= proc_reg_unlocked_ioctl,
485 	.mmap		= proc_reg_mmap,
486 	.get_unmapped_area = proc_reg_get_unmapped_area,
487 	.open		= proc_reg_open,
488 	.release	= proc_reg_release,
489 };
490 #endif
491 
492 static void proc_put_link(void *p)
493 {
494 	unuse_pde(p);
495 }
496 
497 static const char *proc_get_link(struct dentry *dentry,
498 				 struct inode *inode,
499 				 struct delayed_call *done)
500 {
501 	struct proc_dir_entry *pde = PDE(inode);
502 	if (!use_pde(pde))
503 		return ERR_PTR(-EINVAL);
504 	set_delayed_call(done, proc_put_link, pde);
505 	return pde->data;
506 }
507 
508 const struct inode_operations proc_link_inode_operations = {
509 	.get_link	= proc_get_link,
510 };
511 
512 struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
513 {
514 	struct inode *inode = new_inode_pseudo(sb);
515 
516 	if (inode) {
517 		inode->i_ino = de->low_ino;
518 		inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
519 		PROC_I(inode)->pde = de;
520 
521 		if (is_empty_pde(de)) {
522 			make_empty_dir_inode(inode);
523 			return inode;
524 		}
525 		if (de->mode) {
526 			inode->i_mode = de->mode;
527 			inode->i_uid = de->uid;
528 			inode->i_gid = de->gid;
529 		}
530 		if (de->size)
531 			inode->i_size = de->size;
532 		if (de->nlink)
533 			set_nlink(inode, de->nlink);
534 
535 		if (S_ISREG(inode->i_mode)) {
536 			inode->i_op = de->proc_iops;
537 			inode->i_fop = &proc_reg_file_ops;
538 #ifdef CONFIG_COMPAT
539 			if (!de->proc_ops->proc_compat_ioctl) {
540 				inode->i_fop = &proc_reg_file_ops_no_compat;
541 			}
542 #endif
543 		} else if (S_ISDIR(inode->i_mode)) {
544 			inode->i_op = de->proc_iops;
545 			inode->i_fop = de->proc_dir_ops;
546 		} else if (S_ISLNK(inode->i_mode)) {
547 			inode->i_op = de->proc_iops;
548 			inode->i_fop = NULL;
549 		} else
550 			BUG();
551 	} else
552 	       pde_put(de);
553 	return inode;
554 }
555