Skip to contentSkip to conversation

Add Nitro Secure Module support

PATCH Superseded Alexander Graf 5 participants 15 messages Oct 9, 2023 – Oct 13, 2023

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v4 1/2 Import CBOR library

In reply to Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

On top of the upstream code base, I added kernel module as well as kernel header path awareness and made checkpatch happy.

---

v3 -> v4:

- New in v4

---
 MAINTAINERS                  |   8 ++
 include/linux/cbor/base.h    |  94 ++++++++++++++
 include/linux/cbor/cbor.h    |  22 ++++
 include/linux/cbor/decoder.h |  42 ++++++
 include/linux/cbor/encoder.h |  48 +++++++
 include/linux/cbor/helper.h  |  41 ++++++
 include/linux/cbor/ieee754.h |  52 ++++++++
 include/linux/cbor/parser.h  |  32 +++++
 lib/Kconfig                  |   3 +
 lib/Makefile                 |   2 +
 lib/cbor/Makefile            |  12 ++
 lib/cbor/common.c            | 105 +++++++++++++++
 lib/cbor/decoder.c           | 170 ++++++++++++++++++++++++
 lib/cbor/encoder.c           | 218 +++++++++++++++++++++++++++++++
 lib/cbor/helper.c            | 175 +++++++++++++++++++++++++
 lib/cbor/ieee754.c           | 205 +++++++++++++++++++++++++++++
 lib/cbor/parser.c            | 243 +++++++++++++++++++++++++++++++++++
 17 files changed, 1472 insertions(+)
 create mode 100644 include/linux/cbor/base.h
 create mode 100644 include/linux/cbor/cbor.h
 create mode 100644 include/linux/cbor/decoder.h
 create mode 100644 include/linux/cbor/encoder.h
 create mode 100644 include/linux/cbor/helper.h
 create mode 100644 include/linux/cbor/ieee754.h
 create mode 100644 include/linux/cbor/parser.h
 create mode 100644 lib/cbor/Makefile
 create mode 100644 lib/cbor/common.c
 create mode 100644 lib/cbor/decoder.c
 create mode 100644 lib/cbor/encoder.c
 create mode 100644 lib/cbor/helper.c
 create mode 100644 lib/cbor/ieee754.c
 create mode 100644 lib/cbor/parser.c
MAINTAINERS +8 -0
index 6c4cce45a09d..5c23081ede95 100644
@@ -4661,6 +4661,14 @@ S:	Odd Fixes
 F:	Documentation/devicetree/bindings/arm/cavium-thunder2.txt
 F:	arch/arm64/boot/dts/cavium/thunder2-99xx*
 
+CBOR LIBRARY
+M:	Alexander Graf <[email protected]>
+L:	[email protected]
+L:	The AWS Nitro Enclaves Team <[email protected]>
+S:	Supported
+F:	include/linux/cbor/
+F:	lib/cbor/
+
 CBS/ETF/TAPRIO QDISCS
 M:	Vinicius Costa Gomes <[email protected]>
 L:	[email protected]
include/linux/cbor/base.h new file +94 -0

94 lines not on this page · raw message

include/linux/cbor/cbor.h new file +22 -0
new file mode 100644
index 000000000000..92d69ac7ae40
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * SPDX-FileCopyrightText: 2021 Kyunghwan Kwon <[email protected]>
+ */
+
+#ifndef CBOR_H
+#define CBOR_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#include <linux/cbor/parser.h>
+#include <linux/cbor/decoder.h>
+#include <linux/cbor/encoder.h>
+#include <linux/cbor/helper.h>
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* CBOR_H */
include/linux/cbor/decoder.h new file +42 -0

42 lines not on this page · raw message

include/linux/cbor/encoder.h new file +48 -0

48 lines not on this page · raw message

include/linux/cbor/helper.h new file +41 -0

41 lines not on this page · raw message

include/linux/cbor/ieee754.h new file +52 -0

52 lines not on this page · raw message

