Skip to contentSkip to conversation

Add Nitro Secure Module support

PATCH Superseded Alexander Graf 3 participants 9 messages Sep 29, 2023 – Oct 3, 2023

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Alexander Graf

author maintainer
#0 lore raw

v2 cover Add Nitro Secure Module support

We already have support for the Nitro Enclave kernel module in upstream Linux, which is needed to control a Nitro Enclave's lifecycle.

However, users typically want to run Linux inside the Enclave as well. To do that well, they need the ability to communicate to the Nitro Secure
Module: A virtio based PV device that provides access to PCRs, an attestation document as well as access to entropy.

These patches add driver support for NSM. With them in place, upstream Linux has everything that's needed to run as a Nitro Enclave kernel.

Alex

v1 -> v2:

- Remove boilerplate
- Add uapi header

Alexander Graf (2):
misc: Add Nitro Secure Module driver
hwrng: Add support for Nitro Secure Module

 MAINTAINERS                      |  11 +
 drivers/char/hw_random/Kconfig   |  12 +
 drivers/char/hw_random/Makefile  |   1 +
 drivers/char/hw_random/nsm-rng.c | 272 +++++++++++++++++++
 drivers/misc/Kconfig             |  11 +
 drivers/misc/Makefile            |   1 +
 drivers/misc/nsm.c               | 448 +++++++++++++++++++++++++++++++
 include/linux/nsm.h              |  35 +++
 include/uapi/linux/nsm.h         |  24 ++
 9 files changed, 815 insertions(+)
 create mode 100644 drivers/char/hw_random/nsm-rng.c
 create mode 100644 drivers/misc/nsm.c
 create mode 100644 include/linux/nsm.h
 create mode 100644 include/uapi/linux/nsm.h
Signature · 12 lines
2.40.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
author maintainer
#8 lore raw

PATCH v2 2/2 hwrng: Add support for Nitro Secure Module

When running Linux inside a Nitro Enclave, the Nitro Secure Module provides a virtio message that can be used to receive entropy. This patch adds support to read that entropy on demand and expose it through the hwrng device.

---

v1 -> v2:

- Remove boilerplate

---
 MAINTAINERS                      |   1 +
 drivers/char/hw_random/Kconfig   |  12 ++
 drivers/char/hw_random/Makefile  |   1 +
 drivers/char/hw_random/nsm-rng.c | 272 +++++++++++++++++++++++++++++++
 4 files changed, 286 insertions(+)
 create mode 100644 drivers/char/hw_random/nsm-rng.c
MAINTAINERS +1 -0
index d54bf3ea2b9d..da9697639968 100644
@@ -15104,6 +15104,7 @@ L:	[email protected]
 L:	The AWS Nitro Enclaves Team <[email protected]>
 S:	Supported
 W:	https://aws.amazon.com/ec2/nitro/nitro-enclaves/
+F:	drivers/char/hw_random/nsm-rng.c
 F:	drivers/misc/nsm.c
 F:	include/linux/nsm.h
 F:	include/uapi/linux/nsm.h
drivers/char/hw_random/Kconfig +12 -0
index 8de74dcfa18c..5d06e24cfdde 100644
@@ -573,6 +573,18 @@ config HW_RANDOM_JH7110
 	  To compile this driver as a module, choose M here.
 	  The module will be called jh7110-trng.
 
+config HW_RANDOM_NSM
+	tristate "Nitro (Enclaves) Security Module support"
+	depends on NSM
+	help
+	  This driver provides support for the Nitro Security Module
+	  in AWS EC2 Nitro based Enclaves. The driver enables support
+	  for reading RNG data as well as a generic communication
+	  mechanism with the hypervisor.
+
+	  To compile this driver as a module, choose M here.
+	  The module will be called nsm_rng.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
drivers/char/hw_random/Makefile +1 -0
index 32549a1186dc..7e33d1ed40f8 100644
@@ -49,3 +49,4 @@ obj-$(CONFIG_HW_RANDOM_ARM_SMCCC_TRNG) += arm_smccc_trng.o
 obj-$(CONFIG_HW_RANDOM_CN10K) += cn10k-rng.o
 obj-$(CONFIG_HW_RANDOM_POLARFIRE_SOC) += mpfs-rng.o
 obj-$(CONFIG_HW_RANDOM_JH7110) += jh7110-trng.o
