1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/proc/inode.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
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>
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 #include <linux/magic.h>
29 #include <linux/uaccess.h>
33 static void proc_evict_inode(struct inode
*inode
)
35 struct proc_dir_entry
*de
;
36 struct ctl_table_header
*head
;
38 truncate_inode_pages_final(&inode
->i_data
);
41 /* Stop tracking associated processes */
42 put_pid(PROC_I(inode
)->pid
);
44 /* Let go of any associated proc directory entry */
49 head
= PROC_I(inode
)->sysctl
;
51 RCU_INIT_POINTER(PROC_I(inode
)->sysctl
, NULL
);
52 proc_sys_evict_inode(inode
, head
);
56 static struct kmem_cache
*proc_inode_cachep __ro_after_init
;
58 static struct inode
*proc_alloc_inode(struct super_block
*sb
)
60 struct proc_inode
*ei
;
63 ei
= kmem_cache_alloc(proc_inode_cachep
, GFP_KERNEL
);
68 ei
->op
.proc_get_link
= NULL
;
71 ei
->sysctl_entry
= NULL
;
73 inode
= &ei
->vfs_inode
;
77 static void proc_i_callback(struct rcu_head
*head
)
79 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
80 kmem_cache_free(proc_inode_cachep
, PROC_I(inode
));
83 static void proc_destroy_inode(struct inode
*inode
)
85 call_rcu(&inode
->i_rcu
, proc_i_callback
);
88 static void init_once(void *foo
)
90 struct proc_inode
*ei
= (struct proc_inode
*) foo
;
92 inode_init_once(&ei
->vfs_inode
);
95 void __init
proc_init_inodecache(void)
97 proc_inode_cachep
= kmem_cache_create("proc_inode_cache",
98 sizeof(struct proc_inode
),
99 0, (SLAB_RECLAIM_ACCOUNT
|
100 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
|
105 static int proc_show_options(struct seq_file
*seq
, struct dentry
*root
)
107 struct super_block
*sb
= root
->d_sb
;
108 struct pid_namespace
*pid
= sb
->s_fs_info
;
110 if (!gid_eq(pid
->pid_gid
, GLOBAL_ROOT_GID
))
111 seq_printf(seq
, ",gid=%u", from_kgid_munged(&init_user_ns
, pid
->pid_gid
));
112 if (pid
->hide_pid
!= HIDEPID_OFF
)
113 seq_printf(seq
, ",hidepid=%u", pid
->hide_pid
);
118 static const struct super_operations proc_sops
= {
119 .alloc_inode
= proc_alloc_inode
,
120 .destroy_inode
= proc_destroy_inode
,
121 .drop_inode
= generic_delete_inode
,
122 .evict_inode
= proc_evict_inode
,
123 .statfs
= simple_statfs
,
124 .remount_fs
= proc_remount
,
125 .show_options
= proc_show_options
,
128 enum {BIAS
= -1U<<31};
130 static inline int use_pde(struct proc_dir_entry
*pde
)
132 return likely(atomic_inc_unless_negative(&pde
->in_use
));
135 static void unuse_pde(struct proc_dir_entry
*pde
)
137 if (unlikely(atomic_dec_return(&pde
->in_use
) == BIAS
))
138 complete(pde
->pde_unload_completion
);
142 static void close_pdeo(struct proc_dir_entry
*pde
, struct pde_opener
*pdeo
)
145 * close() (proc_reg_release()) can't delete an entry and proceed:
146 * ->release hook needs to be available at the right moment.
148 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
149 * "struct file" needs to be available at the right moment.
151 * Therefore, first process to enter this function does ->release() and
152 * signals its completion to the other process which does nothing.
155 /* somebody else is doing that, just wait */
156 DECLARE_COMPLETION_ONSTACK(c
);
158 spin_unlock(&pde
->pde_unload_lock
);
159 wait_for_completion(&c
);
160 spin_lock(&pde
->pde_unload_lock
);
163 pdeo
->closing
= true;
164 spin_unlock(&pde
->pde_unload_lock
);
166 pde
->proc_fops
->release(file_inode(file
), file
);
167 spin_lock(&pde
->pde_unload_lock
);
168 /* After ->release. */
170 if (unlikely(pdeo
->c
))
176 void proc_entry_rundown(struct proc_dir_entry
*de
)
178 DECLARE_COMPLETION_ONSTACK(c
);
179 /* Wait until all existing callers into module are done. */
180 de
->pde_unload_completion
= &c
;
181 if (atomic_add_return(BIAS
, &de
->in_use
) != BIAS
)
182 wait_for_completion(&c
);
184 /* ->pde_openers list can't grow from now on. */
186 spin_lock(&de
->pde_unload_lock
);
187 while (!list_empty(&de
->pde_openers
)) {
188 struct pde_opener
*pdeo
;
189 pdeo
= list_first_entry(&de
->pde_openers
, struct pde_opener
, lh
);
190 close_pdeo(de
, pdeo
);
192 spin_unlock(&de
->pde_unload_lock
);
195 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
197 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
200 loff_t (*llseek
)(struct file
*, loff_t
, int);
201 llseek
= pde
->proc_fops
->llseek
;
203 llseek
= default_llseek
;
204 rv
= llseek(file
, offset
, whence
);
210 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
212 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
213 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
216 read
= pde
->proc_fops
->read
;
218 rv
= read(file
, buf
, count
, ppos
);
224 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
226 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
227 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
230 write
= pde
->proc_fops
->write
;
232 rv
= write(file
, buf
, count
, ppos
);
238 static __poll_t
proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
240 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
241 __poll_t rv
= DEFAULT_POLLMASK
;
242 __poll_t (*poll
)(struct file
*, struct poll_table_struct
*);
244 poll
= pde
->proc_fops
->poll
;
246 rv
= poll(file
, pts
);
252 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
254 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
256 long (*ioctl
)(struct file
*, unsigned int, unsigned long);
258 ioctl
= pde
->proc_fops
->unlocked_ioctl
;
260 rv
= ioctl(file
, cmd
, arg
);
267 static long proc_reg_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
269 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
271 long (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
273 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
275 rv
= compat_ioctl(file
, cmd
, arg
);
282 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
284 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
286 int (*mmap
)(struct file
*, struct vm_area_struct
*);
288 mmap
= pde
->proc_fops
->mmap
;
290 rv
= mmap(file
, vma
);
297 proc_reg_get_unmapped_area(struct file
*file
, unsigned long orig_addr
,
298 unsigned long len
, unsigned long pgoff
,
301 struct proc_dir_entry
*pde
= PDE(file_inode(file
));
302 unsigned long rv
= -EIO
;
305 typeof(proc_reg_get_unmapped_area
) *get_area
;
307 get_area
= pde
->proc_fops
->get_unmapped_area
;
310 get_area
= current
->mm
->get_unmapped_area
;
314 rv
= get_area(file
, orig_addr
, len
, pgoff
, flags
);
322 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
324 struct proc_dir_entry
*pde
= PDE(inode
);
326 int (*open
)(struct inode
*, struct file
*);
327 int (*release
)(struct inode
*, struct file
*);
328 struct pde_opener
*pdeo
;
332 * 1) PDE's ->release hook will be called no matter what
333 * either normally by close()/->release, or forcefully by
334 * rmmod/remove_proc_entry.
336 * 2) rmmod isn't blocked by opening file in /proc and sitting on
337 * the descriptor (including "rmmod foo </proc/foo" scenario).
339 * Save every "struct file" with custom ->release hook.
341 pdeo
= kmalloc(sizeof(struct pde_opener
), GFP_KERNEL
);
349 open
= pde
->proc_fops
->open
;
350 release
= pde
->proc_fops
->release
;
353 rv
= open(inode
, file
);
355 if (rv
== 0 && release
) {
356 /* To know what to release. */
358 pdeo
->closing
= false;
360 spin_lock(&pde
->pde_unload_lock
);
361 list_add(&pdeo
->lh
, &pde
->pde_openers
);
362 spin_unlock(&pde
->pde_unload_lock
);
370 static int proc_reg_release(struct inode
*inode
, struct file
*file
)
372 struct proc_dir_entry
*pde
= PDE(inode
);
373 struct pde_opener
*pdeo
;
374 spin_lock(&pde
->pde_unload_lock
);
375 list_for_each_entry(pdeo
, &pde
->pde_openers
, lh
) {
376 if (pdeo
->file
== file
) {
377 close_pdeo(pde
, pdeo
);
381 spin_unlock(&pde
->pde_unload_lock
);
385 static const struct file_operations proc_reg_file_ops
= {
386 .llseek
= proc_reg_llseek
,
387 .read
= proc_reg_read
,
388 .write
= proc_reg_write
,
389 .poll
= proc_reg_poll
,
390 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
392 .compat_ioctl
= proc_reg_compat_ioctl
,
394 .mmap
= proc_reg_mmap
,
395 .get_unmapped_area
= proc_reg_get_unmapped_area
,
396 .open
= proc_reg_open
,
397 .release
= proc_reg_release
,
401 static const struct file_operations proc_reg_file_ops_no_compat
= {
402 .llseek
= proc_reg_llseek
,
403 .read
= proc_reg_read
,
404 .write
= proc_reg_write
,
405 .poll
= proc_reg_poll
,
406 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
407 .mmap
= proc_reg_mmap
,
408 .get_unmapped_area
= proc_reg_get_unmapped_area
,
409 .open
= proc_reg_open
,
410 .release
= proc_reg_release
,
414 static void proc_put_link(void *p
)
419 static const char *proc_get_link(struct dentry
*dentry
,
421 struct delayed_call
*done
)
423 struct proc_dir_entry
*pde
= PDE(inode
);
425 return ERR_PTR(-EINVAL
);
426 set_delayed_call(done
, proc_put_link
, pde
);
430 const struct inode_operations proc_link_inode_operations
= {
431 .get_link
= proc_get_link
,
434 struct inode
*proc_get_inode(struct super_block
*sb
, struct proc_dir_entry
*de
)
436 struct inode
*inode
= new_inode_pseudo(sb
);
439 inode
->i_ino
= de
->low_ino
;
440 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
441 PROC_I(inode
)->pde
= de
;
443 if (is_empty_pde(de
)) {
444 make_empty_dir_inode(inode
);
448 inode
->i_mode
= de
->mode
;
449 inode
->i_uid
= de
->uid
;
450 inode
->i_gid
= de
->gid
;
453 inode
->i_size
= de
->size
;
455 set_nlink(inode
, de
->nlink
);
456 WARN_ON(!de
->proc_iops
);
457 inode
->i_op
= de
->proc_iops
;
459 if (S_ISREG(inode
->i_mode
)) {
461 if (!de
->proc_fops
->compat_ioctl
)
463 &proc_reg_file_ops_no_compat
;
466 inode
->i_fop
= &proc_reg_file_ops
;
468 inode
->i_fop
= de
->proc_fops
;
476 int proc_fill_super(struct super_block
*s
, void *data
, int silent
)
478 struct pid_namespace
*ns
= get_pid_ns(s
->s_fs_info
);
479 struct inode
*root_inode
;
482 if (!proc_parse_options(data
, ns
))
485 /* User space would break if executables or devices appear on proc */
486 s
->s_iflags
|= SB_I_USERNS_VISIBLE
| SB_I_NOEXEC
| SB_I_NODEV
;
487 s
->s_flags
|= SB_NODIRATIME
| SB_NOSUID
| SB_NOEXEC
;
488 s
->s_blocksize
= 1024;
489 s
->s_blocksize_bits
= 10;
490 s
->s_magic
= PROC_SUPER_MAGIC
;
491 s
->s_op
= &proc_sops
;
495 * procfs isn't actually a stacking filesystem; however, there is
496 * too much magic going on inside it to permit stacking things on
499 s
->s_stack_depth
= FILESYSTEM_MAX_STACK_DEPTH
;
502 root_inode
= proc_get_inode(s
, &proc_root
);
504 pr_err("proc_fill_super: get root inode failed\n");
508 s
->s_root
= d_make_root(root_inode
);
510 pr_err("proc_fill_super: allocate dentry failed\n");
514 ret
= proc_setup_self(s
);
518 return proc_setup_thread_self(s
);