|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Component Measurement and Authentication (CMA-SPDM, PCIe r6.1 sec 6.31) |
| 4 | + * |
| 5 | + * Copyright (C) 2023 Intel Corporation |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/pci.h> |
| 9 | +#include <linux/sysfs.h> |
| 10 | + |
| 11 | +#include "pci.h" |
| 12 | + |
| 13 | +static ssize_t authenticated_store(struct device *dev, |
| 14 | + struct device_attribute *attr, |
| 15 | + const char *buf, size_t count) |
| 16 | +{ |
| 17 | + struct pci_dev *pdev = to_pci_dev(dev); |
| 18 | + ssize_t rc; |
| 19 | + |
| 20 | + if (!pdev->cma_capable && |
| 21 | + (pdev->cma_init_failed || pdev->doe_init_failed)) |
| 22 | + return -ENOTTY; |
| 23 | + |
| 24 | + rc = pci_cma_reauthenticate(pdev); |
| 25 | + if (rc) |
| 26 | + return rc; |
| 27 | + |
| 28 | + return count; |
| 29 | +} |
| 30 | + |
| 31 | +static ssize_t authenticated_show(struct device *dev, |
| 32 | + struct device_attribute *attr, char *buf) |
| 33 | +{ |
| 34 | + struct pci_dev *pdev = to_pci_dev(dev); |
| 35 | + |
| 36 | + if (!pdev->cma_capable && |
| 37 | + (pdev->cma_init_failed || pdev->doe_init_failed)) |
| 38 | + return -ENOTTY; |
| 39 | + |
| 40 | + return sysfs_emit(buf, "%u\n", test_bit(PCI_CMA_AUTHENTICATED, |
| 41 | + &pdev->priv_flags)); |
| 42 | +} |
| 43 | +static DEVICE_ATTR_RW(authenticated); |
| 44 | + |
| 45 | +static struct attribute *pci_cma_attrs[] = { |
| 46 | + &dev_attr_authenticated.attr, |
| 47 | + NULL |
| 48 | +}; |
| 49 | + |
| 50 | +static umode_t pci_cma_attrs_are_visible(struct kobject *kobj, |
| 51 | + struct attribute *a, int n) |
| 52 | +{ |
| 53 | + struct device *dev = kobj_to_dev(kobj); |
| 54 | + struct pci_dev *pdev = to_pci_dev(dev); |
| 55 | + |
| 56 | + /* |
| 57 | + * If CMA or DOE initialization failed, CMA attributes must be visible |
| 58 | + * and return an error on access. This prevents downgrade attacks |
| 59 | + * where an attacker disturbs memory allocation or DOE communication |
| 60 | + * in order to create the appearance that CMA is unsupported. |
| 61 | + * The attacker may achieve that by simply hogging memory. |
| 62 | + */ |
| 63 | + if (!pdev->cma_capable && |
| 64 | + !pdev->cma_init_failed && !pdev->doe_init_failed) |
| 65 | + return 0; |
| 66 | + |
| 67 | + return a->mode; |
| 68 | +} |
| 69 | + |
| 70 | +const struct attribute_group pci_cma_attr_group = { |
| 71 | + .attrs = pci_cma_attrs, |
| 72 | + .is_visible = pci_cma_attrs_are_visible, |
| 73 | +}; |
0 commit comments