include/linux/cbor/parser.h new file +32 -0
new file mode 100644
index 000000000000..554004799f0a
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * SPDX-FileCopyrightText: 2021 Kyunghwan Kwon <[email protected]>
+ */
+
+#ifndef CBOR_PARSER_H
+#define CBOR_PARSER_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#include <linux/cbor/base.h>
+
+/**
+ * Parse the encoded CBOR messages into items.
+ *
+ * @param[in,out] reader reader context for the actual encoded message
+ * @param[in] msg CBOR encoded message
+ * @param[in] msgsize the @p msg size in bytes
+ * @param[out] nitems_parsed the number of items parsed gets stored if not null
+ *
+ * @return a code of @ref cbor_error_t
+ */
+cbor_error_t cbor_parse(cbor_reader_t *reader, void const *msg, size_t msgsize,
+		size_t *nitems_parsed);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* CBOR_PARSER_H */
lib/Kconfig +3 -0
index c686f4adc124..12fb3d68cd9a 100644
@@ -764,3 +764,6 @@ config ASN1_ENCODER
 
 config POLYNOMIAL
        tristate
+
+config CBOR
+	tristate
lib/Makefile +2 -0
index 740109b6e2c8..287f3dcaff31 100644
@@ -317,6 +317,8 @@ obj-$(CONFIG_ASN1_ENCODER) += asn1_encoder.o
 
 obj-$(CONFIG_FONT_SUPPORT) += fonts/
 
+obj-$(CONFIG_CBOR) += cbor/
+
 hostprogs	:= gen_crc32table
 hostprogs	+= gen_crc64table
 clean-files	:= crc32table.h
lib/cbor/Makefile new file +12 -0
new file mode 100644
index 000000000000..6b090ae0f4a8
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_CBOR) += cbor.o
+
+cbor-y := \
+	common.o \
+	decoder.o \
+	encoder.o \
+	helper.o \
+	parser.o \
+
+cbor-$(CONFIG_CBOR_FLOAT) += ieee754.o
lib/cbor/common.c new file +105 -0

105 lines not on this page · raw message

lib/cbor/decoder.c new file +170 -0

170 lines not on this page · raw message

lib/cbor/encoder.c new file +218 -0

218 lines not on this page · raw message

lib/cbor/helper.c new file +175 -0

175 lines not on this page · raw message

lib/cbor/ieee754.c new file +205 -0

205 lines not on this page · raw message

lib/cbor/parser.c new file +243 -0

243 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

Alexander Graf

author maintainer
#0 lore raw

v4 cover Add Nitro Secure Module support

Nitro Enclaves run Linux inside as well as outside the Enclave. Outside the Enclave, we already have the nitro_enclaves driver in upstream Linux which controls a Nitro Enclave's lifecycle.

Inside the Enclave, the environment looks like a typical Firecracker microvm. In addition to standard virtio devices, an Enclave also has an additional "Nitro Secure Module" (NSM) virtio device which so far was missing an upstream Linux driver. The NSM provides access to PCRs, an attestation document as well as entropy.

To support the NSM communication protocol, Linux needs to learn to generate and parse the Concise Binary Object Representation (CBOR) format. The first patch adds support for a CBOR library. The second adds the actual NSM driver.

With these patches in place, upstream Linux has everything that's needed to run inside a Nitro Enclave.

Alex

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
- Ensure remove only happens on target device
- Drop use of uio.h

v3 -> v4:

- Add CBOR library
- 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

