gsgx_main.c 7.7 KB

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