Home | History | Annotate | Line # | Download | only in common
kern_mod_80.c revision 1.5.4.3
      1  1.5.4.3    martin /*	$NetBSD: kern_mod_80.c,v 1.5.4.3 2020/04/08 14:08:00 martin Exp $	*/
      2  1.5.4.2  christos 
      3  1.5.4.2  christos /*-
      4  1.5.4.2  christos  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.5.4.2  christos  * All rights reserved.
      6  1.5.4.2  christos  *
      7  1.5.4.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.5.4.2  christos  * modification, are permitted provided that the following conditions
      9  1.5.4.2  christos  * are met:
     10  1.5.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.5.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.5.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.5.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.5.4.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.5.4.2  christos  *
     16  1.5.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.5.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.5.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.5.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.5.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.5.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.5.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.5.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.5.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.5.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.5.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.5.4.2  christos  */
     28  1.5.4.2  christos 
     29  1.5.4.2  christos /*
     30  1.5.4.2  christos  * System calls relating to loadable modules.
     31  1.5.4.2  christos  */
     32  1.5.4.2  christos 
     33  1.5.4.2  christos #include <sys/cdefs.h>
     34  1.5.4.3    martin __KERNEL_RCSID(0, "$NetBSD: kern_mod_80.c,v 1.5.4.3 2020/04/08 14:08:00 martin Exp $");
     35  1.5.4.2  christos 
     36  1.5.4.2  christos #ifdef _KERNEL_OPT
     37  1.5.4.2  christos #include "opt_compat_netbsd.h"
     38  1.5.4.2  christos #include "opt_modular.h"
     39  1.5.4.2  christos #endif
     40  1.5.4.2  christos 
     41  1.5.4.2  christos #include <sys/param.h>
     42  1.5.4.2  christos #include <sys/systm.h>
     43  1.5.4.2  christos #include <sys/proc.h>
     44  1.5.4.2  christos #include <sys/namei.h>
     45  1.5.4.2  christos #include <sys/kauth.h>
     46  1.5.4.2  christos #include <sys/kmem.h>
     47  1.5.4.2  christos #include <sys/kobj.h>
     48  1.5.4.2  christos #include <sys/module.h>
     49  1.5.4.2  christos #include <sys/syscall.h>
     50  1.5.4.2  christos #include <sys/syscallargs.h>
     51  1.5.4.2  christos #include <sys/compat_stub.h>
     52  1.5.4.2  christos 
     53  1.5.4.2  christos #include <compat/sys/module.h>
     54  1.5.4.2  christos 
     55  1.5.4.2  christos #include <compat/common/compat_mod.h>
     56  1.5.4.2  christos 
     57  1.5.4.2  christos static int
     58  1.5.4.2  christos compat_80_modstat(int cmd, struct iovec *iov, void *arg)
     59  1.5.4.2  christos {
     60  1.5.4.2  christos 	omodstat_t *oms, *omso;
     61  1.5.4.2  christos 	modinfo_t *mi;
     62  1.5.4.2  christos 	module_t *mod;
     63  1.5.4.2  christos 	vaddr_t addr;
     64  1.5.4.2  christos 	size_t size;
     65  1.5.4.2  christos 	size_t omslen;
     66  1.5.4.2  christos 	size_t used;
     67  1.5.4.2  christos 	int error;
     68  1.5.4.2  christos 	int omscnt;
     69  1.5.4.2  christos 	bool stataddr;
     70  1.5.4.2  christos 	const char *suffix = "...";
     71  1.5.4.2  christos 
     72  1.5.4.2  christos 	if (cmd != MODCTL_OSTAT)
     73  1.5.4.2  christos 		return EINVAL;
     74  1.5.4.2  christos 
     75  1.5.4.2  christos 	error = copyin(arg, iov, sizeof(*iov));
     76  1.5.4.2  christos 	if (error != 0) {
     77  1.5.4.2  christos 		return error;
     78  1.5.4.2  christos 	}
     79  1.5.4.2  christos 
     80  1.5.4.2  christos 	/* If not privileged, don't expose kernel addresses. */
     81  1.5.4.2  christos 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
     82  1.5.4.2  christos 	    0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
     83  1.5.4.2  christos 	stataddr = (error == 0);
     84  1.5.4.2  christos 
     85  1.5.4.2  christos 	kernconfig_lock();
     86  1.5.4.2  christos 	omscnt = 0;
     87  1.5.4.2  christos 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
     88  1.5.4.2  christos 		omscnt++;
     89  1.5.4.2  christos 		mi = mod->mod_info;
     90  1.5.4.2  christos 	}
     91  1.5.4.2  christos 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
     92  1.5.4.2  christos 		omscnt++;
     93  1.5.4.2  christos 		mi = mod->mod_info;
     94  1.5.4.2  christos 	}
     95  1.5.4.2  christos 	omslen = omscnt * sizeof(omodstat_t);
     96  1.5.4.2  christos 	omso = kmem_zalloc(omslen, KM_SLEEP);
     97  1.5.4.2  christos 	oms = omso;
     98  1.5.4.2  christos 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
     99  1.5.4.2  christos 		mi = mod->mod_info;
    100  1.5.4.2  christos 		strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
    101  1.5.4.2  christos 		if (mi->mi_required != NULL) {
    102  1.5.4.2  christos 			used = strlcpy(oms->oms_required, mi->mi_required,
    103  1.5.4.2  christos 			    sizeof(oms->oms_required));
    104  1.5.4.2  christos 			if (used >= sizeof(oms->oms_required)) {
    105  1.5.4.2  christos 				oms->oms_required[sizeof(oms->oms_required) -
    106  1.5.4.2  christos 				    strlen(suffix) - 1] = '\0';
    107  1.5.4.2  christos 				strlcat(oms->oms_required, suffix,
    108  1.5.4.2  christos 				    sizeof(oms->oms_required));
    109  1.5.4.2  christos 			}
    110  1.5.4.2  christos 		}
    111  1.5.4.2  christos 		if (mod->mod_kobj != NULL && stataddr) {
    112  1.5.4.2  christos 			kobj_stat(mod->mod_kobj, &addr, &size);
    113  1.5.4.2  christos 			oms->oms_addr = addr;
    114  1.5.4.2  christos 			oms->oms_size = size;
    115  1.5.4.2  christos 		}
    116  1.5.4.2  christos 		oms->oms_class = mi->mi_class;
    117  1.5.4.2  christos 		oms->oms_refcnt = mod->mod_refcnt;
    118  1.5.4.2  christos 		oms->oms_source = mod->mod_source;
    119  1.5.4.2  christos 		oms->oms_flags = mod->mod_flags;
    120  1.5.4.2  christos 		oms++;
    121  1.5.4.2  christos 	}
    122  1.5.4.2  christos 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    123  1.5.4.2  christos 		mi = mod->mod_info;
    124  1.5.4.2  christos 		strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
    125  1.5.4.2  christos 		if (mi->mi_required != NULL) {
    126  1.5.4.2  christos 			used = strlcpy(oms->oms_required, mi->mi_required,
    127  1.5.4.2  christos 			    sizeof(oms->oms_required));
    128  1.5.4.2  christos 			if (used >= sizeof(oms->oms_required)) {
    129  1.5.4.2  christos 				oms->oms_required[sizeof(oms->oms_required) -
    130  1.5.4.2  christos 				    strlen(suffix) - 1] = '\0';
    131  1.5.4.2  christos 				strlcat(oms->oms_required, suffix,
    132  1.5.4.2  christos 				    sizeof(oms->oms_required));
    133  1.5.4.2  christos 			}
    134  1.5.4.2  christos 		}
    135  1.5.4.2  christos 		if (mod->mod_kobj != NULL && stataddr) {
    136  1.5.4.2  christos 			kobj_stat(mod->mod_kobj, &addr, &size);
    137  1.5.4.2  christos 			oms->oms_addr = addr;
    138  1.5.4.2  christos 			oms->oms_size = size;
    139  1.5.4.2  christos 		}
    140  1.5.4.2  christos 		oms->oms_class = mi->mi_class;
    141  1.5.4.2  christos 		oms->oms_refcnt = -1;
    142  1.5.4.2  christos 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
    143  1.5.4.2  christos 		oms->oms_source = mod->mod_source;
    144  1.5.4.2  christos 		oms++;
    145  1.5.4.2  christos 	}
    146  1.5.4.2  christos 	kernconfig_unlock();
    147  1.5.4.2  christos 	error = copyout(omso, iov->iov_base, uimin(omslen, iov->iov_len));
    148  1.5.4.2  christos 	kmem_free(omso, omslen);
    149  1.5.4.2  christos 	if (error == 0) {
    150  1.5.4.2  christos 		iov->iov_len = omslen;
    151  1.5.4.2  christos 		error = copyout(iov, arg, sizeof(*iov));
    152  1.5.4.2  christos 	}
    153  1.5.4.2  christos 
    154  1.5.4.2  christos 	return error;
    155  1.5.4.2  christos }
    156  1.5.4.2  christos 
    157  1.5.4.2  christos void
    158  1.5.4.2  christos kern_mod_80_init(void)
    159  1.5.4.2  christos {
    160  1.5.4.2  christos 
    161  1.5.4.3    martin 	MODULE_HOOK_SET(compat_modstat_80_hook, compat_80_modstat);
    162  1.5.4.2  christos }
    163  1.5.4.2  christos 
    164  1.5.4.2  christos void
    165  1.5.4.2  christos kern_mod_80_fini(void)
    166  1.5.4.2  christos {
    167  1.5.4.2  christos 
    168  1.5.4.2  christos 	MODULE_HOOK_UNSET(compat_modstat_80_hook);
    169  1.5.4.2  christos }
    170