gsgx_main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * (C) Copyright 2013 Intel Corporation
  3. * Author: Jarkko Sakkinen <jarkko.sakkinen@intel.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/version.h>
  13. #include <linux/highmem.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/module.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/security.h>
  18. #include <asm/tlbflush.h>
  19. #include "gsgx.h"
  20. #define DRV_DESCRIPTION "Graphene SGX Driver"
  21. #define DRV_VERSION "0.10"
  22. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  23. MODULE_AUTHOR("Chia-Che Tsai <chia-che.tsai@intel.com>");
  24. MODULE_VERSION(DRV_VERSION);
  25. IMPORT_KSYM(dac_mmap_min_addr);
  26. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  27. static void __enable_fsgsbase(void *v)
  28. {
  29. write_cr4(read_cr4() | X86_CR4_FSGSBASE);
  30. }
  31. #endif
  32. static long gsgx_ioctl_enclave_create(struct file *filep, unsigned int cmd,
  33. unsigned long arg)
  34. {
  35. struct gsgx_enclave_create *createp = (struct gsgx_enclave_create *) arg;
  36. struct sgx_enclave_create isgx_create;
  37. unsigned long old_mmap_min_addr = *KSYM(dac_mmap_min_addr);
  38. int ret;
  39. if (createp->src != GSGX_ENCLAVE_CREATE_NO_ADDR &&
  40. createp->src < old_mmap_min_addr) {
  41. *KSYM(dac_mmap_min_addr) = createp->src;
  42. old_mmap_min_addr = 0;
  43. }
  44. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  45. __enable_fsgsbase(NULL);
  46. smp_call_function(__enable_fsgsbase, NULL, 1);
  47. #endif
  48. isgx_create.src = createp->src;
  49. filep->private_data = (void *) createp->src;
  50. ret = KSYM(isgx_ioctl_enclave_create)(filep, SGX_IOC_ENCLAVE_CREATE,
  51. (unsigned long) &isgx_create);
  52. if (old_mmap_min_addr)
  53. *KSYM(dac_mmap_min_addr) = old_mmap_min_addr;
  54. return ret;
  55. }
  56. static long gsgx_ioctl_enclave_add_pages(struct file *filep, unsigned int cmd,
  57. unsigned long arg)
  58. {
  59. struct gsgx_enclave_add_pages *addp = (struct gsgx_enclave_add_pages *) arg;
  60. struct sgx_enclave_add_page isgx_add;
  61. uint64_t off;
  62. int ret = 0;
  63. if (!addp->addr || (addp->addr & (PAGE_SIZE - 1)))
  64. return -EINVAL;
  65. if (!addp->size || (addp->size & (PAGE_SIZE - 1)))
  66. return -EINVAL;
  67. if (!addp->secinfo)
  68. return -EINVAL;
  69. isgx_add.secinfo = addp->secinfo;
  70. for (off = 0 ; off < addp->size ; off += PAGE_SIZE) {
  71. isgx_add.addr = addp->addr + off;
  72. isgx_add.src =
  73. addp->flags & GSGX_ENCLAVE_ADD_PAGES_REPEAT_SRC ?
  74. addp->user_addr : addp->user_addr + off;
  75. isgx_add.mrmask =
  76. addp->flags & GSGX_ENCLAVE_ADD_PAGES_SKIP_EEXTEND ?
  77. 0 : ~0;
  78. ret = KSYM(isgx_ioctl_enclave_add_page)(filep,
  79. SGX_IOC_ENCLAVE_ADD_PAGE, (unsigned long) &isgx_add);
  80. if (ret < 0)
  81. break;
  82. }
  83. return ret;
  84. }
  85. static long gsgx_ioctl_enclave_init(struct file *filep, unsigned int cmd,
  86. unsigned long arg)
  87. {
  88. struct gsgx_enclave_init *initp = (struct gsgx_enclave_init *) arg;
  89. struct sgx_enclave_init isgx_init;
  90. isgx_init.addr = initp->addr;
  91. isgx_init.sigstruct = initp->sigstruct;
  92. isgx_init.einittoken = initp->einittoken;
  93. return KSYM(isgx_ioctl_enclave_init)(filep, SGX_IOC_ENCLAVE_INIT,
  94. (unsigned long) &isgx_init);
  95. }
  96. typedef long (*ioctl_t)(struct file *filep, unsigned int cmd, unsigned long arg);
  97. long gsgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  98. {
  99. char data[256];
  100. ioctl_t handler = NULL;
  101. long ret;
  102. switch (cmd) {
  103. case GSGX_IOCTL_ENCLAVE_CREATE:
  104. handler = gsgx_ioctl_enclave_create;
  105. break;
  106. case GSGX_IOCTL_ENCLAVE_ADD_PAGES:
  107. handler = gsgx_ioctl_enclave_add_pages;
  108. break;
  109. case GSGX_IOCTL_ENCLAVE_INIT:
  110. handler = gsgx_ioctl_enclave_init;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. if (copy_from_user(data, (void __user *) arg, _IOC_SIZE(cmd)))
  116. return -EFAULT;
  117. ret = handler(filep, cmd, (unsigned long) ((void *) data));
  118. if (!ret && (cmd & IOC_OUT)) {
  119. if (copy_to_user((void __user *) arg, data, _IOC_SIZE(cmd)))
  120. return -EFAULT;
  121. }
  122. return ret;
  123. }
  124. static int gsgx_mmap(struct file *file, struct vm_area_struct *vma)
  125. {
  126. return KSYM(isgx_mmap)(file, vma);
  127. }
  128. static unsigned long gsgx_get_unmapped_area(struct file *file,
  129. unsigned long addr,
  130. unsigned long len,
  131. unsigned long pgoff,
  132. unsigned long flags)
  133. {
  134. if (file->private_data == (void *) GSGX_ENCLAVE_CREATE_NO_ADDR) {
  135. unsigned long unmapped_addr =
  136. KSYM(isgx_get_unmapped_area)(file, addr, len,
  137. pgoff, flags);
  138. file->private_data = (void *) unmapped_addr;
  139. return unmapped_addr;
  140. } else {
  141. unsigned long unmapped_addr = (unsigned long) file->private_data;
  142. struct mm_struct *mm = current->mm;
  143. struct vm_area_struct *vma = find_vma(mm, unmapped_addr);
  144. if (vma && vma->vm_start <= len)
  145. return -EINVAL;
  146. return unmapped_addr;
  147. }
  148. }
  149. static const struct file_operations gsgx_fops = {
  150. .owner = THIS_MODULE,
  151. .unlocked_ioctl = gsgx_ioctl,
  152. #ifdef CONFIG_COMPAT
  153. .compat_ioctl = gsgx_ioctl,
  154. #endif
  155. .mmap = gsgx_mmap,
  156. .get_unmapped_area = gsgx_get_unmapped_area,
  157. };
  158. static struct miscdevice gsgx_dev = {
  159. .minor = GSGX_MINOR,
  160. .name = "gsgx",
  161. .fops = &gsgx_fops,
  162. .mode = S_IRUGO | S_IWUGO,
  163. };
  164. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_create, long,
  165. struct file *filep, unsigned int cmd, unsigned long arg);
  166. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_init, long,
  167. struct file *filep, unsigned int cmd, unsigned long arg);
  168. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_add_page, long,
  169. struct file *filep, unsigned int cmd, unsigned long arg);
  170. IMPORT_KSYM(isgx_enclave_release);
  171. IMPORT_KSYM_PROTO(isgx_mmap, int, struct file *, struct vm_area_struct *);
  172. IMPORT_KSYM_PROTO(isgx_get_unmapped_area, unsigned long,
  173. struct file *, unsigned long, unsigned long,
  174. unsigned long, unsigned long);
  175. static int gsgx_lookup_ksyms(void)
  176. {
  177. int ret;
  178. if ((ret = LOOKUP_KSYM(dac_mmap_min_addr)))
  179. return ret;
  180. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_create)))
  181. return ret;
  182. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_init)))
  183. return ret;
  184. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_add_page)))
  185. return ret;
  186. if ((ret = LOOKUP_KSYM(isgx_enclave_release)))
  187. return ret;
  188. if ((ret = LOOKUP_KSYM(isgx_mmap)))
  189. return ret;
  190. if ((ret = LOOKUP_KSYM(isgx_get_unmapped_area)))
  191. return ret;
  192. return 0;
  193. }
  194. struct file *isgx_dev;
  195. static int gsgx_setup(void)
  196. {
  197. unsigned cpu;
  198. int ret;
  199. isgx_dev = filp_open("/dev/isgx", O_RDONLY, 0);
  200. if (!isgx_dev) {
  201. return PTR_ERR(isgx_dev);
  202. }
  203. ret = misc_register(&gsgx_dev);
  204. if (ret) {
  205. pr_err("gsgx: misc_register() failed\n");
  206. gsgx_dev.this_device = NULL;
  207. return ret;
  208. }
  209. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
  210. for_each_online_cpu(cpu) {
  211. per_cpu(cpu_tlbstate.cr4, cpu) |= X86_CR4_FSGSBASE;
  212. }
  213. #endif
  214. return 0;
  215. }
  216. static void gsgx_teardown(void)
  217. {
  218. if (gsgx_dev.this_device)
  219. misc_deregister(&gsgx_dev);
  220. if (isgx_dev)
  221. filp_close(isgx_dev, NULL);
  222. }
  223. static int __init gsgx_init(void)
  224. {
  225. int ret;
  226. pr_info("gsgx: " DRV_DESCRIPTION " v" DRV_VERSION "\n");
  227. ret = gsgx_lookup_ksyms();
  228. if (ret) {
  229. pr_err("Likely module \"isgx\" is not loaded\n");
  230. return ret;
  231. }
  232. ret = gsgx_setup();
  233. if (ret) {
  234. pr_err("Likely module \"isgx\" is not loaded\n");
  235. gsgx_teardown();
  236. return ret;
  237. }
  238. return 0;
  239. }
  240. static void __exit gsgx_exit(void)
  241. {
  242. gsgx_teardown();
  243. }
  244. module_init(gsgx_init);
  245. module_exit(gsgx_exit);
  246. MODULE_LICENSE("GPL");