blob: 9bbed7abc153855947ca625702862ff69f848f8d [file]
// SPDX-License-Identifier: GPL-2.0
/*
* DMTF Security Protocol and Data Model (SPDM)
* https://www.dmtf.org/dsp/DSP0274
*
* Requester role: sysfs interface
*
* Copyright (C) 2023-24 Intel Corporation
*/
#include "spdm.h"
#include <linux/pci.h>
/**
* dev_to_spdm_state() - Retrieve SPDM session state for given device
*
* @dev: Responder device
*
* Returns a pointer to the device's SPDM session state,
* %NULL if the device doesn't have one or
* %ERR_PTR if it couldn't be determined whether SPDM is supported.
*
* In the %ERR_PTR case, attributes are visible but return an error on access.
* This prevents downgrade attacks where an attacker disturbs memory allocation
* or communication with the device in order to create the appearance that SPDM
* is unsupported. E.g. with PCI devices, the attacker may foil CMA or DOE
* initialization by simply hogging memory.
*/
static struct spdm_state *dev_to_spdm_state(struct device *dev)
{
if (dev_is_pci(dev))
return pci_dev_to_spdm_state(to_pci_dev(dev));
/* Insert mappers for further bus types here. */
return NULL;
}
/* authenticated attribute */
static umode_t spdm_attrs_are_visible(struct kobject *kobj,
struct attribute *a, int n)
{
struct device *dev = kobj_to_dev(kobj);
struct spdm_state *spdm_state = dev_to_spdm_state(dev);
if (!spdm_state)
return SYSFS_GROUP_INVISIBLE;
return a->mode;
}
static ssize_t authenticated_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct spdm_state *spdm_state = dev_to_spdm_state(dev);
int rc;
if (IS_ERR(spdm_state))
return PTR_ERR(spdm_state);
if (sysfs_streq(buf, "re")) {
rc = spdm_authenticate(spdm_state);
if (rc)
return rc;
} else {
return -EINVAL;
}
return count;
}
static ssize_t authenticated_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct spdm_state *spdm_state = dev_to_spdm_state(dev);
if (IS_ERR(spdm_state))
return PTR_ERR(spdm_state);
return sysfs_emit(buf, "%u\n", spdm_state->authenticated);
}
static DEVICE_ATTR_RW(authenticated);
static struct attribute *spdm_attrs[] = {
&dev_attr_authenticated.attr,
NULL
};
const struct attribute_group spdm_attr_group = {
.attrs = spdm_attrs,
.is_visible = spdm_attrs_are_visible,
};