Skip to contentSkip to conversation

s390: Ultravisor device

PATCH Superseded Steffen Eiden 3 participants 8 messages Feb 17, 2022 – Feb 21, 2022

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover s390: Ultravisor device

This series adds an Ultravisor(UV) device letting the userspace send some Ultravisor calls to the UV. Currently two calls are supported. Query Ultravisor Information (QUI) and
Receive Attestation Measurement (Attest[ation]).

The UV device is implemented as a miscdevice accepting only IOCTLs. The IOCTL cmd specifies the UV call and the IOCTL arg the request and response data depending on the UV call.
The device driver writes the UV response in the ioctl argument data.

The 'uvdevice' does no checks on the request beside faulty userspace addresses, if sizes are in a sane range before allocating in kernel space, and other tests that prevent the system from corruption. Especially, no checks are made, that will be performed by the UV anyway (E.g. 'invalid command' in case of attestation on unsupported hardware). These errors are reported back to Userspace using the UV return code field.

Steffen Eiden (3):
drivers/s390/char: Add Ultravisor io device drivers/s390/char: Add Ultravisor attestation to uvdevice
selftests: drivers/s390x: Add uvdevice tests

 MAINTAINERS                                   |   3 +
 arch/s390/include/asm/uv.h                    |  23 +-
 arch/s390/include/uapi/asm/uvdevice.h         |  46 +++
 drivers/s390/char/Kconfig                     |   9 +
 drivers/s390/char/Makefile                    |   1 +
 drivers/s390/char/uvdevice.c                  | 325 ++++++++++++++++++
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/drivers/.gitignore    |   1 +
 .../selftests/drivers/s390x/uvdevice/Makefile |  22 ++
 .../selftests/drivers/s390x/uvdevice/config   |   1 +
 .../drivers/s390x/uvdevice/test_uvdevice.c    | 280 +++++++++++++++
 11 files changed, 711 insertions(+), 1 deletion(-)
 create mode 100644 arch/s390/include/uapi/asm/uvdevice.h
 create mode 100644 drivers/s390/char/uvdevice.c
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/Makefile
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/config
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 drivers/s390/char: Add Ultravisor io device

This patch adds a new miscdevice to expose some Ultravisor functions to userspace. Userspace can send IOCTLis to the uvdevice that will then emit a corresponding Ultravisor Call and hands the result over to userspace. The uvdevice is available if the Ultravisor Call facility is present.

Userspace is now able to call the Query Ultravisor Information Ultravisor Command through the uvdevice.

---
 MAINTAINERS                           |   2 +
 arch/s390/include/uapi/asm/uvdevice.h |  27 +++++
 drivers/s390/char/Kconfig             |   9 ++
 drivers/s390/char/Makefile            |   1 +
 drivers/s390/char/uvdevice.c          | 162 ++++++++++++++++++++++++++
 5 files changed, 201 insertions(+)
 create mode 100644 arch/s390/include/uapi/asm/uvdevice.h
 create mode 100644 drivers/s390/char/uvdevice.c
MAINTAINERS +2 -0
index 5250298d2817..c7d8d0fe48cf 100644
@@ -10457,9 +10457,11 @@ F:	Documentation/virt/kvm/s390*
 F:	arch/s390/include/asm/gmap.h
 F:	arch/s390/include/asm/kvm*
 F:	arch/s390/include/uapi/asm/kvm*
+F:	arch/s390/include/uapi/asm/uvdevice.h
 F:	arch/s390/kernel/uv.c
 F:	arch/s390/kvm/
 F:	arch/s390/mm/gmap.c
+F:	drivers/s390/char/uvdevice.c
 F:	tools/testing/selftests/kvm/*/s390x/
 F:	tools/testing/selftests/kvm/s390x/
 
arch/s390/include/uapi/asm/uvdevice.h new file +27 -0
new file mode 100644
index 000000000000..f2e4984a6e2e
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+#ifndef __S390X_ASM_UVDEVICE_H
+#define __S390X_ASM_UVDEVICE_H
+
+#include <linux/types.h>
+
+struct uvio_ioctl_cb {
+	__u32 flags;			/* Currently no flags defined, must be zero */
+	__u16 uv_rc;			/* UV header rc value */
+	__u16 uv_rrc;			/* UV header rrc value */
+	__u64 argument_addr;		/* Userspace address of uvio argument */
+	__u32 argument_len;
+	__u8  reserved14[0x40 - 0x14];	/* must be zero */
+};
+
+#define UVIO_QUI_MAX_LEN		0x8000
+
+#define UVIO_DEVICE_NAME "uv"
+#define UVIO_TYPE_UVC 'u'
+
+#define UVIO_IOCTL_QUI _IOWR(UVIO_TYPE_UVC, 0x01, struct uvio_ioctl_cb)
+
+#endif  /* __S390X_ASM_UVDEVICE_H */
drivers/s390/char/Kconfig +9 -0
index 6cc4b19acf85..933c0d0062d6 100644
@@ -184,3 +184,12 @@ config S390_VMUR
 	depends on S390
 	help
 	  Character device driver for z/VM reader, puncher and printer.
+
+config UV_UAPI
+	def_tristate m
+	prompt "Ultravisor userspace API"
+	depends on PROTECTED_VIRTUALIZATION_GUEST
+	help
+	  Selecting exposes parts of the UV interface to userspace
+	  by providing a misc character device. Using IOCTLs one
+	  can interact with the UV.
drivers/s390/char/Makefile +1 -0
index c6fdb81a068a..b5c83092210e 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_MONREADER) += monreader.o
 obj-$(CONFIG_MONWRITER) += monwriter.o
 obj-$(CONFIG_S390_VMUR) += vmur.o
 obj-$(CONFIG_CRASH_DUMP) += sclp_sdias.o zcore.o
+obj-$(CONFIG_UV_UAPI) += uvdevice.o
 
 hmcdrv-objs := hmcdrv_mod.o hmcdrv_dev.o hmcdrv_ftp.o hmcdrv_cache.o diag_ftp.o sclp_ftp.o
 obj-$(CONFIG_HMC_DRV) += hmcdrv.o
drivers/s390/char/uvdevice.c new file +162 -0

162 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/3] drivers/s390/char: Add Ultravisor io device

Quoted 174 lines · Steffen Eiden

This patch adds a new miscdevice to expose some Ultravisor functions to userspace. Userspace can send IOCTLis to the uvdevice that will then emit a corresponding Ultravisor Call and hands the result over to userspace. The uvdevice is available if the Ultravisor Call facility is present.

Userspace is now able to call the Query Ultravisor Information Ultravisor Command through the uvdevice.

---
 MAINTAINERS                           |   2 +
 arch/s390/include/uapi/asm/uvdevice.h |  27 +++++
 drivers/s390/char/Kconfig             |   9 ++
 drivers/s390/char/Makefile            |   1 +
 drivers/s390/char/uvdevice.c          | 162 ++++++++++++++++++++++++++
 5 files changed, 201 insertions(+)
 create mode 100644 arch/s390/include/uapi/asm/uvdevice.h
 create mode 100644 drivers/s390/char/uvdevice.c
MAINTAINERS +2 -0
index 5250298d2817..c7d8d0fe48cf 100644
@@ -10457,9 +10457,11 @@ F:	Documentation/virt/kvm/s390*
 F:	arch/s390/include/asm/gmap.h
 F:	arch/s390/include/asm/kvm*
 F:	arch/s390/include/uapi/asm/kvm*