+obj-$(CONFIG_HW_RANDOM_NSM) += nsm-rng.o
drivers/char/hw_random/nsm-rng.c new file +272 -0

272 lines not on this page · raw message

Signature · 12 lines
2.40.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
author maintainer
#1 lore raw

PATCH v2 1/2 misc: Add Nitro Secure Module driver

In reply to Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "NSM". This device has 2 main functions:

1) Provide attestation reports
2) Modify PCR state
3) Provide entropy

This patch adds the core NSM driver that exposes a /dev/nsm device node which user space can use to request attestation documents and influence PCR states. A follow up patch will add a hwrng driver to feed its entropy into the kernel.

---

v1 -> v2:

- Remove boilerplate
- Add uapi header

---
 MAINTAINERS              |  10 +
 drivers/misc/Kconfig     |  11 +
 drivers/misc/Makefile    |   1 +
 drivers/misc/nsm.c       | 448 +++++++++++++++++++++++++++++++++++++++
 include/linux/nsm.h      |  35 +++
 include/uapi/linux/nsm.h |  24 +++
 6 files changed, 529 insertions(+)
 create mode 100644 drivers/misc/nsm.c
 create mode 100644 include/linux/nsm.h
 create mode 100644 include/uapi/linux/nsm.h
MAINTAINERS +10 -0
index b19995690904..d54bf3ea2b9d 100644
@@ -15098,6 +15098,16 @@ F:	include/linux/nitro_enclaves.h
 F:	include/uapi/linux/nitro_enclaves.h
 F:	samples/nitro_enclaves/
 
+NITRO SECURE MODULE (NSM)
+M:	Alexander Graf <[email protected]>
+L:	[email protected]
+L:	The AWS Nitro Enclaves Team <[email protected]>
+S:	Supported
+W:	https://aws.amazon.com/ec2/nitro/nitro-enclaves/
+F:	drivers/misc/nsm.c
+F:	include/linux/nsm.h
+F:	include/uapi/linux/nsm.h
+
 NOHZ, DYNTICKS SUPPORT
 M:	Frederic Weisbecker <[email protected]>
 M:	Thomas Gleixner <[email protected]>
drivers/misc/Kconfig +11 -0
index cadd4a820c03..7b1ed309d469 100644
@@ -562,6 +562,17 @@ config TPS6594_PFSM
 	  This driver can also be built as a module.  If so, the module
 	  will be called tps6594-pfsm.
 
+config NSM
+	tristate "Nitro (Enclaves) Security Module support"
+	depends on VIRTIO
+	help
+	  This driver provides support for the Nitro Security Module
+	  in AWS EC2 Nitro based Enclaves. The driver exposes a /dev/nsm
+	  device user space can use to communicate with the hypervisor.
+
+	  To compile this driver as a module, choose M here.
+	  The module will be called nsm.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
drivers/misc/Makefile +1 -0
index f2a4d1ff65d4..ea6ea5bbbc9c 100644
@@ -67,3 +67,4 @@ obj-$(CONFIG_TMR_MANAGER)      += xilinx_tmr_manager.o
 obj-$(CONFIG_TMR_INJECT)	+= xilinx_tmr_inject.o
 obj-$(CONFIG_TPS6594_ESM)	+= tps6594-esm.o
 obj-$(CONFIG_TPS6594_PFSM)	+= tps6594-pfsm.o
+obj-$(CONFIG_NSM)		+= nsm.o
drivers/misc/nsm.c new file +448 -0

448 lines not on this page · raw message

