1 /*- 2 * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by BAE Systems, the University of Cambridge 6 * Computer Laboratory, and Memorial University under DARPA/AFRL contract 7 * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing 8 * (TC) research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/capsicum.h> 35 #include <sys/conf.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/file.h> 39 #include <sys/proc.h> 40 41 #include <machine/sgx.h> 42 #include <machine/../linux/linux.h> 43 #include <machine/../linux/linux_proto.h> 44 #include <compat/linux/linux_ioctl.h> 45 46 #include <amd64/sgx/sgxvar.h> 47 48 #include <sys/ioccom.h> 49 50 #define SGX_LINUX_IOCTL_MIN (SGX_IOC_ENCLAVE_CREATE & 0xffff) 51 #define SGX_LINUX_IOCTL_MAX (SGX_IOC_ENCLAVE_INIT & 0xffff) 52 53 static int 54 sgx_linux_ioctl(struct thread *td, struct linux_ioctl_args *args) 55 { 56 uint8_t data[SGX_IOCTL_MAX_DATA_LEN]; 57 cap_rights_t rights; 58 struct file *fp; 59 u_long cmd; 60 int error; 61 int len; 62 63 error = fget(td, args->fd, cap_rights_init_one(&rights, CAP_IOCTL), 64 &fp); 65 if (error != 0) 66 return (error); 67 68 cmd = args->cmd; 69 70 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT); 71 if ((cmd & LINUX_IOC_IN) != 0) 72 args->cmd |= IOC_IN; 73 if ((cmd & LINUX_IOC_OUT) != 0) 74 args->cmd |= IOC_OUT; 75 76 len = IOCPARM_LEN(cmd); 77 if (len > SGX_IOCTL_MAX_DATA_LEN) { 78 error = EINVAL; 79 goto out; 80 } 81 82 if ((cmd & LINUX_IOC_IN) != 0) { 83 error = copyin((void *)args->arg, data, len); 84 if (error != 0) 85 goto out; 86 } 87 88 error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td); 89 out: 90 fdrop(fp, td); 91 return (error); 92 } 93 94 static struct linux_ioctl_handler sgx_linux_handler = { 95 sgx_linux_ioctl, 96 SGX_LINUX_IOCTL_MIN, 97 SGX_LINUX_IOCTL_MAX, 98 }; 99 100 SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE, 101 linux_ioctl_register_handler, &sgx_linux_handler); 102 SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE, 103 linux_ioctl_unregister_handler, &sgx_linux_handler); 104 105 static int 106 sgx_linux_modevent(module_t mod, int type, void *data) 107 { 108 109 return (0); 110 } 111 112 DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL); 113 MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1); 114