+F:	arch/s390/include/uapi/asm/uvdevice.h
 F:	arch/s390/kernel/uv.c
 F:	arch/s390/kvm/
 F:	arch/s390/mm/gmap.c
+F:	drivers/s390/char/uvdevice.c
 F:	tools/testing/selftests/kvm/*/s390x/
 F:	tools/testing/selftests/kvm/s390x/
 
arch/s390/include/uapi/asm/uvdevice.h new file +27 -0
new file mode 100644
index 000000000000..f2e4984a6e2e
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+#ifndef __S390X_ASM_UVDEVICE_H
+#define __S390X_ASM_UVDEVICE_H
+
+#include <linux/types.h>
+
+struct uvio_ioctl_cb {
+	__u32 flags;			/* Currently no flags defined, must be zero */
+	__u16 uv_rc;			/* UV header rc value */
+	__u16 uv_rrc;			/* UV header rrc value */
+	__u64 argument_addr;		/* Userspace address of uvio argument */
+	__u32 argument_len;
+	__u8  reserved14[0x40 - 0x14];	/* must be zero */
+};
+
+#define UVIO_QUI_MAX_LEN		0x8000
+
+#define UVIO_DEVICE_NAME "uv"
+#define UVIO_TYPE_UVC 'u'
+
+#define UVIO_IOCTL_QUI _IOWR(UVIO_TYPE_UVC, 0x01, struct uvio_ioctl_cb)
+
+#endif  /* __S390X_ASM_UVDEVICE_H */
drivers/s390/char/Kconfig +9 -0
index 6cc4b19acf85..933c0d0062d6 100644
@@ -184,3 +184,12 @@ config S390_VMUR
 	depends on S390
 	help
 	  Character device driver for z/VM reader, puncher and printer.
+
+config UV_UAPI
+	def_tristate m
+	prompt "Ultravisor userspace API"
+	depends on PROTECTED_VIRTUALIZATION_GUEST
+	help
+	  Selecting exposes parts of the UV interface to userspace
+	  by providing a misc character device. Using IOCTLs one
+	  can interact with the UV.
drivers/s390/char/Makefile +1 -0
index c6fdb81a068a..b5c83092210e 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_MONREADER) += monreader.o
 obj-$(CONFIG_MONWRITER) += monwriter.o
 obj-$(CONFIG_S390_VMUR) += vmur.o
 obj-$(CONFIG_CRASH_DUMP) += sclp_sdias.o zcore.o
+obj-$(CONFIG_UV_UAPI) += uvdevice.o
 
 hmcdrv-objs := hmcdrv_mod.o hmcdrv_dev.o hmcdrv_ftp.o hmcdrv_cache.o diag_ftp.o sclp_ftp.o
 obj-$(CONFIG_HMC_DRV) += hmcdrv.o
drivers/s390/char/uvdevice.c new file +70 -0
new file mode 100644
index 000000000000..e8efcbf0e7ab
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt ".\n"
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <linux/types.h>
+#include <linux/stddef.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+
+#include <asm/uvdevice.h>
+#include <asm/uv.h>
+
+/**
+ * uvio_qui() - Perform a Query Ultravisor Information UVC.
+ *
+ * uv_ioctl: ioctl control block
+ *
+ * uvio_qui() does a Query Ultravisor Information (QUI) Ultravisor Call.
+ * It creates the uvc qui request and sends it to the Ultravisor. After that
+ * it copies the response to userspace and fills the rc and rrc of uv_ioctl
+ * uv_call with the response values of the Ultravisor.
+ *
+ * Create the UVC structure, send the UVC to UV and write the response in the ioctl struct.
+ *
+ * Return: 0 on success or a negative error code on error.
+ */
+static int uvio_qui(struct uvio_ioctl_cb *uv_ioctl)
+{
+	u8 __user *user_buf_addr = (__user u8 *)uv_ioctl->argument_addr;
+	size_t user_buf_len = uv_ioctl->argument_len;
+	struct uv_cb_header *uvcb_qui = NULL;
+	int ret;
+
+	/*
+	 * Do not check for a too small buffer. If userspace provides a buffer
+	 * that is too small the Ultravisor will complain.
+	 */
+	ret = -EINVAL;
+	if (!user_buf_len || user_buf_len > UVIO_QUI_MAX_LEN)
+		goto out;
+	ret = -ENOMEM;
+	uvcb_qui = kvzalloc(user_buf_len, GFP_KERNEL);
+	if (!uvcb_qui)
+		goto out;
+	uvcb_qui->len = user_buf_len;
+	uvcb_qui->cmd = UVC_CMD_QUI;
+
+	uv_call(0, (u64)uvcb_qui);
+
+	ret = -EFAULT;
+	if (copy_to_user(user_buf_addr, uvcb_qui, uvcb_qui->len))
+		goto out;
+	uv_ioctl->uv_rc = uvcb_qui->rc;
+	uv_ioctl->uv_rrc = uvcb_qui->rrc;
+
+	ret = 0;
+out:
+	kvfree(uvcb_qui);
+	return ret;
+}
+
+static int uvio_copy_and_check_ioctl(struct uvio_ioctl_cb *ioctl, void __user *argp)
+{
+	u64 sum = 0;

drivers/s390/char/uvdevice.c line 80

Quoted 10 lines
drivers/s390/char/uvdevice.c new file +10 -0
+	u64 i;
+
+	if (copy_from_user(ioctl, argp, sizeof(*ioctl)))
+		return -EFAULT;
+	if (ioctl->flags != 0)
+		return -EINVAL;
+	for (i = 0; i < ARRAY_SIZE(ioctl->reserved14); i++)
+		sum += ioctl->reserved14[i];
+	if (sum)
+		return -EINVAL;

So you can have -1, 1, -1, 1, and so on and cause this to be an incorrect check. Just test for 0 and bail out early please.

drivers/s390/char/uvdevice.c line 93

Quoted 13 lines
drivers/s390/char/uvdevice.c new file +13 -0
+
+	return 0;
+}
+
+static int uvio_dev_open(struct inode *inode, struct file *filp)
+{
+	return 0;
+}
+
+static int uvio_dev_close(struct inode *inodep, struct file *filp)
+{
+	return 0;
+}

If open/close do nothing, no need to provide it at all, just drop them.

Quoted 14 lines
drivers/s390/char/uvdevice.c new file +14 -0
+
+/*
+ * IOCTL entry point for the Ultravisor device.
+ */
+static long uvio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	struct uvio_ioctl_cb *uv_ioctl;
+	long ret;
+
+	ret = -ENOMEM;
+	uv_ioctl = vzalloc(sizeof(*uv_ioctl));
+	if (!uv_ioctl)
+		goto out;

drivers/s390/char/uvdevice.c line 117

