open-dice.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 - Google LLC
  4. * Author: David Brazdil <[email protected]>
  5. *
  6. * Driver for Open Profile for DICE.
  7. *
  8. * This driver takes ownership of a reserved memory region containing data
  9. * generated by the Open Profile for DICE measured boot protocol. The memory
  10. * contents are not interpreted by the kernel but can be mapped into a userspace
  11. * process via a misc device. Userspace can also request a wipe of the memory.
  12. *
  13. * Userspace can access the data with (w/o error handling):
  14. *
  15. * fd = open("/dev/open-dice0", O_RDWR);
  16. * read(fd, &size, sizeof(unsigned long));
  17. * data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  18. * write(fd, NULL, 0); // wipe
  19. * close(fd);
  20. */
  21. #include <linux/io.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/of_reserved_mem.h>
  26. #include <linux/platform_device.h>
  27. #define DRIVER_NAME "open-dice"
  28. struct open_dice_drvdata {
  29. struct mutex lock;
  30. char name[16];
  31. struct reserved_mem *rmem;
  32. struct miscdevice misc;
  33. };
  34. static inline struct open_dice_drvdata *to_open_dice_drvdata(struct file *filp)
  35. {
  36. return container_of(filp->private_data, struct open_dice_drvdata, misc);
  37. }
  38. static int open_dice_wipe(struct open_dice_drvdata *drvdata)
  39. {
  40. void *kaddr;
  41. mutex_lock(&drvdata->lock);
  42. kaddr = devm_memremap(drvdata->misc.this_device, drvdata->rmem->base,
  43. drvdata->rmem->size, MEMREMAP_WC);
  44. if (IS_ERR(kaddr)) {
  45. mutex_unlock(&drvdata->lock);
  46. return PTR_ERR(kaddr);
  47. }
  48. memset(kaddr, 0, drvdata->rmem->size);
  49. devm_memunmap(drvdata->misc.this_device, kaddr);
  50. mutex_unlock(&drvdata->lock);
  51. return 0;
  52. }
  53. /*
  54. * Copies the size of the reserved memory region to the user-provided buffer.
  55. */
  56. static ssize_t open_dice_read(struct file *filp, char __user *ptr, size_t len,
  57. loff_t *off)
  58. {
  59. unsigned long val = to_open_dice_drvdata(filp)->rmem->size;
  60. return simple_read_from_buffer(ptr, len, off, &val, sizeof(val));
  61. }
  62. /*
  63. * Triggers a wipe of the reserved memory region. The user-provided pointer
  64. * is never dereferenced.
  65. */
  66. static ssize_t open_dice_write(struct file *filp, const char __user *ptr,
  67. size_t len, loff_t *off)
  68. {
  69. if (open_dice_wipe(to_open_dice_drvdata(filp)))
  70. return -EIO;
  71. /* Consume the input buffer. */
  72. return len;
  73. }
  74. /*
  75. * Creates a mapping of the reserved memory region in user address space.
  76. */
  77. static int open_dice_mmap(struct file *filp, struct vm_area_struct *vma)
  78. {
  79. struct open_dice_drvdata *drvdata = to_open_dice_drvdata(filp);
  80. /* Do not allow userspace to modify the underlying data. */
  81. if ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED))
  82. return -EPERM;
  83. /* Ensure userspace cannot acquire VM_WRITE + VM_SHARED later. */
  84. if (vma->vm_flags & VM_WRITE)
  85. vm_flags_clear(vma, VM_MAYSHARE);
  86. else if (vma->vm_flags & VM_SHARED)
  87. vm_flags_clear(vma, VM_MAYWRITE);
  88. /* Create write-combine mapping so all clients observe a wipe. */
  89. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  90. vm_flags_set(vma, VM_DONTCOPY | VM_DONTDUMP);
  91. return vm_iomap_memory(vma, drvdata->rmem->base, drvdata->rmem->size);
  92. }
  93. static const struct file_operations open_dice_fops = {
  94. .owner = THIS_MODULE,
  95. .read = open_dice_read,
  96. .write = open_dice_write,
  97. .mmap = open_dice_mmap,
  98. };
  99. static int __init open_dice_probe(struct platform_device *pdev)
  100. {
  101. static unsigned int dev_idx;
  102. struct device *dev = &pdev->dev;
  103. struct reserved_mem *rmem;
  104. struct open_dice_drvdata *drvdata;
  105. int ret;
  106. rmem = of_reserved_mem_lookup(dev->of_node);
  107. if (!rmem) {
  108. dev_err(dev, "failed to lookup reserved memory\n");
  109. return -EINVAL;
  110. }
  111. if (!rmem->size || (rmem->size > ULONG_MAX)) {
  112. dev_err(dev, "invalid memory region size\n");
  113. return -EINVAL;
  114. }
  115. if (!PAGE_ALIGNED(rmem->base) || !PAGE_ALIGNED(rmem->size)) {
  116. dev_err(dev, "memory region must be page-aligned\n");
  117. return -EINVAL;
  118. }
  119. drvdata = devm_kmalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  120. if (!drvdata)
  121. return -ENOMEM;
  122. *drvdata = (struct open_dice_drvdata){
  123. .lock = __MUTEX_INITIALIZER(drvdata->lock),
  124. .rmem = rmem,
  125. .misc = (struct miscdevice){
  126. .parent = dev,
  127. .name = drvdata->name,
  128. .minor = MISC_DYNAMIC_MINOR,
  129. .fops = &open_dice_fops,
  130. .mode = 0600,
  131. },
  132. };
  133. /* Index overflow check not needed, misc_register() will fail. */
  134. snprintf(drvdata->name, sizeof(drvdata->name), DRIVER_NAME"%u", dev_idx++);
  135. ret = misc_register(&drvdata->misc);
  136. if (ret) {
  137. dev_err(dev, "failed to register misc device '%s': %d\n",
  138. drvdata->name, ret);
  139. return ret;
  140. }
  141. platform_set_drvdata(pdev, drvdata);
  142. return 0;
  143. }
  144. static int open_dice_remove(struct platform_device *pdev)
  145. {
  146. struct open_dice_drvdata *drvdata = platform_get_drvdata(pdev);
  147. misc_deregister(&drvdata->misc);
  148. return 0;
  149. }
  150. static const struct of_device_id open_dice_of_match[] = {
  151. { .compatible = "google,open-dice" },
  152. {},
  153. };
  154. static struct platform_driver open_dice_driver = {
  155. .remove = open_dice_remove,
  156. .driver = {
  157. .name = DRIVER_NAME,
  158. .of_match_table = open_dice_of_match,
  159. },
  160. };
  161. static int __init open_dice_init(void)
  162. {
  163. int ret = platform_driver_probe(&open_dice_driver, open_dice_probe);
  164. /* DICE regions are optional. Succeed even with zero instances. */
  165. return (ret == -ENODEV) ? 0 : ret;
  166. }
  167. static void __exit open_dice_exit(void)
  168. {
  169. platform_driver_unregister(&open_dice_driver);
  170. }
  171. module_init(open_dice_init);
  172. module_exit(open_dice_exit);
  173. MODULE_LICENSE("GPL v2");
  174. MODULE_AUTHOR("David Brazdil <[email protected]>");