1 /* $NetBSD: sys_module.c,v 1.22 2017/06/01 02:45:13 chs 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: sys_module.c,v 1.22 2017/06/01 02:45:13 chs 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 51 /* 52 * Arbitrary limit to avoid DoS for excessive memory allocation. 53 */ 54 #define MAXPROPSLEN 4096 55 56 int 57 handle_modctl_load(const char *ml_filename, int ml_flags, const char *ml_props, 58 size_t ml_propslen) 59 { 60 char *path; 61 char *props; 62 int error; 63 prop_dictionary_t dict; 64 size_t propslen = 0; 65 66 if ((ml_props != NULL && ml_propslen == 0) || 67 (ml_props == NULL && ml_propslen > 0)) { 68 return EINVAL; 69 } 70 71 path = PNBUF_GET(); 72 error = copyinstr(ml_filename, path, MAXPATHLEN, NULL); 73 if (error != 0) 74 goto out1; 75 76 if (ml_props != NULL) { 77 if (ml_propslen > MAXPROPSLEN) { 78 error = ENOMEM; 79 goto out1; 80 } 81 propslen = ml_propslen + 1; 82 83 props = kmem_alloc(propslen, KM_SLEEP); 84 error = copyinstr(ml_props, props, propslen, NULL); 85 if (error != 0) 86 goto out2; 87 88 dict = prop_dictionary_internalize(props); 89 if (dict == NULL) { 90 error = EINVAL; 91 goto out2; 92 } 93 } else { 94 dict = NULL; 95 props = NULL; 96 } 97 98 error = module_load(path, ml_flags, dict, MODULE_CLASS_ANY); 99 100 if (dict != NULL) { 101 prop_object_release(dict); 102 } 103 104 out2: 105 if (props != NULL) { 106 kmem_free(props, propslen); 107 } 108 out1: 109 PNBUF_PUT(path); 110 return error; 111 } 112 113 static int 114 handle_modctl_stat(struct iovec *iov, void *arg) 115 { 116 modstat_t *ms, *mso; 117 modinfo_t *mi; 118 module_t *mod; 119 vaddr_t addr; 120 size_t size; 121 size_t mslen; 122 int error; 123 124 kernconfig_lock(); 125 mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t); 126 mso = kmem_zalloc(mslen, KM_SLEEP); 127 ms = mso; 128 TAILQ_FOREACH(mod, &module_list, mod_chain) { 129 mi = mod->mod_info; 130 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name)); 131 if (mi->mi_required != NULL) { 132 strlcpy(ms->ms_required, mi->mi_required, 133 sizeof(ms->ms_required)); 134 } 135 if (mod->mod_kobj != NULL) { 136 kobj_stat(mod->mod_kobj, &addr, &size); 137 ms->ms_addr = addr; 138 ms->ms_size = size; 139 } 140 ms->ms_class = mi->mi_class; 141 ms->ms_refcnt = mod->mod_refcnt; 142 ms->ms_source = mod->mod_source; 143 ms->ms_flags = mod->mod_flags; 144 ms++; 145 } 146 TAILQ_FOREACH(mod, &module_builtins, mod_chain) { 147 mi = mod->mod_info; 148 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name)); 149 if (mi->mi_required != NULL) { 150 strlcpy(ms->ms_required, mi->mi_required, 151 sizeof(ms->ms_required)); 152 } 153 if (mod->mod_kobj != NULL) { 154 kobj_stat(mod->mod_kobj, &addr, &size); 155 ms->ms_addr = addr; 156 ms->ms_size = size; 157 } 158 ms->ms_class = mi->mi_class; 159 ms->ms_refcnt = -1; 160 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL); 161 ms->ms_source = mod->mod_source; 162 ms++; 163 } 164 kernconfig_unlock(); 165 error = copyout(mso, iov->iov_base, 166 min(mslen - sizeof(modstat_t), iov->iov_len)); 167 kmem_free(mso, mslen); 168 if (error == 0) { 169 iov->iov_len = mslen - sizeof(modstat_t); 170 error = copyout(iov, arg, sizeof(*iov)); 171 } 172 173 return error; 174 } 175 176 int 177 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap, 178 register_t *retval) 179 { 180 /* { 181 syscallarg(int) cmd; 182 syscallarg(void *) arg; 183 } */ 184 char buf[MAXMODNAME]; 185 struct iovec iov; 186 modctl_load_t ml; 187 int error; 188 void *arg; 189 #ifdef MODULAR 190 uintptr_t loadtype; 191 #endif 192 193 arg = SCARG(uap, arg); 194 195 switch (SCARG(uap, cmd)) { 196 case MODCTL_LOAD: 197 error = copyin(arg, &ml, sizeof(ml)); 198 if (error != 0) 199 break; 200 error = handle_modctl_load(ml.ml_filename, ml.ml_flags, 201 ml.ml_props, ml.ml_propslen); 202 break; 203 204 case MODCTL_UNLOAD: 205 error = copyinstr(arg, buf, sizeof(buf), NULL); 206 if (error == 0) { 207 error = module_unload(buf); 208 } 209 break; 210 211 case MODCTL_STAT: 212 error = copyin(arg, &iov, sizeof(iov)); 213 if (error != 0) { 214 break; 215 } 216 error = handle_modctl_stat(&iov, arg); 217 break; 218 219 case MODCTL_EXISTS: 220 #ifndef MODULAR 221 error = ENOSYS; 222 #else 223 loadtype = (uintptr_t)arg; 224 switch (loadtype) { /* 0 = modload, 1 = autoload */ 225 case 0: /* FALLTHROUGH */ 226 case 1: 227 error = kauth_authorize_system(kauth_cred_get(), 228 KAUTH_SYSTEM_MODULE, 0, 229 (void *)(uintptr_t)MODCTL_LOAD, 230 (void *)loadtype, NULL); 231 break; 232 233 default: 234 error = EINVAL; 235 break; 236 } 237 #endif 238 break; 239 240 default: 241 error = EINVAL; 242 break; 243 } 244 245 return error; 246 } 247