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