Quoted 10 lines
drivers/s390/char/uvdevice.c new file +10 -0
+
+	switch (cmd) {
+	case UVIO_IOCTL_QUI:
+		ret = uvio_copy_and_check_ioctl(uv_ioctl, argp);
+		if (ret)
+			goto out;
+		ret = uvio_qui(uv_ioctl);
+		break;
+	default:
+		ret = -EINVAL;

Wrong error value :(

Quoted 35 lines
drivers/s390/char/uvdevice.c new file +35 -0
+		break;
+	}
+	if (ret)
+		goto out;
+
+	if (copy_to_user(argp, uv_ioctl, sizeof(*uv_ioctl)))
+		ret = -EFAULT;
+
+ out:
+	vfree(uv_ioctl);
+	return ret;
+}
+
+static const struct file_operations uvio_dev_fops = {
+	.owner = THIS_MODULE,
+	.unlocked_ioctl = uvio_ioctl,
+	.open = uvio_dev_open,
+	.release = uvio_dev_close,
+	.llseek = no_llseek,
+};
+
+static struct miscdevice uvio_dev_miscdev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = UVIO_DEVICE_NAME,
+	.fops = &uvio_dev_fops,
+};
+
+static void __exit uvio_dev_exit(void)
+{
+	misc_deregister(&uvio_dev_miscdev);
+}
+
+static int __init uvio_dev_init(void)
+{
+	if (!test_facility(158))

drivers/s390/char/uvdevice.c line 162

Quoted 10 lines
drivers/s390/char/uvdevice.c new file +10 -0
+		return -ENXIO;
+	return misc_register(&uvio_dev_miscdev);
+}
+
+module_init(uvio_dev_init);
+module_exit(uvio_dev_exit);
+
+MODULE_AUTHOR("IBM Corporation");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Ultravisor UAPI driver");

Nothing to cause this to automatically be loaded when the "hardware" is present?

thanks,

greg k-h

#3 lore raw

Re: [PATCH 1/3] drivers/s390/char: Add Ultravisor io device

Hey Greg,
thanks for your review.

Quoted 180 lines · Greg KH
Quoted 179 lines · Steffen Eiden

This patch adds a new miscdevice to expose some Ultravisor functions to userspace. Userspace can send IOCTLis to the uvdevice that will then emit a corresponding Ultravisor Call and hands the result over to userspace. The uvdevice is available if the Ultravisor Call facility is present.

Userspace is now able to call the Query Ultravisor Information Ultravisor Command through the uvdevice.

---
  MAINTAINERS                           |   2 +
  arch/s390/include/uapi/asm/uvdevice.h |  27 +++++
  drivers/s390/char/Kconfig             |   9 ++
  drivers/s390/char/Makefile            |   1 +
  drivers/s390/char/uvdevice.c          | 162 ++++++++++++++++++++++++++
  5 files changed, 201 insertions(+)

create mode 100644 arch/s390/include/uapi/asm/uvdevice.h create mode 100644 drivers/s390/char/uvdevice.c

MAINTAINERS +2 -0
index 5250298d2817..c7d8d0fe48cf 100644
@@ -10457,9 +10457,11 @@ F:	Documentation/virt/kvm/s390*
  F:	arch/s390/include/asm/gmap.h
  F:	arch/s390/include/asm/kvm*
  F:	arch/s390/include/uapi/asm/kvm*
+F:	arch/s390/include/uapi/asm/uvdevice.h
  F:	arch/s390/kernel/uv.c
  F:	arch/s390/kvm/
  F:	arch/s390/mm/gmap.c
+F:	drivers/s390/char/uvdevice.c
  F:	tools/testing/selftests/kvm/*/s390x/
  F:	tools/testing/selftests/kvm/s390x/
 
arch/s390/include/uapi/asm/uvdevice.h new file +27 -0
new file mode 100644
index 000000000000..f2e4984a6e2e
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+#ifndef __S390X_ASM_UVDEVICE_H
+#define __S390X_ASM_UVDEVICE_H
+
+#include <linux/types.h>
+
+struct uvio_ioctl_cb {
+	__u32 flags;			/* Currently no flags defined, must be zero */
+	__u16 uv_rc;			/* UV header rc value */
+	__u16 uv_rrc;			/* UV header rrc value */
+	__u64 argument_addr;		/* Userspace address of uvio argument */
+	__u32 argument_len;
+	__u8  reserved14[0x40 - 0x14];	/* must be zero */
+};
+
+#define UVIO_QUI_MAX_LEN		0x8000
+
+#define UVIO_DEVICE_NAME "uv"
+#define UVIO_TYPE_UVC 'u'
+
+#define UVIO_IOCTL_QUI _IOWR(UVIO_TYPE_UVC, 0x01, struct uvio_ioctl_cb)
+
+#endif  /* __S390X_ASM_UVDEVICE_H */
drivers/s390/char/Kconfig +9 -0
index 6cc4b19acf85..933c0d0062d6 100644
@@ -184,3 +184,12 @@ config S390_VMUR
  	depends on S390
  	help
  	  Character device driver for z/VM reader, puncher and printer.
+
+config UV_UAPI
+	def_tristate m
+	prompt "Ultravisor userspace API"
+	depends on PROTECTED_VIRTUALIZATION_GUEST
+	help
+	  Selecting exposes parts of the UV interface to userspace
+	  by providing a misc character device. Using IOCTLs one
+	  can interact with the UV.
drivers/s390/char/Makefile +1 -0
index c6fdb81a068a..b5c83092210e 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_MONREADER) += monreader.o
  obj-$(CONFIG_MONWRITER) += monwriter.o
  obj-$(CONFIG_S390_VMUR) += vmur.o
  obj-$(CONFIG_CRASH_DUMP) += sclp_sdias.o zcore.o
+obj-$(CONFIG_UV_UAPI) += uvdevice.o
 
  hmcdrv-objs := hmcdrv_mod.o hmcdrv_dev.o hmcdrv_ftp.o hmcdrv_cache.o diag_ftp.o sclp_ftp.o
  obj-$(CONFIG_HMC_DRV) += hmcdrv.o
drivers/s390/char/uvdevice.c new file +75 -0
new file mode 100644
index 000000000000..e8efcbf0e7ab
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt ".\n"
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <linux/types.h>
+#include <linux/stddef.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+
+#include <asm/uvdevice.h>
+#include <asm/uv.h>
+
+/**
+ * uvio_qui() - Perform a Query Ultravisor Information UVC.
+ *
+ * uv_ioctl: ioctl control block
+ *
+ * uvio_qui() does a Query Ultravisor Information (QUI) Ultravisor Call.
+ * It creates the uvc qui request and sends it to the Ultravisor. After that
+ * it copies the response to userspace and fills the rc and rrc of uv_ioctl
+ * uv_call with the response values of the Ultravisor.
+ *
+ * Create the UVC structure, send the UVC to UV and write the response in the ioctl struct.
+ *
+ * Return: 0 on success or a negative error code on error.
+ */
+static int uvio_qui(struct uvio_ioctl_cb *uv_ioctl)
+{
+	u8 __user *user_buf_addr = (__user u8 *)uv_ioctl->argument_addr;
+	size_t user_buf_len = uv_ioctl->argument_len;
+	struct uv_cb_header *uvcb_qui = NULL;
+	int ret;
+
+	/*
+	 * Do not check for a too small buffer. If userspace provides a buffer
+	 * that is too small the Ultravisor will complain.
+	 */
+	ret = -EINVAL;
+	if (!user_buf_len || user_buf_len > UVIO_QUI_MAX_LEN)
+		goto out;
+	ret = -ENOMEM;
+	uvcb_qui = kvzalloc(user_buf_len, GFP_KERNEL);
+	if (!uvcb_qui)
+		goto out;
+	uvcb_qui->len = user_buf_len;
+	uvcb_qui->cmd = UVC_CMD_QUI;
+
+	uv_call(0, (u64)uvcb_qui);
+
+	ret = -EFAULT;
+	if (copy_to_user(user_buf_addr, uvcb_qui, uvcb_qui->len))
+		goto out;
+	uv_ioctl->uv_rc = uvcb_qui->rc;
+	uv_ioctl->uv_rrc = uvcb_qui->rrc;
+
+	ret = 0;
+out:
+	kvfree(uvcb_qui);
+	return ret;
+}
+
+static int uvio_copy_and_check_ioctl(struct uvio_ioctl_cb *ioctl, void __user *argp)
+{
+	u64 sum = 0;
+	u64 i;
+
+	if (copy_from_user(ioctl, argp, sizeof(*ioctl)))
+		return -EFAULT;
+	if (ioctl->flags != 0)
Quoted 8 lines

drivers/s390/char/uvdevice.c line 80

Quoted 5 lines
drivers/s390/char/uvdevice.c new file +5 -0
+		return -EINVAL;
+	for (i = 0; i < ARRAY_SIZE(ioctl->reserved14); i++)
+		sum += ioctl->reserved14[i];
+	if (sum)
+		return -EINVAL;

So you can have -1, 1, -1, 1, and so on and cause this to be an incorrect check. Just test for 0 and bail out early please.

These ints are unsigned, your szenario cannot happen. However, I changed it to bailout early anyway.

Quoted 18 lines
Quoted 6 lines
drivers/s390/char/uvdevice.c new file +3 -0
+
+	return 0;
+}