include/linux/nsm.h new file +35 -0
new file mode 100644
index 000000000000..82753de5485e
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Amazon Nitro Secure Module driver.
+ *
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ */
+
+#ifndef __LINUX_NSM_H
+#define __LINUX_NSM_H
+
+#include <linux/uio.h>
+#include <linux/virtio.h>
+
+#define NSM_RESPONSE_MAX_SIZE 0x3000
+
+struct nsm_hwrng {
+	int (*probe)(struct virtio_device *dev);
+	void (*remove)(struct virtio_device *dev);
+};
+
+int nsm_register_hwrng(struct nsm_hwrng *nsm_hwrng);
+void nsm_unregister_hwrng(struct nsm_hwrng *nsm_hwrng);
+
+/* Copy of NSM message in kernel-space */
+struct nsm_kernel_message {
+	/* Copy of user request in kernel memory */
+	struct kvec request;
+	/* Copy of user response in kernel memory */
+	struct kvec response;
+};
+
+int nsm_communicate_with_device(struct virtio_device *dev,
+				struct nsm_kernel_message *message);
+
+#endif /* __LINUX_NSM_H */
include/uapi/linux/nsm.h new file +24 -0
new file mode 100644
index 000000000000..794791631ffb
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ */
+
+#ifndef __UAPI_LINUX_NSM_H
+#define __UAPI_LINUX_NSM_H
+
+#include <linux/uio.h>
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+/* NSM message from user-space */
+struct nsm_message {
+	/* Request from user */
+	struct iovec request;
+	/* Response to user */
+	struct iovec response;
+};
+
+#define NSM_MAGIC		0x0A
+#define NSM_IOCTL_REQUEST	_IOWR(NSM_MAGIC, 0, struct nsm_message)
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
Signature · 12 lines
2.40.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
maintainer
#2 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

Quoted 7 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "NSM". This device has 2 main functions:

1) Provide attestation reports
2) Modify PCR state
3) Provide entropy

Quoted 7 lines

This patch adds the core NSM driver that exposes a /dev/nsm device node which user space can use to request attestation documents and influence PCR states. A follow up patch will add a hwrng driver to feed its entropy into the kernel.

Hi Alex,

I've taken a first look at this driver and have some minor comments. The main point here is that I think we need to look at possible alternatives for the user space interface, and (if possible) change to a set of higher-level ioctl commands from the simple passthrough.

Quoted 11 lines
drivers/misc/nsm.c new file +11 -0
+/* Virtio MMIO device definition */
+struct virtio_mmio_device {
+	struct virtio_device vdev;
+	struct platform_device *pdev;
+
+	void __iomem *base;
+	unsigned long version;
+
+	/* a list of queues so we can dispatch IRQs */
+	spinlock_t lock;
+	struct list_head virtqueues;

drivers/misc/nsm.c line 60

Quoted 10 lines
drivers/misc/nsm.c new file +10 -0
+};
+
+/* Virtqueue list entry */
+struct virtio_mmio_vq_info {
+	/* The actual virtqueue */
+	struct virtqueue *vq;
+
+	/* The list node for the virtqueues list */
+	struct list_head node;
+};

It looks like you are duplicating these structures from the virtio_mmio.c file, which seems like a bad idea for a number of reasons. What is it that you actually need that the virtio subsystem does not provide? Can you add interfaces to the common code instead?

drivers/misc/nsm.c line 66

Quoted 5 lines
drivers/misc/nsm.c new file +5 -0
+static struct virtio_device *nsm_vdev;
+static struct nsm_hwrng *nsm_hwrng;
+static struct mutex nsm_lock;
+static wait_queue_head_t nsm_waitqueue;
+static bool nsm_device_notified;

Instead of global structures, these should ideally all be part of a per-device structure, even if you are sure that there is only ever one of these devices.

