gsgx_main.c 7.0 KB

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