drivers/s390/char/uvdevice.c line 93

Quoted 10 lines
drivers/s390/char/uvdevice.c new file +10 -0
+
+static int uvio_dev_open(struct inode *inode, struct file *filp)
+{
+	return 0;
+}
+
+static int uvio_dev_close(struct inode *inodep, struct file *filp)
+{
+	return 0;
+}

If open/close do nothing, no need to provide it at all, just drop them.

Makes sense.

Quoted 19 lines
Quoted 19 lines
drivers/s390/char/uvdevice.c new file +18 -0
+
+/*
+ * IOCTL entry point for the Ultravisor device.
+ */
+static long uvio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+	struct uvio_ioctl_cb *uv_ioctl;
+	long ret;
+
+	ret = -ENOMEM;
+	uv_ioctl = vzalloc(sizeof(*uv_ioctl));
+	if (!uv_ioctl)
+		goto out;
+
+	switch (cmd) {
+	case UVIO_IOCTL_QUI:
+		ret = uvio_copy_and_check_ioctl(uv_ioctl, argp);
Quoted 8 lines

drivers/s390/char/uvdevice.c line 117

Quoted 6 lines
drivers/s390/char/uvdevice.c new file +6 -0
+		if (ret)
+			goto out;
+		ret = uvio_qui(uv_ioctl);
+		break;
+	default:
+		ret = -EINVAL;

Wrong error value :(

changed

Quoted 41 lines
Quoted 41 lines
drivers/s390/char/uvdevice.c new file +40 -0
+		break;
+	}
+	if (ret)
+		goto out;
+
+	if (copy_to_user(argp, uv_ioctl, sizeof(*uv_ioctl)))
+		ret = -EFAULT;
+
+ out:
+	vfree(uv_ioctl);
+	return ret;
+}
+
+static const struct file_operations uvio_dev_fops = {
+	.owner = THIS_MODULE,
+	.unlocked_ioctl = uvio_ioctl,
+	.open = uvio_dev_open,
+	.release = uvio_dev_close,
+	.llseek = no_llseek,
+};
+
+static struct miscdevice uvio_dev_miscdev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = UVIO_DEVICE_NAME,
+	.fops = &uvio_dev_fops,
+};
+
+static void __exit uvio_dev_exit(void)
+{
+	misc_deregister(&uvio_dev_miscdev);
+}
+
+static int __init uvio_dev_init(void)
+{
+	if (!test_facility(158))
+		return -ENXIO;
+	return misc_register(&uvio_dev_miscdev);
+}
+
+module_init(uvio_dev_init);
Quoted 8 lines

drivers/s390/char/uvdevice.c line 162

Quoted 5 lines
drivers/s390/char/uvdevice.c new file +5 -0
+module_exit(uvio_dev_exit);
+
+MODULE_AUTHOR("IBM Corporation");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Ultravisor UAPI driver");

Nothing to cause this to automatically be loaded when the "hardware" is present?

We do not have anything like a s390 facility bus which could trigger such automatic loads. Developing such a bus would be an overkill.

However we could do the approach e.g. kvm-s390 takes. Define MODULE_ALIAS(devname:uv) that will trigger an automatic module load if someone calls open on /dev/uv the first time.
IIRC we need to define a fixed misc minor number with this approach.

something like that:

drivers/s390/char/uvdevice.c +0 -0
@@ -309,3 +309,5
 MODULE_AUTHOR("IBM Corporation");
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Ultravisor UAPI driver");
+MODULE_ALIAS_MISCDEV(SOME_FIXED_MISC_MINOR);
+MODULE_ALIAS("devname:uv");

We then maybe need to discuss if 'uv' is unique enough.

Quoted 5 lines

thanks,

greg k-h

Steffen

#4 lore raw

PATCH 2/3 drivers/s390/char: Add Ultravisor attestation to uvdevice

In reply to Steffen Eiden

This patch enables userspace to call the Retrieve Attestation Measurement Ultravisor Call using IOCTLs on the uvdevice.

The uvdevice will do some sanity checks first. Then, copy the request data to kernel space, build the uvcb, perform the UV call, and copy the result back to userspace.

Userspace is now able to call the Retrieve Attestation Measurement Ultravisor Call through the uvdevice.

---
 arch/s390/include/asm/uv.h            |  23 +++-
 arch/s390/include/uapi/asm/uvdevice.h |  19 +++
 drivers/s390/char/uvdevice.c          | 163 ++++++++++++++++++++++++++
 3 files changed, 204 insertions(+), 1 deletion(-)
arch/s390/include/asm/uv.h +22 -1
index 72d3e49c2860..648375750259 100644
@@ -2,7 +2,7 @@
 /*
  * Ultravisor Interfaces
  *
- * Copyright IBM Corp. 2019
+ * Copyright IBM Corp. 2019, 2022
  *
  * Author(s):
  *	Vasily Gorbik <[email protected]>
@@ -52,6 +52,7 @@
 #define UVC_CMD_UNPIN_PAGE_SHARED	0x0342
 #define UVC_CMD_SET_SHARED_ACCESS	0x1000
 #define UVC_CMD_REMOVE_SHARED_ACCESS	0x1001
+#define UVC_CMD_RETR_ATTEST		0x1020
 
 /* Bits in installed uv calls */
 enum uv_cmds_inst {
@@ -76,6 +77,7 @@ enum uv_cmds_inst {
 	BIT_UVC_CMD_UNSHARE_ALL = 20,
 	BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
 	BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
+	BIT_UVC_CMD_RETR_ATTEST = 28,
 };
 
 enum uv_feat_ind {
@@ -218,6 +220,25 @@ struct uv_cb_share {
 	u64 reserved28;
 } __packed __aligned(8);
 
+/* Retrieve Attestation Measurement */
+struct uv_cb_attest {
+	struct uv_cb_header header;	/* 0x0000 */
+	u64 reserved08[2];		/* 0x0008 */
+	u64 arcb_addr;			/* 0x0018 */
+	u64 cont_token;			/* 0x0020 */
+	u8  reserved28[6];		/* 0x0028 */
+	u16 user_data_len;		/* 0x002e */
+	u8  user_data[256];		/* 0x0030 */
+	u32 reserved130[3];		/* 0x0130 */
+	u32 meas_len;			/* 0x013c */
+	u64 meas_addr;			/* 0x0140 */
+	u8  config_uid[16];		/* 0x0148 */
+	u32 reserved158;		/* 0x0158 */
+	u32 add_data_len;		/* 0x015c */
+	u64 add_data_addr;		/* 0x0160 */
+	u64 reserved168[4];		/* 0x0168 */
+} __packed __aligned(8);
+
 static inline int __uv_call(unsigned long r1, unsigned long r2)
 {
 	int cc;
arch/s390/include/uapi/asm/uvdevice.h +19 -0
index f2e4984a6e2e..7359f77583e0 100644
@@ -17,11 +17,30 @@ struct uvio_ioctl_cb {
 	__u8  reserved14[0x40 - 0x14];	/* must be zero */
 };
 
+#define UVIO_ATT_USER_DATA_LEN		0x100
+#define UVIO_ATT_UID_LEN		0x10
+struct uvio_attest {
+	__u64 arcb_addr;				/* 0x0000 */
+	__u64 meas_addr;				/* 0x0008 */
+	__u64 add_data_addr;				/* 0x0010 */
+	__u8  user_data[UVIO_ATT_USER_DATA_LEN];	/* 0x0018 */
+	__u8  config_uid[UVIO_ATT_UID_LEN];		/* 0x0118 */
+	__u32 arcb_len;					/* 0x0128 */
+	__u32 meas_len;					/* 0x012c */
+	__u32 add_data_len;				/* 0x0130 */
+	__u16 user_data_len;				/* 0x0134 */
+	__u16 reserved136;				/* 0x0136 */
+};
+
 #define UVIO_QUI_MAX_LEN		0x8000
+#define UVIO_ATT_ARCB_MAX_LEN		0x100000
+#define UVIO_ATT_MEASUREMENT_MAX_LEN	0x8000
+#define UVIO_ATT_ADDITIONAL_MAX_LEN	0x8000
 
 #define UVIO_DEVICE_NAME "uv"
 #define UVIO_TYPE_UVC 'u'
 
 #define UVIO_IOCTL_QUI _IOWR(UVIO_TYPE_UVC, 0x01, struct uvio_ioctl_cb)
+#define UVIO_IOCTL_ATT _IOWR(UVIO_TYPE_UVC, 0x02, struct uvio_ioctl_cb)
 
 #endif  /* __S390X_ASM_UVDEVICE_H */
drivers/s390/char/uvdevice.c +163 -0

175 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

PATCH 3/3 selftests: drivers/s390x: Add uvdevice tests

In reply to Steffen Eiden

Adds some selftests to test ioctl error paths of the uv-uapi.

---
 MAINTAINERS                                   |   1 +
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/drivers/.gitignore    |   1 +
 .../selftests/drivers/s390x/uvdevice/Makefile |  22 ++
 .../selftests/drivers/s390x/uvdevice/config   |   1 +
 .../drivers/s390x/uvdevice/test_uvdevice.c    | 280 ++++++++++++++++++
 6 files changed, 306 insertions(+)
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/Makefile
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/config
 create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c
MAINTAINERS +1 -0
index c7d8d0fe48cf..c6a0311c3fa8 100644
@@ -10462,6 +10462,7 @@ F:	arch/s390/kernel/uv.c
 F:	arch/s390/kvm/
 F:	arch/s390/mm/gmap.c
 F:	drivers/s390/char/uvdevice.c
+F:	tools/testing/selftests/drivers/s390x/uvdevice/
 F:	tools/testing/selftests/kvm/*/s390x/
 F:	tools/testing/selftests/kvm/s390x/
 
tools/testing/selftests/Makefile +1 -0
index c852eb40c4f7..3b8abaee9271 100644
@@ -9,6 +9,7 @@ TARGETS += core
 TARGETS += cpufreq
 TARGETS += cpu-hotplug
 TARGETS += drivers/dma-buf
+TARGETS += drivers/s390x/uvdevice
 TARGETS += efivarfs
 TARGETS += exec
 TARGETS += filesystems
tools/testing/selftests/drivers/.gitignore +1 -0
index ca74f2e1c719..09e23b5afa96 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /dma-buf/udmabuf
+/s390x/uvdevice/test_uvdevice
tools/testing/selftests/drivers/s390x/uvdevice/Makefile new file +22 -0
new file mode 100644
index 000000000000..5e701d2708d4
@@ -0,0 +1,22 @@
+include ../../../../../build/Build.include
+
+UNAME_M := $(shell uname -m)
+
+ifneq ($(UNAME_M),s390x)
+nothing:
+.PHONY: all clean run_tests install
+.SILENT:
+else
+
+TEST_GEN_PROGS := test_uvdevice
+
+top_srcdir ?= ../../../../../..
+KSFT_KHDR_INSTALL := 1
+khdr_dir = $(top_srcdir)/usr/include
+LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+
+CFLAGS += -Wall -Werror -static -I$(khdr_dir) -I$(LINUX_TOOL_ARCH_INCLUDE)
+
+include ../../../lib.mk
+
+endif
tools/testing/selftests/drivers/s390x/uvdevice/config new file +1 -0
new file mode 100644
index 000000000000..f28a04b99eff
@@ -0,0 +1 @@
+CONFIG_S390_UV_UAPI=y
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +280 -0

280 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#6 lore raw

Re: [PATCH 3/3] selftests: drivers/s390x: Add uvdevice tests

Quoted 2 lines · Steffen Eiden

Adds some selftests to test ioctl error paths of the uv-uapi.

Please add information on how to run this test and example output.

Quoted 242 lines
---
  MAINTAINERS                                   |   1 +
  tools/testing/selftests/Makefile              |   1 +
  tools/testing/selftests/drivers/.gitignore    |   1 +
  .../selftests/drivers/s390x/uvdevice/Makefile |  22 ++
  .../selftests/drivers/s390x/uvdevice/config   |   1 +
  .../drivers/s390x/uvdevice/test_uvdevice.c    | 280 ++++++++++++++++++
  6 files changed, 306 insertions(+)

create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/Makefile create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/config create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c

MAINTAINERS +1 -0
index c7d8d0fe48cf..c6a0311c3fa8 100644
@@ -10462,6 +10462,7 @@ F:	arch/s390/kernel/uv.c
  F:	arch/s390/kvm/
  F:	arch/s390/mm/gmap.c
  F:	drivers/s390/char/uvdevice.c
+F:	tools/testing/selftests/drivers/s390x/uvdevice/
  F:	tools/testing/selftests/kvm/*/s390x/
  F:	tools/testing/selftests/kvm/s390x/
 
tools/testing/selftests/Makefile +1 -0
index c852eb40c4f7..3b8abaee9271 100644
@@ -9,6 +9,7 @@ TARGETS += core
  TARGETS += cpufreq
  TARGETS += cpu-hotplug
  TARGETS += drivers/dma-buf
+TARGETS += drivers/s390x/uvdevice
  TARGETS += efivarfs
  TARGETS += exec
  TARGETS += filesystems
tools/testing/selftests/drivers/.gitignore +1 -0
index ca74f2e1c719..09e23b5afa96 100644
@@ -1,2 +1,3 @@
  # SPDX-License-Identifier: GPL-2.0-only
  /dma-buf/udmabuf
+/s390x/uvdevice/test_uvdevice
tools/testing/selftests/drivers/s390x/uvdevice/Makefile new file +22 -0
new file mode 100644
index 000000000000..5e701d2708d4
@@ -0,0 +1,22 @@
+include ../../../../../build/Build.include
+
+UNAME_M := $(shell uname -m)
+
+ifneq ($(UNAME_M),s390x)
+nothing:
+.PHONY: all clean run_tests install
+.SILENT:
+else
+
+TEST_GEN_PROGS := test_uvdevice
+
+top_srcdir ?= ../../../../../..
+KSFT_KHDR_INSTALL := 1
+khdr_dir = $(top_srcdir)/usr/include
+LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+
+CFLAGS += -Wall -Werror -static -I$(khdr_dir) -I$(LINUX_TOOL_ARCH_INCLUDE)
+
+include ../../../lib.mk
+
+endif
tools/testing/selftests/drivers/s390x/uvdevice/config new file +1 -0
new file mode 100644
index 000000000000..f28a04b99eff
@@ -0,0 +1 @@
+CONFIG_S390_UV_UAPI=y
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +156 -0
new file mode 100644
index 000000000000..f23663bcab03
@@ -0,0 +1,280 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  selftest for the Ultravisor UAPI device
+ *
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <asm/uvdevice.h>
+
+#include "../../../kselftest_harness.h"
+
+#define BUFFER_SIZE 0x200
+FIXTURE(uvio_fixture) {
+	int uv_fd;
+	struct uvio_ioctl_cb uvio_ioctl;
+	uint8_t buffer[BUFFER_SIZE];
+	__u64 fault_page;
+};
+
+FIXTURE_VARIANT(uvio_fixture) {
+	unsigned long ioctl_cmd;
+	uint32_t arg_size;
+};
+
+FIXTURE_VARIANT_ADD(uvio_fixture, qui) {
+	.ioctl_cmd = UVIO_IOCTL_QUI,
+	.arg_size = BUFFER_SIZE,
+};
+
+FIXTURE_VARIANT_ADD(uvio_fixture, att) {
+	.ioctl_cmd = UVIO_IOCTL_ATT,
+	.arg_size = sizeof(struct uvio_attest),
+};
+
+FIXTURE_SETUP(uvio_fixture)
+{
+	self->uv_fd = open("/dev/uv", O_RDWR);
+
+	self->uvio_ioctl.argument_addr = (__u64)self->buffer;
+	self->uvio_ioctl.argument_len = variant->arg_size;
+	self->fault_page =
+		(__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, MAP_ANONYMOUS, -1, 0);
+}
+
+FIXTURE_TEARDOWN(uvio_fixture)
+{
+	if (self->uv_fd)
+		close(self->uv_fd);
+	munmap((void *)self->fault_page, (size_t)getpagesize());
+}
+
+TEST_F(uvio_fixture, fault_ioctl_arg)
+{
+	int rc, errno_cache;
+
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, NULL);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, self->fault_page);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+}
+
+TEST_F(uvio_fixture, fault_uvio_arg)
+{
+	int rc, errno_cache;
+
+	self->uvio_ioctl.argument_addr = 0;
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+
+	self->uvio_ioctl.argument_addr = self->fault_page;
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+}
+
+/*
+ * Test to verify that IOCTLs with invalid values in the ioctl_control block
+ * are rejected.
+ */
+TEST_F(uvio_fixture, inval_ioctl_cb)
+{
+	int rc, errno_cache;
+
+	self->uvio_ioctl.argument_len = 0;
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+
+	self->uvio_ioctl.argument_len = (uint32_t)-1;
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+	self->uvio_ioctl.argument_len = variant->arg_size;
+
+	self->uvio_ioctl.flags = (uint32_t)-1;
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+	self->uvio_ioctl.flags = 0;
+
+	memset(self->uvio_ioctl.reserved14, 0xff, sizeof(self->uvio_ioctl.reserved14));
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+
+	memset(&self->uvio_ioctl, 0x11, sizeof(self->uvio_ioctl));
+	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+	ASSERT_EQ(rc, -1);
+}
+
+TEST_F(uvio_fixture, inval_ioctl_cmd)
+{
+	int rc, errno_cache;
+	uint8_t nr = _IOC_NR(variant->ioctl_cmd);
+	unsigned long cmds[] = {
+		_IOWR('a', nr, struct uvio_ioctl_cb),
+		_IOWR(UVIO_TYPE_UVC, nr, int),
+		_IO(UVIO_TYPE_UVC, nr),
+		_IOR(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
+		_IOW(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cmds); i++) {
+		rc = ioctl(self->uv_fd, cmds[i], &self->uvio_ioctl);
+		errno_cache = errno;
+		ASSERT_EQ(rc, -1);
+		ASSERT_EQ(errno_cache, EINVAL);
+	}
+}
+
+struct test_attest_buffer {
+	uint8_t arcb[0x180];
+	uint8_t meas[64];
+	uint8_t add[32];
+};
+
+FIXTURE(attest_fixture) {

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 166

Quoted 10 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +10 -0
+	int uv_fd;
+	struct uvio_ioctl_cb uvio_ioctl;
+	struct uvio_attest uvio_attest;
+	struct test_attest_buffer attest_buffer;
+	__u64 fault_page;
+};
+
+FIXTURE_SETUP(attest_fixture)
+{
+	self->uv_fd = open("/dev/uv", O_RDWR);

So each test opne and closes the devuce. Can this file stay open for the duraction of the test run and close it in main() after test exits?

Quoted 99 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +99 -0
+
+	self->uvio_ioctl.argument_addr = (__u64)&self->uvio_attest;
+	self->uvio_ioctl.argument_len = sizeof(self->uvio_attest);
+
+	self->uvio_attest.arcb_addr = (__u64)&self->attest_buffer.arcb;
+	self->uvio_attest.arcb_len = sizeof(self->attest_buffer.arcb);
+
+	self->uvio_attest.meas_addr = (__u64)&self->attest_buffer.meas;
+	self->uvio_attest.meas_len = sizeof(self->attest_buffer.meas);
+
+	self->uvio_attest.add_data_addr = (__u64)&self->attest_buffer.add;
+	self->uvio_attest.add_data_len = sizeof(self->attest_buffer.add);
+	self->fault_page =
+		(__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, MAP_ANONYMOUS, -1, 0);
+}
+
+FIXTURE_TEARDOWN(attest_fixture)
+{
+	if (self->uv_fd)
+		close(self->uv_fd);
+	munmap((void *)self->fault_page, (size_t)getpagesize());
+}
+
+static void att_inval_sizes_test(uint32_t *size, uint32_t max_size, bool test_zero,
+				 struct __test_metadata *_metadata,
+				 FIXTURE_DATA(attest_fixture) *self)
+{
+	int rc, errno_cache;
+	uint32_t tmp = *size;
+
+	if (test_zero) {
+		*size = 0;
+		rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+		errno_cache = errno;
+		ASSERT_EQ(rc, -1);
+		ASSERT_EQ(errno_cache, EINVAL);
+	}
+	*size = max_size + 1;
+	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+	*size = tmp;
+}
+
+/*
+ * Test to verify that attestation IOCTLs with invalid values in the UVIO
+ * attestation control block are rejected.
+ */
+TEST_F(attest_fixture, att_inval_request)
+{
+	int rc, errno_cache;
+
+	att_inval_sizes_test(&self->uvio_attest.add_data_len, UVIO_ATT_ADDITIONAL_MAX_LEN,
+			     false, _metadata, self);
+	att_inval_sizes_test(&self->uvio_attest.meas_len, UVIO_ATT_MEASUREMENT_MAX_LEN,
+			     true, _metadata, self);
+	att_inval_sizes_test(&self->uvio_attest.arcb_len, UVIO_ATT_ARCB_MAX_LEN,
+			     true, _metadata, self);
+
+	self->uvio_attest.reserved136 = (uint16_t)-1;
+	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EINVAL);
+
+	memset(&self->uvio_attest, 0x11, sizeof(self->uvio_attest));
+	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+	ASSERT_EQ(rc, -1);
+}
+
+static void att_inval_addr_test(__u64 *addr, struct __test_metadata *_metadata,
+				FIXTURE_DATA(attest_fixture) *self)
+{
+	int rc, errno_cache;
+	__u64 tmp = *addr;
+
+	*addr = 0;
+	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+	*addr = self->fault_page;
+	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+	errno_cache = errno;
+	ASSERT_EQ(rc, -1);
+	ASSERT_EQ(errno_cache, EFAULT);
+	*addr = tmp;
+}
+
+TEST_F(attest_fixture, att_inval_addr)
+{
+	att_inval_addr_test(&self->uvio_attest.arcb_addr, _metadata, self);
+	att_inval_addr_test(&self->uvio_attest.add_data_addr, _metadata, self);
+	att_inval_addr_test(&self->uvio_attest.meas_addr, _metadata, self);
+}
+
+static void __attribute__((constructor)) __constructor_order_last(void)
+{

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 275

Quoted 10 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +10 -0
+	if (!__constructor_order)
+		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
+}
+
+int main(int argc, char **argv)
+{
+	int fd = open("/dev/uv", O_RDWR);
+
+	if (fd < 0) {
+		ksft_exit_skip("No uv-device.\n");

Please add information on which confg options need to be enabled to run this test - CONFIG_S390_UV_UAPI?

Also add a check and skip when run by non-root if this test requires root privileges.

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 276

Quoted 1 line
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +1 -0
+		ksft_exit(KSFT_SKIP);

You don't need this - ksft_exit_skip() calls exit(KSFT_SKIP)

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 278

Quoted 2 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +2 -0
+	}
+	close(fd);

Closing the file before test inocation?

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 280

Quoted 3 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +2 -0
+	return test_harness_run(argc, argv);
+}

thanks,
-- Shuah

#7 lore raw

Re: [PATCH 3/3] selftests: drivers/s390x: Add uvdevice tests

Hey Shuah,
thanks for your review.

Quoted 6 lines · Shuah Khan
Quoted 2 lines · Steffen Eiden

Adds some selftests to test ioctl error paths of the uv-uapi.

Please add information on how to run this test and example output.

I will do it in a v2

Quoted 251 lines
Quoted 251 lines

---
  MAINTAINERS                                   |   1 + tools/testing/selftests/Makefile              |   1 + tools/testing/selftests/drivers/.gitignore    |   1 + .../selftests/drivers/s390x/uvdevice/Makefile |  22 ++ .../selftests/drivers/s390x/uvdevice/config   |   1 + .../drivers/s390x/uvdevice/test_uvdevice.c    | 280 ++++++++++++++++++

  6 files changed, 306 insertions(+)

  create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/Makefile create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/config create mode 100644 tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c

MAINTAINERS +0 -0
index c7d8d0fe48cf..c6a0311c3fa8 100644
@@ -10462,6 +10462,7 @@ F:    arch/s390/kernel/uv.c

  F:    arch/s390/kvm/
  F:    arch/s390/mm/gmap.c
  F:    drivers/s390/char/uvdevice.c
+F:    tools/testing/selftests/drivers/s390x/uvdevice/ F:    tools/testing/selftests/kvm/*/s390x/ F:    tools/testing/selftests/kvm/s390x/

tools/testing/selftests/Makefile +0 -0
index c852eb40c4f7..3b8abaee9271 100644
@@ -9,6 +9,7 @@ TARGETS += core

  TARGETS += cpufreq
  TARGETS += cpu-hotplug
  TARGETS += drivers/dma-buf
+TARGETS += drivers/s390x/uvdevice
  TARGETS += efivarfs
  TARGETS += exec
  TARGETS += filesystems

tools/testing/selftests/drivers/.gitignore +0 -0
index ca74f2e1c719..09e23b5afa96 100644
@@ -1,2 +1,3 @@
   # SPDX-License-Identifier: GPL-2.0-only

  /dma-buf/udmabuf

tools/testing/selftests/drivers/.gitignore +1 -0
+/s390x/uvdevice/test_uvdevice
tools/testing/selftests/drivers/s390x/uvdevice/Makefile new file +22 -1
new file mode 100644
index 000000000000..5e701d2708d4
@@ -0,0 +1,22 @@
+include ../../../../../build/Build.include
+
+UNAME_M := $(shell uname -m)
+
+ifneq ($(UNAME_M),s390x)
+nothing:
+.PHONY: all clean run_tests install
+.SILENT:
+else
+
+TEST_GEN_PROGS := test_uvdevice
+
+top_srcdir ?= ../../../../../..
+KSFT_KHDR_INSTALL := 1
+khdr_dir = $(top_srcdir)/usr/include
+LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+
+CFLAGS += -Wall -Werror -static -I$(khdr_dir) 
-I$(LINUX_TOOL_ARCH_INCLUDE)
+
+include ../../../lib.mk
+
+endif
tools/testing/selftests/drivers/s390x/uvdevice/config new file +1 -0
new file mode 100644
index 000000000000..f28a04b99eff
@@ -0,0 +1 @@
+CONFIG_S390_UV_UAPI=y
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +49 -0
new file mode 100644
index 000000000000..f23663bcab03
@@ -0,0 +1,280 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  selftest for the Ultravisor UAPI device
+ *
+ *  Copyright IBM Corp. 2022
+ *  Author(s): Steffen Eiden <[email protected]>
+ */
+
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <asm/uvdevice.h>
+
+#include "../../../kselftest_harness.h"
+
+#define BUFFER_SIZE 0x200
+FIXTURE(uvio_fixture) {
+    int uv_fd;
+    struct uvio_ioctl_cb uvio_ioctl;
+    uint8_t buffer[BUFFER_SIZE];
+    __u64 fault_page;
+};
+
+FIXTURE_VARIANT(uvio_fixture) {
+    unsigned long ioctl_cmd;
+    uint32_t arg_size;
+};
+
+FIXTURE_VARIANT_ADD(uvio_fixture, qui) {
+    .ioctl_cmd = UVIO_IOCTL_QUI,
+    .arg_size = BUFFER_SIZE,
+};
+
+FIXTURE_VARIANT_ADD(uvio_fixture, att) {
+    .ioctl_cmd = UVIO_IOCTL_ATT,
+    .arg_size = sizeof(struct uvio_attest),
+};
+
+FIXTURE_SETUP(uvio_fixture)
+{
+    self->uv_fd = open("/dev/uv", O_RDWR);
+
+    self->uvio_ioctl.argument_addr = (__u64)self->buffer;
+    self->uvio_ioctl.argument_len = variant->arg_size;
+    self->fault_page =
+        (__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, 

MAP_ANONYMOUS, -1, 0);

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +43 -0
+}
+
+FIXTURE_TEARDOWN(uvio_fixture)
+{
+    if (self->uv_fd)
+        close(self->uv_fd);
+    munmap((void *)self->fault_page, (size_t)getpagesize());
+}
+
+TEST_F(uvio_fixture, fault_ioctl_arg)
+{
+    int rc, errno_cache;
+
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, NULL);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, self->fault_page);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+}
+
+TEST_F(uvio_fixture, fault_uvio_arg)
+{
+    int rc, errno_cache;
+
+    self->uvio_ioctl.argument_addr = 0;
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+
+    self->uvio_ioctl.argument_addr = self->fault_page;
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+}
+
+/*
+ * Test to verify that IOCTLs with invalid values in the 

ioctl_control block
+ * are rejected.
+ */

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +25 -0
+TEST_F(uvio_fixture, inval_ioctl_cb)
+{
+    int rc, errno_cache;
+
+    self->uvio_ioctl.argument_len = 0;
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);
+
+    self->uvio_ioctl.argument_len = (uint32_t)-1;
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);
+    self->uvio_ioctl.argument_len = variant->arg_size;
+
+    self->uvio_ioctl.flags = (uint32_t)-1;
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);
+    self->uvio_ioctl.flags = 0;
+
+    memset(self->uvio_ioctl.reserved14, 0xff, 

sizeof(self->uvio_ioctl.reserved14));
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl); +    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +39 -0
+
+    memset(&self->uvio_ioctl, 0x11, sizeof(self->uvio_ioctl));
+    rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
+    ASSERT_EQ(rc, -1);
+}
+
+TEST_F(uvio_fixture, inval_ioctl_cmd)
+{
+    int rc, errno_cache;
+    uint8_t nr = _IOC_NR(variant->ioctl_cmd);
+    unsigned long cmds[] = {
+        _IOWR('a', nr, struct uvio_ioctl_cb),
+        _IOWR(UVIO_TYPE_UVC, nr, int),
+        _IO(UVIO_TYPE_UVC, nr),
+        _IOR(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
+        _IOW(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
+    };
+
+    for (size_t i = 0; i < ARRAY_SIZE(cmds); i++) {
+        rc = ioctl(self->uv_fd, cmds[i], &self->uvio_ioctl);
+        errno_cache = errno;
+        ASSERT_EQ(rc, -1);
+        ASSERT_EQ(errno_cache, EINVAL);
+    }
+}
+
+struct test_attest_buffer {
+    uint8_t arcb[0x180];
+    uint8_t meas[64];
+    uint8_t add[32];
+};
+
+FIXTURE(attest_fixture) {
+    int uv_fd;
+    struct uvio_ioctl_cb uvio_ioctl;
+    struct uvio_attest uvio_attest;
+    struct test_attest_buffer attest_buffer;
+    __u64 fault_page;
+};
Quoted 8 lines

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 166

Quoted 4 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +4 -0
+
+FIXTURE_SETUP(attest_fixture)
+{
+    self->uv_fd = open("/dev/uv", O_RDWR);

So each test opne and closes the devuce. Can this file stay open for the duraction of the test run and close it in main() after test exits?

IIRC 'test_harness_run' will never return and instead exit the program.
Therefore, I cannot do anything after that function including closing files if I want a clean cleanup of my test.

Quoted 113 lines
Quoted 113 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +14 -0
+
+    self->uvio_ioctl.argument_addr = (__u64)&self->uvio_attest;
+    self->uvio_ioctl.argument_len = sizeof(self->uvio_attest);
+
+    self->uvio_attest.arcb_addr = (__u64)&self->attest_buffer.arcb;
+    self->uvio_attest.arcb_len = sizeof(self->attest_buffer.arcb);
+
+    self->uvio_attest.meas_addr = (__u64)&self->attest_buffer.meas;
+    self->uvio_attest.meas_len = sizeof(self->attest_buffer.meas);
+
+    self->uvio_attest.add_data_addr = (__u64)&self->attest_buffer.add;
+    self->uvio_attest.add_data_len = sizeof(self->attest_buffer.add);
+    self->fault_page =
+        (__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, 

MAP_ANONYMOUS, -1, 0);

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +10 -0
+}
+
+FIXTURE_TEARDOWN(attest_fixture)
+{
+    if (self->uv_fd)
+        close(self->uv_fd);
+    munmap((void *)self->fault_page, (size_t)getpagesize());
+}
+
+static void att_inval_sizes_test(uint32_t *size, uint32_t max_size, 

bool test_zero,
+                 struct __test_metadata *_metadata, +                 FIXTURE_DATA(attest_fixture) *self)

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +21 -0
+{
+    int rc, errno_cache;
+    uint32_t tmp = *size;
+
+    if (test_zero) {
+        *size = 0;
+        rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+        errno_cache = errno;
+        ASSERT_EQ(rc, -1);
+        ASSERT_EQ(errno_cache, EINVAL);
+    }
+    *size = max_size + 1;
+    rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);
+    *size = tmp;
+}
+
+/*
+ * Test to verify that attestation IOCTLs with invalid values in the 

UVIO
+ * attestation control block are rejected.
+ */

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +5 -0
+TEST_F(attest_fixture, att_inval_request)
+{
+    int rc, errno_cache;
+
+    att_inval_sizes_test(&self->uvio_attest.add_data_len, 

UVIO_ATT_ADDITIONAL_MAX_LEN,
+                 false, _metadata, self);
+    att_inval_sizes_test(&self->uvio_attest.meas_len, UVIO_ATT_MEASUREMENT_MAX_LEN,
+                 true, _metadata, self);
+    att_inval_sizes_test(&self->uvio_attest.arcb_len, UVIO_ATT_ARCB_MAX_LEN,
+                 true, _metadata, self);

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +13 -0
+
+    self->uvio_attest.reserved136 = (uint16_t)-1;
+    rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EINVAL);
+
+    memset(&self->uvio_attest, 0x11, sizeof(self->uvio_attest));
+    rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+    ASSERT_EQ(rc, -1);
+}
+
+static void att_inval_addr_test(__u64 *addr, struct __test_metadata 

*_metadata,
+                FIXTURE_DATA(attest_fixture) *self)

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +21 -0
+{
+    int rc, errno_cache;
+    __u64 tmp = *addr;
+
+    *addr = 0;
+    rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+    *addr = self->fault_page;
+    rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
+    errno_cache = errno;
+    ASSERT_EQ(rc, -1);
+    ASSERT_EQ(errno_cache, EFAULT);
+    *addr = tmp;
+}
+
+TEST_F(attest_fixture, att_inval_addr)
+{
+    att_inval_addr_test(&self->uvio_attest.arcb_addr, _metadata, self);
+    att_inval_addr_test(&self->uvio_attest.add_data_addr, _metadata, 

self);
+    att_inval_addr_test(&self->uvio_attest.meas_addr, _metadata, self);

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +9 -0
+}
+
+static void __attribute__((constructor)) __constructor_order_last(void)
+{
+    if (!__constructor_order)
+        __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
+}
+
+int main(int argc, char **argv)
Quoted 8 lines

tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c line 275

Quoted 5 lines
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c new file +5 -0
+{
+    int fd = open("/dev/uv", O_RDWR);
+
+    if (fd < 0) {
+        ksft_exit_skip("No uv-device.\n");

Please add information on which confg options need to be enabled to run this test - CONFIG_S390_UV_UAPI?

OK.

Quoted 3 lines

Also add a check and skip when run by non-root if this test requires root privileges.

The test just needs RDWR access to /dev/uv.

Quoted 4 lines
Quoted 2 lines

+        ksft_exit(KSFT_SKIP);

You don't need this - ksft_exit_skip() calls exit(KSFT_SKIP)

thanks, I missed that.

Quoted 6 lines
Quoted 4 lines

+    }
+    close(fd);

Closing the file before test inocation?

See comment about fixture setup/cleanup.
If you prefer, I can skip the close and use fd in the tests instead of self->uv_fd. However, I am unabale to do an explicit close to /dev/uv after running the tests.

Quoted 7 lines
Quoted 4 lines

+    return test_harness_run(argc, argv);
+}

thanks,
-- Shuah

Steffen