Quoted 23 lines
drivers/misc/nsm.c new file +23 -0
+/* Copy an entire message from user-space to kernel-space */
+static int message_memdup_from_user(struct nsm_kernel_message *dst,
+	struct nsm_message *src)
+{
+	struct nsm_message shallow_copy;
+
+	if (!src || !dst)
+		return -EINVAL;
+
+	/* The destination's request and response buffers should be NULL. */
+	if (dst->request.iov_base || dst->response.iov_base)
+		return -EINVAL;
+
+	/* First, make a shallow copy to be able to read the inner pointers */
+	if (copy_from_user(&shallow_copy, src, sizeof(shallow_copy)) != 0)
+		return -EINVAL;
+
+	/* Verify the user input size. */
+	if (shallow_copy.request.iov_len > NSM_REQUEST_MAX_SIZE)
+		return -EMSGSIZE;
+
+	/* Allocate kernel memory for the user request */
+	dst->request.iov_len = shallow_copy.request.iov_len;

drivers/misc/nsm.c line 113

Quoted 10 lines
drivers/misc/nsm.c new file +10 -0
+	dst->request.iov_base = kmalloc(dst->request.iov_len, GFP_KERNEL);
+	if (!dst->request.iov_base)
+		return -ENOMEM;
+
+	/* Copy the request content */
+	if (copy_from_user(dst->request.iov_base,
+		shallow_copy.request.iov_base, dst->request.iov_len) != 0) {
+		kfree(dst->request.iov_base);
+		return -EFAULT;
+	}

It looks like the ioctl interface just provides an interface for passing through raw messages, which is often not the best idea. Are you able to enumerate the possible request types and provide a separate ioctl for each one?

drivers/misc/nsm.c line 320

Quoted 6 lines
drivers/misc/nsm.c new file +6 -0
+/* Supported driver operations */
+static const struct file_operations nsm_dev_fops = {
+	.open = nsm_dev_file_open,
+	.release = nsm_dev_file_close,
+	.unlocked_ioctl = nsm_dev_ioctl,
+};

This breaks on 32-bit userspace, which would need a separate .compat_ioctl handler with the current command definition. It's often better to define the ioctl interface to be the same on 32-bit and 64-bit userspace and then use the trivial compat_ptr_ioctl wrapper.

drivers/misc/nsm.c line 328

Quoted 7 lines
drivers/misc/nsm.c new file +7 -0
+/* Driver configuration */
+static struct miscdevice nsm_driver_miscdevice = {
+	.minor	= MISC_DYNAMIC_MINOR,
+	.name	= NSM_DEV_NAME,
+	.fops	= &nsm_dev_fops,
+	.mode	= 0666
+};

I would suggest expanding NSM_DEV_NAME here, it's much easier to grep for the actual string if a user wants to know which driver is responsible. Probably even less code.

drivers/misc/nsm.c line 368

Quoted 6 lines
drivers/misc/nsm.c new file +6 -0
+	if (nsm_hwrng)
+		nsm_hwrng->probe(vdev);
+
+	pr_debug("NSM device has been probed.\n");
+	return 0;
+}

The debug statements can probably get removed, especially the whitespace damaged ones.

drivers/misc/nsm.c line 396

Quoted 12 lines
drivers/misc/nsm.c new file +12 -0
+int nsm_register_hwrng(struct nsm_hwrng *_nsm_hwrng)
+{
+	if (nsm_hwrng)
+		return -EEXIST;
+
+	nsm_hwrng = _nsm_hwrng;
+	if (nsm_vdev)
+		nsm_hwrng->probe(nsm_vdev);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nsm_register_hwrng);

This should get easier of you reverse the dependency between the two drivers and just call into the nsm_hwrng_probe() function from the main driver's probe.

drivers/misc/nsm.c line 427

Quoted 2 lines
drivers/misc/nsm.c new file +2 -0
+	mutex_init(&nsm_lock);
+	init_waitqueue_head(&nsm_waitqueue);

You can simply use DEFINE_MUTEX() and DECLARE_WAIT_QUEUE_HEAD() if you still need the global objects (rather than making them per device).

Quoted 7 lines
drivers/misc/nsm.c new file +7 -0
+
+	rc = register_virtio_driver(&virtio_nsm_driver);
+	if (rc)
+		pr_err("NSM driver initialization error: %d.\n", rc);
+
+	return rc;
+}

drivers/misc/nsm.c line 444

Quoted 10 lines
drivers/misc/nsm.c new file +10 -0
+
+static void __exit nsm_driver_exit(void)
+{
+	unregister_virtio_driver(&virtio_nsm_driver);
+	mutex_destroy(&nsm_lock);
+	pr_debug("NSM driver exited.\n");
+}
+
+module_init(nsm_driver_init);
+module_exit(nsm_driver_exit);

Then this can use module_virtio_driver()

Arnd

author maintainer
#3 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

Hi Arnd!

Quoted 13 lines · Arnd Bergmann
Quoted 12 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "NSM". This device has 2 main functions:

1) Provide attestation reports
2) Modify PCR state
3) Provide entropy

