1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2022
6 * This file provides a Linux misc device to give userspace access to some
7 * Ultravisor (UV) functions. The device only accepts IOCTLs and will only
8 * be present if the Ultravisor facility (158) is present.
10 * When userspace sends a valid IOCTL uvdevice will copy the input data to
11 * kernel space, do some basic validity checks to avoid kernel/system
12 * corruption. Any other check that the Ultravisor does will not be done by
13 * the uvdevice to keep changes minimal when adding new functionalities
14 * to existing UV-calls.
15 * After the checks uvdevice builds a corresponding
16 * Ultravisor Call Control Block, and sends the request to the Ultravisor.
17 * Then, it copies the response, including the return codes, back to userspace.
18 * It is the responsibility of the userspace to check for any error issued
19 * by UV and to interpret the UV response. The uvdevice acts as a communication
20 * channel for userspace to the Ultravisor.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/types.h>
27 #include <linux/stddef.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/cpufeature.h>
32 #include <asm/uvdevice.h>
35 #define BIT_UVIO_INTERNAL U32_MAX
36 /* Mapping from IOCTL-nr to UVC-bit */
37 static const u32 ioctl_nr_to_uvc_bit[] __initconst = {
38 [UVIO_IOCTL_UVDEV_INFO_NR] = BIT_UVIO_INTERNAL,
39 [UVIO_IOCTL_ATT_NR] = BIT_UVC_CMD_RETR_ATTEST,
40 [UVIO_IOCTL_ADD_SECRET_NR] = BIT_UVC_CMD_ADD_SECRET,
41 [UVIO_IOCTL_LIST_SECRETS_NR] = BIT_UVC_CMD_LIST_SECRETS,
42 [UVIO_IOCTL_LOCK_SECRETS_NR] = BIT_UVC_CMD_LOCK_SECRETS,
45 static_assert(ARRAY_SIZE(ioctl_nr_to_uvc_bit) == UVIO_IOCTL_NUM_IOCTLS);
47 static struct uvio_uvdev_info uvdev_info = {
48 .supp_uvio_cmds = GENMASK_ULL(UVIO_IOCTL_NUM_IOCTLS - 1, 0),
51 static void __init set_supp_uv_cmds(unsigned long *supp_uv_cmds)
55 for (i = 0; i < UVIO_IOCTL_NUM_IOCTLS; i++) {
56 if (ioctl_nr_to_uvc_bit[i] == BIT_UVIO_INTERNAL)
58 if (!test_bit_inv(ioctl_nr_to_uvc_bit[i], uv_info.inst_calls_list))
60 __set_bit(i, supp_uv_cmds);
65 * uvio_uvdev_info() - get information about the uvdevice
67 * @uv_ioctl: ioctl control block
69 * Lists all IOCTLs that are supported by this uvdevice
71 static int uvio_uvdev_info(struct uvio_ioctl_cb *uv_ioctl)
73 void __user *user_buf_arg = (void __user *)uv_ioctl->argument_addr;
75 if (uv_ioctl->argument_len < sizeof(uvdev_info))
77 if (copy_to_user(user_buf_arg, &uvdev_info, sizeof(uvdev_info)))
80 uv_ioctl->uv_rc = UVC_RC_EXECUTED;
84 static int uvio_build_uvcb_attest(struct uv_cb_attest *uvcb_attest, u8 *arcb,
85 u8 *meas, u8 *add_data, struct uvio_attest *uvio_attest)
87 void __user *user_buf_arcb = (void __user *)uvio_attest->arcb_addr;
89 if (copy_from_user(arcb, user_buf_arcb, uvio_attest->arcb_len))
92 uvcb_attest->header.len = sizeof(*uvcb_attest);
93 uvcb_attest->header.cmd = UVC_CMD_RETR_ATTEST;
94 uvcb_attest->arcb_addr = (u64)arcb;
95 uvcb_attest->cont_token = 0;
96 uvcb_attest->user_data_len = uvio_attest->user_data_len;
97 memcpy(uvcb_attest->user_data, uvio_attest->user_data, sizeof(uvcb_attest->user_data));
98 uvcb_attest->meas_len = uvio_attest->meas_len;
99 uvcb_attest->meas_addr = (u64)meas;
100 uvcb_attest->add_data_len = uvio_attest->add_data_len;
101 uvcb_attest->add_data_addr = (u64)add_data;
106 static int uvio_copy_attest_result_to_user(struct uv_cb_attest *uvcb_attest,
107 struct uvio_ioctl_cb *uv_ioctl,
108 u8 *measurement, u8 *add_data,
109 struct uvio_attest *uvio_attest)
111 struct uvio_attest __user *user_uvio_attest = (void __user *)uv_ioctl->argument_addr;
112 void __user *user_buf_add = (void __user *)uvio_attest->add_data_addr;
113 void __user *user_buf_meas = (void __user *)uvio_attest->meas_addr;
114 void __user *user_buf_uid = &user_uvio_attest->config_uid;
116 if (copy_to_user(user_buf_meas, measurement, uvio_attest->meas_len))
118 if (add_data && copy_to_user(user_buf_add, add_data, uvio_attest->add_data_len))
120 if (copy_to_user(user_buf_uid, uvcb_attest->config_uid, sizeof(uvcb_attest->config_uid)))
125 static int get_uvio_attest(struct uvio_ioctl_cb *uv_ioctl, struct uvio_attest *uvio_attest)
127 u8 __user *user_arg_buf = (u8 __user *)uv_ioctl->argument_addr;
129 if (copy_from_user(uvio_attest, user_arg_buf, sizeof(*uvio_attest)))
132 if (uvio_attest->arcb_len > UVIO_ATT_ARCB_MAX_LEN)
134 if (uvio_attest->arcb_len == 0)
136 if (uvio_attest->meas_len > UVIO_ATT_MEASUREMENT_MAX_LEN)
138 if (uvio_attest->meas_len == 0)
140 if (uvio_attest->add_data_len > UVIO_ATT_ADDITIONAL_MAX_LEN)
142 if (uvio_attest->reserved136)
148 * uvio_attestation() - Perform a Retrieve Attestation Measurement UVC.
150 * @uv_ioctl: ioctl control block
152 * uvio_attestation() does a Retrieve Attestation Measurement Ultravisor Call.
153 * It verifies that the given userspace addresses are valid and request sizes
154 * are sane. Every other check is made by the Ultravisor (UV) and won't result
155 * in a negative return value. It copies the input to kernelspace, builds the
156 * request, sends the UV-call, and copies the result to userspace.
158 * The Attestation Request has two input and two outputs.
159 * ARCB and User Data are inputs for the UV generated by userspace.
160 * Measurement and Additional Data are outputs for userspace generated by UV.
162 * The Attestation Request Control Block (ARCB) is a cryptographically verified
163 * and secured request to UV and User Data is some plaintext data which is
164 * going to be included in the Attestation Measurement calculation.
166 * Measurement is a cryptographic measurement of the callers properties,
167 * optional data configured by the ARCB and the user data. If specified by the
168 * ARCB, UV will add some Additional Data to the measurement calculation.
169 * This Additional Data is then returned as well.
171 * If the Retrieve Attestation Measurement UV facility is not present,
172 * UV will return invalid command rc. This won't be fenced in the driver
173 * and does not result in a negative return value.
175 * Context: might sleep
177 * Return: 0 on success or a negative error code on error.
179 static int uvio_attestation(struct uvio_ioctl_cb *uv_ioctl)
181 struct uv_cb_attest *uvcb_attest = NULL;
182 struct uvio_attest *uvio_attest = NULL;
183 u8 *measurement = NULL;
189 if (uv_ioctl->argument_len != sizeof(*uvio_attest))
193 uvio_attest = kzalloc(sizeof(*uvio_attest), GFP_KERNEL);
197 ret = get_uvio_attest(uv_ioctl, uvio_attest);
202 arcb = kvzalloc(uvio_attest->arcb_len, GFP_KERNEL);
203 measurement = kvzalloc(uvio_attest->meas_len, GFP_KERNEL);
204 if (!arcb || !measurement)
207 if (uvio_attest->add_data_len) {
208 add_data = kvzalloc(uvio_attest->add_data_len, GFP_KERNEL);
213 uvcb_attest = kzalloc(sizeof(*uvcb_attest), GFP_KERNEL);
217 ret = uvio_build_uvcb_attest(uvcb_attest, arcb, measurement, add_data, uvio_attest);
221 uv_call_sched(0, (u64)uvcb_attest);
223 uv_ioctl->uv_rc = uvcb_attest->header.rc;
224 uv_ioctl->uv_rrc = uvcb_attest->header.rrc;
226 ret = uvio_copy_attest_result_to_user(uvcb_attest, uv_ioctl, measurement, add_data,
237 /** uvio_add_secret() - perform an Add Secret UVC
239 * @uv_ioctl: ioctl control block
241 * uvio_add_secret() performs the Add Secret Ultravisor Call.
243 * The given userspace argument address and size are verified to be
244 * valid but every other check is made by the Ultravisor
245 * (UV). Therefore UV errors won't result in a negative return
246 * value. The request is then copied to kernelspace, the UV-call is
247 * performed and the results are copied back to userspace.
249 * The argument has to point to an Add Secret Request Control Block
250 * which is an encrypted and cryptographically verified request that
251 * inserts a protected guest's secrets into the Ultravisor for later
254 * If the Add Secret UV facility is not present, UV will return
255 * invalid command rc. This won't be fenced in the driver and does not
256 * result in a negative return value.
258 * Context: might sleep
260 * Return: 0 on success or a negative error code on error.
262 static int uvio_add_secret(struct uvio_ioctl_cb *uv_ioctl)
264 void __user *user_buf_arg = (void __user *)uv_ioctl->argument_addr;
265 struct uv_cb_guest_addr uvcb = {
266 .header.len = sizeof(uvcb),
267 .header.cmd = UVC_CMD_ADD_SECRET,
272 if (uv_ioctl->argument_len > UVIO_ADD_SECRET_MAX_LEN)
274 if (uv_ioctl->argument_len == 0)
277 asrcb = kvzalloc(uv_ioctl->argument_len, GFP_KERNEL);
282 if (copy_from_user(asrcb, user_buf_arg, uv_ioctl->argument_len))
286 uvcb.addr = (u64)asrcb;
287 uv_call_sched(0, (u64)&uvcb);
288 uv_ioctl->uv_rc = uvcb.header.rc;
289 uv_ioctl->uv_rrc = uvcb.header.rrc;
296 /** uvio_list_secrets() - perform a List Secret UVC
297 * @uv_ioctl: ioctl control block
299 * uvio_list_secrets() performs the List Secret Ultravisor Call. It verifies
300 * that the given userspace argument address is valid and its size is sane.
301 * Every other check is made by the Ultravisor (UV) and won't result in a
302 * negative return value. It builds the request, performs the UV-call, and
303 * copies the result to userspace.
305 * The argument specifies the location for the result of the UV-Call.
307 * If the List Secrets UV facility is not present, UV will return invalid
308 * command rc. This won't be fenced in the driver and does not result in a
309 * negative return value.
311 * Context: might sleep
313 * Return: 0 on success or a negative error code on error.
315 static int uvio_list_secrets(struct uvio_ioctl_cb *uv_ioctl)
317 void __user *user_buf_arg = (void __user *)uv_ioctl->argument_addr;
318 struct uv_cb_guest_addr uvcb = {
319 .header.len = sizeof(uvcb),
320 .header.cmd = UVC_CMD_LIST_SECRETS,
322 void *secrets = NULL;
325 if (uv_ioctl->argument_len != UVIO_LIST_SECRETS_LEN)
328 secrets = kvzalloc(UVIO_LIST_SECRETS_LEN, GFP_KERNEL);
332 uvcb.addr = (u64)secrets;
333 uv_call_sched(0, (u64)&uvcb);
334 uv_ioctl->uv_rc = uvcb.header.rc;
335 uv_ioctl->uv_rrc = uvcb.header.rrc;
337 if (copy_to_user(user_buf_arg, secrets, UVIO_LIST_SECRETS_LEN))
344 /** uvio_lock_secrets() - perform a Lock Secret Store UVC
345 * @uv_ioctl: ioctl control block
347 * uvio_lock_secrets() performs the Lock Secret Store Ultravisor Call. It
348 * performs the UV-call and copies the return codes to the ioctl control block.
349 * After this call was dispatched successfully every following Add Secret UVC
350 * and Lock Secrets UVC will fail with return code 0x102.
352 * The argument address and size must be 0.
354 * If the Lock Secrets UV facility is not present, UV will return invalid
355 * command rc. This won't be fenced in the driver and does not result in a
356 * negative return value.
358 * Context: might sleep
360 * Return: 0 on success or a negative error code on error.
362 static int uvio_lock_secrets(struct uvio_ioctl_cb *ioctl)
364 struct uv_cb_nodata uvcb = {
365 .header.len = sizeof(uvcb),
366 .header.cmd = UVC_CMD_LOCK_SECRETS,
369 if (ioctl->argument_addr || ioctl->argument_len)
372 uv_call(0, (u64)&uvcb);
373 ioctl->uv_rc = uvcb.header.rc;
374 ioctl->uv_rrc = uvcb.header.rrc;
379 static int uvio_copy_and_check_ioctl(struct uvio_ioctl_cb *ioctl, void __user *argp,
382 u8 nr = _IOC_NR(cmd);
384 if (_IOC_DIR(cmd) != (_IOC_READ | _IOC_WRITE))
386 if (_IOC_TYPE(cmd) != UVIO_TYPE_UVC)
388 if (nr >= UVIO_IOCTL_NUM_IOCTLS)
390 if (_IOC_SIZE(cmd) != sizeof(*ioctl))
392 if (copy_from_user(ioctl, argp, sizeof(*ioctl)))
394 if (ioctl->flags != 0)
396 if (memchr_inv(ioctl->reserved14, 0, sizeof(ioctl->reserved14)))
403 * IOCTL entry point for the Ultravisor device.
405 static long uvio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
407 void __user *argp = (void __user *)arg;
408 struct uvio_ioctl_cb uv_ioctl = { };
412 nr = uvio_copy_and_check_ioctl(&uv_ioctl, argp, cmd);
417 case UVIO_IOCTL_UVDEV_INFO_NR:
418 ret = uvio_uvdev_info(&uv_ioctl);
420 case UVIO_IOCTL_ATT_NR:
421 ret = uvio_attestation(&uv_ioctl);
423 case UVIO_IOCTL_ADD_SECRET_NR:
424 ret = uvio_add_secret(&uv_ioctl);
426 case UVIO_IOCTL_LIST_SECRETS_NR:
427 ret = uvio_list_secrets(&uv_ioctl);
429 case UVIO_IOCTL_LOCK_SECRETS_NR:
430 ret = uvio_lock_secrets(&uv_ioctl);
439 if (copy_to_user(argp, &uv_ioctl, sizeof(uv_ioctl)))
445 static const struct file_operations uvio_dev_fops = {
446 .owner = THIS_MODULE,
447 .unlocked_ioctl = uvio_ioctl,
451 static struct miscdevice uvio_dev_miscdev = {
452 .minor = MISC_DYNAMIC_MINOR,
453 .name = UVIO_DEVICE_NAME,
454 .fops = &uvio_dev_fops,
457 static void __exit uvio_dev_exit(void)
459 misc_deregister(&uvio_dev_miscdev);
462 static int __init uvio_dev_init(void)
464 set_supp_uv_cmds((unsigned long *)&uvdev_info.supp_uv_cmds);
465 return misc_register(&uvio_dev_miscdev);
468 module_cpu_feature_match(S390_CPU_FEATURE_UV, uvio_dev_init);
469 module_exit(uvio_dev_exit);
471 MODULE_AUTHOR("IBM Corporation");
472 MODULE_LICENSE("GPL");
473 MODULE_DESCRIPTION("Ultravisor UAPI driver");