xref: /netbsd-src/sys/miscfs/procfs/procfs_vfsops.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: procfs_vfsops.c,v 1.110 2020/12/28 22:36:16 riastradh 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.110 2020/12/28 22:36:16 riastradh 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/kauth.h>
92 #include <sys/kernel.h>
93 #include <sys/module.h>
94 #include <sys/mount.h>
95 #include <sys/proc.h>
96 #include <sys/signalvar.h>
97 #include <sys/sysctl.h>
98 #include <sys/syslog.h>
99 #include <sys/systm.h>
100 #include <sys/time.h>
101 #include <sys/vnode.h>
102 
103 #include <miscfs/genfs/genfs.h>
104 
105 #include <miscfs/procfs/procfs.h>
106 
107 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
108 
109 MODULE(MODULE_CLASS_VFS, procfs, "ptrace_common");
110 
111 VFS_PROTOS(procfs);
112 
113 static kauth_listener_t procfs_listener;
114 
115 /*
116  * VFS Operations.
117  *
118  * mount system call
119  */
120 /* ARGSUSED */
121 int
122 procfs_mount(
123     struct mount *mp,
124     const char *path,
125     void *data,
126     size_t *data_len)
127 {
128 	struct lwp *l = curlwp;
129 	struct procfsmount *pmnt;
130 	struct procfs_args *args = data;
131 	int error;
132 
133 	if (args == NULL)
134 		return EINVAL;
135 
136 	if (UIO_MX & (UIO_MX-1)) {
137 		log(LOG_ERR, "procfs: invalid directory entry size");
138 		return (EINVAL);
139 	}
140 
141 	if (mp->mnt_flag & MNT_GETARGS) {
142 		if (*data_len < sizeof *args)
143 			return EINVAL;
144 
145 		pmnt = VFSTOPROC(mp);
146 		if (pmnt == NULL)
147 			return EIO;
148 		args->version = PROCFS_ARGSVERSION;
149 		args->flags = pmnt->pmnt_flags;
150 		*data_len = sizeof *args;
151 		return 0;
152 	}
153 
154 	if (mp->mnt_flag & MNT_UPDATE)
155 		return (EOPNOTSUPP);
156 
157 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
158 		return EINVAL;
159 
160 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
161 
162 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
163 	mp->mnt_flag |= MNT_LOCAL;
164 	mp->mnt_data = pmnt;
165 	vfs_getnewfsid(mp);
166 
167 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
168 	    mp->mnt_op->vfs_name, mp, l);
169 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
170 	if (*data_len >= sizeof *args)
171 		pmnt->pmnt_flags = args->flags;
172 	else
173 		pmnt->pmnt_flags = 0;
174 
175 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_SHRLOOKUP;
176 	return error;
177 }
178 
179 /*
180  * unmount system call
181  */
182 int
183 procfs_unmount(struct mount *mp, int mntflags)
184 {
185 	int error;
186 	int flags = 0;
187 
188 	if (mntflags & MNT_FORCE)
189 		flags |= FORCECLOSE;
190 
191 	if ((error = vflush(mp, 0, flags)) != 0)
192 		return (error);
193 
194 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
195 
196 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
197 	mp->mnt_data = NULL;
198 
199 	return 0;
200 }
201 
202 int
203 procfs_root(struct mount *mp, int lktype, struct vnode **vpp)
204 {
205 	int error;
206 
207 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1);
208 	if (error == 0) {
209 		error = vn_lock(*vpp, lktype);
210 		if (error != 0) {
211 			vrele(*vpp);
212 			*vpp = NULL;
213 		}
214 	}
215 
216 	return error;
217 }
218 
219 /* ARGSUSED */
220 int
221 procfs_start(struct mount *mp, int flags)
222 {
223 
224 	return (0);
225 }
226 
227 /*
228  * Get file system statistics.
229  */
230 int
231 procfs_statvfs(struct mount *mp, struct statvfs *sbp)
232 {
233 
234 	genfs_statvfs(mp, sbp);
235 
236 	sbp->f_bsize = PAGE_SIZE;
237 	sbp->f_frsize = PAGE_SIZE;
238 	sbp->f_iosize = PAGE_SIZE;
239 	sbp->f_blocks = 1;
240 	sbp->f_files = maxproc;					/* approx */
241 	sbp->f_ffree = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
242 	sbp->f_favail = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
243 
244 	return (0);
245 }
246 
247 /*ARGSUSED*/
248 int
249 procfs_sync(
250     struct mount *mp,
251     int waitfor,
252     kauth_cred_t uc)
253 {
254 
255 	return (0);
256 }
257 
258 /*ARGSUSED*/
259 int
260 procfs_vget(struct mount *mp, ino_t ino, int lktype,
261     struct vnode **vpp)
262 {
263 	return (EOPNOTSUPP);
264 }
265 
266 int
267 procfs_loadvnode(struct mount *mp, struct vnode *vp,
268     const void *key, size_t key_len, const void **new_key)
269 {
270 	int error;
271 	struct pfskey pfskey;
272 	struct pfsnode *pfs;
273 
274 	KASSERT(key_len == sizeof(pfskey));
275 	memcpy(&pfskey, key, key_len);
276 
277 	pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP);
278 	pfs->pfs_pid = pfskey.pk_pid;
279 	pfs->pfs_type = pfskey.pk_type;
280 	pfs->pfs_fd = pfskey.pk_fd;
281 	pfs->pfs_vnode = vp;
282 	pfs->pfs_flags = 0;
283 	pfs->pfs_fileno =
284 	    PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd);
285 	vp->v_tag = VT_PROCFS;
286 	vp->v_op = procfs_vnodeop_p;
287 	vp->v_data = pfs;
288 
289 	switch (pfs->pfs_type) {
290 	case PFSroot:	/* /proc = dr-xr-xr-x */
291 		vp->v_vflag |= VV_ROOT;
292 		/*FALLTHROUGH*/
293 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
294 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
295 		vp->v_type = VDIR;
296 		break;
297 
298 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
299 		if (pfs->pfs_fd == -1) {
300 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
301 			    S_IROTH|S_IXOTH;
302 			vp->v_type = VDIR;
303 			break;
304 		}
305 		/*FALLTHROUGH*/
306 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
307 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
308 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
309 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
310 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
311 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
312 		vp->v_type = VLNK;
313 		break;
314 
315 	case PFSfd:
316 		if (pfs->pfs_fd == -1) {	/* /proc/N/fd = dr-x------ */
317 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
318 			vp->v_type = VDIR;
319 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
320 			file_t *fp;
321 			vnode_t *vxp;
322 			struct proc *p;
323 
324 			mutex_enter(&proc_lock);
325 			p = procfs_proc_find(mp, pfs->pfs_pid);
326 			mutex_exit(&proc_lock);
327 			if (p == NULL) {
328 				error = ENOENT;
329 				goto bad;
330 			}
331 			KASSERT(rw_read_held(&p->p_reflock));
332 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
333 				error = EBADF;
334 				goto bad;
335 			}
336 
337 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
338 			switch (fp->f_type) {
339 			case DTYPE_VNODE:
340 				vxp = fp->f_vnode;
341 
342 				/*
343 				 * We make symlinks for directories
344 				 * to avoid cycles.
345 				 */
346 				if (vxp->v_type == VDIR)
347 					goto symlink;
348 				vp->v_type = vxp->v_type;
349 				break;
350 			case DTYPE_PIPE:
351 				vp->v_type = VFIFO;
352 				break;
353 			case DTYPE_SOCKET:
354 				vp->v_type = VSOCK;
355 				break;
356 			case DTYPE_KQUEUE:
357 			case DTYPE_MISC:
358 			case DTYPE_SEM:
359 			symlink:
360 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
361 				    S_IXGRP|S_IROTH|S_IXOTH;
362 				vp->v_type = VLNK;
363 				break;
364 			default:
365 				error = EOPNOTSUPP;
366 				closef(fp);
367 				goto bad;
368 			}
369 			closef(fp);
370 		}
371 		break;
372 
373 	case PFSfile:	/* /proc/N/file = -rw------- */
374 	case PFSmem:	/* /proc/N/mem = -rw------- */
375 	case PFSregs:	/* /proc/N/regs = -rw------- */
376 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
377 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
378 		vp->v_type = VREG;
379 		break;
380 
381 	case PFSnote:	/* /proc/N/note = --w------ */
382 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
383 		pfs->pfs_mode = S_IWUSR;
384 		vp->v_type = VREG;
385 		break;
386 
387 	case PFSmap:		/* /proc/N/map = -r-------- */
388 	case PFSmaps:		/* /proc/N/maps = -r-------- */
389 	case PFSauxv:		/* /proc/N/auxv = -r-------- */
390 	case PFSenviron:	/* /proc/N/environ = -r-------- */
391 		pfs->pfs_mode = S_IRUSR;
392 		vp->v_type = VREG;
393 		break;
394 
395 	case PFSstatus:		/* /proc/N/status = -r--r--r-- */
396 	case PFSstat:		/* /proc/N/stat = -r--r--r-- */
397 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
398 	case PFSemul:		/* /proc/N/emul = -r--r--r-- */
399 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
400 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
401 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
402 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
403 	case PFSuptime:		/* /proc/uptime = -r--r--r-- */
404 	case PFSmounts:		/* /proc/mounts = -r--r--r-- */
405 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
406 	case PFSstatm:		/* /proc/N/statm = -r--r--r-- */
407 	case PFSversion:	/* /proc/version = -r--r--r-- */
408 	case PFSlimit:		/* /proc/limit = -r--r--r-- */
409 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
410 		vp->v_type = VREG;
411 		break;
412 
413 #ifdef __HAVE_PROCFS_MACHDEP
414 	PROCFS_MACHDEP_NODETYPE_CASES
415 		procfs_machdep_allocvp(vp);
416 		break;
417 #endif
418 
419 	default:
420 		panic("procfs_allocvp");
421 	}
422 
423 	uvm_vnp_setsize(vp, 0);
424 	*new_key = &pfs->pfs_key;
425 
426 	return 0;
427 
428 bad:
429 	vp->v_tag =VT_NON;
430 	vp->v_type = VNON;
431 	vp->v_op = NULL;
432 	vp->v_data = NULL;
433 	kmem_free(pfs, sizeof(*pfs));
434 	return error;
435 }
436 
437 void
438 procfs_init(void)
439 {
440 
441 }
442 
443 void
444 procfs_reinit(void)
445 {
446 
447 }
448 
449 void
450 procfs_done(void)
451 {
452 
453 }
454 
455 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
456 
457 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
458 	&procfs_vnodeop_opv_desc,
459 	NULL,
460 };
461 
462 struct vfsops procfs_vfsops = {
463 	.vfs_name = MOUNT_PROCFS,
464 	.vfs_min_mount_data = sizeof (struct procfs_args),
465 	.vfs_mount = procfs_mount,
466 	.vfs_start = procfs_start,
467 	.vfs_unmount = procfs_unmount,
468 	.vfs_root = procfs_root,
469 	.vfs_quotactl = (void *)eopnotsupp,
470 	.vfs_statvfs = procfs_statvfs,
471 	.vfs_sync = procfs_sync,
472 	.vfs_vget = procfs_vget,
473 	.vfs_loadvnode = procfs_loadvnode,
474 	.vfs_fhtovp = (void *)eopnotsupp,
475 	.vfs_vptofh = (void *)eopnotsupp,
476 	.vfs_init = procfs_init,
477 	.vfs_reinit = procfs_reinit,
478 	.vfs_done = procfs_done,
479 	.vfs_snapshot = (void *)eopnotsupp,
480 	.vfs_extattrctl = vfs_stdextattrctl,
481 	.vfs_suspendctl = genfs_suspendctl,
482 	.vfs_renamelock_enter = genfs_renamelock_enter,
483 	.vfs_renamelock_exit = genfs_renamelock_exit,
484 	.vfs_fsync = (void *)eopnotsupp,
485 	.vfs_opv_descs = procfs_vnodeopv_descs
486 };
487 
488 static int
489 procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
490     void *arg0, void *arg1, void *arg2, void *arg3)
491 {
492 	struct proc *p;
493 	struct pfsnode *pfs;
494 	int result;
495 
496 	result = KAUTH_RESULT_DEFER;
497 	p = arg0;
498 	pfs = arg1;
499 
500 	if (action != KAUTH_PROCESS_PROCFS)
501 		return result;
502 
503 	switch (pfs->pfs_type) {
504 	case PFSregs:
505 	case PFSfpregs:
506 	case PFSmem:
507 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
508 		    ISSET(p->p_flag, PK_SUGID))
509 			break;
510 
511 		/*FALLTHROUGH*/
512 	default:
513 		result = KAUTH_RESULT_ALLOW;
514 		break;
515 	}
516 
517 	return result;
518 }
519 
520 SYSCTL_SETUP(procfs_sysctl_setup, "procfs sysctl")
521 {
522 
523 	sysctl_createv(clog, 0, NULL, NULL,
524 		       CTLFLAG_PERMANENT,
525 		       CTLTYPE_NODE, "procfs",
526 		       SYSCTL_DESCR("Process file system"),
527 		       NULL, 0, NULL, 0,
528 		       CTL_VFS, 12, CTL_EOL);
529 	/*
530 	 * XXX the "12" above could be dynamic, thereby eliminating
531 	 * one more instance of the "number to vfs" mapping problem,
532 	 * but "12" is the order as taken from sys/mount.h
533 	 */
534 }
535 
536 static int
537 procfs_modcmd(modcmd_t cmd, void *arg)
538 {
539 	int error;
540 
541 	switch (cmd) {
542 	case MODULE_CMD_INIT:
543 		error = vfs_attach(&procfs_vfsops);
544 		if (error != 0)
545 			break;
546 
547 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
548 		    procfs_listener_cb, NULL);
549 
550 		break;
551 	case MODULE_CMD_FINI:
552 		error = vfs_detach(&procfs_vfsops);
553 		if (error != 0)
554 			break;
555 		kauth_unlisten_scope(procfs_listener);
556 		break;
557 	default:
558 		error = ENOTTY;
559 		break;
560 	}
561 
562 	return (error);
563 }
564