This patch adds the core NSM driver that exposes a /dev/nsm device node which user space can use to request attestation documents and influence PCR states. A follow up patch will add a hwrng driver to feed its entropy into the kernel.

Quoted 5 lines
Quoted 2 lines

Hi Alex,

I've taken a first look at this driver and have some minor comments.

Thanks a bunch!

Quoted 3 lines

The main point here is that I think we need to look at possible alternatives for the user space interface, and (if possible) change to a set of higher-level ioctl commands from the simple passthrough.

I'm slightly torn on that bit. I think in hindsight the NSM device probably should have been a reserved vsock CID and the hwrng one should have just been virtio-rng.

The problem is that Nitro Enclaves were launched in 2020 and since an ecosystem developed in multiple languages to support building code inside:

https://github.com/aws/aws-nitro-enclaves-nsm-api/blob/main/src/driver/mod.rs#L66
https://github.com/donkersgoed/aws-nsm-interface/blob/main/aws_nsm_interface/__init__.py#L264-L274
  https://github.com/hf/nsm/blob/main/nsm.go#L99-L129

All of these use the (downstream) ioctl that this patch also implements. We could change it, but instead of making it easier for user space to adapt the device node, it would probably hurt more.

I agree that this is not a great place to be in. This driver absolutely should have been upstreamed 3 years ago. But I can't turn back time (yet) :).

Quoted 20 lines
Quoted 20 lines
drivers/misc/nsm.c new file +19 -0
+/* Virtio MMIO device definition */
+struct virtio_mmio_device {
+     struct virtio_device vdev;
+     struct platform_device *pdev;
+
+     void __iomem *base;
+     unsigned long version;
+
+     /* a list of queues so we can dispatch IRQs */
+     spinlock_t lock;
+     struct list_head virtqueues;
+};
+
+/* Virtqueue list entry */
+struct virtio_mmio_vq_info {
+     /* The actual virtqueue */
+     struct virtqueue *vq;
+
+     /* The list node for the virtqueues list */
Quoted 8 lines

drivers/misc/nsm.c line 60

Quoted 2 lines
drivers/misc/nsm.c new file +2 -0
+     struct list_head node;
+};

It looks like you are duplicating these structures from the virtio_mmio.c file, which seems like a bad idea for a number of reasons. What is it that you actually need that the virtio subsystem does not provide? Can you add interfaces to the common code instead?

Thanks for catching this. There are proper interfaces to get the virt queues already, let me use them instead.

Quoted 9 lines

drivers/misc/nsm.c line 66

Quoted 6 lines
drivers/misc/nsm.c new file +5 -0
+static struct virtio_device *nsm_vdev;
+static struct nsm_hwrng *nsm_hwrng;
+static struct mutex nsm_lock;
+static wait_queue_head_t nsm_waitqueue;
+static bool nsm_device_notified;

Instead of global structures, these should ideally all be part of a per-device structure, even if you are sure that there is only ever one of these devices.

Let me give that a try :)

