Skip to contentSkip to conversation

WIP: iommu: Export dmar_platform_optin() as a securityfs attribute

PATCH New Richard Hughes 1 participant 1 message May 7, 2020 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 WIP: iommu: Export dmar_platform_optin() as a securityfs attribute

Although we can get the same information from /sys/bus/thunderbolt/devices/domainX/iommu_dma_protection, the IP block on the SOC may be deactivated. That means that it would not be exported even though pre boot DMA was enabled and available.

This patch is work in progress and is only supposed to spark discussion. I'm new here, so please be gentle :)

---
 drivers/iommu/Kconfig            | 10 ++++++
 drivers/iommu/Makefile           |  1 +
 drivers/iommu/iommu-securityfs.c | 55 ++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c            |  1 +
 include/linux/iommu.h            |  7 ++++
 5 files changed, 74 insertions(+)
 create mode 100644 drivers/iommu/iommu-securityfs.c
drivers/iommu/Kconfig +6 -0
index 2ab07ce17abb..e31fa9a459d6 100644
@@ -79,6 +79,16 @@ config IOMMU_DEBUGFS
 	  debug/iommu directory, and then populate a subdirectory with
 	  entries as required.
 
+config IOMMU_SECURITYFS
+	bool "Export IOMMU attributes in SecurityFS"
+	depends on SECURITY
+	help
+	  Allows exposure of IOMMU security attributes. Devices can,
+	  at initialization time, cause the IOMMU code to create a top-

level
+ security/iommu directory, and then populate entries as
required.
+
+ If unsure, say N here.
+
config IOMMU_DEFAULT_PASSTHROUGH
bool "IOMMU passthrough by default"
depends on IOMMU_API

drivers/iommu/Makefile +1 -0
index 9f33fdb3bb05..f6c9c07b86b6 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_IOMMU_API) += iommu.o
 obj-$(CONFIG_IOMMU_API) += iommu-traces.o
 obj-$(CONFIG_IOMMU_API) += iommu-sysfs.o
 obj-$(CONFIG_IOMMU_DEBUGFS) += iommu-debugfs.o
+obj-$(CONFIG_IOMMU_SECURITYFS) += iommu-securityfs.o
 obj-$(CONFIG_IOMMU_DMA) += dma-iommu.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o
drivers/iommu/iommu- renamed from drivers/iommu/iommu-securityfs.c +0 -0

securityfs.c
new file mode 100644
index 000000000000..71210c56311a

drivers/iommu/iommu-securityfs.c new file +26 -0
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * IOMMU security interface
+ *
+ * Copyright 2020 (c) Richard Hughes ([email protected])
+ */
+
+#include <linux/pci.h>
+#include <linux/iommu.h>
+#include <linux/security.h>
+#include <linux/dmar.h>
+
+struct dentry *iommu_securityfs_dir;
+EXPORT_SYMBOL_GPL(iommu_securityfs_dir);
+
+struct dentry *iommu_dmar_enabled;
+
+static ssize_t iommu_dmar_enabled_read(struct file *filp,
+				       char __user *buf,
+				       size_t count,
+				       loff_t *ppos)
+{
+	char tmp[2];
+
+	sprintf(tmp, "%d\n", dmar_platform_optin());
+	return simple_read_from_buffer(buf, count, ppos, tmp,

sizeof(tmp));
+}
+
+static const struct file_operations iommu_dmar_enabled_ops = {
+ .read = iommu_dmar_enabled_read,
+};
+
+/**
+ * iommu_securityfs_setup - create the iommu directory in security with any
+ * shared attributes
+ *
+ * Provide base enablement for using security to expose internal data of an
+ * IOMMU driver. When called, this function creates the
+ * /sys/kernel/security/iommu directory.
+ *
+ * This function is called from iommu_init; drivers may then use
+ * iommu_securityfs_dir to instantiate vendor-specific attributes.
+ */
+void iommu_securityfs_setup(void)
+{
+ if (!iommu_securityfs_dir) {
+ iommu_securityfs_dir = securityfs_create_dir("iommu",
NULL);
+ if (IS_ERR(iommu_dmar_enabled))
+ return;
+ iommu_dmar_enabled =
+ securityfs_create_file("dmar_enabled",
+ 0600, iommu_securityfs_dir,
NULL,
+ &iommu_dmar_enabled_ops);
+ }
+}

drivers/iommu/iommu.c +1 -0
index 7b375421afba..d8256485a7af 100644
@@ -2192,6 +2192,7 @@ static int __init iommu_init(void)
 	BUG_ON(!iommu_group_kset);
 
 	iommu_debugfs_setup();
+	iommu_securityfs_setup();
 
 	return 0;
 }
include/linux/iommu.h +7 -0
index 7ef8b0bda695..5d6721ec9bf9 100644
@@ -1101,4 +1101,11 @@ void iommu_debugfs_setup(void);
 static inline void iommu_debugfs_setup(void) {}
 #endif
 
+#ifdef CONFIG_SECURITY
+extern	struct dentry *iommu_securityfs_dir;
+void iommu_securityfs_setup(void);
+#else
+static inline void iommu_securityfs_setup(void) {}
+#endif
+
 #endif /* __LINUX_IOMMU_H */

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu