Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.112
      1 /*	$NetBSD: procfs_subr.c,v 1.112 2018/04/16 20:27:38 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This code is derived from software contributed to Berkeley by
     37  * Jan-Simon Pendry.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     64  */
     65 
     66 /*
     67  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     68  * Copyright (c) 1993 Jan-Simon Pendry
     69  *
     70  * This code is derived from software contributed to Berkeley by
     71  * Jan-Simon Pendry.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.112 2018/04/16 20:27:38 hannken Exp $");
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/time.h>
    110 #include <sys/kernel.h>
    111 #include <sys/proc.h>
    112 #include <sys/fstrans.h>
    113 #include <sys/vnode.h>
    114 #include <sys/stat.h>
    115 #include <sys/file.h>
    116 #include <sys/filedesc.h>
    117 #include <sys/kauth.h>
    118 #include <sys/sysctl.h>
    119 
    120 #include <miscfs/procfs/procfs.h>
    121 
    122 /*
    123  * Allocate a pfsnode/vnode pair.  The vnode is referenced.
    124  * The pid, type, and file descriptor uniquely identify a pfsnode.
    125  */
    126 int
    127 procfs_allocvp(struct mount *mp, struct vnode **vpp, pid_t pid,
    128     pfstype type, int fd)
    129 {
    130 	struct pfskey key;
    131 
    132 	memset(&key, 0, sizeof(key));
    133 	key.pk_type = type;
    134 	key.pk_pid = pid;
    135 	key.pk_fd = fd;
    136 
    137 	return vcache_get(mp, &key, sizeof(key), vpp);
    138 }
    139 
    140 int
    141 procfs_rw(void *v)
    142 {
    143 	struct vop_read_args *ap = v;
    144 	struct vnode *vp = ap->a_vp;
    145 	struct uio *uio = ap->a_uio;
    146 	struct lwp *curl;
    147 	struct lwp *l;
    148 	struct pfsnode *pfs = VTOPFS(vp);
    149 	struct proc *p;
    150 	int error;
    151 
    152 	if (uio->uio_offset < 0)
    153 		return EINVAL;
    154 
    155 	if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
    156 		return error;
    157 
    158 	curl = curlwp;
    159 
    160 	/*
    161 	 * Do not allow init to be modified while in secure mode; it
    162 	 * could be duped into changing the security level.
    163 	 */
    164 #define	M2K(m)	((m) == UIO_READ ? KAUTH_REQ_PROCESS_PROCFS_READ : \
    165 		 KAUTH_REQ_PROCESS_PROCFS_WRITE)
    166 	mutex_enter(p->p_lock);
    167 	error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_PROCFS,
    168 	    p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
    169 	mutex_exit(p->p_lock);
    170 	if (error) {
    171 		procfs_proc_unlock(p);
    172 		return (error);
    173 	}
    174 #undef	M2K
    175 
    176 	mutex_enter(p->p_lock);
    177 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    178 		if (l->l_stat != LSZOMB)
    179 			break;
    180 	}
    181 	/* Process is exiting if no-LWPS or all LWPs are LSZOMB */
    182 	if (l == NULL) {
    183 		mutex_exit(p->p_lock);
    184 		procfs_proc_unlock(p);
    185 		return ESRCH;
    186 	}
    187 
    188 	lwp_addref(l);
    189 	mutex_exit(p->p_lock);
    190 
    191 	switch (pfs->pfs_type) {
    192 	case PFSnote:
    193 	case PFSnotepg:
    194 		error = procfs_donote(curl, p, pfs, uio);
    195 		break;
    196 
    197 	case PFSregs:
    198 		error = procfs_doregs(curl, l, pfs, uio);
    199 		break;
    200 
    201 	case PFSfpregs:
    202 		error = procfs_dofpregs(curl, l, pfs, uio);
    203 		break;
    204 
    205 	case PFSstatus:
    206 		error = procfs_dostatus(curl, l, pfs, uio);
    207 		break;
    208 
    209 	case PFSstat:
    210 		error = procfs_do_pid_stat(curl, l, pfs, uio);
    211 		break;
    212 
    213 	case PFSmap:
    214 		error = procfs_domap(curl, p, pfs, uio, 0);
    215 		break;
    216 
    217 	case PFSmaps:
    218 		error = procfs_domap(curl, p, pfs, uio, 1);
    219 		break;
    220 
    221 	case PFSmem:
    222 		error = procfs_domem(curl, l, pfs, uio);
    223 		break;
    224 
    225 	case PFScmdline:
    226 		error = procfs_doprocargs(curl, p, pfs, uio, KERN_PROC_ARGV);
    227 		break;
    228 
    229 	case PFSenviron:
    230 		error = procfs_doprocargs(curl, p, pfs, uio, KERN_PROC_ENV);
    231 		break;
    232 
    233 	case PFSmeminfo:
    234 		error = procfs_domeminfo(curl, p, pfs, uio);
    235 		break;
    236 
    237 	case PFSdevices:
    238 		error = procfs_dodevices(curl, p, pfs, uio);
    239 		break;
    240 
    241 	case PFScpuinfo:
    242 		error = procfs_docpuinfo(curl, p, pfs, uio);
    243 		break;
    244 
    245 	case PFScpustat:
    246 		error = procfs_docpustat(curl, p, pfs, uio);
    247 		break;
    248 
    249 	case PFSloadavg:
    250 		error = procfs_doloadavg(curl, p, pfs, uio);
    251 		break;
    252 
    253 	case PFSstatm:
    254 		error = procfs_do_pid_statm(curl, l, pfs, uio);
    255 		break;
    256 
    257 	case PFSfd:
    258 		error = procfs_dofd(curl, p, pfs, uio);
    259 		break;
    260 
    261 	case PFSuptime:
    262 		error = procfs_douptime(curl, p, pfs, uio);
    263 		break;
    264 
    265 	case PFSmounts:
    266 		error = procfs_domounts(curl, p, pfs, uio);
    267 		break;
    268 
    269 	case PFSemul:
    270 		error = procfs_doemul(curl, p, pfs, uio);
    271 		break;
    272 
    273 	case PFSversion:
    274 		error = procfs_doversion(curl, p, pfs, uio);
    275 		break;
    276 
    277 	case PFSauxv:
    278 		error = procfs_doauxv(curl, p, pfs, uio);
    279 		break;
    280 
    281 #ifdef __HAVE_PROCFS_MACHDEP
    282 	PROCFS_MACHDEP_NODETYPE_CASES
    283 		error = procfs_machdep_rw(curl, l, pfs, uio);
    284 		break;
    285 #endif
    286 
    287 	default:
    288 		error = EOPNOTSUPP;
    289 		break;
    290 	}
    291 
    292 	/*
    293 	 * Release the references that we acquired earlier.
    294 	 */
    295 	lwp_delref(l);
    296 	procfs_proc_unlock(p);
    297 
    298 	return (error);
    299 }
    300 
    301 /*
    302  * Get a string from userland into (bf).  Strip a trailing
    303  * nl character (to allow easy access from the shell).
    304  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    305  * will automatically add a nul char at the end.
    306  *
    307  * Returns 0 on success or the following errors
    308  *
    309  * EINVAL:    file offset is non-zero.
    310  * EMSGSIZE:  message is longer than kernel buffer
    311  * EFAULT:    user i/o buffer is not addressable
    312  */
    313 int
    314 vfs_getuserstr(struct uio *uio, char *bf, int *buflenp)
    315 {
    316 	int xlen;
    317 	int error;
    318 
    319 	if (uio->uio_offset != 0)
    320 		return (EINVAL);
    321 
    322 	xlen = *buflenp;
    323 
    324 	/* must be able to read the whole string in one go */
    325 	if (xlen < uio->uio_resid)
    326 		return (EMSGSIZE);
    327 	xlen = uio->uio_resid;
    328 
    329 	if ((error = uiomove(bf, xlen, uio)) != 0)
    330 		return (error);
    331 
    332 	/* allow multiple writes without seeks */
    333 	uio->uio_offset = 0;
    334 
    335 	/* cleanup string and remove trailing newline */
    336 	bf[xlen] = '\0';
    337 	xlen = strlen(bf);
    338 	if (xlen > 0 && bf[xlen-1] == '\n')
    339 		bf[--xlen] = '\0';
    340 	*buflenp = xlen;
    341 
    342 	return (0);
    343 }
    344 
    345 const vfs_namemap_t *
    346 vfs_findname(const vfs_namemap_t *nm, const char *bf, int buflen)
    347 {
    348 
    349 	for (; nm->nm_name; nm++)
    350 		if (memcmp(bf, nm->nm_name, buflen+1) == 0)
    351 			return (nm);
    352 
    353 	return (0);
    354 }
    355 
    356 static bool
    357 procfs_revoke_selector(void *arg, struct vnode *vp)
    358 {
    359 	struct proc *p = arg;
    360 	struct pfsnode *pfs;
    361 
    362 	KASSERT(mutex_owned(vp->v_interlock));
    363 
    364 	pfs = VTOPFS(vp);
    365 
    366 	return (pfs != NULL && pfs->pfs_pid == p->p_pid);
    367 }
    368 
    369 void
    370 procfs_revoke_vnodes(struct proc *p, void *arg)
    371 {
    372 	int error;
    373 	bool suspended;
    374 	struct vnode *vp;
    375 	struct vnode_iterator *marker;
    376 	struct mount *mp = (struct mount *)arg;
    377 
    378 	if (!(p->p_flag & PK_SUGID))
    379 		return;
    380 
    381 	suspended = false;
    382 	vfs_vnode_iterator_init(mp, &marker);
    383 
    384 	while ((vp = vfs_vnode_iterator_next(marker,
    385 	    procfs_revoke_selector, p)) != NULL) {
    386 		if (vrecycle(vp))
    387 			continue;
    388 		/* Vnode is busy, we have to suspend the mount for vgone(). */
    389 		while (! suspended) {
    390 			error = vfs_suspend(mp, 0);
    391 			if (error == 0) {
    392 				suspended = true;
    393 			} else if (error != EINTR && error != ERESTART) {
    394 				KASSERT(error == EOPNOTSUPP);
    395 				break;
    396 			}
    397 		}
    398 		vgone(vp);
    399 	}
    400 
    401 	if (suspended)
    402 		vfs_resume(mp);
    403 
    404 	vfs_vnode_iterator_destroy(marker);
    405 }
    406 
    407 int
    408 procfs_proc_lock(int pid, struct proc **bunghole, int notfound)
    409 {
    410 	struct proc *tp;
    411 	int error = 0;
    412 
    413 	mutex_enter(proc_lock);
    414 
    415 	if (pid == 0)
    416 		tp = &proc0;
    417 	else if ((tp = proc_find(pid)) == NULL)
    418 		error = notfound;
    419 	if (tp != NULL && !rw_tryenter(&tp->p_reflock, RW_READER))
    420 		error = EBUSY;
    421 
    422 	mutex_exit(proc_lock);
    423 
    424 	*bunghole = tp;
    425 	return error;
    426 }
    427 
    428 void
    429 procfs_proc_unlock(struct proc *p)
    430 {
    431 
    432 	rw_exit(&p->p_reflock);
    433 }
    434 
    435 int
    436 procfs_doemul(struct lwp *curl, struct proc *p,
    437     struct pfsnode *pfs, struct uio *uio)
    438 {
    439 	const char *ename = p->p_emul->e_name;
    440 	return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
    441 }
    442