Quoted 30 lines
Quoted 30 lines
drivers/misc/nsm.c new file +29 -0
+/* Copy an entire message from user-space to kernel-space */
+static int message_memdup_from_user(struct nsm_kernel_message *dst,
+     struct nsm_message *src)
+{
+     struct nsm_message shallow_copy;
+
+     if (!src || !dst)
+             return -EINVAL;
+
+     /* The destination's request and response buffers should be NULL. */
+     if (dst->request.iov_base || dst->response.iov_base)
+             return -EINVAL;
+
+     /* First, make a shallow copy to be able to read the inner pointers */
+     if (copy_from_user(&shallow_copy, src, sizeof(shallow_copy)) != 0)
+             return -EINVAL;
+
+     /* Verify the user input size. */
+     if (shallow_copy.request.iov_len > NSM_REQUEST_MAX_SIZE)
+             return -EMSGSIZE;
+
+     /* Allocate kernel memory for the user request */
+     dst->request.iov_len = shallow_copy.request.iov_len;
+     dst->request.iov_base = kmalloc(dst->request.iov_len, GFP_KERNEL);
+     if (!dst->request.iov_base)
+             return -ENOMEM;
+
+     /* Copy the request content */
+     if (copy_from_user(dst->request.iov_base,
Quoted 8 lines

drivers/misc/nsm.c line 113

Quoted 4 lines
drivers/misc/nsm.c new file +4 -0
+             shallow_copy.request.iov_base, dst->request.iov_len) != 0) {
+             kfree(dst->request.iov_base);
+             return -EFAULT;
+     }

It looks like the ioctl interface just provides an interface for passing through raw messages, which is often not the best idea. Are you able to enumerate the possible request types and provide a separate ioctl for each one?

See above. I could, but I think it would not improve the situation. It would also require a significant amount of CBOR parsing in the kernel which I'd rather avoid :).

Quoted 32 lines

drivers/misc/nsm.c line 320

Quoted 7 lines
drivers/misc/nsm.c new file +6 -0
+/* Supported driver operations */
+static const struct file_operations nsm_dev_fops = {
+     .open = nsm_dev_file_open,
+     .release = nsm_dev_file_close,
+     .unlocked_ioctl = nsm_dev_ioctl,
+};

This breaks on 32-bit userspace, which would need a separate .compat_ioctl handler with the current command definition. It's often better to define the ioctl interface to be the same on 32-bit and 64-bit userspace and then use the trivial compat_ptr_ioctl wrapper.

drivers/misc/nsm.c line 328

Quoted 7 lines
drivers/misc/nsm.c new file +7 -0
+/* Driver configuration */
+static struct miscdevice nsm_driver_miscdevice = {
+     .minor  = MISC_DYNAMIC_MINOR,
+     .name   = NSM_DEV_NAME,
+     .fops   = &nsm_dev_fops,
+     .mode   = 0666
+};

I would suggest expanding NSM_DEV_NAME here, it's much easier to grep for the actual string if a user wants to know which driver is responsible. Probably even less code.

drivers/misc/nsm.c line 368

Quoted 6 lines
drivers/misc/nsm.c new file +6 -0
+     if (nsm_hwrng)
+             nsm_hwrng->probe(vdev);
+
+     pr_debug("NSM device has been probed.\n");
+     return 0;
+}

The debug statements can probably get removed, especially the whitespace damaged ones.

Quoted 15 lines

drivers/misc/nsm.c line 396

Quoted 12 lines
drivers/misc/nsm.c new file +12 -0
+int nsm_register_hwrng(struct nsm_hwrng *_nsm_hwrng)
+{
+     if (nsm_hwrng)
+             return -EEXIST;
+
+     nsm_hwrng = _nsm_hwrng;
+     if (nsm_vdev)
+             nsm_hwrng->probe(nsm_vdev);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(nsm_register_hwrng);

This should get easier of you reverse the dependency between the two drivers and just call into the nsm_hwrng_probe() function from the main driver's probe.

I don't understand what you mean by reversing the dependency. Nsm_rng is a downstream of Nsm, because Nsm is the virtio device that owns the channel.

Quoted 17 lines

drivers/misc/nsm.c line 427

Quoted 3 lines
drivers/misc/nsm.c new file +2 -0
+     mutex_init(&nsm_lock);
+     init_waitqueue_head(&nsm_waitqueue);

You can simply use DEFINE_MUTEX() and DECLARE_WAIT_QUEUE_HEAD() if you still need the global objects (rather than making them per device).

Quoted 10 lines
drivers/misc/nsm.c new file +10 -0
+
+     rc = register_virtio_driver(&virtio_nsm_driver);
+     if (rc)
+             pr_err("NSM driver initialization error: %d.\n", rc);
+
+     return rc;
+}
+
+static void __exit nsm_driver_exit(void)
+{
Quoted 8 lines

drivers/misc/nsm.c line 444

Quoted 7 lines
drivers/misc/nsm.c new file +7 -0
+     unregister_virtio_driver(&virtio_nsm_driver);
+     mutex_destroy(&nsm_lock);
+     pr_debug("NSM driver exited.\n");
+}
+
+module_init(nsm_driver_init);
+module_exit(nsm_driver_exit);

Then this can use module_virtio_driver()

Definitely!

Thanks :). I'll have some typing to do.

Alex

Quoted 2 lines

Arnd

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#4 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

