Skip to contentSkip to conversation

misc: Add Nitro Secure Module driver

PATCH In review Alexander Graf 4 participants 8 messages Oct 11, 2023 – Feb 22, 2024

Subsystems (3)
lore
Share & tools
lore

Alexander Graf

author maintainer
#0 lore raw

PATCH v7 1/1 misc: Add Nitro Secure Module driver

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

---

v1 -> v2:

- Remove boilerplate
- Add uapi header

v2 -> v3:

- Move globals to device struct
- Add compat handling
- Simplify some naming
- Remove debug prints
- Use module_virtio_driver
- Drop use of uio.h

v3 -> v4:

- Merge hwrng into the misc driver
- Add dependency on CBOR library
- Add internal and ioctl logic for all current NSM actions
- Use in-struct arrays instead of kvecs
- Add sysfs entries for NSM metadata
- Use dev_ print and devm_ allocation helpers

v4 -> v5:

- Remove CBOR parsing and generation again
- Remove support for any non-raw ioctls

v5 -> v6:

- Change devm callers to normal ones when devm logic is not needed
- Remove double free
- Remove superfluous list_head

v6 -> v7:

- Use big endian number helpers for cbor arrays length parsing
- Replace waitqueue with completion
- Move transfer buffer into struct nsm
- Remove useless file ops for open/close

---
 MAINTAINERS              |   9 +
 drivers/misc/Kconfig     |  13 +
 drivers/misc/Makefile    |   1 +
 drivers/misc/nsm.c       | 506 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/nsm.h |  31 +++
 5 files changed, 560 insertions(+)
 create mode 100644 drivers/misc/nsm.c
 create mode 100644 include/uapi/linux/nsm.h
MAINTAINERS +9 -0
index 6c4cce45a09d..d7afb3dedbd2 100644
@@ -15096,6 +15096,15 @@ 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/uapi/linux/nsm.h
+
 NOHZ, DYNTICKS SUPPORT
 M:	Frederic Weisbecker <[email protected]>
 M:	Thomas Gleixner <[email protected]>
drivers/misc/Kconfig +13 -0
index cadd4a820c03..236f36a8e8d4 100644
@@ -562,6 +562,19 @@ 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
+	select HW_RANDOM
+	select CBOR
+	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 +506 -0

506 lines not on this page · raw message

include/uapi/linux/nsm.h new file +31 -0
new file mode 100644
index 000000000000..e529f232f6c0
@@ -0,0 +1,31 @@
+/* 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/ioctl.h>
+#include <linux/types.h>
+
+#define NSM_MAGIC		0x0A
+
+#define NSM_REQUEST_MAX_SIZE	0x1000
+#define NSM_RESPONSE_MAX_SIZE	0x3000
+
+struct nsm_iovec {
+	__u64 addr; /* Virtual address of target buffer */
+	__u64 len;  /* Length of target buffer */
+};
+
+/* Raw NSM message. Only available with CAP_SYS_ADMIN. */
+struct nsm_raw {
+	/* Request from user */
+	struct nsm_iovec request;
+	/* Response to user */
+	struct nsm_iovec response;
+};
+#define NSM_IOCTL_RAW		_IOWR(NSM_MAGIC, 0x0, struct nsm_raw)
+
+#endif /* __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
#1 lore raw
Quoted 8 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

Quoted 8 lines

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Ping for inclusion? I haven't seen any further comments on v7 of this patch, so I'd assume it's good to go? :)

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

#2 lore raw
Quoted 15 lines · Alexander Graf
Quoted 14 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Quoted 6 lines
Quoted 2 lines

Ping for inclusion? I haven't seen any further comments on v7 of this patch, so I'd assume it's good to go? :)

Ah, I thought there would be a new version for some reason, sorry about that. I'll review it after -rc1 is out, in the middle of the merge window right now, I can't add anything to my trees, but new drivers can always be added after that.

thanks,

greg k-h

maintainer Reviewed-by
#3 lore raw
Quoted 20 lines · Greg Kroah-Hartman
Quoted 19 lines · Alexander Graf
Quoted 16 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Quoted 7 lines
Quoted 2 lines

Ping for inclusion? I haven't seen any further comments on v7 of this patch, so I'd assume it's good to go? :)

Ah, I thought there would be a new version for some reason, sorry about that. I'll review it after -rc1 is out, in the middle of the merge window right now, I can't add anything to my trees, but new drivers can always be added after that.

FWIW, all my comments have been addressed in this version. I'm not exactly happy with the design, but none of the alternatives we looked at were better, so I guess merging this version is better than having it maintained out of tree.

#4 lore raw
In reply to Alexander Graf
Quoted 8 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

Quoted 8 lines

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Alex are you going to publish the spec patch for this device? Important so we don't need to guess at behaviour when e.g. making changes to virtio APIs. Also, which tree do you want this to go through?

Signature · 1 line
MST
author maintainer
#5 lore raw

Hey Michael,

Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Quoted 5 lines
Quoted 2 lines

Alex are you going to publish the spec patch for this device? Important so we don't need to guess at behaviour when e.g. making changes to virtio APIs. Also, which tree do you want this to go through?

The spec patch including ping mail are sitting on the virtio-comments mailing list since October. I haven't seen any reply unfortunately :(

https://lore.kernel.org/virtio-comment/[email protected]/

Happy to read feedback if you have any :).

This patch here is already applied in Greg's misc tree which I'm happy to have it trickle to Linus through.

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

#6 lore raw
Quoted 25 lines · Alexander Graf

Hey Michael,

Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "Nitro Security Module" (NSM). This device has 3 main functions:

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

This patch adds a driver for NSM that exposes a /dev/nsm device node which user space can issue an ioctl on this device with raw NSM CBOR formatted commands to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend.

Quoted 5 lines
Quoted 2 lines

Alex are you going to publish the spec patch for this device? Important so we don't need to guess at behaviour when e.g. making changes to virtio APIs. Also, which tree do you want this to go through?

Quoted 6 lines

The spec patch including ping mail are sitting on the virtio-comments mailing list since October. I haven't seen any reply unfortunately :(

https://lore.kernel.org/virtio-comment/[email protected]/

Happy to read feedback if you have any :).

Oh I forgot.
Now that I've read the driver, I actually have some :) I think there's an assumption that there's a request buffer and response buffer queued by the driver, and that the device always first consumes the request buffer followed by consuming the response buffer.
If that is right then driver is ok but spec needs clarification, will note on virtio-comment.

Quoted 18 lines

This patch here is already applied in Greg's misc tree which I'm happy to have it trickle to Linus through.

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

#7 lore raw
In reply to Alexander Graf
Quoted 5 lines · Alexander Graf

This patch here is already applied in Greg's misc tree which I'm happy to have it trickle to Linus through.

Alex

Kernel test bot was unhappy with this patch btw. Know why?

Signature · 1 line
MST