@@ -3,7 +3,7 @@
obj-y := component.o core.o bus.o dd.o syscore.o \
driver.o class.o platform.o \
- cpu.o firmware.o init.o map.o devres.o \
+ cpu.o firmware.o init.o map.o devres.o trust.o \
attribute_container.o transport_class.o \
topology.o container.o property.o cacheinfo.o \
swnode.o faux.o
@@ -10,6 +10,7 @@
* shared outside of the drivers/base/ directory.
*
*/
+#include <linux/device/trust.h>
#include <linux/notifier.h>
/**
@@ -88,6 +89,13 @@ struct driver_private {
};
#define to_driver(obj) container_of(obj, struct driver_private, kobj)
+enum device_trust_state {
+ DEVICE_TRUST_SELECTABLE,
+ DEVICE_TRUST_WAITING,
+ DEVICE_TRUST_AUTHORIZED,
+ DEVICE_TRUST_COMMITTED,
+};
+
/**
* struct device_private - structure to hold the private to the driver core
* portions of the device structure.
@@ -105,6 +113,9 @@ struct driver_private {
* dev_err_probe() for later retrieval via debugfs
* @device: pointer back to the struct device that this structure is
* associated with.
+ * @trust_committed_level: operating level retained across bind transactions
+ * @trust_state: policy admission state
+ * @trust_initialized: bus and boot policy have initialized admission state
* @dead: This device is currently either in the process of or has been
* removed from the system. Any asynchronous events scheduled for this
* device should exit without taking any action.
@@ -121,6 +132,9 @@ struct device_private {
const struct device_driver *async_driver;
char *deferred_probe_reason;
struct device *device;
+ enum device_trust_level trust_committed_level;
+ enum device_trust_state trust_state;
+ u8 trust_initialized:1;
u8 dead:1;
};
#define to_device_private_parent(obj) \
@@ -166,6 +180,11 @@ void bus_remove_driver(struct device_driver *drv);
void device_release_driver_internal(struct device *dev, const struct device_driver *drv,
struct device *parent);
+/* All active trust level transitions are serialized by the device lock. */
+void device_trust_init_state(struct device *dev);
+int device_trust_prepare(struct device *dev, const struct device_driver *drv);
+void device_trust_clear(struct device *dev);
+
void driver_detach(const struct device_driver *drv);
void driver_deferred_probe_del(struct device *dev);
void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf);
@@ -3681,6 +3681,8 @@ int device_add(struct device *dev)
if (error)
goto name_error;
+ device_trust_init_state(dev);
+
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
parent = get_device(dev->parent);
@@ -545,13 +545,21 @@ int device_bind_driver(struct device *dev)
{
int ret;
+ if (dev->bus && dev->bus->trust_resolve)
+ return -EOPNOTSUPP;
+
+ ret = device_trust_prepare(dev, dev->driver);
+ if (ret)
+ return ret;
+
ret = driver_sysfs_add(dev);
if (!ret) {
device_links_force_bind(dev);
driver_bound(dev);
- }
- else
+ } else {
bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
+ device_trust_clear(dev);
+ }
return ret;
}
EXPORT_SYMBOL_GPL(device_bind_driver);
@@ -679,6 +687,14 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
}
re_probe:
+ ret = device_trust_prepare(dev, drv);
+ if (ret) {
+ device_links_no_driver(dev);
+ /* Report it like a probe error so a scan tries the next driver. */
+ ret = -ret;
+ goto done;
+ }
+
device_set_driver(dev, drv);
/* If using pinctrl, bind pins now before probing */
@@ -745,6 +761,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
if (dev->bus && dev->bus->dma_cleanup)
dev->bus->dma_cleanup(dev);
device_unbind_cleanup(dev);
+ device_trust_clear(dev);
goto re_probe;
}
@@ -771,6 +788,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
pinctrl_bind_failed:
device_links_no_driver(dev);
device_unbind_cleanup(dev);
+ device_trust_clear(dev);
done:
return ret;
}
@@ -1405,6 +1423,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)
dev->bus->dma_cleanup(dev);
device_unbind_cleanup(dev);
+ device_trust_clear(dev);
device_links_driver_cleanup(dev);
klist_remove(&dev->p->knode_driver);
new file mode 100644
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <kunit/visibility.h>
+
+#include "base.h"
+
+void device_trust_init_state(struct device *dev)
+{
+ if (dev->p->trust_initialized)
+ return;
+
+ dev->p->trust_state = dev->trust_policy == DEVICE_TRUST_POLICY_DISABLED ?
+ DEVICE_TRUST_WAITING : DEVICE_TRUST_SELECTABLE;
+ dev->p->trust_initialized = true;
+}
+
+static bool device_trust_policy_matches(enum device_trust_policy policy,
+ enum device_trust_level level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ return true;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ return level == DEVICE_TRUST_DISABLED;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ return level == DEVICE_TRUST_ADVERSARY;
+ case DEVICE_TRUST_POLICY_FULL:
+ return level == DEVICE_TRUST_FULL;
+ default:
+ return false;
+ }
+}
+
+static int device_trust_resolve_default(enum device_trust_policy policy,
+ enum device_trust_level *level)
+{
+ switch (policy) {
+ case DEVICE_TRUST_POLICY_DEFAULT:
+ case DEVICE_TRUST_POLICY_FULL:
+ *level = DEVICE_TRUST_FULL;
+ return 0;
+ case DEVICE_TRUST_POLICY_DISABLED:
+ *level = DEVICE_TRUST_DISABLED;
+ return 0;
+ case DEVICE_TRUST_POLICY_ADVERSARY:
+ *level = DEVICE_TRUST_ADVERSARY;
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+int device_trust_prepare(struct device *dev, const struct device_driver *drv)
+{
+ enum device_trust_policy policy;
+ enum device_trust_level level = DEVICE_TRUST_DISABLED;
+ int ret;
+
+ device_lock_assert(dev);
+ policy = dev->trust_policy;
+
+ if (WARN_ON_ONCE(READ_ONCE(dev->trust_level) != DEVICE_TRUST_DISABLED))
+ return -EBUSY;
+ if (policy == DEVICE_TRUST_POLICY_DISABLED)
+ return -EPERM;
+ if (dev->bus && dev->bus->trust_resolve)
+ ret = dev->bus->trust_resolve(dev, drv, policy, &level);
+ else
+ ret = device_trust_resolve_default(policy, &level);
+ if (ret)
+ return ret;
+
+ if (!device_trust_policy_matches(policy, level))
+ return -EINVAL;
+ if (level == DEVICE_TRUST_DISABLED)
+ return -EPERM;
+ if (level != DEVICE_TRUST_ADVERSARY && level != DEVICE_TRUST_FULL)
+ return -EINVAL;
+ if (dev->p->trust_state == DEVICE_TRUST_COMMITTED &&
+ dev->p->trust_committed_level != level)
+ return -EINVAL;
+
+ dev->p->trust_committed_level = level;
+ dev->p->trust_state = DEVICE_TRUST_COMMITTED;
+ WRITE_ONCE(dev->trust_level, level);
+ return 0;
+}
+
+void device_trust_clear(struct device *dev)
+{
+ device_lock_assert(dev);
+ WRITE_ONCE(dev->trust_level, DEVICE_TRUST_DISABLED);
+}
+
+/**
+ * device_get_trust_level - Return a device's active trust level
+ * @dev: device to inspect
+ *
+ * The value is stable from the start of DMA configuration through the end of
+ * DMA cleanup. An unbound device has %DEVICE_TRUST_DISABLED as its active
+ * trust level.
+ *
+ * Return: The active trust level for the current bind transaction.
+ */
+enum device_trust_level device_get_trust_level(const struct device *dev)
+{
+ return READ_ONCE(dev->trust_level);
+}
+EXPORT_SYMBOL_GPL(device_get_trust_level);
+
+/**
+ * device_is_adversarial - Test whether a device is operated as an adversary
+ * @dev: device to inspect
+ *
+ * Return: True when the active trust level is %DEVICE_TRUST_ADVERSARY.
+ */
+bool device_is_adversarial(const struct device *dev)
+{
+ return device_get_trust_level(dev) == DEVICE_TRUST_ADVERSARY;
+}
+EXPORT_SYMBOL_GPL(device_is_adversarial);
@@ -30,6 +30,7 @@
#include <linux/device/class.h>
#include <linux/device/devres.h>
#include <linux/device/driver.h>
+#include <linux/device/trust.h>
#include <linux/cleanup.h>
#include <asm/device.h>
@@ -695,6 +696,9 @@ enum struct_device_flags {
* @removable: Whether the device can be removed from the system. This
* should be set by the subsystem / bus driver that discovered
* the device.
+ * @trust_policy: Requested operating policy, resolved before driver probe.
+ * @trust_level: Active trust level while a driver is bound, see
+ * device_get_trust_level().
* @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify.
*
* At the lowest level, every device in a Linux system is represented by an
@@ -798,6 +802,8 @@ struct device {
struct device_physical_location *physical_location;
enum device_removable removable;
+ enum device_trust_policy trust_policy;
+ enum device_trust_level trust_level;
DECLARE_BITMAP(flags, DEV_FLAG_COUNT);
};
@@ -17,6 +17,7 @@
#include <linux/kobject.h>
#include <linux/klist.h>
#include <linux/pm.h>
+#include <linux/device/trust.h>
struct device_driver;
struct fwnode_handle;
@@ -59,6 +60,11 @@ struct fwnode_handle;
* @resume: Called to bring a device on this bus out of sleep mode.
* @num_vf: Called to find out how many virtual functions a device on this
* bus supports.
+ * @trust_resolve: Resolve a requested trust policy to an active trust level
+ * immediately before driver probe. The device lock is held and
+ * @drv is the candidate driver, which dev->driver does not name
+ * yet. Return 0 on success or a negative errno. This callback
+ * must not enable the device.
* @dma_configure: Called to setup DMA configuration on a device on
* this bus.
* @dma_cleanup: Called to cleanup DMA configuration on a device on
@@ -102,6 +108,10 @@ struct bus_type {
int (*num_vf)(struct device *dev);
+ int (*trust_resolve)(struct device *dev,
+ const struct device_driver *drv,
+ enum device_trust_policy policy,
+ enum device_trust_level *level);
int (*dma_configure)(struct device *dev);
void (*dma_cleanup)(struct device *dev);
new file mode 100644
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _DEVICE_TRUST_H_
+#define _DEVICE_TRUST_H_
+
+#include <linux/types.h>
+
+struct device;
+
+/**
+ * enum device_trust_level - active trust level for a device
+ * @DEVICE_TRUST_DISABLED: The device cannot be operated by a driver
+ * @DEVICE_TRUST_ADVERSARY: Operate the device with adversarial restrictions
+ * @DEVICE_TRUST_FULL: Operate the device with the usual kernel privileges
+ *
+ * Active trust levels are discrete states, not an ordered privilege scale.
+ */
+enum device_trust_level {
+ DEVICE_TRUST_DISABLED,
+ DEVICE_TRUST_ADVERSARY,
+ DEVICE_TRUST_FULL,
+};
+
+/**
+ * enum device_trust_policy - requested device trust policy
+ * @DEVICE_TRUST_POLICY_DEFAULT: Let the bus select the active trust level
+ * @DEVICE_TRUST_POLICY_DISABLED: Request that the device remain disabled
+ * @DEVICE_TRUST_POLICY_ADVERSARY: Request adversarial operation
+ * @DEVICE_TRUST_POLICY_FULL: Request normal operation
+ *
+ * A requested policy is resolved to an active trust level before a driver is
+ * probed.
+ */
+enum device_trust_policy {
+ DEVICE_TRUST_POLICY_DEFAULT,
+ DEVICE_TRUST_POLICY_DISABLED,
+ DEVICE_TRUST_POLICY_ADVERSARY,
+ DEVICE_TRUST_POLICY_FULL,
+};
+
+enum device_trust_level device_get_trust_level(const struct device *dev);
+bool device_is_adversarial(const struct device *dev);
+
+#endif /* _DEVICE_TRUST_H_ */