Quoted 43 lines · Alexander Graf

Hi Arnd!

Quoted 13 lines · Arnd Bergmann
Quoted 12 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "NSM". This device has 2 main functions:

1) Provide attestation reports
2) Modify PCR state
3) Provide entropy

This patch adds the core NSM driver that exposes a /dev/nsm device node which user space can use to request attestation documents and influence PCR states. A follow up patch will add a hwrng driver to feed its entropy into the kernel.

Quoted 5 lines
Quoted 2 lines

Hi Alex,

I've taken a first look at this driver and have some minor comments.

Thanks a bunch!

Quoted 3 lines

The main point here is that I think we need to look at possible alternatives for the user space interface, and (if possible) change to a set of higher-level ioctl commands from the simple passthrough.

I'm slightly torn on that bit. I think in hindsight the NSM device probably should have been a reserved vsock CID and the hwrng one should have just been virtio-rng.

The problem is that Nitro Enclaves were launched in 2020 and since an ecosystem developed in multiple languages to support building code inside:

https://github.com/aws/aws-nitro-enclaves-nsm-api/blob/main/src/driver/mod.rs#L66
https://github.com/donkersgoed/aws-nsm-interface/blob/main/aws_nsm_interface/__init__.py#L264-L274
  https://github.com/hf/nsm/blob/main/nsm.go#L99-L129

Quoted 7 lines

All of these use the (downstream) ioctl that this patch also implements. We could change it, but instead of making it easier for user space to adapt the device node, it would probably hurt more.

I agree that this is not a great place to be in. This driver absolutely should have been upstreamed 3 years ago. But I can't turn back time (yet) :).

As you know, this is no excuse to put an api in the kernel that isn't correct or good for the long-term. Just because people do foolish things outside of the kernel tree never means we have to accept them in our tree. Instead we can ask them to fix them properly as part of us taking the code.

So please, work on doing this right.

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

Hey Greg,

Quoted 49 lines · Greg Kroah-Hartman
Quoted 41 lines · Alexander Graf

Hi Arnd!

Quoted 13 lines · Arnd Bergmann
Quoted 12 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "NSM". This device has 2 main functions:

1) Provide attestation reports
2) Modify PCR state
3) Provide entropy

This patch adds the core NSM driver that exposes a /dev/nsm device node which user space can use to request attestation documents and influence PCR states. A follow up patch will add a hwrng driver to feed its entropy into the kernel.

Quoted 5 lines
Quoted 2 lines

Hi Alex,

I've taken a first look at this driver and have some minor comments.

Thanks a bunch!

Quoted 3 lines

The main point here is that I think we need to look at possible alternatives for the user space interface, and (if possible) change to a set of higher-level ioctl commands from the simple passthrough.

I'm slightly torn on that bit. I think in hindsight the NSM device probably should have been a reserved vsock CID and the hwrng one should have just been virtio-rng.

The problem is that Nitro Enclaves were launched in 2020 and since an ecosystem developed in multiple languages to support building code inside:

https://github.com/aws/aws-nitro-enclaves-nsm-api/blob/main/src/driver/mod.rs#L66
https://github.com/donkersgoed/aws-nsm-interface/blob/main/aws_nsm_interface/__init__.py#L264-L274
https://github.com/hf/nsm/blob/main/nsm.go#L99-L129

Quoted 7 lines

All of these use the (downstream) ioctl that this patch also implements. We could change it, but instead of making it easier for user space to adapt the device node, it would probably hurt more.

I agree that this is not a great place to be in. This driver absolutely should have been upstreamed 3 years ago. But I can't turn back time (yet) :).

Quoted 7 lines

As you know, this is no excuse to put an api in the kernel that isn't correct or good for the long-term. Just because people do foolish things outside of the kernel tree never means we have to accept them in our tree. Instead we can ask them to fix them properly as part of us taking the code.

So please, work on doing this right.

Sorry if my message above came over as a push to put an "incorrect api" into the kernel.

In situations like this where you can either give user space full access to the device's command space through a generic API or you can create command awareness in the kernel and make it the kernel's task to learn about each command, IMHO it's never a clear cut on which one is better. Especially in virtual environments where the set of commands can change quickly over time.

