Skip to contentSkip to conversation

nvme: add sysfs attribute 'tls_keyring'

PATCH In review Hannes Reinecke 3 participants 4 messages Mar 8, 2024 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: add sysfs attribute 'tls_keyring'

Add a sysfs attribute 'tls_keyring' to hold the contents for the 'keyring' fabrics option. This option is only meaningful if the 'tls' option has been specified; if the 'tls_key' option is specified the key is looked up directly and the keyring setting is ignored. So blank out the keyring value when 'tls_key' is specified.

---
 drivers/nvme/host/sysfs.c | 55 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
drivers/nvme/host/sysfs.c +53 -2
index 6c7f1d5c056f..7b65ff7426c9 100644
@@ -5,11 +5,12 @@
  * Copyright (c) 2011-2014, Intel Corporation.
  */
 
-#include <linux/nvme-auth.h>
-
 #include "nvme.h"
 #include "fabrics.h"
 
+#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+
 static ssize_t nvme_sysfs_reset(struct device *dev,
 				struct device_attribute *attr, const char *buf,
 				size_t count)
@@ -677,6 +678,52 @@ static ssize_t tls_key_show(struct device *dev,
 	return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
 }
 static DEVICE_ATTR_RO(tls_key);
+
+static ssize_t tls_keyring_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	struct key *keyring;
+
+	if (!ctrl->tls_key)
+		return 0;
+	/* Do not display the keyring for user-selected keys */
+	if (ctrl->opts->tls_key)
+		return 0;
+	keyring = ctrl->opts->keyring;
+	if (!keyring) {
+		keyring = key_lookup(nvme_keyring_id());
+		if (!keyring)
+			return -EINVAL;
+	}
+	return sysfs_emit(buf, "%s", keyring->description);
+}
+
+static ssize_t tls_keyring_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	int err;
+	u32 key_id;
+	struct key *keyring;
+
+	if (!strlen(buf))
+		return -EINVAL;
+	err = kstrtou32(buf, 0, &key_id);
+	if (err)
+		return err;
+	keyring = key_lookup(key_id);
+	if (IS_ERR(keyring)) {
+		pr_err("key %08x not found\n", key_id);
+		return -EINVAL;
+	}
+	if (ctrl->opts->keyring)
+		key_put(ctrl->opts->keyring);
+	ctrl->opts->keyring = keyring;
+	return 0;
+}
+static DEVICE_ATTR(tls_keyring, S_IRUGO | S_IWUSR,
+		tls_keyring_show, tls_keyring_store);
 #endif
 
 static struct attribute *nvme_dev_attrs[] = {
@@ -708,6 +755,7 @@ static struct attribute *nvme_dev_attrs[] = {
 #endif
 #ifdef CONFIG_NVME_TCP_TLS
 	&dev_attr_tls_key.attr,
+	&dev_attr_tls_keyring.attr,
 #endif
 	&dev_attr_adm_passthru_err_log_enabled.attr,
 	NULL
@@ -743,6 +791,9 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
 	if (a == &dev_attr_tls_key.attr &&
 	    (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp")))
 		return 0;
+	if (a == &dev_attr_tls_keyring.attr &&
+	    (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp")))
+		return 0;
 #endif
 
 	return a->mode;
Signature · 1 line
2.35.3
maintainer
#1 lore raw
Quoted 41 lines · Hannes Reinecke

Add a sysfs attribute 'tls_keyring' to hold the contents for the 'keyring' fabrics option. This option is only meaningful if the 'tls' option has been specified; if the 'tls_key' option is specified the key is looked up directly and the keyring setting is ignored. So blank out the keyring value when 'tls_key' is specified.

---
  drivers/nvme/host/sysfs.c | 55 +++++++++++++++++++++++++++++++++++++--
  1 file changed, 53 insertions(+), 2 deletions(-)
drivers/nvme/host/sysfs.c +10 -2
index 6c7f1d5c056f..7b65ff7426c9 100644
@@ -5,11 +5,12 @@
   * Copyright (c) 2011-2014, Intel Corporation.
   */
 
-#include <linux/nvme-auth.h>
-
  #include "nvme.h"
  #include "fabrics.h"
 
+#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+
  static ssize_t nvme_sysfs_reset(struct device *dev,
  				struct device_attribute *attr, const char *buf,
  				size_t count)
@@ -677,6 +678,52 @@ static ssize_t tls_key_show(struct device *dev,
  	return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
  }
  static DEVICE_ATTR_RO(tls_key);
+
+static ssize_t tls_keyring_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	struct key *keyring;
+

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 697

Quoted 10 lines
drivers/nvme/host/sysfs.c +10 -0
+	if (!ctrl->tls_key)
+		return 0;
+	/* Do not display the keyring for user-selected keys */
+	if (ctrl->opts->tls_key)
+		return 0;
+	keyring = ctrl->opts->keyring;
+	if (!keyring) {
+		keyring = key_lookup(nvme_keyring_id());
+		if (!keyring)
+			return -EINVAL;

Weird if this failed...

Quoted 17 lines
drivers/nvme/host/sysfs.c +17 -0
+	}
+	return sysfs_emit(buf, "%s", keyring->description);
+}
+
+static ssize_t tls_keyring_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	int err;
+	u32 key_id;
+	struct key *keyring;
+
+	if (!strlen(buf))
+		return -EINVAL;
+	err = kstrtou32(buf, 0, &key_id);
+	if (err)
+		return err;

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 724

Quoted 10 lines
drivers/nvme/host/sysfs.c +10 -0
+	keyring = key_lookup(key_id);
+	if (IS_ERR(keyring)) {
+		pr_err("key %08x not found\n", key_id);
+		return -EINVAL;
+	}
+	if (ctrl->opts->keyring)
+		key_put(ctrl->opts->keyring);
+	ctrl->opts->keyring = keyring;
+	return 0;
+}

Why should there be a _store() for this?

Quoted 14 lines
drivers/nvme/host/sysfs.c +3 -0
+static DEVICE_ATTR(tls_keyring, S_IRUGO | S_IWUSR,
+		tls_keyring_show, tls_keyring_store);
  #endif
 
  static struct attribute *nvme_dev_attrs[] = {
@@ -708,6 +755,7 @@ static struct attribute *nvme_dev_attrs[] = {
  #endif
  #ifdef CONFIG_NVME_TCP_TLS
  	&dev_attr_tls_key.attr,
+	&dev_attr_tls_keyring.attr,
  #endif
  	&dev_attr_adm_passthru_err_log_enabled.attr,
  	NULL

drivers/nvme/host/sysfs.c static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, · line 796

Quoted 6 lines
drivers/nvme/host/sysfs.c +3 -0
@@ -743,6 +791,9 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  	if (a == &dev_attr_tls_key.attr &&
  	    (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp")))
  		return 0;
+	if (a == &dev_attr_tls_keyring.attr &&
+	    (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp")))
+		return 0;

Wouldn't it make better sense to expose it only if tls was requested?

#2 lore raw
Quoted 46 lines · Sagi Grimberg
Quoted 43 lines · Hannes Reinecke

Add a sysfs attribute 'tls_keyring' to hold the contents for the 'keyring' fabrics option. This option is only meaningful if the 'tls' option has been specified; if the 'tls_key' option is specified the key is looked up directly and the keyring setting is ignored. So blank out the keyring value when 'tls_key' is specified.

---
  drivers/nvme/host/sysfs.c | 55 +++++++++++++++++++++++++++++++++++++--

  1 file changed, 53 insertions(+), 2 deletions(-)
drivers/nvme/host/sysfs.c +14 -2
index 6c7f1d5c056f..7b65ff7426c9 100644
@@ -5,11 +5,12 @@
    * Copyright (c) 2011-2014, Intel Corporation.
    */
-#include <linux/nvme-auth.h>
-
   #include "nvme.h"
   #include "fabrics.h"
+#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+
   static ssize_t nvme_sysfs_reset(struct device *dev,
                   struct device_attribute *attr, const char *buf,
                   size_t count)
@@ -677,6 +678,52 @@ static ssize_t tls_key_show(struct device *dev,
       return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
   }
   static DEVICE_ATTR_RO(tls_key);
+
+static ssize_t tls_keyring_show(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+    struct key *keyring;
+
+    if (!ctrl->tls_key)
+        return 0;
+    /* Do not display the keyring for user-selected keys */
+    if (ctrl->opts->tls_key)
Quoted 9 lines

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 697

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+        return 0;
+    keyring = ctrl->opts->keyring;
+    if (!keyring) {
+        keyring = key_lookup(nvme_keyring_id());
+        if (!keyring)
+            return -EINVAL;

Weird if this failed...

Yeah. But feels even weirder to _not_ check it.
WARN_ON?

Quoted 21 lines
Quoted 21 lines
drivers/nvme/host/sysfs.c +21 -0
+    }
+    return sysfs_emit(buf, "%s", keyring->description);
+}
+
+static ssize_t tls_keyring_store(struct device *dev,
+        struct device_attribute *attr, const char *buf, size_t count)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+    int err;
+    u32 key_id;
+    struct key *keyring;
+
+    if (!strlen(buf))
+        return -EINVAL;
+    err = kstrtou32(buf, 0, &key_id);
+    if (err)
+        return err;
+    keyring = key_lookup(key_id);
+    if (IS_ERR(keyring)) {
+        pr_err("key %08x not found\n", key_id);
+        return -EINVAL;
Quoted 9 lines

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 724

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+    }
+    if (ctrl->opts->keyring)
+        key_put(ctrl->opts->keyring);
+    ctrl->opts->keyring = keyring;
+    return 0;
+}

Why should there be a _store() for this?

Technically one can change the keyring. But I can drop it for now, and revisit the issue once I get around to fully test key revokation and key rotation.

Quoted 14 lines
Quoted 14 lines
drivers/nvme/host/sysfs.c +3 -0
+static DEVICE_ATTR(tls_keyring, S_IRUGO | S_IWUSR,
+        tls_keyring_show, tls_keyring_store);
   #endif
   static struct attribute *nvme_dev_attrs[] = {
@@ -708,6 +755,7 @@ static struct attribute *nvme_dev_attrs[] = {
   #endif
   #ifdef CONFIG_NVME_TCP_TLS
       &dev_attr_tls_key.attr,
+    &dev_attr_tls_keyring.attr,
   #endif
       &dev_attr_adm_passthru_err_log_enabled.attr,

      NULL

drivers/nvme/host/sysfs.c +0 -0
@@ -743,6 +791,9 @@ static umode_t nvme_dev_attrs_are_visible(struct 
 kobject *kobj,
Quoted 8 lines
Quoted 6 lines

      if (a == &dev_attr_tls_key.attr &&
          (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))) return 0;
+    if (a == &dev_attr_tls_keyring.attr &&
+        (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))) +        return 0;

Wouldn't it make better sense to expose it only if tls was requested?

Sort of. But we never did for the 'tls_key' attribute, too, which would have the same restriction. So I didn't do it here.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
maintainer
#3 lore raw
Quoted 57 lines · Hannes Reinecke
Quoted 47 lines · Sagi Grimberg
Quoted 44 lines · Hannes Reinecke

Add a sysfs attribute 'tls_keyring' to hold the contents for the 'keyring' fabrics option. This option is only meaningful if the 'tls' option has been specified; if the 'tls_key' option is specified the key is looked up directly and the keyring setting is ignored. So blank out the keyring value when 'tls_key' is specified.

---
  drivers/nvme/host/sysfs.c | 55
+++++++++++++++++++++++++++++++++++++--

  1 file changed, 53 insertions(+), 2 deletions(-)
drivers/nvme/host/sysfs.c +14 -2
index 6c7f1d5c056f..7b65ff7426c9 100644
@@ -5,11 +5,12 @@
    * Copyright (c) 2011-2014, Intel Corporation.
    */
-#include <linux/nvme-auth.h>
-
   #include "nvme.h"
   #include "fabrics.h"
+#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+
   static ssize_t nvme_sysfs_reset(struct device *dev,
                   struct device_attribute *attr, const char *buf,
                   size_t count)
@@ -677,6 +678,52 @@ static ssize_t tls_key_show(struct device *dev,
       return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
   }
   static DEVICE_ATTR_RO(tls_key);
+
+static ssize_t tls_keyring_show(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+    struct key *keyring;
+
+    if (!ctrl->tls_key)
+        return 0;
+    /* Do not display the keyring for user-selected keys */
+    if (ctrl->opts->tls_key)
Quoted 9 lines

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 697

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+        return 0;
+    keyring = ctrl->opts->keyring;
+    if (!keyring) {
+        keyring = key_lookup(nvme_keyring_id());
+        if (!keyring)
+            return -EINVAL;

Weird if this failed...

Quoted 2 lines

Yeah. But feels even weirder to _not_ check it.
WARN_ON?

WARN_ON is better I guess as this is really unexpected.

Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
drivers/nvme/host/sysfs.c +21 -0
+    }
+    return sysfs_emit(buf, "%s", keyring->description);
+}
+
+static ssize_t tls_keyring_store(struct device *dev,
+        struct device_attribute *attr, const char *buf, size_t count)
+{
+    struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+    int err;
+    u32 key_id;
+    struct key *keyring;
+
+    if (!strlen(buf))
+        return -EINVAL;
+    err = kstrtou32(buf, 0, &key_id);
+    if (err)
+        return err;
+    keyring = key_lookup(key_id);
+    if (IS_ERR(keyring)) {
+        pr_err("key %08x not found\n", key_id);
+        return -EINVAL;
Quoted 9 lines

drivers/nvme/host/sysfs.c static ssize_t tls_key_show(struct device *dev, · line 724

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+    }
+    if (ctrl->opts->keyring)
+        key_put(ctrl->opts->keyring);
+    ctrl->opts->keyring = keyring;
+    return 0;
+}

Why should there be a _store() for this?

Quoted 3 lines

Technically one can change the keyring. But I can drop it for now, and revisit the issue once I get around to fully test key revokation and key rotation.

Still I don't see a sane use-case where this would be used.

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/nvme/host/sysfs.c +3 -0
+static DEVICE_ATTR(tls_keyring, S_IRUGO | S_IWUSR,
+        tls_keyring_show, tls_keyring_store);
   #endif
   static struct attribute *nvme_dev_attrs[] = {
@@ -708,6 +755,7 @@ static struct attribute *nvme_dev_attrs[] = {
   #endif
   #ifdef CONFIG_NVME_TCP_TLS
       &dev_attr_tls_key.attr,
+    &dev_attr_tls_keyring.attr,
   #endif
       &dev_attr_adm_passthru_err_log_enabled.attr,

      NULL

drivers/nvme/host/sysfs.c +0 -0
@@ -743,6 +791,9 @@ static umode_t nvme_dev_attrs_are_visible(struct 
 kobject *kobj,
Quoted 8 lines
Quoted 6 lines

      if (a == &dev_attr_tls_key.attr &&
          (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))) return 0;
+    if (a == &dev_attr_tls_keyring.attr &&
+        (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))) +        return 0;

Wouldn't it make better sense to expose it only if tls was requested?

Quoted 2 lines

Sort of. But we never did for the 'tls_key' attribute, too, which would have the same restriction. So I didn't do it here.

OK, not a must. Can be added later...