From nobody Tue Jun 30 02:38:07 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA120C433FE for ; Wed, 26 Jan 2022 23:12:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233170AbiAZXM4 (ORCPT ); Wed, 26 Jan 2022 18:12:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233121AbiAZXMz (ORCPT ); Wed, 26 Jan 2022 18:12:55 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8472BC061748 for ; Wed, 26 Jan 2022 15:12:54 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id f7-20020a1cc907000000b0034b63f314ccso604425wmb.6 for ; Wed, 26 Jan 2022 15:12:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sw/3yITQaa2AI87O7smfBOiWG12oWUzPV7X0KBcd7nk=; b=BUsie+AXwt7AWpY9TnHNZeIiyXHyQbqfkwIHzGrhFTBME5i3WqC4AhEpN0QrKhePz8 oN5+d4TWoIM3pQGBs/WOxhmKn2QTVg9Bbv/jydyhatTZjl4+MGJXuSgg/pNKVkmV68WZ 5ykuX/mWFbFHFihLfpWCXDJzP9MydnZ6miL5YZlDPdS+/72FCFW8vTe20igyekIvcPv1 AdGW2iETcQk0g6BF9vzbW9fQFNI+Bxg4c7ct+vUJFN0ekMZpuQTWdS8xcNuLaLCF6351 Ut1CGYowO16eiz/1jpVIQar4AhFStMKjG/FBSbAsA0ClSmFlH0i7HBmTjuxJxhcKHnjC qx4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=sw/3yITQaa2AI87O7smfBOiWG12oWUzPV7X0KBcd7nk=; b=uH1/g5WVnBBPBGBd7xnovSY55c0VTu+xHXl4RjdVxNRjok5bpuzYLbLMtbmG6+I0Dh IO7dvVal2wlKIc2ZY2bhzrXoga3H1PJr0fjNPTYT/o+jYXk2xLztlPkvSJ1k4PMiCzWk OyoaWtH4iCx8wI54jR9u53m4Sl1lbOcpFiVMzrKFO6dGeaVw/UA9UI5yQO7ng19yeTID 1TQjQTYTJSHX7YgYdPuH7K1rae4GRPrjOkJBFGwXuXBzMBVrg4/G7sAIotyxY20kGFty CBdraUOb1CWWoTnYawfatNhDYAj4ete7Haj5g5cKmfdE4zejGorrnG5zJ5nD3DkHv9fd doBg== X-Gm-Message-State: AOAM531HM1b3rY54FamZPCCHDx9leSjJykxHoJozfb2Wi+aMnTybKB1e yX58KofcfE15A6w9DRmgZnTn43rdQh0Fag== X-Google-Smtp-Source: ABdhPJyBPaSI9+WP8jN2fbeMWC9uetOno56Gd95R4SghhzY2jtECOKhrp5yPSmXzxXBe+UYvC6hdSCo4XutxAg== X-Received: from dbrazdil.lon.corp.google.com ([2a00:79e0:d:209:bd2e:4d3b:b065:fa46]) (user=dbrazdil job=sendgmr) by 2002:a1c:a5d2:: with SMTP id o201mr809207wme.82.1643238773138; Wed, 26 Jan 2022 15:12:53 -0800 (PST) Date: Wed, 26 Jan 2022 23:12:36 +0000 In-Reply-To: <20220126231237.529308-1-dbrazdil@google.com> Message-Id: <20220126231237.529308-2-dbrazdil@google.com> Mime-Version: 1.0 References: <20220126231237.529308-1-dbrazdil@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 1/2] dt-bindings: reserved-memory: Open Profile for DICE From: David Brazdil To: Greg Kroah-Hartman Cc: Rob Herring , Arnd Bergmann , Frank Rowand , David Brazdil , Will Deacon , Andrew Scull , Wedson Almeida Filho , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add DeviceTree bindings for Open Profile for DICE, an open protocol for measured boot. Firmware uses DICE to measure the hardware/software combination and generates Compound Device Identifier (CDI) certificates. These are stored in memory and the buffer is described in the DT as a reserved memory region compatible with 'google,open-dice'. 'no-map' is required to ensure the memory region is never treated by the kernel as system memory. Signed-off-by: David Brazdil --- .../reserved-memory/google,open-dice.yaml | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Documentation/devicetree/bindings/reserved-memory/googl= e,open-dice.yaml diff --git a/Documentation/devicetree/bindings/reserved-memory/google,open-= dice.yaml b/Documentation/devicetree/bindings/reserved-memory/google,open-d= ice.yaml new file mode 100644 index 000000000000..257a0b51994a --- /dev/null +++ b/Documentation/devicetree/bindings/reserved-memory/google,open-dice.ya= ml @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/reserved-memory/google,open-dice.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Open Profile for DICE Device Tree Bindings + +description: | + This binding represents a reserved memory region containing data + generated by the Open Profile for DICE protocol. + + See https://pigweed.googlesource.com/open-dice/ + +maintainers: + - David Brazdil + +allOf: + - $ref: "reserved-memory.yaml" + +properties: + compatible: + const: google,open-dice + + reg: + description: page-aligned region of memory containing DICE data + +required: + - compatible + - reg + - no-map + +unevaluatedProperties: false + +examples: + - | + reserved-memory { + #address-cells =3D <2>; + #size-cells =3D <1>; + + dice: dice@12340000 { + compatible =3D "google,open-dice"; + reg =3D <0x00 0x12340000 0x2000>; + no-map; + }; + }; --=20 2.35.0.rc0.227.g00780c9af4-goog From nobody Tue Jun 30 02:38:07 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BE1FC433F5 for ; Wed, 26 Jan 2022 23:13:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233573AbiAZXNO (ORCPT ); Wed, 26 Jan 2022 18:13:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233433AbiAZXM6 (ORCPT ); Wed, 26 Jan 2022 18:12:58 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81E56C061748 for ; Wed, 26 Jan 2022 15:12:57 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id f7-20020a1cc907000000b0034b63f314ccso604469wmb.6 for ; Wed, 26 Jan 2022 15:12:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jz5nT6IxUun/IIsla62xxjEmgJ8/cgjOJmGN7YMpXI0=; b=nvHjSZ4oFlP/ViObmLn7mhwyUQVPTnkH/BBHa5zW2QQ1t97/kP1AZW2Mgq/2lKRVSw BjnYPtR3X7NmIDE7HHavhQBit6bYkOhk4sR3PlW5EJHmedp0vfnNTiW6/PbPbQ/kJOEz ph63f1rJLpsscpyf8s2db0vRfplPdlbVt4u/iVtKMgIVjWyka7tyikOGzx9s8D+IQpbd NuPx+XjvTAsmaXNZ5mjby4fDmhcnfaiT+2c7lkC96gC/MXP+8DhJB0201gkdYWsyUU3X HLIBk2e+WOH0tT/LVUuIfKEogaJ2q1/H7K2KAuFQcxg1GlNDIRXQDKsfnMvBY+BLEOVP /ydA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jz5nT6IxUun/IIsla62xxjEmgJ8/cgjOJmGN7YMpXI0=; b=KRXckK7QIwQNlcorzCvraFY4ivrXyg4SNbaGZbCl8kONorxG3FI6OO/hxDBWAZ2sdZ eNrQPy4eOxgyu2jFfcg9byIV508lASc1h9BPW+PNizi/Gisjqi7R7AS9BGuFIzjh1Fyp D/j4z0PBWjNor+cJO6m7gJCwEM8VM8696FFik23uykcTKkfmXSvxuIiX2gOINbLpuCK7 gvuV5cmso4srKCIGXU8T8gHj3uei9LZ8AUBWekOTEYTdbr1ZFiFPKpuLzjbSvvQBpGuW 0pb6SDgZ2JBhnb4jgTHJ5/G2igQhNsUCfo7TRajqfN8gvb9YbsLW7Br24LHWFBUxAayp e1qw== X-Gm-Message-State: AOAM532Y14wXlmtUwMJrfMCfR75pVJoZ9G6kZwBZ+EZFo+zvQf9HOeMj Vw4rDJxRyt5qNspXzveiPJcTfJzKLOJGPA== X-Google-Smtp-Source: ABdhPJzsM21LS3Q7uRnccC+nmYJODL2vfxJW9tZS+muJ2O6O0w/UPo9iNAe6UU3VDPprEgTavXFuP+KoazV2IQ== X-Received: from dbrazdil.lon.corp.google.com ([2a00:79e0:d:209:bd2e:4d3b:b065:fa46]) (user=dbrazdil job=sendgmr) by 2002:a7b:c145:: with SMTP id z5mr941296wmi.0.1643238775707; Wed, 26 Jan 2022 15:12:55 -0800 (PST) Date: Wed, 26 Jan 2022 23:12:37 +0000 In-Reply-To: <20220126231237.529308-1-dbrazdil@google.com> Message-Id: <20220126231237.529308-3-dbrazdil@google.com> Mime-Version: 1.0 References: <20220126231237.529308-1-dbrazdil@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v7 2/2] misc: open-dice: Add driver to expose DICE data to userspace From: David Brazdil To: Greg Kroah-Hartman Cc: Rob Herring , Arnd Bergmann , Frank Rowand , David Brazdil , Will Deacon , Andrew Scull , Wedson Almeida Filho , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Rob Herring Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Open Profile for DICE is an open protocol for measured boot compatible with the Trusted Computing Group's Device Identifier Composition Engine (DICE) specification. The generated Compound Device Identifier (CDI) certificates represent the hardware/software combination measured by DICE, and can be used for remote attestation and sealing. Add a driver that exposes reserved memory regions populated by firmware with DICE CDIs and exposes them to userspace via a character device. Userspace obtains the memory region's size from read() and calls mmap() to create a mapping of the memory region in its address space. The mapping is not allowed to be write+shared, giving userspace a guarantee that the data were not overwritten by another process. Userspace can also call write(), which triggers a wipe of the DICE data by the driver. Because both the kernel and userspace mappings use write-combine semantics, all clients observe the memory as zeroed after the syscall has returned. Acked-by: Rob Herring Cc: Andrew Scull Cc: Will Deacon Signed-off-by: David Brazdil --- drivers/misc/Kconfig | 12 +++ drivers/misc/Makefile | 1 + drivers/misc/open-dice.c | 208 +++++++++++++++++++++++++++++++++++++++ drivers/of/platform.c | 1 + 4 files changed, 222 insertions(+) create mode 100644 drivers/misc/open-dice.c diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 0f5a49fc7c9e..a2b26426efba 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -470,6 +470,18 @@ config HISI_HIKEY_USB switching between the dual-role USB-C port and the USB-A host ports using only one USB controller. =20 +config OPEN_DICE + tristate "Open Profile for DICE driver" + depends on OF_RESERVED_MEM + help + This driver exposes a DICE reserved memory region to userspace via + a character device. The memory region contains Compound Device + Identifiers (CDIs) generated by firmware as an output of DICE + measured boot flow. Userspace can use CDIs for remote attestation + and sealing. + + If unsure, say N. + source "drivers/misc/c2port/Kconfig" source "drivers/misc/eeprom/Kconfig" source "drivers/misc/cb710/Kconfig" diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index a086197af544..70e800e9127f 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -59,3 +59,4 @@ obj-$(CONFIG_UACCE) +=3D uacce/ obj-$(CONFIG_XILINX_SDFEC) +=3D xilinx_sdfec.o obj-$(CONFIG_HISI_HIKEY_USB) +=3D hisi_hikey_usb.o obj-$(CONFIG_HI6421V600_IRQ) +=3D hi6421v600-irq.o +obj-$(CONFIG_OPEN_DICE) +=3D open-dice.o diff --git a/drivers/misc/open-dice.c b/drivers/misc/open-dice.c new file mode 100644 index 000000000000..c61be3404c6f --- /dev/null +++ b/drivers/misc/open-dice.c @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2021 - Google LLC + * Author: David Brazdil + * + * Driver for Open Profile for DICE. + * + * This driver takes ownership of a reserved memory region containing data + * generated by the Open Profile for DICE measured boot protocol. The memo= ry + * contents are not interpreted by the kernel but can be mapped into a use= rspace + * process via a misc device. Userspace can also request a wipe of the mem= ory. + * + * Userspace can access the data with (w/o error handling): + * + * fd =3D open("/dev/open-dice0", O_RDWR); + * read(fd, &size, sizeof(unsigned long)); + * data =3D mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); + * write(fd, NULL, 0); // wipe + * close(fd); + */ + +#include +#include +#include +#include +#include +#include + +#define DRIVER_NAME "open-dice" + +struct open_dice_drvdata { + struct mutex lock; + char name[16]; + struct reserved_mem *rmem; + struct miscdevice misc; +}; + +static inline struct open_dice_drvdata *to_open_dice_drvdata(struct file *= filp) +{ + return container_of(filp->private_data, struct open_dice_drvdata, misc); +} + +static int open_dice_wipe(struct open_dice_drvdata *drvdata) +{ + void *kaddr; + + mutex_lock(&drvdata->lock); + kaddr =3D devm_memremap(drvdata->misc.this_device, drvdata->rmem->base, + drvdata->rmem->size, MEMREMAP_WC); + if (IS_ERR(kaddr)) { + mutex_unlock(&drvdata->lock); + return PTR_ERR(kaddr); + } + + memset(kaddr, 0, drvdata->rmem->size); + devm_memunmap(drvdata->misc.this_device, kaddr); + mutex_unlock(&drvdata->lock); + return 0; +} + +/* + * Copies the size of the reserved memory region to the user-provided buff= er. + */ +static ssize_t open_dice_read(struct file *filp, char __user *ptr, size_t = len, + loff_t *off) +{ + unsigned long val =3D to_open_dice_drvdata(filp)->rmem->size; + + return simple_read_from_buffer(ptr, len, off, &val, sizeof(val)); +} + +/* + * Triggers a wipe of the reserved memory region. The user-provided pointer + * is never dereferenced. + */ +static ssize_t open_dice_write(struct file *filp, const char __user *ptr, + size_t len, loff_t *off) +{ + if (open_dice_wipe(to_open_dice_drvdata(filp))) + return -EIO; + + /* Consume the input buffer. */ + return len; +} + +/* + * Creates a mapping of the reserved memory region in user address space. + */ +static int open_dice_mmap(struct file *filp, struct vm_area_struct *vma) +{ + struct open_dice_drvdata *drvdata =3D to_open_dice_drvdata(filp); + + /* Do not allow userspace to modify the underlying data. */ + if ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED)) + return -EPERM; + + /* Ensure userspace cannot acquire VM_WRITE + VM_SHARED later. */ + if (vma->vm_flags & VM_WRITE) + vma->vm_flags &=3D ~VM_MAYSHARE; + else if (vma->vm_flags & VM_SHARED) + vma->vm_flags &=3D ~VM_MAYWRITE; + + /* Create write-combine mapping so all clients observe a wipe. */ + vma->vm_page_prot =3D pgprot_writecombine(vma->vm_page_prot); + vma->vm_flags |=3D VM_DONTCOPY | VM_DONTDUMP; + return vm_iomap_memory(vma, drvdata->rmem->base, drvdata->rmem->size); +} + +static const struct file_operations open_dice_fops =3D { + .owner =3D THIS_MODULE, + .read =3D open_dice_read, + .write =3D open_dice_write, + .mmap =3D open_dice_mmap, +}; + +static int __init open_dice_probe(struct platform_device *pdev) +{ + static unsigned int dev_idx; + struct device *dev =3D &pdev->dev; + struct reserved_mem *rmem; + struct open_dice_drvdata *drvdata; + int ret; + + rmem =3D of_reserved_mem_lookup(dev->of_node); + if (!rmem) { + dev_err(dev, "failed to lookup reserved memory\n"); + return -EINVAL; + } + + if (!rmem->size || (rmem->size > ULONG_MAX)) { + dev_err(dev, "invalid memory region size\n"); + return -EINVAL; + } + + if (!PAGE_ALIGNED(rmem->base) || !PAGE_ALIGNED(rmem->size)) { + dev_err(dev, "memory region must be page-aligned\n"); + return -EINVAL; + } + + drvdata =3D devm_kmalloc(dev, sizeof(*drvdata), GFP_KERNEL); + if (!drvdata) + return -ENOMEM; + + *drvdata =3D (struct open_dice_drvdata){ + .lock =3D __MUTEX_INITIALIZER(drvdata->lock), + .rmem =3D rmem, + .misc =3D (struct miscdevice){ + .parent =3D dev, + .name =3D drvdata->name, + .minor =3D MISC_DYNAMIC_MINOR, + .fops =3D &open_dice_fops, + .mode =3D 0600, + }, + }; + + /* Index overflow check not needed, misc_register() will fail. */ + snprintf(drvdata->name, sizeof(drvdata->name), DRIVER_NAME"%u", dev_idx++= ); + + ret =3D misc_register(&drvdata->misc); + if (ret) { + dev_err(dev, "failed to register misc device '%s': %d\n", + drvdata->name, ret); + return ret; + } + + platform_set_drvdata(pdev, drvdata); + return 0; +} + +static int open_dice_remove(struct platform_device *pdev) +{ + struct open_dice_drvdata *drvdata =3D platform_get_drvdata(pdev); + + misc_deregister(&drvdata->misc); + return 0; +} + +static const struct of_device_id open_dice_of_match[] =3D { + { .compatible =3D "google,open-dice" }, + {}, +}; + +static struct platform_driver open_dice_driver =3D { + .remove =3D open_dice_remove, + .driver =3D { + .name =3D DRIVER_NAME, + .of_match_table =3D open_dice_of_match, + }, +}; + +static int __init open_dice_init(void) +{ + int ret =3D platform_driver_probe(&open_dice_driver, open_dice_probe); + + /* DICE regions are optional. Succeed even with zero instances. */ + return (ret =3D=3D -ENODEV) ? 0 : ret; +} + +static void __exit open_dice_exit(void) +{ + platform_driver_unregister(&open_dice_driver); +} + +module_init(open_dice_init); +module_exit(open_dice_exit); + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("David Brazdil "); diff --git a/drivers/of/platform.c b/drivers/of/platform.c index 793350028906..a16b74f32aa9 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -514,6 +514,7 @@ static const struct of_device_id reserved_mem_matches[]= =3D { { .compatible =3D "qcom,smem" }, { .compatible =3D "ramoops" }, { .compatible =3D "nvmem-rmem" }, + { .compatible =3D "google,open-dice" }, {} }; =20 --=20 2.35.0.rc0.227.g00780c9af4-goog