Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_module.c revision 1.8
      1  1.8  pgoyette /*	$NetBSD: netbsd32_module.c,v 1.8 2019/01/27 02:08:40 pgoyette Exp $	*/
      2  1.1    martin 
      3  1.1    martin /*-
      4  1.1    martin  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1    martin  * All rights reserved.
      6  1.1    martin  *
      7  1.1    martin  * This code is derived from software developed for The NetBSD Foundation.
      8  1.1    martin  *
      9  1.1    martin  * Redistribution and use in source and binary forms, with or without
     10  1.1    martin  * modification, are permitted provided that the following conditions
     11  1.1    martin  * are met:
     12  1.1    martin  * 1. Redistributions of source code must retain the above copyright
     13  1.1    martin  *    notice, this list of conditions and the following disclaimer.
     14  1.1    martin  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1    martin  *    notice, this list of conditions and the following disclaimer in the
     16  1.1    martin  *    documentation and/or other materials provided with the distribution.
     17  1.1    martin  *
     18  1.1    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.1    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.1    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1    martin  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1    martin  */
     30  1.1    martin 
     31  1.1    martin #include <sys/cdefs.h>
     32  1.8  pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.8 2019/01/27 02:08:40 pgoyette Exp $");
     33  1.1    martin 
     34  1.1    martin #include <sys/param.h>
     35  1.1    martin #include <sys/dirent.h>
     36  1.2   msaitoh #include <sys/kauth.h>
     37  1.1    martin #include <sys/module.h>
     38  1.1    martin #include <sys/kobj.h>
     39  1.1    martin 
     40  1.1    martin #include <compat/netbsd32/netbsd32.h>
     41  1.1    martin #include <compat/netbsd32/netbsd32_syscall.h>
     42  1.1    martin #include <compat/netbsd32/netbsd32_syscallargs.h>
     43  1.1    martin #include <compat/netbsd32/netbsd32_conv.h>
     44  1.1    martin 
     45  1.3      maxv static int
     46  1.3      maxv modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
     47  1.3      maxv {
     48  1.8  pgoyette 	int ms_cnt;
     49  1.3      maxv 	modstat_t *ms, *mso;
     50  1.8  pgoyette 	size_t ms_len;
     51  1.8  pgoyette 	int req_cnt;
     52  1.8  pgoyette 	char *req, *reqo;
     53  1.8  pgoyette 	size_t req_len;
     54  1.8  pgoyette 	char *out_p;
     55  1.8  pgoyette 	size_t out_s;
     56  1.8  pgoyette 
     57  1.3      maxv 	modinfo_t *mi;
     58  1.3      maxv 	module_t *mod;
     59  1.3      maxv 	vaddr_t addr;
     60  1.3      maxv 	size_t size;
     61  1.8  pgoyette 	size_t used;
     62  1.8  pgoyette 	int off;
     63  1.3      maxv 	int error;
     64  1.6      maxv 	bool stataddr;
     65  1.6      maxv 
     66  1.6      maxv 	/* If not privileged, don't expose kernel addresses. */
     67  1.6      maxv 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
     68  1.6      maxv 	    0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
     69  1.6      maxv 	stataddr = (error == 0);
     70  1.3      maxv 
     71  1.3      maxv 	kernconfig_lock();
     72  1.8  pgoyette 	ms_cnt = 0;
     73  1.8  pgoyette 	req_len = 1;
     74  1.8  pgoyette 
     75  1.8  pgoyette 	/*
     76  1.8  pgoyette 	 * Count up the number of modstat_t needed, and total size of
     77  1.8  pgoyette 	 * require_module lists on both active and built-in lists
     78  1.8  pgoyette 	 */
     79  1.8  pgoyette 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
     80  1.8  pgoyette 		ms_cnt++;
     81  1.8  pgoyette 		mi = mod->mod_info;
     82  1.8  pgoyette 		if (mi->mi_required != NULL) {
     83  1.8  pgoyette 			req_cnt++;
     84  1.8  pgoyette 			req_len += strlen(mi->mi_required) + 1;
     85  1.8  pgoyette 		}
     86  1.8  pgoyette 	}
     87  1.8  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
     88  1.8  pgoyette 		ms_cnt++;
     89  1.8  pgoyette 		mi = mod->mod_info;
     90  1.8  pgoyette 		if (mi->mi_required != NULL) {
     91  1.8  pgoyette 			req_cnt++;
     92  1.8  pgoyette 			req_len += strlen(mi->mi_required) + 1;
     93  1.8  pgoyette 		}
     94  1.8  pgoyette 	}
     95  1.8  pgoyette 
     96  1.8  pgoyette 	/* Allocate internal buffers to hold all the output data */
     97  1.8  pgoyette 	ms_len = ms_cnt * sizeof(modstat_t);
     98  1.8  pgoyette 	ms = kmem_zalloc(ms_len, KM_SLEEP);
     99  1.8  pgoyette 	req = kmem_zalloc(req_len, KM_SLEEP);
    100  1.8  pgoyette 
    101  1.8  pgoyette 	mso = ms;
    102  1.8  pgoyette 	reqo = req++;
    103  1.8  pgoyette 	off = 1;
    104  1.8  pgoyette 
    105  1.8  pgoyette 	/*
    106  1.8  pgoyette 	 * Load data into our internal buffers for both active and
    107  1.8  pgoyette 	 * build-in module lists
    108  1.8  pgoyette 	 */
    109  1.3      maxv 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    110  1.3      maxv 		mi = mod->mod_info;
    111  1.3      maxv 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    112  1.3      maxv 		if (mi->mi_required != NULL) {
    113  1.8  pgoyette 			ms->ms_reqoffset = off;
    114  1.8  pgoyette 			used = strlcpy(req,  mi->mi_required, req_len - off);
    115  1.8  pgoyette 			KASSERTMSG(used < req_len - off, "reqlist grew!");
    116  1.8  pgoyette 			off = used + 1;
    117  1.8  pgoyette 			req += used + 1;
    118  1.8  pgoyette 		} else
    119  1.8  pgoyette 			ms->ms_reqoffset = 0;
    120  1.6      maxv 		if (mod->mod_kobj != NULL && stataddr) {
    121  1.3      maxv 			kobj_stat(mod->mod_kobj, &addr, &size);
    122  1.3      maxv 			ms->ms_addr = addr;
    123  1.3      maxv 			ms->ms_size = size;
    124  1.3      maxv 		}
    125  1.3      maxv 		ms->ms_class = mi->mi_class;
    126  1.3      maxv 		ms->ms_refcnt = mod->mod_refcnt;
    127  1.3      maxv 		ms->ms_source = mod->mod_source;
    128  1.4      maxv 		ms->ms_flags = mod->mod_flags;
    129  1.3      maxv 		ms++;
    130  1.3      maxv 	}
    131  1.3      maxv 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    132  1.3      maxv 		mi = mod->mod_info;
    133  1.3      maxv 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
    134  1.3      maxv 		if (mi->mi_required != NULL) {
    135  1.8  pgoyette 			ms->ms_reqoffset = off;
    136  1.8  pgoyette 			used = strlcpy(req,  mi->mi_required, req_len - off);
    137  1.8  pgoyette 			KASSERTMSG(used < req_len - off, "reqlist grew!");
    138  1.8  pgoyette 			off += used + 1;
    139  1.8  pgoyette 			req += used + 1;
    140  1.8  pgoyette 		} else
    141  1.8  pgoyette 			ms->ms_reqoffset = 0;
    142  1.6      maxv 		if (mod->mod_kobj != NULL && stataddr) {
    143  1.3      maxv 			kobj_stat(mod->mod_kobj, &addr, &size);
    144  1.3      maxv 			ms->ms_addr = addr;
    145  1.3      maxv 			ms->ms_size = size;
    146  1.3      maxv 		}
    147  1.3      maxv 		ms->ms_class = mi->mi_class;
    148  1.3      maxv 		ms->ms_refcnt = -1;
    149  1.3      maxv 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
    150  1.3      maxv 		ms->ms_source = mod->mod_source;
    151  1.3      maxv 		ms++;
    152  1.3      maxv 	}
    153  1.3      maxv 	kernconfig_unlock();
    154  1.8  pgoyette 
    155  1.8  pgoyette 	/*
    156  1.8  pgoyette 	 * Now copyout our internal buffers back to userland
    157  1.8  pgoyette 	 */
    158  1.8  pgoyette 	out_p = NETBSD32PTR64(iov->iov_base);
    159  1.8  pgoyette 	out_s = iov->iov_len;
    160  1.8  pgoyette 	size = sizeof(ms_cnt);
    161  1.8  pgoyette 
    162  1.8  pgoyette 	/* Copy out the count of modstat_t */
    163  1.8  pgoyette 	if (out_s) {
    164  1.8  pgoyette 		size = uimin(sizeof(ms_cnt), out_s);
    165  1.8  pgoyette 		error = copyout(&ms_cnt, out_p, size);
    166  1.8  pgoyette 		out_p += size;
    167  1.8  pgoyette 		out_s -= size;
    168  1.8  pgoyette 	}
    169  1.8  pgoyette 	/* Copy out the modstat_t array */
    170  1.8  pgoyette 	if (out_s && error == 0) {
    171  1.8  pgoyette 		size = uimin(ms_len, out_s);
    172  1.8  pgoyette 		error = copyout(mso, out_p, size);
    173  1.8  pgoyette 		out_p += size;
    174  1.8  pgoyette 		out_s -= size;
    175  1.8  pgoyette 	}
    176  1.8  pgoyette 	/* Copy out the "required" strings */
    177  1.8  pgoyette 	if (out_s && error == 0) {
    178  1.8  pgoyette 		size = uimin(req_len, out_s);
    179  1.8  pgoyette 		error = copyout(reqo, out_p, size);
    180  1.8  pgoyette 		out_p += size;
    181  1.8  pgoyette 		out_s -= size;
    182  1.8  pgoyette 	}
    183  1.8  pgoyette 	kmem_free(mso, ms_len);
    184  1.8  pgoyette 	kmem_free(reqo, req_len);
    185  1.8  pgoyette 
    186  1.8  pgoyette 	/* Finally, update the userland copy of the iovec's length */
    187  1.3      maxv 	if (error == 0) {
    188  1.8  pgoyette 		iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
    189  1.3      maxv 		error = copyout(iov, arg, sizeof(*iov));
    190  1.3      maxv 	}
    191  1.3      maxv 
    192  1.3      maxv 	return error;
    193  1.3      maxv }
    194  1.3      maxv 
    195  1.1    martin int
    196  1.8  pgoyette compat32_80_modctl_compat_stub(struct lwp *lwp,
    197  1.8  pgoyette     const struct netbsd32_modctl_args *uap, register_t *result)
    198  1.8  pgoyette {
    199  1.8  pgoyette 
    200  1.8  pgoyette 	return EPASSTHROUGH;
    201  1.8  pgoyette }
    202  1.8  pgoyette 
    203  1.8  pgoyette int
    204  1.1    martin netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
    205  1.1    martin 	register_t *result)
    206  1.1    martin {
    207  1.1    martin 	/* {
    208  1.1    martin 		syscallarg(int) cmd;
    209  1.1    martin 		syscallarg(netbsd32_voidp) arg;
    210  1.1    martin 	} */
    211  1.1    martin 	char buf[MAXMODNAME];
    212  1.1    martin 	struct netbsd32_iovec iov;
    213  1.1    martin 	struct netbsd32_modctl_load ml;
    214  1.1    martin 	int error;
    215  1.1    martin 	void *arg;
    216  1.1    martin #ifdef MODULAR
    217  1.1    martin 	uintptr_t loadtype;
    218  1.1    martin #endif
    219  1.1    martin 
    220  1.1    martin 	arg = SCARG_P32(uap, arg);
    221  1.1    martin 
    222  1.8  pgoyette 	MODULE_CALL_HOOK(compat32_80_modctl_hook, (lwp, uap, result),
    223  1.8  pgoyette 	    enosys(), error);
    224  1.8  pgoyette 	if (error != EPASSTHROUGH && error != ENOSYS)
    225  1.8  pgoyette 		return error;
    226  1.8  pgoyette 
    227  1.1    martin 	switch (SCARG(uap, cmd)) {
    228  1.1    martin 	case MODCTL_LOAD:
    229  1.1    martin 		error = copyin(arg, &ml, sizeof(ml));
    230  1.1    martin 		if (error != 0)
    231  1.1    martin 			break;
    232  1.1    martin 		error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
    233  1.1    martin 		     ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
    234  1.1    martin 		break;
    235  1.1    martin 
    236  1.1    martin 	case MODCTL_UNLOAD:
    237  1.1    martin 		error = copyinstr(arg, buf, sizeof(buf), NULL);
    238  1.1    martin 		if (error == 0) {
    239  1.1    martin 			error = module_unload(buf);
    240  1.1    martin 		}
    241  1.1    martin 		break;
    242  1.1    martin 
    243  1.1    martin 	case MODCTL_STAT:
    244  1.1    martin 		error = copyin(arg, &iov, sizeof(iov));
    245  1.1    martin 		if (error != 0) {
    246  1.1    martin 			break;
    247  1.1    martin 		}
    248  1.3      maxv 		error = modctl32_handle_stat(&iov, arg);
    249  1.1    martin 		break;
    250  1.1    martin 
    251  1.1    martin 	case MODCTL_EXISTS:
    252  1.1    martin #ifndef MODULAR
    253  1.1    martin 		error = ENOSYS;
    254  1.1    martin #else
    255  1.1    martin 		loadtype = (uintptr_t)arg;
    256  1.1    martin 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
    257  1.1    martin 		case 0:			/* FALLTHROUGH */
    258  1.1    martin 		case 1:
    259  1.1    martin 			error = kauth_authorize_system(kauth_cred_get(),
    260  1.1    martin 			     KAUTH_SYSTEM_MODULE, 0,
    261  1.1    martin 			     (void *)(uintptr_t)MODCTL_LOAD,
    262  1.1    martin 			     (void *)loadtype, NULL);
    263  1.1    martin 			break;
    264  1.1    martin 
    265  1.1    martin 		default:
    266  1.1    martin 			error = EINVAL;
    267  1.1    martin 			break;
    268  1.1    martin 		}
    269  1.1    martin #endif
    270  1.1    martin 		break;
    271  1.1    martin 
    272  1.1    martin 	default:
    273  1.1    martin 		error = EINVAL;
    274  1.1    martin 		break;
    275  1.1    martin 	}
    276  1.1    martin 
    277  1.1    martin 	return error;
    278  1.1    martin }
    279