Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_module.c revision 1.4
      1 /*	$NetBSD: netbsd32_module.c,v 1.4 2015/12/19 13:15:21 maxv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.4 2015/12/19 13:15:21 maxv Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/dirent.h>
     36 #include <sys/kauth.h>
     37 #include <sys/module.h>
     38 #include <sys/kobj.h>
     39 
     40 #include <compat/netbsd32/netbsd32.h>
     41 #include <compat/netbsd32/netbsd32_syscall.h>
     42 #include <compat/netbsd32/netbsd32_syscallargs.h>
     43 #include <compat/netbsd32/netbsd32_conv.h>
     44 
     45 static int
     46 modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
     47 {
     48 	modstat_t *ms, *mso;
     49 	modinfo_t *mi;
     50 	module_t *mod;
     51 	vaddr_t addr;
     52 	size_t size;
     53 	size_t mslen;
     54 	int error;
     55 
     56 	kernconfig_lock();
     57 	mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
     58 	mso = kmem_zalloc(mslen, KM_SLEEP);
     59 	if (mso == NULL) {
     60 		kernconfig_unlock();
     61 		return ENOMEM;
     62 	}
     63 	ms = mso;
     64 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
     65 		mi = mod->mod_info;
     66 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
     67 		if (mi->mi_required != NULL) {
     68 			strlcpy(ms->ms_required, mi->mi_required,
     69 			    sizeof(ms->ms_required));
     70 		}
     71 		if (mod->mod_kobj != NULL) {
     72 			kobj_stat(mod->mod_kobj, &addr, &size);
     73 			ms->ms_addr = addr;
     74 			ms->ms_size = size;
     75 		}
     76 		ms->ms_class = mi->mi_class;
     77 		ms->ms_refcnt = mod->mod_refcnt;
     78 		ms->ms_source = mod->mod_source;
     79 		ms->ms_flags = mod->mod_flags;
     80 		ms++;
     81 	}
     82 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
     83 		mi = mod->mod_info;
     84 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
     85 		if (mi->mi_required != NULL) {
     86 			strlcpy(ms->ms_required, mi->mi_required,
     87 			    sizeof(ms->ms_required));
     88 		}
     89 		if (mod->mod_kobj != NULL) {
     90 			kobj_stat(mod->mod_kobj, &addr, &size);
     91 			ms->ms_addr = addr;
     92 			ms->ms_size = size;
     93 		}
     94 		ms->ms_class = mi->mi_class;
     95 		ms->ms_refcnt = -1;
     96 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
     97 		ms->ms_source = mod->mod_source;
     98 		ms++;
     99 	}
    100 	kernconfig_unlock();
    101 	error = copyout(mso, NETBSD32PTR64(iov->iov_base),
    102 	    min(mslen - sizeof(modstat_t), iov->iov_len));
    103 	kmem_free(mso, mslen);
    104 	if (error == 0) {
    105 		iov->iov_len = mslen - sizeof(modstat_t);
    106 		error = copyout(iov, arg, sizeof(*iov));
    107 	}
    108 
    109 	return error;
    110 }
    111 
    112 int
    113 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
    114 	register_t *result)
    115 {
    116 	/* {
    117 		syscallarg(int) cmd;
    118 		syscallarg(netbsd32_voidp) arg;
    119 	} */
    120 	char buf[MAXMODNAME];
    121 	struct netbsd32_iovec iov;
    122 	struct netbsd32_modctl_load ml;
    123 	int error;
    124 	void *arg;
    125 #ifdef MODULAR
    126 	uintptr_t loadtype;
    127 #endif
    128 
    129 	arg = SCARG_P32(uap, arg);
    130 
    131 	switch (SCARG(uap, cmd)) {
    132 	case MODCTL_LOAD:
    133 		error = copyin(arg, &ml, sizeof(ml));
    134 		if (error != 0)
    135 			break;
    136 		error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
    137 		     ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
    138 		break;
    139 
    140 	case MODCTL_UNLOAD:
    141 		error = copyinstr(arg, buf, sizeof(buf), NULL);
    142 		if (error == 0) {
    143 			error = module_unload(buf);
    144 		}
    145 		break;
    146 
    147 	case MODCTL_STAT:
    148 		error = copyin(arg, &iov, sizeof(iov));
    149 		if (error != 0) {
    150 			break;
    151 		}
    152 		error = modctl32_handle_stat(&iov, arg);
    153 		break;
    154 
    155 	case MODCTL_EXISTS:
    156 #ifndef MODULAR
    157 		error = ENOSYS;
    158 #else
    159 		loadtype = (uintptr_t)arg;
    160 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
    161 		case 0:			/* FALLTHROUGH */
    162 		case 1:
    163 			error = kauth_authorize_system(kauth_cred_get(),
    164 			     KAUTH_SYSTEM_MODULE, 0,
    165 			     (void *)(uintptr_t)MODCTL_LOAD,
    166 			     (void *)loadtype, NULL);
    167 			break;
    168 
    169 		default:
    170 			error = EINVAL;
    171 			break;
    172 		}
    173 #endif
    174 		break;
    175 
    176 	default:
    177 		error = EINVAL;
    178 		break;
    179 	}
    180 
    181 	return error;
    182 }
    183 
    184 
    185