misc: Add Nitro Secure Module driver
Subsystems (3)
Alexander Graf
maintainerPATCH v6 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
--- MAINTAINERS | 9 + drivers/misc/Kconfig | 13 + drivers/misc/Makefile | 1 + drivers/misc/nsm.c | 572 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/nsm.h | 31 +++ 5 files changed, 626 insertions(+) create mode 100644 drivers/misc/nsm.c create mode 100644 include/uapi/linux/nsm.h
MAINTAINERS +9 -0
@@ -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
@@ -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
@@ -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 +572 -0
572 lines not on this page · raw message
include/uapi/linux/nsm.h new file +31 -0
@@ -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
Arnd Bergmann
maintainerQuoted 5 lines · Alexander Graf
This patch adds a driver for NSM that exposes a
/dev/nsmdevice 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.
I haven't had a chance to actually read the v3 submission in enough detail, but assuming we're going to go with the simple pass-through interface here, I've looked for some mostly cosmetic improvements that you may want to integrate.
drivers/misc/nsm.c line 37
Quoted 9 lines
drivers/misc/nsm.c new file +9
-0+/* Timeout for NSM virtqueue respose in milliseconds. */ +#define NSM_DEFAULT_TIMEOUT_MSECS (120000)/* 2 minutes */ + +struct nsm { +struct virtio_device *vdev; +struct virtqueue *vq; +struct mutex lock; +wait_queue_head_t wq; +bool device_notified;
Instead of a manual wait queue plus a bool, using a 'struct completion' simplifies this a little bit.
drivers/misc/nsm.c line 59
Quoted 12 lines
drivers/misc/nsm.c new file +11
-0+ +/* Maximum length input data */ +struct nsm_data_req { +__u32 len; +__u8 data[NSM_REQUEST_MAX_SIZE ]; +}; +/* Maximum length output data */ +struct nsm_data_resp { +__u32 len; +__u8 data[NSM_RESPONSE_MAX_SIZE ]; +};
You have endian-conversion for some of the data fields but not the 'len field here, I guess these should be __le32 instead of __u32, with the appropriate le32_to_cpu() and cpu_to_le32() conversion when passing the native u32 word from userspace.
It does seem odd that you have a little-endian length field here, but big-endian length fields inside of the cbor data.
drivers/misc/nsm.c line 97
Quoted 4 lines
drivers/misc/nsm.c new file +4
-0+#define CBOR_HEADER_SIZE_U8 (CBOR_HEADER_SIZE_SHORT + sizeof(u8)) +#define CBOR_HEADER_SIZE_U16 (CBOR_HEADER_SIZE_SHORT + sizeof(u16)) +#define CBOR_HEADER_SIZE_U32 (CBOR_HEADER_SIZE_SHORT + sizeof(u32)) +#define CBOR_HEADER_SIZE_U64 (CBOR_HEADER_SIZE_SHORT + sizeof(u64))
Similarly, I guess these should be __be16/__be32/__be64?
Quoted 11 lines
drivers/misc/nsm.c new file +11
-0+ }else if (cbor_short_size ==CBOR_LONG_SIZE_U32 ) { +if (cbor_object_size <CBOR_HEADER_SIZE_U32 ) +return -EFAULT ; +/* 4 bytes */ + array_len = cbor_object[1 ] <<24 | + cbor_object[2 ] <<16 | + cbor_object[3 ] <<8 | + cbor_object[4 ]; + array_offset =CBOR_HEADER_SIZE_U32 ; + }else if (cbor_short_size ==CBOR_LONG_SIZE_U64 ) { +if (cbor_object_size <CBOR_HEADER_SIZE_U64 )
drivers/misc/nsm.c line 165
Quoted 10 lines
drivers/misc/nsm.c new file +10
-0+return -EFAULT ; +/* 8 bytes */ + array_len = (u64 ) cbor_object[1 ] <<56 | + (u64 ) cbor_object[2 ] <<48 | + (u64 ) cbor_object[3 ] <<40 | + (u64 ) cbor_object[4 ] <<32 | + (u64 ) cbor_object[5 ] <<24 | + (u64 ) cbor_object[6 ] <<16 | + (u64 ) cbor_object[7 ] <<8 | + (u64 ) cbor_object[8 ];
These could use be{!6,32,64}_to_cpup() for clarity.
Quoted 17 lines
+static int
nsm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)drivers/misc/nsm.c new file +15
-0+{ +struct nsm *nsm =hwrng_to_nsm (rng); +struct device *dev = &nsm->vdev->dev; +struct nsm_msg *msg; +int rc =0 ; + +/* NSM always needs to wait for a response */ +if (!wait) +return 0 ; + + msg =kzalloc (sizeof (*msg),GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + rc =fill_req_get_random (nsm, &msg->req);
drivers/misc/nsm.c line 393
Quoted 10 lines
drivers/misc/nsm.c new file +10
-0+if (rc !=0 ) + goto out; + + rc =nsm_sendrecv_msg (nsm, msg); +if (rc !=0 ) + goto out; + + rc =parse_resp_get_random (nsm, &msg->resp, data, max); +if (rc <0 ) + goto out;
It looks like the bulk of this function happens inside of nsm_sendrecv_msg(), which uses a mutex for serialization.
In this case, I think you can replace the dynamic allocation during read() with a preallocated buffer in the device that always gets used here, after you extend the mutex out to the entire fill_req_get_random()/nsm_sendrecv_msg()/parse_resp_get_random() block.
Quoted 30 lines
drivers/misc/nsm.c new file +30
-0+static long nsm_dev_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +void __user *argp =u64_to_user_ptr ((u64 )arg); +struct nsm *nsm =file_to_nsm (file); +struct nsm_msg *msg; +struct nsm_raw raw; +int r =0 ; + +if (cmd !=NSM_IOCTL_RAW ) +return -EINVAL ; + +if (_IOC_SIZE (cmd) !=sizeof (raw)) +return -EINVAL ; + +/* Allocate message buffers to device */ + r = -ENOMEM ; + msg =kzalloc (sizeof (*msg),GFP_KERNEL ); +if (!msg) + goto out; + +/* Copy user argument struct to kernel argument struct */ + r = -EFAULT ; +if (copy_from_user (&raw, argp,_IOC_SIZE (cmd))) + goto out; + +/* Convert kernel argument struct to device request */ + r =fill_req_raw (nsm, &msg->req, &raw); +if (r) + goto out;
drivers/misc/nsm.c line 440
Quoted 10 lines
drivers/misc/nsm.c new file +10
-0+ +/* Send message to NSM and read reply */ + r =nsm_sendrecv_msg (nsm, msg); +if (r) + goto out; + +/* Parse device response into kernel argument struct */ + r =parse_resp_raw (nsm, &msg->resp, &raw); +if (r) + goto out;
And the same is probably true here.
drivers/misc/nsm.c line 462
Quoted 9 lines
drivers/misc/nsm.c new file +9
-0+static int nsm_dev_file_open (struct inode *node,struct file *file) +{ +return 0 ; +} + +static int nsm_dev_file_close (struct inode *inode,struct file *file) +{ +return 0 ; +}
These are not needed if they don't do anything.
Arnd
Arnd Bergmann
maintainerQuoted 11 lines · Arnd Bergmann
drivers/misc/nsm.c line 59
Quoted 5 lines
drivers/misc/nsm.c new file +5
-0+/* Maximum length output data */ +struct nsm_data_resp { +__u32 len; +__u8 data[NSM_RESPONSE_MAX_SIZE ]; +};You have endian-conversion for some of the data fields but not the 'len field here, I guess these should be
__le32instead of__u32, with the appropriatele32_to_cpu()andcpu_to_le32()conversion when passing the native u32 word from userspace.
Ignore this one, I misread how the length field is used, this is obviously not fixed endian as it is only used for communicating with the kernel's virtio code, not the device.
Arnd