open-dice.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 - Google LLC
  4. * Author: David Brazdil <dbrazdil@google.com>
  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. if (vma->vm_flags & VM_MAYSHARE) {
  81. /* Do not allow userspace to modify the underlying data. */
  82. if (vma->vm_flags & VM_WRITE)
  83. return -EPERM;
  84. /* Ensure userspace cannot acquire VM_WRITE later. */
  85. vm_flags_clear(vma, VM_MAYWRITE);
  86. }
  87. /* Create write-combine mapping so all clients observe a wipe. */
  88. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  89. vm_flags_set(vma, VM_DONTCOPY | VM_DONTDUMP);
  90. return vm_iomap_memory(vma, drvdata->rmem->base, drvdata->rmem->size);
  91. }
  92. static const struct file_operations open_dice_fops = {
  93. .owner = THIS_MODULE,
  94. .read = open_dice_read,
  95. .write = open_dice_write,
  96. .mmap = open_dice_mmap,
  97. };
  98. static int __init open_dice_probe(struct platform_device *pdev)
  99. {
  100. static unsigned int dev_idx;
  101. struct device *dev = &pdev->dev;
  102. struct reserved_mem *rmem;
  103. struct open_dice_drvdata *drvdata;
  104. int ret;
  105. rmem = of_reserved_mem_lookup(dev->of_node);
  106. if (!rmem) {
  107. dev_err(dev, "failed to lookup reserved memory\n");
  108. return -EINVAL;
  109. }
  110. if (!rmem->size || (rmem->size > ULONG_MAX)) {
  111. dev_err(dev, "invalid memory region size\n");
  112. return -EINVAL;
  113. }
  114. if (!PAGE_ALIGNED(rmem->base) || !PAGE_ALIGNED(rmem->size)) {
  115. dev_err(dev, "memory region must be page-aligned\n");
  116. return -EINVAL;
  117. }
  118. drvdata = devm_kmalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  119. if (!drvdata)
  120. return -ENOMEM;
  121. *drvdata = (struct open_dice_drvdata){
  122. .rmem = rmem,
  123. .misc = (struct miscdevice){
  124. .parent = dev,
  125. .name = drvdata->name,
  126. .minor = MISC_DYNAMIC_MINOR,
  127. .fops = &open_dice_fops,
  128. .mode = 0600,
  129. },
  130. };
  131. mutex_init(&drvdata->lock);
  132. /* Index overflow check not needed, misc_register() will fail. */
  133. snprintf(drvdata->name, sizeof(drvdata->name), DRIVER_NAME"%u", dev_idx++);
  134. ret = misc_register(&drvdata->misc);
  135. if (ret) {
  136. dev_err(dev, "failed to register misc device '%s': %d\n",
  137. drvdata->name, ret);
  138. return ret;
  139. }
  140. platform_set_drvdata(pdev, drvdata);
  141. return 0;
  142. }
  143. static void open_dice_remove(struct platform_device *pdev)
  144. {
  145. struct open_dice_drvdata *drvdata = platform_get_drvdata(pdev);
  146. misc_deregister(&drvdata->misc);
  147. }
  148. static const struct of_device_id open_dice_of_match[] = {
  149. { .compatible = "google,open-dice" },
  150. {},
  151. };
  152. static struct platform_driver open_dice_driver = {
  153. .remove_new = open_dice_remove,
  154. .driver = {
  155. .name = DRIVER_NAME,
  156. .of_match_table = open_dice_of_match,
  157. },
  158. };
  159. static int __init open_dice_init(void)
  160. {
  161. int ret = platform_driver_probe(&open_dice_driver, open_dice_probe);
  162. /* DICE regions are optional. Succeed even with zero instances. */
  163. return (ret == -ENODEV) ? 0 : ret;
  164. }
  165. static void __exit open_dice_exit(void)
  166. {
  167. platform_driver_unregister(&open_dice_driver);
  168. }
  169. module_init(open_dice_init);
  170. module_exit(open_dice_exit);
  171. MODULE_DESCRIPTION("Driver for Open Profile for DICE.");
  172. MODULE_LICENSE("GPL v2");
  173. MODULE_AUTHOR("David Brazdil <dbrazdil@google.com>");