Alexander Graf (2):
Import CBOR library
misc: Add Nitro Secure Module driver

 MAINTAINERS                  |   17 +
 drivers/misc/Kconfig         |   13 +
 drivers/misc/Makefile        |    1 +
 drivers/misc/nsm.c           | 1466 ++++++++++++++++++++++++++++++++++
 include/linux/cbor/base.h    |   94 +++
 include/linux/cbor/cbor.h    |   22 +
 include/linux/cbor/decoder.h |   42 +
 include/linux/cbor/encoder.h |   48 ++
 include/linux/cbor/helper.h  |   41 +
 include/linux/cbor/ieee754.h |   52 ++
 include/linux/cbor/parser.h  |   32 +
 include/uapi/linux/nsm.h     |  188 +++++
 lib/Kconfig                  |    3 +
 lib/Makefile                 |    2 +
 lib/cbor/Makefile            |   12 +
 lib/cbor/common.c            |  105 +++
 lib/cbor/decoder.c           |  170 ++++
 lib/cbor/encoder.c           |  218 +++++
 lib/cbor/helper.c            |  175 ++++
 lib/cbor/ieee754.c           |  205 +++++
 lib/cbor/parser.c            |  243 ++++++
 21 files changed, 3149 insertions(+)
 create mode 100644 drivers/misc/nsm.c
 create mode 100644 include/linux/cbor/base.h
 create mode 100644 include/linux/cbor/cbor.h
 create mode 100644 include/linux/cbor/decoder.h
 create mode 100644 include/linux/cbor/encoder.h
 create mode 100644 include/linux/cbor/helper.h
 create mode 100644 include/linux/cbor/ieee754.h
 create mode 100644 include/linux/cbor/parser.h
 create mode 100644 include/uapi/linux/nsm.h
 create mode 100644 lib/cbor/Makefile
 create mode 100644 lib/cbor/common.c
 create mode 100644 lib/cbor/decoder.c
 create mode 100644 lib/cbor/encoder.c
 create mode 100644 lib/cbor/helper.c
 create mode 100644 lib/cbor/ieee754.c
 create mode 100644 lib/cbor/parser.c
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
#12 lore raw

PATCH v4 2/2 misc: Add Nitro Secure Module driver

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "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 ioctls on to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend and exposes some of the NSM device metadata as sysfs entries.

---

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

---
 MAINTAINERS              |    9 +
 drivers/misc/Kconfig     |   13 +
 drivers/misc/Makefile    |    1 +
 drivers/misc/nsm.c       | 1466 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/nsm.h |  188 +++++
 5 files changed, 1677 insertions(+)
 create mode 100644 drivers/misc/nsm.c
 create mode 100644 include/uapi/linux/nsm.h
MAINTAINERS +9 -0
index 5c23081ede95..28f861eea8e5 100644
@@ -15104,6 +15104,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 +1466 -0

1,466 lines not on this page · raw message

include/uapi/linux/nsm.h new file +188 -0

188 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
#2 lore raw

Re: [PATCH v4 1/2] Import CBOR library

In reply to Alexander Graf
Quoted 4 lines · Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

What is "CBOR"? I don't see a description of it here.

