xref: /netbsd-src/sys/miscfs/procfs/procfs_vfsops.c (revision 897be3a4bac39d8b2e92077bf29f4a2e67d31983)
1 /*	$NetBSD: procfs_vfsops.c,v 1.117 2024/07/01 01:35:53 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
35  */
36 
37 /*
38  * Copyright (c) 1993 Jan-Simon Pendry
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
72  */
73 
74 /*
75  * procfs VFS interface
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.117 2024/07/01 01:35:53 christos Exp $");
80 
81 #if defined(_KERNEL_OPT)
82 #include "opt_compat_netbsd.h"
83 #endif
84 
85 #include <sys/param.h>
86 #include <sys/atomic.h>
87 #include <sys/buf.h>
88 #include <sys/dirent.h>
89 #include <sys/file.h>
90 #include <sys/filedesc.h>
91 #include <sys/fstrans.h>
92 #include <sys/kauth.h>
93 #include <sys/kernel.h>
94 #include <sys/module.h>
95 #include <sys/mount.h>
96 #include <sys/proc.h>
97 #include <sys/signalvar.h>
98 #include <sys/sysctl.h>
99 #include <sys/syslog.h>
100 #include <sys/systm.h>
101 #include <sys/time.h>
102 #include <sys/vnode.h>
103 
104 #include <miscfs/genfs/genfs.h>
105 
106 #include <miscfs/procfs/procfs.h>
107 
108 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
109 
110 MODULE(MODULE_CLASS_VFS, procfs, "ptrace_common");
111 
112 VFS_PROTOS(procfs);
113 
114 #define PROCFS_HASHSIZE	256
115 #define PROCFS_EXEC_HOOK ((void *)1)
116 #define PROCFS_EXIT_HOOK ((void *)2)
117 
118 static kauth_listener_t procfs_listener;
119 static void *procfs_exechook;
120 static void *procfs_exithook;
121 LIST_HEAD(hashhead, pfsnode);
122 static u_long procfs_hashmask;
123 static struct hashhead *procfs_hashtab;
124 static kmutex_t procfs_hashlock;
125 
126 static struct hashhead *
127 procfs_hashhead(pid_t pid)
128 {
129 
130 	return &procfs_hashtab[pid & procfs_hashmask];
131 }
132 
133 void
134 procfs_hashrem(struct pfsnode *pfs)
135 {
136 
137 	mutex_enter(&procfs_hashlock);
138 	LIST_REMOVE(pfs, pfs_hash);
139 	mutex_exit(&procfs_hashlock);
140 }
141 
142 /*
143  * VFS Operations.
144  *
145  * mount system call
146  */
147 /* ARGSUSED */
148 int
149 procfs_mount(
150     struct mount *mp,
151     const char *path,
152     void *data,
153     size_t *data_len)
154 {
155 	struct lwp *l = curlwp;
156 	struct procfsmount *pmnt;
157 	struct procfs_args *args = data;
158 	int error;
159 
160 	if (args == NULL)
161 		return EINVAL;
162 
163 	if (UIO_MX & (UIO_MX-1)) {
164 		log(LOG_ERR, "procfs: invalid directory entry size");
165 		return (EINVAL);
166 	}
167 
168 	if (mp->mnt_flag & MNT_GETARGS) {
169 		if (*data_len < sizeof *args)
170 			return EINVAL;
171 
172 		pmnt = VFSTOPROC(mp);
173 		if (pmnt == NULL)
174 			return EIO;
175 		args->version = PROCFS_ARGSVERSION;
176 		args->flags = pmnt->pmnt_flags;
177 		*data_len = sizeof *args;
178 		return 0;
179 	}
180 
181 	if (mp->mnt_flag & MNT_UPDATE)
182 		return (EOPNOTSUPP);
183 
184 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
185 		return EINVAL;
186 
187 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
188 
189 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
190 	mp->mnt_flag |= MNT_LOCAL;
191 	mp->mnt_data = pmnt;
192 	vfs_getnewfsid(mp);
193 
194 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
195 	    mp->mnt_op->vfs_name, mp, l);
196 	if (*data_len >= sizeof *args)
197 		pmnt->pmnt_flags = args->flags;
198 	else
199 		pmnt->pmnt_flags = 0;
200 
201 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_SHRLOOKUP;
202 	return error;
203 }
204 
205 /*
206  * unmount system call
207  */
208 int
209 procfs_unmount(struct mount *mp, int mntflags)
210 {
211 	int error;
212 	int flags = 0;
213 
214 	if (mntflags & MNT_FORCE)
215 		flags |= FORCECLOSE;
216 
217 	if ((error = vflush(mp, 0, flags)) != 0)
218 		return (error);
219 
220 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
221 	mp->mnt_data = NULL;
222 
223 	return 0;
224 }
225 
226 int
227 procfs_root(struct mount *mp, int lktype, struct vnode **vpp)
228 {
229 	int error;
230 
231 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1);
232 	if (error == 0) {
233 		error = vn_lock(*vpp, lktype);
234 		if (error != 0) {
235 			vrele(*vpp);
236 			*vpp = NULL;
237 		}
238 	}
239 
240 	return error;
241 }
242 
243 /* ARGSUSED */
244 int
245 procfs_start(struct mount *mp, int flags)
246 {
247 
248 	return (0);
249 }
250 
251 /*
252  * Get file system statistics.
253  */
254 int
255 procfs_statvfs(struct mount *mp, struct statvfs *sbp)
256 {
257 
258 	genfs_statvfs(mp, sbp);
259 
260 	sbp->f_bsize = PAGE_SIZE;
261 	sbp->f_frsize = PAGE_SIZE;
262 	sbp->f_iosize = PAGE_SIZE;
263 	sbp->f_blocks = 1;
264 	sbp->f_files = maxproc;					/* approx */
265 	sbp->f_ffree = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
266 	sbp->f_favail = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
267 
268 	return (0);
269 }
270 
271 /*ARGSUSED*/
272 int
273 procfs_sync(
274     struct mount *mp,
275     int waitfor,
276     kauth_cred_t uc)
277 {
278 
279 	return (0);
280 }
281 
282 /*ARGSUSED*/
283 int
284 procfs_vget(struct mount *mp, ino_t ino, int lktype,
285     struct vnode **vpp)
286 {
287 	return (EOPNOTSUPP);
288 }
289 
290 int
291 procfs_loadvnode(struct mount *mp, struct vnode *vp,
292     const void *key, size_t key_len, const void **new_key)
293 {
294 	int error;
295 	struct pfskey pfskey;
296 	struct pfsnode *pfs;
297 
298 	KASSERT(key_len == sizeof(pfskey));
299 	memcpy(&pfskey, key, key_len);
300 
301 	pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP);
302 	pfs->pfs_pid = pfskey.pk_pid;
303 	pfs->pfs_type = pfskey.pk_type;
304 	pfs->pfs_fd = pfskey.pk_fd;
305 	pfs->pfs_vnode = vp;
306 	pfs->pfs_mount = mp;
307 	pfs->pfs_flags = 0;
308 	pfs->pfs_fileno =
309 	    PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd);
310 	vp->v_tag = VT_PROCFS;
311 	vp->v_op = procfs_vnodeop_p;
312 	vp->v_data = pfs;
313 
314 	switch (pfs->pfs_type) {
315 	case PFSroot:	/* /proc = dr-xr-xr-x */
316 		vp->v_vflag |= VV_ROOT;
317 		/*FALLTHROUGH*/
318 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
319 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
320 		vp->v_type = VDIR;
321 		break;
322 
323 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
324 		if (pfs->pfs_fd == -1) {
325 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
326 			    S_IROTH|S_IXOTH;
327 			vp->v_type = VDIR;
328 			break;
329 		}
330 		/*FALLTHROUGH*/
331 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
332 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
333 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
334 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
335 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
336 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
337 		vp->v_type = VLNK;
338 		break;
339 
340 	case PFSfd:
341 		if (pfs->pfs_fd == -1) {	/* /proc/N/fd = dr-x------ */
342 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
343 			vp->v_type = VDIR;
344 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
345 			file_t *fp;
346 			vnode_t *vxp;
347 			struct proc *p;
348 
349 			mutex_enter(&proc_lock);
350 			p = procfs_proc_find(mp, pfs->pfs_pid);
351 			mutex_exit(&proc_lock);
352 			if (p == NULL) {
353 				error = ENOENT;
354 				goto bad;
355 			}
356 			KASSERT(rw_read_held(&p->p_reflock));
357 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
358 				error = EBADF;
359 				goto bad;
360 			}
361 
362 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
363 			switch (fp->f_type) {
364 			case DTYPE_VNODE:
365 				vxp = fp->f_vnode;
366 
367 				/*
368 				 * We make symlinks for directories
369 				 * to avoid cycles.
370 				 */
371 				if (vxp->v_type == VDIR ||
372 				    procfs_proc_is_linux_compat())
373 					goto symlink;
374 				vp->v_type = vxp->v_type;
375 				break;
376 			case DTYPE_PIPE:
377 				vp->v_type = VFIFO;
378 				break;
379 			case DTYPE_SOCKET:
380 				vp->v_type = VSOCK;
381 				break;
382 			case DTYPE_KQUEUE:
383 			case DTYPE_MISC:
384 			case DTYPE_SEM:
385 			symlink:
386 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
387 				    S_IXGRP|S_IROTH|S_IXOTH;
388 				vp->v_type = VLNK;
389 				break;
390 			default:
391 				error = EOPNOTSUPP;
392 				closef(fp);
393 				goto bad;
394 			}
395 			closef(fp);
396 		}
397 		break;
398 
399 	case PFSfile:	/* /proc/N/file = -rw------- */
400 	case PFSmem:	/* /proc/N/mem = -rw------- */
401 	case PFSregs:	/* /proc/N/regs = -rw------- */
402 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
403 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
404 		vp->v_type = VREG;
405 		break;
406 
407 	case PFSnote:	/* /proc/N/note = --w------ */
408 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
409 		pfs->pfs_mode = S_IWUSR;
410 		vp->v_type = VREG;
411 		break;
412 
413 	case PFSmap:		/* /proc/N/map = -r-------- */
414 	case PFSmaps:		/* /proc/N/maps = -r-------- */
415 	case PFSauxv:		/* /proc/N/auxv = -r-------- */
416 	case PFSenviron:	/* /proc/N/environ = -r-------- */
417 		pfs->pfs_mode = S_IRUSR;
418 		vp->v_type = VREG;
419 		break;
420 
421 	case PFSstatus:		/* /proc/N/status = -r--r--r-- */
422 	case PFSstat:		/* /proc/N/stat = -r--r--r-- */
423 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
424 	case PFSemul:		/* /proc/N/emul = -r--r--r-- */
425 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
426 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
427 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
428 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
429 	case PFSuptime:		/* /proc/uptime = -r--r--r-- */
430 	case PFSmounts:		/* /proc/mounts = -r--r--r-- */
431 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
432 	case PFSstatm:		/* /proc/N/statm = -r--r--r-- */
433 	case PFSversion:	/* /proc/version = -r--r--r-- */
434 	case PFSlimit:		/* /proc/N/limit = -r--r--r-- */
435 	case PFSlimits:		/* /proc/N/limits = -r--r--r-- */
436 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
437 		vp->v_type = VREG;
438 		break;
439 
440 	case PFSsys:	/* /proc/sys = dr-xr-xr-x */
441 	case PFSsysfs:	/* /proc/sys/fs = dr-xr-xr-x */
442 	case PFSmqueue:	/* /proc/sys/fs/mqueue = dr-xr-xr-x */
443 	case PFSsysvipc:/* /proc/sysvipc = dr-xr-xr-x */
444 		if (pfs->pfs_fd == -1) {
445 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
446 			    S_IROTH|S_IXOTH;
447 			vp->v_type = VDIR;
448 			break;
449 		}
450 		/*FALLTHROUGH*/
451 	case PFSmq_msg_def:	/* /proc/sys/fs/mqueue/msg_default = -r--r--r-- */
452 	case PFSmq_msg_max:	/* /proc/sys/fs/mqueue/msg_max = -r--r--r-- */
453 	case PFSmq_siz_def:	/* /proc/sys/fs/mqueue/msgsize_default = -r--r--r-- */
454 	case PFSmq_siz_max:	/* /proc/sys/fs/mqueue/msgsize_max = -r--r--r-- */
455 	case PFSmq_qmax:	/* /proc/sys/fs/mqueue/queues_max = -r--r--r-- */
456 	case PFSsysvipc_msg:	/* /proc/sysvipc/msg = -r--r--r-- */
457 	case PFSsysvipc_sem:	/* /proc/sysvipc/sem = -r--r--r-- */
458 	case PFSsysvipc_shm:	/* /proc/sysvipc/shm = -r--r--r-- */
459 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
460 		vp->v_type = VREG;
461 		break;
462 
463 #ifdef __HAVE_PROCFS_MACHDEP
464 	PROCFS_MACHDEP_NODETYPE_CASES
465 		procfs_machdep_allocvp(vp);
466 		break;
467 #endif
468 
469 	default:
470 		panic("procfs_allocvp");
471 	}
472 
473 	mutex_enter(&procfs_hashlock);
474 	LIST_INSERT_HEAD(procfs_hashhead(pfs->pfs_pid), pfs, pfs_hash);
475 	mutex_exit(&procfs_hashlock);
476 
477 	uvm_vnp_setsize(vp, 0);
478 	*new_key = &pfs->pfs_key;
479 
480 	return 0;
481 
482 bad:
483 	vp->v_tag =VT_NON;
484 	vp->v_type = VNON;
485 	vp->v_op = NULL;
486 	vp->v_data = NULL;
487 	kmem_free(pfs, sizeof(*pfs));
488 	return error;
489 }
490 
491 void
492 procfs_init(void)
493 {
494 
495 }
496 
497 void
498 procfs_reinit(void)
499 {
500 
501 }
502 
503 void
504 procfs_done(void)
505 {
506 
507 }
508 
509 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
510 
511 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
512 	&procfs_vnodeop_opv_desc,
513 	NULL,
514 };
515 
516 struct vfsops procfs_vfsops = {
517 	.vfs_name = MOUNT_PROCFS,
518 	.vfs_min_mount_data = sizeof (struct procfs_args),
519 	.vfs_mount = procfs_mount,
520 	.vfs_start = procfs_start,
521 	.vfs_unmount = procfs_unmount,
522 	.vfs_root = procfs_root,
523 	.vfs_quotactl = (void *)eopnotsupp,
524 	.vfs_statvfs = procfs_statvfs,
525 	.vfs_sync = procfs_sync,
526 	.vfs_vget = procfs_vget,
527 	.vfs_loadvnode = procfs_loadvnode,
528 	.vfs_fhtovp = (void *)eopnotsupp,
529 	.vfs_vptofh = (void *)eopnotsupp,
530 	.vfs_init = procfs_init,
531 	.vfs_reinit = procfs_reinit,
532 	.vfs_done = procfs_done,
533 	.vfs_snapshot = (void *)eopnotsupp,
534 	.vfs_extattrctl = vfs_stdextattrctl,
535 	.vfs_suspendctl = genfs_suspendctl,
536 	.vfs_renamelock_enter = genfs_renamelock_enter,
537 	.vfs_renamelock_exit = genfs_renamelock_exit,
538 	.vfs_fsync = (void *)eopnotsupp,
539 	.vfs_opv_descs = procfs_vnodeopv_descs
540 };
541 
542 static void
543 procfs_exechook_cb(struct proc *p, void *arg)
544 {
545 	struct hashhead *head;
546 	struct pfsnode *pfs;
547 	struct mount *mp;
548 	struct pfskey key;
549 	struct vnode *vp;
550 	int error;
551 
552 	if (arg == PROCFS_EXEC_HOOK && !(p->p_flag & PK_SUGID))
553 		return;
554 
555 	head = procfs_hashhead(p->p_pid);
556 
557 again:
558 	mutex_enter(&procfs_hashlock);
559 	LIST_FOREACH(pfs, head, pfs_hash) {
560 		if (pfs->pfs_pid != p->p_pid)
561 			continue;
562 		mp = pfs->pfs_mount;
563 		key = pfs->pfs_key;
564 		vfs_ref(mp);
565 		mutex_exit(&procfs_hashlock);
566 
567 		error = vcache_get(mp, &key, sizeof(key), &vp);
568 		vfs_rele(mp);
569 		if (error != 0)
570 			goto again;
571 		if (vrecycle(vp))
572 			goto again;
573 		do {
574 			error = vfs_suspend(mp, 0);
575 		} while (error == EINTR || error == ERESTART);
576 		vgone(vp);
577 		if (error == 0)
578 			vfs_resume(mp);
579 		goto again;
580 	}
581 	mutex_exit(&procfs_hashlock);
582 }
583 
584 static int
585 procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
586     void *arg0, void *arg1, void *arg2, void *arg3)
587 {
588 	struct proc *p;
589 	struct pfsnode *pfs;
590 	int result;
591 
592 	result = KAUTH_RESULT_DEFER;
593 	p = arg0;
594 	pfs = arg1;
595 
596 	if (action != KAUTH_PROCESS_PROCFS)
597 		return result;
598 
599 	switch (pfs->pfs_type) {
600 	case PFSregs:
601 	case PFSfpregs:
602 	case PFSmem:
603 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
604 		    ISSET(p->p_flag, PK_SUGID))
605 			break;
606 
607 		/*FALLTHROUGH*/
608 	default:
609 		result = KAUTH_RESULT_ALLOW;
610 		break;
611 	}
612 
613 	return result;
614 }
615 
616 SYSCTL_SETUP(procfs_sysctl_setup, "procfs sysctl")
617 {
618 
619 	sysctl_createv(clog, 0, NULL, NULL,
620 		       CTLFLAG_PERMANENT,
621 		       CTLTYPE_NODE, "procfs",
622 		       SYSCTL_DESCR("Process file system"),
623 		       NULL, 0, NULL, 0,
624 		       CTL_VFS, 12, CTL_EOL);
625 	/*
626 	 * XXX the "12" above could be dynamic, thereby eliminating
627 	 * one more instance of the "number to vfs" mapping problem,
628 	 * but "12" is the order as taken from sys/mount.h
629 	 */
630 }
631 
632 static int
633 procfs_modcmd(modcmd_t cmd, void *arg)
634 {
635 	int error;
636 
637 	switch (cmd) {
638 	case MODULE_CMD_INIT:
639 		error = vfs_attach(&procfs_vfsops);
640 		if (error != 0)
641 			break;
642 
643 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
644 		    procfs_listener_cb, NULL);
645 
646 		procfs_exechook = exechook_establish(procfs_exechook_cb,
647 		    PROCFS_EXEC_HOOK);
648 		procfs_exithook = exithook_establish(procfs_exechook_cb,
649 		    PROCFS_EXIT_HOOK);
650 
651 		mutex_init(&procfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
652 		procfs_hashtab = hashinit(PROCFS_HASHSIZE, HASH_LIST, true,
653 		    &procfs_hashmask);
654 
655 		break;
656 	case MODULE_CMD_FINI:
657 		error = vfs_detach(&procfs_vfsops);
658 		if (error != 0)
659 			break;
660 		kauth_unlisten_scope(procfs_listener);
661 		exechook_disestablish(procfs_exechook);
662 		exithook_disestablish(procfs_exithook);
663 		mutex_destroy(&procfs_hashlock);
664 		hashdone(procfs_hashtab, HASH_LIST, procfs_hashmask);
665 		break;
666 	default:
667 		error = ENOTTY;
668 		break;
669 	}
670 
671 	return (error);
672 }
673