So what I was trying to say above is that *if* we consider both paths equally viable, I'd err on the one that enables the existing ecosystem. However if there are good reasons to not do command pass-through, I'm all for abstracting it away :)

Looking at prior art, the most similar implementations to this are TPMs and virtio-vsock. With virtio-vsock, kernel space has no idea what it talks to on the other hand and makes it 100% user space's problem. With TPMs, you typically use /dev/tpm0 to gain raw command access to the target device. So while we could engineer something smarter here, I'm not convinced yet it's a net win.

Alex

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

maintainer
#6 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

Quoted 35 lines · Alexander Graf
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Alexander Graf

On 29.09.23 19:28, Arnd Bergmann wrote:

All of these use the (downstream) ioctl that this patch also implements. We could change it, but instead of making it easier for user space to adapt the device node, it would probably hurt more.

I agree that this is not a great place to be in. This driver absolutely should have been upstreamed 3 years ago. But I can't turn back time (yet) :).

Quoted 7 lines

As you know, this is no excuse to put an api in the kernel that isn't correct or good for the long-term. Just because people do foolish things outside of the kernel tree never means we have to accept them in our tree. Instead we can ask them to fix them properly as part of us taking the code.

So please, work on doing this right.

Sorry if my message above came over as a push to put an "incorrect api" into the kernel.

In situations like this where you can either give user space full access to the device's command space through a generic API or you can create command awareness in the kernel and make it the kernel's task to learn about each command, IMHO it's never a clear cut on which one is better. Especially in virtual environments where the set of commands can change quickly over time.

So what I was trying to say above is that *if* we consider both paths equally viable, I'd err on the one that enables the existing ecosystem. However if there are good reasons to not do command pass-through, I'm all for abstracting it away :)

Quoted 6 lines

Looking at prior art, the most similar implementations to this are TPMs and virtio-vsock. With virtio-vsock, kernel space has no idea what it talks to on the other hand and makes it 100% user space's problem. With TPMs, you typically use /dev/tpm0 to gain raw command access to the target device. So while we could engineer something smarter here, I'm not convinced yet it's a net win.

Generally speaking, I can see a number of advantages to using an in-kernel abstraction:

- if there are both in-kernel and userspace API users, or multiple concurrent userspace clients, an abstraction layer helps to serialize between any stateful commands.

- in an abstract interface, the kernel can enforce command specific permission checks, rather than allowing access either to all or none of the commands.

- having the actual commands created by the kernel means that a bug in the virtio device implementation parsing the commands is less likely to be exploitable from user space.

- An explicit set of defined ioctl commands is easier to review and audit for kernel developers as we try to ensure that this is a sensible kernel interface

I don't know enough about your use cases or the specific command set to tell if any of those points actually matter here. In the python implementation you linked to, there are only a handful of commands that actually get passed through. It should be fairly easy to prototype a kernel driver that implements them as individual ioctl commands, to give us a better idea of how this compares to the current code.

Arnd

maintainer
#7 lore raw

Re: [PATCH v2 1/2] misc: Add Nitro Secure Module driver

In reply to Alexander Graf
Quoted 21 lines · Alexander Graf
Quoted 18 lines · Arnd Bergmann

drivers/misc/nsm.c line 396

Quoted 12 lines · Alexander Graf
drivers/misc/nsm.c new file +12 -0
+int nsm_register_hwrng(struct nsm_hwrng *_nsm_hwrng)
+{
+     if (nsm_hwrng)
+             return -EEXIST;
+
+     nsm_hwrng = _nsm_hwrng;
+     if (nsm_vdev)
+             nsm_hwrng->probe(nsm_vdev);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(nsm_register_hwrng);

This should get easier of you reverse the dependency between the two drivers and just call into the nsm_hwrng_probe() function from the main driver's probe.

Quoted 2 lines

I don't understand what you mean by reversing the dependency. Nsm_rng is a downstream of Nsm, because Nsm is the virtio device that owns the channel.

To clarify: I don't really consider Nsm_rng as a downstream driver, since the main driver does not have a bus model or device detection below it that would allow probing any function specific drivers.

I would remove the dynamic registration entirely and just merge both parts into a single file that happens to expose an rng interface.

Arnd