1 1.8 pgoyette /* $NetBSD: netbsd32_compat_80.c,v 1.8 2023/07/29 13:57:28 pgoyette Exp $ */ 2 1.2 pgoyette 3 1.2 pgoyette /*- 4 1.2 pgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 1.2 pgoyette * All rights reserved. 6 1.2 pgoyette * 7 1.2 pgoyette * This code is derived from software developed for The NetBSD Foundation. 8 1.2 pgoyette * 9 1.2 pgoyette * Redistribution and use in source and binary forms, with or without 10 1.2 pgoyette * modification, are permitted provided that the following conditions 11 1.2 pgoyette * are met: 12 1.2 pgoyette * 1. Redistributions of source code must retain the above copyright 13 1.2 pgoyette * notice, this list of conditions and the following disclaimer. 14 1.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright 15 1.2 pgoyette * notice, this list of conditions and the following disclaimer in the 16 1.2 pgoyette * documentation and/or other materials provided with the distribution. 17 1.2 pgoyette * 18 1.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.2 pgoyette * POSSIBILITY OF SUCH DAMAGE. 29 1.2 pgoyette */ 30 1.2 pgoyette 31 1.2 pgoyette #include <sys/cdefs.h> 32 1.8 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_80.c,v 1.8 2023/07/29 13:57:28 pgoyette Exp $"); 33 1.2 pgoyette 34 1.2 pgoyette #include <sys/param.h> 35 1.2 pgoyette #include <sys/dirent.h> 36 1.2 pgoyette #include <sys/kauth.h> 37 1.2 pgoyette #include <sys/module.h> 38 1.2 pgoyette #include <sys/kobj.h> 39 1.2 pgoyette 40 1.2 pgoyette #include <compat/sys/siginfo.h> 41 1.2 pgoyette 42 1.3 pgoyette #include <compat/sys/module.h> 43 1.3 pgoyette 44 1.2 pgoyette #include <compat/netbsd32/netbsd32.h> 45 1.2 pgoyette #include <compat/netbsd32/netbsd32_syscall.h> 46 1.2 pgoyette #include <compat/netbsd32/netbsd32_syscallargs.h> 47 1.2 pgoyette #include <compat/netbsd32/netbsd32_conv.h> 48 1.2 pgoyette 49 1.2 pgoyette int netbsd32_80_modctl(struct lwp *, const struct netbsd32_modctl_args *, 50 1.2 pgoyette register_t *); 51 1.2 pgoyette 52 1.2 pgoyette static int 53 1.2 pgoyette modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg) 54 1.2 pgoyette { 55 1.2 pgoyette omodstat_t *oms, *omso; 56 1.2 pgoyette modinfo_t *mi; 57 1.2 pgoyette module_t *mod; 58 1.2 pgoyette vaddr_t addr; 59 1.2 pgoyette size_t size; 60 1.2 pgoyette size_t omslen; 61 1.2 pgoyette size_t used; 62 1.2 pgoyette int error; 63 1.2 pgoyette int omscnt; 64 1.2 pgoyette bool stataddr; 65 1.2 pgoyette const char *suffix = "..."; 66 1.2 pgoyette 67 1.2 pgoyette if (cmd != MODCTL_OSTAT) 68 1.2 pgoyette return EINVAL; 69 1.2 pgoyette 70 1.2 pgoyette /* If not privileged, don't expose kernel addresses. */ 71 1.2 pgoyette error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, 72 1.2 pgoyette 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL); 73 1.2 pgoyette stataddr = (error == 0); 74 1.2 pgoyette 75 1.2 pgoyette kernconfig_lock(); 76 1.2 pgoyette omscnt = 0; 77 1.2 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) { 78 1.2 pgoyette omscnt++; 79 1.2 pgoyette mi = mod->mod_info; 80 1.2 pgoyette } 81 1.2 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) { 82 1.2 pgoyette omscnt++; 83 1.2 pgoyette mi = mod->mod_info; 84 1.2 pgoyette } 85 1.2 pgoyette omslen = omscnt * sizeof(omodstat_t); 86 1.2 pgoyette omso = kmem_zalloc(omslen, KM_SLEEP); 87 1.2 pgoyette oms = omso; 88 1.2 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) { 89 1.2 pgoyette mi = mod->mod_info; 90 1.2 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name)); 91 1.2 pgoyette if (mi->mi_required != NULL) { 92 1.2 pgoyette used = strlcpy(oms->oms_required, mi->mi_required, 93 1.2 pgoyette sizeof(oms->oms_required)); 94 1.2 pgoyette if (used >= sizeof(oms->oms_required)) { 95 1.2 pgoyette oms->oms_required[sizeof(oms->oms_required) - 96 1.2 pgoyette strlen(suffix) - 1] = '\0'; 97 1.2 pgoyette strlcat(oms->oms_required, suffix, 98 1.2 pgoyette sizeof(oms->oms_required)); 99 1.2 pgoyette } 100 1.2 pgoyette } 101 1.2 pgoyette if (mod->mod_kobj != NULL && stataddr) { 102 1.2 pgoyette kobj_stat(mod->mod_kobj, &addr, &size); 103 1.2 pgoyette oms->oms_addr = addr; 104 1.2 pgoyette oms->oms_size = size; 105 1.2 pgoyette } 106 1.2 pgoyette oms->oms_class = mi->mi_class; 107 1.2 pgoyette oms->oms_refcnt = mod->mod_refcnt; 108 1.2 pgoyette oms->oms_source = mod->mod_source; 109 1.2 pgoyette oms->oms_flags = mod->mod_flags; 110 1.2 pgoyette oms++; 111 1.2 pgoyette } 112 1.2 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) { 113 1.2 pgoyette mi = mod->mod_info; 114 1.2 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name)); 115 1.2 pgoyette if (mi->mi_required != NULL) { 116 1.2 pgoyette used = strlcpy(oms->oms_required, mi->mi_required, 117 1.2 pgoyette sizeof(oms->oms_required)); 118 1.2 pgoyette if (used >= sizeof(oms->oms_required)) { 119 1.2 pgoyette oms->oms_required[sizeof(oms->oms_required) - 120 1.2 pgoyette strlen(suffix) - 1] = '\0'; 121 1.2 pgoyette strlcat(oms->oms_required, suffix, 122 1.2 pgoyette sizeof(oms->oms_required)); 123 1.2 pgoyette } 124 1.2 pgoyette } 125 1.2 pgoyette if (mod->mod_kobj != NULL && stataddr) { 126 1.2 pgoyette kobj_stat(mod->mod_kobj, &addr, &size); 127 1.2 pgoyette oms->oms_addr = addr; 128 1.2 pgoyette oms->oms_size = size; 129 1.2 pgoyette } 130 1.2 pgoyette oms->oms_class = mi->mi_class; 131 1.2 pgoyette oms->oms_refcnt = -1; 132 1.2 pgoyette KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL); 133 1.2 pgoyette oms->oms_source = mod->mod_source; 134 1.2 pgoyette oms++; 135 1.2 pgoyette } 136 1.2 pgoyette kernconfig_unlock(); 137 1.2 pgoyette error = copyout(omso, NETBSD32PTR64(iov->iov_base), 138 1.2 pgoyette uimin(omslen - sizeof(modstat_t), iov->iov_len)); 139 1.2 pgoyette kmem_free(omso, omslen); 140 1.2 pgoyette if (error == 0) { 141 1.2 pgoyette iov->iov_len = omslen - sizeof(modstat_t); 142 1.2 pgoyette error = copyout(iov, arg, sizeof(*iov)); 143 1.2 pgoyette } 144 1.2 pgoyette 145 1.2 pgoyette return error; 146 1.2 pgoyette } 147 1.2 pgoyette 148 1.2 pgoyette int 149 1.2 pgoyette netbsd32_80_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap, 150 1.2 pgoyette register_t *result) 151 1.2 pgoyette { 152 1.2 pgoyette /* { 153 1.2 pgoyette syscallarg(int) cmd; 154 1.2 pgoyette syscallarg(netbsd32_voidp) arg; 155 1.2 pgoyette } */ 156 1.2 pgoyette struct netbsd32_iovec iov; 157 1.2 pgoyette int error; 158 1.2 pgoyette void *arg; 159 1.2 pgoyette 160 1.2 pgoyette arg = SCARG_P32(uap, arg); 161 1.2 pgoyette 162 1.2 pgoyette switch (SCARG(uap, cmd)) { 163 1.2 pgoyette case MODCTL_OSTAT: 164 1.2 pgoyette error = copyin(arg, &iov, sizeof(iov)); 165 1.2 pgoyette if (error != 0) { 166 1.2 pgoyette break; 167 1.2 pgoyette } 168 1.2 pgoyette error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg); 169 1.2 pgoyette break; 170 1.2 pgoyette default: 171 1.2 pgoyette error = EPASSTHROUGH; 172 1.2 pgoyette break; 173 1.2 pgoyette } 174 1.2 pgoyette 175 1.2 pgoyette return error; 176 1.2 pgoyette } 177 1.2 pgoyette 178 1.8 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_80, "compat_netbsd32_90,compat_80"); 179 1.2 pgoyette 180 1.2 pgoyette static int 181 1.2 pgoyette compat_netbsd32_80_modcmd(modcmd_t cmd, void *arg) 182 1.2 pgoyette { 183 1.2 pgoyette 184 1.2 pgoyette switch (cmd) { 185 1.2 pgoyette case MODULE_CMD_INIT: 186 1.6 pgoyette MODULE_HOOK_SET(compat32_80_modctl_hook, netbsd32_80_modctl); 187 1.2 pgoyette return 0; 188 1.2 pgoyette 189 1.2 pgoyette case MODULE_CMD_FINI: 190 1.5 pgoyette MODULE_HOOK_UNSET(compat32_80_modctl_hook); 191 1.2 pgoyette return 0; 192 1.2 pgoyette 193 1.2 pgoyette default: 194 1.2 pgoyette return ENOTTY; 195 1.2 pgoyette } 196 1.2 pgoyette } 197