And I guess you are going to keep this in sync with upstream? Or do you really need the full library here (you #ifdef the float stuff out), does your module really need all of the functionality and complexity of this library, or can it use just a much smaller one instead?

Quoted 2 lines

On top of the upstream code base, I added kernel module as well as kernel header path awareness and made checkpatch happy.

If only the one module needs this, why not put it in the directory for the module itself, and then when/if anyone else needs it, it could be moved?

thanks,

greg k-h

#13 lore raw

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

In reply to Alexander Graf
Quoted 32 lines · Alexander Graf

When running Linux inside a Nitro Enclave, the hypervisor provides a special virtio device called "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 ioctls on to request attestation documents, influence PCR states, read entropy and enumerate status of the device. In addition, the driver implements a hwrng backend and exposes some of the NSM device metadata as sysfs entries.

---

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

Quoted 7 lines

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

You forgot Documentation/ABI/ entries for your new sysfs files so we can see what exactly they are and if they are sane.

thanks,

greg k-h

author maintainer
#3 lore raw

Re: [PATCH v4 1/2] Import CBOR library

In reply to Greg Kroah-Hartman

Hey Greg,

Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

What is "CBOR"? I don't see a description of it here.

CBOR is the "Concise Binary Object Representation" (https://en.wikipedia.org/wiki/CBOR) binary format.

Quoted 5 lines

And I guess you are going to keep this in sync with upstream? Or do you really need the full library here (you #ifdef the float stuff out), does your module really need all of the functionality and complexity of this library, or can it use just a much smaller one instead?

CBOR knows a total of 9 data types:

  - Unsigned integers
  - Signed integers
  - Binary string
  - UTF-8 string
  - Arrays
  - Maps (like a python dictionary)
  - Semantic tag
  - Bools
  - Floats

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 6 lines
Quoted 3 lines

On top of the upstream code base, I added kernel module as well as kernel header path awareness and made checkpatch happy.

If only the one module needs this, why not put it in the directory for the module itself, and then when/if anyone else needs it, it could be moved?

That sounds like a great idea! Let me do that :)

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

#4 lore raw

Re: [PATCH v4 1/2] Import CBOR library

Quoted 34 lines · Alexander Graf

Hey Greg,

Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

What is "CBOR"? I don't see a description of it here.

CBOR is the "Concise Binary Object Representation"
(https://en.wikipedia.org/wiki/CBOR) binary format.

Quoted 5 lines

And I guess you are going to keep this in sync with upstream? Or do you really need the full library here (you #ifdef the float stuff out), does your module really need all of the functionality and complexity of this library, or can it use just a much smaller one instead?

CBOR knows a total of 9 data types:

  - Unsigned integers
  - Signed integers
  - Binary string
  - UTF-8 string
  - Arrays
  - Maps (like a python dictionary)
  - Semantic tag
  - Bools
  - Floats

Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATCH v4 1/2] Import CBOR library

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

Hey Greg,

Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

What is "CBOR"? I don't see a description of it here.

CBOR is the "Concise Binary Object Representation"
(https://en.wikipedia.org/wiki/CBOR) binary format.

Quoted 4 lines

And I guess you are going to keep this in sync with upstream? Or do you really need the full library here (you #ifdef the float stuff out), does your module really need all of the functionality and complexity of this library, or can it use just a much smaller one instead?

CBOR knows a total of 9 data types:

- Unsigned integers
- Signed integers
- Binary string
- UTF-8 string
- Arrays
- Maps (like a python dictionary)
- Semantic tag
- Bools
- Floats

Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

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

Re: [PATCH v4 1/2] Import CBOR library

Quoted 52 lines · Alexander Graf
Quoted 41 lines · Greg Kroah-Hartman
Quoted 31 lines · Alexander Graf

Hey Greg,

Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · Alexander Graf

To fully support the Nitro Secure Module communication protocol, we need to encode and decode CBOR binary data. Import an MIT licensed library from https://github.com/libmcu/cbor (commit f3d1696f886) so that we can easily consume CBOR data.

What is "CBOR"? I don't see a description of it here.

CBOR is the "Concise Binary Object Representation"
(https://en.wikipedia.org/wiki/CBOR) binary format.

Quoted 4 lines

And I guess you are going to keep this in sync with upstream? Or do you really need the full library here (you #ifdef the float stuff out), does your module really need all of the functionality and complexity of this library, or can it use just a much smaller one instead?

CBOR knows a total of 9 data types:

- Unsigned integers
- Signed integers
- Binary string
- UTF-8 string
- Arrays
- Maps (like a python dictionary)
- Semantic tag
- Bools
- Floats

Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

it's a tough call, let's see what a unified driver, no parser, looks like, that should be pretty simple and small so maybe stick with that?

thanks,

greg k-h

maintainer
#7 lore raw

Re: [PATCH v4 1/2] Import CBOR library

Quoted 31 lines · Greg Kroah-Hartman
Quoted 19 lines · Alexander Graf
Quoted 8 lines · Greg Kroah-Hartman
Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

Quoted 7 lines

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

I was definitely expecting something simpler than what was possible in the v4 patch. I had another look now, and it's clear that the ioctl interface is still not great because the variable data structures shine through for some of the calls, and even to get to this point, a whole lot of complexity is required underneath.

To get anything better, one would probably have to redesign the entire interface stack (hypervisor, kernel and userland) to use regular fixed data structures, and this seems unlikely to happen.

Arnd

#8 lore raw

Re: [PATCH v4 1/2] Import CBOR library

Quoted 46 lines · Arnd Bergmann
Quoted 31 lines · Greg Kroah-Hartman
Quoted 19 lines · Alexander Graf
Quoted 8 lines · Greg Kroah-Hartman
Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

Quoted 7 lines

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

I was definitely expecting something simpler than what was possible in the v4 patch. I had another look now, and it's clear that the ioctl interface is still not great because the variable data structures shine through for some of the calls, and even to get to this point, a whole lot of complexity is required underneath.

Quoted 3 lines

To get anything better, one would probably have to redesign the entire interface stack (hypervisor, kernel and userland) to use regular fixed data structures, and this seems unlikely to happen.

Why not fix this and do it properly? What's preventing that from happening? We don't want to create an interface here that is broken, or insecure, or a pain to maintain, right?

thanks,

greg k-h

author maintainer
#9 lore raw

Re: [PATCH v4 1/2] Import CBOR library

Quoted 44 lines · Greg Kroah-Hartman
Quoted 42 lines · Arnd Bergmann
Quoted 28 lines · Greg Kroah-Hartman
Quoted 18 lines · Alexander Graf
Quoted 8 lines · Greg Kroah-Hartman
Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

Quoted 7 lines

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

I was definitely expecting something simpler than what was possible in the v4 patch. I had another look now, and it's clear that the ioctl interface is still not great because the variable data structures shine through for some of the calls, and even to get to this point, a whole lot of complexity is required underneath.

Quoted 6 lines
Quoted 3 lines

To get anything better, one would probably have to redesign the entire interface stack (hypervisor, kernel and userland) to use regular fixed data structures, and this seems unlikely to happen.

Why not fix this and do it properly? What's preventing that from happening? We don't want to create an interface here that is broken, or insecure, or a pain to maintain, right?

The interface is neither broken, insecure nor a pain to maintain. It's merely not as pretty as could be. But it's there to stay because it's what the hypervisor has been exposing for the last 2 years.

I also frankly don't think that doing static structures is any more "proper" than what this interface is doing today. It's serialized data of variable length. That's just the nature of the beast - the backing data for some of these operations *is* dynamic.

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

#11 lore raw

Re: [PATCH v4 1/2] Import CBOR library

In reply to Petre Eftime
Quoted 78 lines · Petre Eftime
Quoted 52 lines · Greg Kroah-Hartman
Quoted 46 lines · Arnd Bergmann
Quoted 31 lines · Greg Kroah-Hartman
Quoted 19 lines · Alexander Graf
Quoted 8 lines · Greg Kroah-Hartman
Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

Quoted 7 lines

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

I was definitely expecting something simpler than what was possible in the v4 patch. I had another look now, and it's clear that the ioctl interface is still not great because the variable data structures shine through for some of the calls, and even to get to this point, a whole lot of complexity is required underneath.

Quoted 3 lines

To get anything better, one would probably have to redesign the entire interface stack (hypervisor, kernel and userland) to use regular fixed data structures, and this seems unlikely to happen.

Quoted 7 lines

Why not fix this and do it properly? What's preventing that from happening? We don't want to create an interface here that is broken, or insecure, or a pain to maintain, right?

thanks,

greg k-h

I would think the proposal to have fixed structures would be a downgrade in terms of maintainability, usability and security, not an improvement.

This current interface allows the hypervisor to extend the existing functionality at any time, and the Linux kernel does not have to change anything for that to work, the application does not have to be recompiled to use the new kernel headers at any point. Adding new APIs, adding new fields to API responses, or adding optional parameters to the API is fully backwards compatible, would also not require changes in userspace, as the CBOR data structures that are not recognized can simply be skipped. This allows easy backwards compatibility in most cases, and the userspace would be able to opt in to new features only if it requires them, without forcing the upgrade if it's not required.

Quoted 4 lines

With fixed structures, then the driver would need to be more explicitly versioned and would need to be able to handle multiple versions of the API at the same time, which is both more complex, less flexible and more prone to errors.

Yes, but you are trading off the complexity of adding yet-another-protocol-parser to the kernel vs. making a strict user/kernel api, right?

Which one is correct? Can you verify that this parser really is correct and doesn't have any overflows/security issues in it? I can't, maybe someone needs to write it in rust :)

thanks,

greg k-h

#10 lore raw

Re: [PATCH v4 1/2] Import CBOR library

In reply to Greg Kroah-Hartman
Quoted 52 lines · Greg Kroah-Hartman
Quoted 46 lines · Arnd Bergmann
Quoted 31 lines · Greg Kroah-Hartman
Quoted 19 lines · Alexander Graf
Quoted 8 lines · Greg Kroah-Hartman
Quoted 8 lines

Out of these, the NSM communication protocol uses all except Semantic tags and Floats. The CBOR library that this patch imports does not have special handling for Semantic tags, which leaves only floats which are already #ifdef'ed out. That means there is not much to trim.

What you see here is what's needed to parse CBOR in kernel - if that's what we want to do. I'm happy to rip it out again and make it a pure user space problem to do CBOR :).

Quoted 7 lines

Yes, why are we parsing this in the kernel? What could go wrong with adding yet-another-parser in privileged context? :)

Why does this have to be in the kernel, the data sent/recieved is over virtio, so why does the kernel have to parse it? I couldn't figure that out from the driver, yet the driver seems to have a lot of hard-coded parsing logic in it to assume specific message formats?

Quoted 4 lines

The parsing doesn't have to be in kernel and it probably shouldn't be either. V3 of the patch was punting all the parsing to user space, at which point you and Arnd said I should give it a try to do the protocol parsing in kernel space instead. That's why the parser is here.

Arnd said that, not me :)

Quoted 3 lines

If we conclude that all this in-kernel parsing is not worth it, I'm very happy to just go back to the the v3 ioctl interface and post v5 with hwrng merged into misc, but remove all CBOR logic again :)

Quoted 7 lines

I think the less parsers we have in the kernel, the safer we are for obvious reasons. Unless you have a parser for this in rust? :)

I don't really know, having a generic interface is good, but at the expense of this api is probably not good. individual ioctls might be better if there are not going to be any other drivers for this type of thing?

I was definitely expecting something simpler than what was possible in the v4 patch. I had another look now, and it's clear that the ioctl interface is still not great because the variable data structures shine through for some of the calls, and even to get to this point, a whole lot of complexity is required underneath.

Quoted 3 lines

To get anything better, one would probably have to redesign the entire interface stack (hypervisor, kernel and userland) to use regular fixed data structures, and this seems unlikely to happen.

Quoted 7 lines

Why not fix this and do it properly? What's preventing that from happening? We don't want to create an interface here that is broken, or insecure, or a pain to maintain, right?

thanks,

greg k-h

I would think the proposal to have fixed structures would be a downgrade in terms of maintainability, usability and security, not an improvement.

This current interface allows the hypervisor to extend the existing functionality at any time, and the Linux kernel does not have to change anything for that to work, the application does not have to be recompiled to use the new kernel headers at any point. Adding new APIs, adding new fields to API responses, or adding optional parameters to the API is fully backwards compatible, would also not require changes in userspace, as the CBOR data structures that are not recognized can simply be skipped. This allows easy backwards compatibility in most cases, and the userspace would be able to opt in to new features only if it requires them, without forcing the upgrade if it's not required.

With fixed structures, then the driver would need to be more explicitly versioned and would need to be able to handle multiple versions of the API at the same time, which is both more complex, less flexible and more prone to errors.

-- epetre

#14 lore raw

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

In reply to Alexander Graf
kernel test robot noticed the following build errors:

Hi Alexander,

kernel test robot noticed the following build errors:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus soc/for-next linus/master v6.6-rc5 next-20231012] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexander-Graf/Import-CBOR-library/20231010-052529
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20231009212053.2007-3-graf%40amazon.com
patch subject: [PATCH v4 2/2] misc: Add Nitro Secure Module driver
config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20231013/[email protected]/config)
compiler: powerpc64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231013/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/asm-generic/ioctl.h:5,

                    from arch/powerpc/include/uapi/asm/ioctl.h:12,
                    from include/uapi/linux/ioctl.h:5,
                    from include/uapi/linux/random.h:12,
                    from include/linux/random.h:10,
                    from include/linux/nodemask.h:97,
                    from include/linux/list_lru.h:12,
                    from include/linux/fs.h:13,
                    from drivers/misc/nsm.c:23:
>> include/uapi/asm-generic/ioctl.h:70:9: error: initializer element is not constant
      70 |         (((dir)  << _IOC_DIRSHIFT) | \
         |         ^
   include/uapi/asm-generic/ioctl.h:88:33: note: in expansion of macro '_IOC'
      88 | #define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
         |                                 ^~~~
   include/uapi/linux/nsm.h:172:33: note: in expansion of macro '_IOWR'
     172 | #define NSM_IOCTL_ATTESTATION   _IOWR(NSM_MAGIC, 0x6, union nsm_attestation)
         |                                 ^~~~~
   drivers/misc/nsm.c:1209:11: note: in expansion of macro 'NSM_IOCTL_ATTESTATION'
    1209 |         { NSM_IOCTL_ATTESTATION, _fill_req_attestation, _parse_resp_attestation },
         |           ^~~~~~~~~~~~~~~~~~~~~
   include/uapi/asm-generic/ioctl.h:70:9: note: (near initialization for 'nsm_ioctls[6].cmd')
      70 |         (((dir)  << _IOC_DIRSHIFT) | \
         |         ^
   include/uapi/asm-generic/ioctl.h:88:33: note: in expansion of macro '_IOC'
      88 | #define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
         |                                 ^~~~
   include/uapi/linux/nsm.h:172:33: note: in expansion of macro '_IOWR'
     172 | #define NSM_IOCTL_ATTESTATION   _IOWR(NSM_MAGIC, 0x6, union nsm_attestation)
         |                                 ^~~~~
   drivers/misc/nsm.c:1209:11: note: in expansion of macro 'NSM_IOCTL_ATTESTATION'
    1209 |         { NSM_IOCTL_ATTESTATION, _fill_req_attestation, _parse_resp_attestation },
         |           ^~~~~~~~~~~~~~~~~~~~~
>> include/uapi/asm-generic/ioctl.h:70:9: error: initializer element is not constant
      70 |         (((dir)  << _IOC_DIRSHIFT) | \
         |         ^
   include/uapi/asm-generic/ioctl.h:88:33: note: in expansion of macro '_IOC'
      88 | #define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
         |                                 ^~~~
   include/uapi/linux/nsm.h:186:33: note: in expansion of macro '_IOWR'
     186 | #define NSM_IOCTL_RANDOM        _IOWR(NSM_MAGIC, 0x7, union nsm_get_random)
         |                                 ^~~~~
   drivers/misc/nsm.c:1210:11: note: in expansion of macro 'NSM_IOCTL_RANDOM'
    1210 |         { NSM_IOCTL_RANDOM, _fill_req_get_random, _parse_resp_get_random },
         |           ^~~~~~~~~~~~~~~~
   include/uapi/asm-generic/ioctl.h:70:9: note: (near initialization for 'nsm_ioctls[7].cmd')
      70 |         (((dir)  << _IOC_DIRSHIFT) | \
         |         ^
   include/uapi/asm-generic/ioctl.h:88:33: note: in expansion of macro '_IOC'
      88 | #define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
         |                                 ^~~~
   include/uapi/linux/nsm.h:186:33: note: in expansion of macro '_IOWR'
     186 | #define NSM_IOCTL_RANDOM        _IOWR(NSM_MAGIC, 0x7, union nsm_get_random)
         |                                 ^~~~~

drivers/misc/nsm.c:1210:11: note: in expansion of macro 'NSM_IOCTL_RANDOM'
1210 | { NSM_IOCTL_RANDOM, _fill_req_get_random, _parse_resp_get_random },
| ^~~~~~~~~~~~~~~~

vim +70 include/uapi/asm-generic/ioctl.h

8a1ab3155c2ac7 David Howells 2012-10-04 68

8a1ab3155c2ac7 David Howells 2012-10-04  69  #define _IOC(dir,type,nr,size) \
8a1ab3155c2ac7 David Howells 2012-10-04 @70  	(((dir)  << _IOC_DIRSHIFT) | \
8a1ab3155c2ac7 David Howells 2012-10-04  71  	 ((type) << _IOC_TYPESHIFT) | \
8a1ab3155c2ac7 David Howells 2012-10-04  72  	 ((nr)   << _IOC_NRSHIFT) | \
8a1ab3155c2ac7 David Howells 2012-10-04  73  	 ((size) << _IOC_SIZESHIFT))

8a1ab3155c2ac7 David Howells 2012-10-04 74

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki