[PATCH v5 7/7] platform/chrome: cros_ec_chardev: Secure cros_ec_device via revocable

Tzung-Bi Shih posted 7 patches 2 months ago
[PATCH v5 7/7] platform/chrome: cros_ec_chardev: Secure cros_ec_device via revocable
Posted by Tzung-Bi Shih 2 months ago
Miscdevice now supports revocable fops replacement.  Use it to secure
the cros_ec_device.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
PoC patch.

v5:
- No primary changes but modify the API usage accordingly.

v4: https://lore.kernel.org/chrome-platform/20250923075302.591026-8-tzungbi@kernel.org
- New in the series.

 drivers/platform/chrome/cros_ec_chardev.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/platform/chrome/cros_ec_chardev.c b/drivers/platform/chrome/cros_ec_chardev.c
index c9d80ad5b57e..01691b023d7e 100644
--- a/drivers/platform/chrome/cros_ec_chardev.c
+++ b/drivers/platform/chrome/cros_ec_chardev.c
@@ -13,6 +13,7 @@
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/fs.h>
+#include <linux/fs_revocable.h>
 #include <linux/miscdevice.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
@@ -166,7 +167,6 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->ec_dev = ec_dev;
 	priv->cmd_offset = ec->cmd_offset;
 	filp->private_data = priv;
 	INIT_LIST_HEAD(&priv->events);
@@ -370,6 +370,19 @@ static const struct file_operations chardev_fops = {
 #endif
 };
 
+static int cros_ec_chardev_rev_try_access(struct revocable **revs,
+					  size_t num_revs, void *data)
+{
+	struct chardev_priv *priv = data;
+
+	priv->ec_dev = revocable_try_access(revs[0]);
+	return priv->ec_dev ? 0 : -ENODEV;
+}
+
+static const struct fs_revocable_operations cros_ec_chardev_frops = {
+	.try_access = cros_ec_chardev_rev_try_access,
+};
+
 static int cros_ec_chardev_probe(struct platform_device *pdev)
 {
 	struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
@@ -386,6 +399,13 @@ static int cros_ec_chardev_probe(struct platform_device *pdev)
 	misc->name = ec_platform->ec_name;
 	misc->parent = pdev->dev.parent;
 
+	misc->rps = devm_kcalloc(&pdev->dev, 1, sizeof(*misc->rps), GFP_KERNEL);
+	if (!misc->rps)
+		return -ENOMEM;
+	misc->rps[0] = ec->ec_dev->revocable_provider;
+	misc->num_rps = 1;
+	misc->frops = &cros_ec_chardev_frops;
+
 	dev_set_drvdata(&pdev->dev, misc);
 
 	return misc_register(misc);
-- 
2.51.0.788.g6d19910ace-goog