1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2022, 2024
4 * Author(s): Steffen Eiden <seiden@linux.ibm.com>
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
,
43 [UVIO_IOCTL_RETR_SECRET_NR
] = BIT_UVC_CMD_RETR_ATTEST
,
46 static_assert(ARRAY_SIZE(ioctl_nr_to_uvc_bit
) == UVIO_IOCTL_NUM_IOCTLS
);
48 static struct uvio_uvdev_info uvdev_info
= {
49 .supp_uvio_cmds
= GENMASK_ULL(UVIO_IOCTL_NUM_IOCTLS
- 1, 0),
52 static void __init
set_supp_uv_cmds(unsigned long *supp_uv_cmds
)
56 for (i
= 0; i
< UVIO_IOCTL_NUM_IOCTLS
; i
++) {
57 if (ioctl_nr_to_uvc_bit
[i
] == BIT_UVIO_INTERNAL
)
59 if (!test_bit_inv(ioctl_nr_to_uvc_bit
[i
], uv_info
.inst_calls_list
))
61 __set_bit(i
, supp_uv_cmds
);
66 * uvio_uvdev_info() - Get information about the uvdevice
68 * @uv_ioctl: ioctl control block
70 * Lists all IOCTLs that are supported by this uvdevice
72 * Return: 0 on success or a negative error code on error
74 static int uvio_uvdev_info(struct uvio_ioctl_cb
*uv_ioctl
)
76 void __user
*user_buf_arg
= (void __user
*)uv_ioctl
->argument_addr
;
78 if (uv_ioctl
->argument_len
< sizeof(uvdev_info
))
80 if (copy_to_user(user_buf_arg
, &uvdev_info
, sizeof(uvdev_info
)))
83 uv_ioctl
->uv_rc
= UVC_RC_EXECUTED
;
87 static int uvio_build_uvcb_attest(struct uv_cb_attest
*uvcb_attest
, u8
*arcb
,
88 u8
*meas
, u8
*add_data
, struct uvio_attest
*uvio_attest
)
90 void __user
*user_buf_arcb
= (void __user
*)uvio_attest
->arcb_addr
;
92 if (copy_from_user(arcb
, user_buf_arcb
, uvio_attest
->arcb_len
))
95 uvcb_attest
->header
.len
= sizeof(*uvcb_attest
);
96 uvcb_attest
->header
.cmd
= UVC_CMD_RETR_ATTEST
;
97 uvcb_attest
->arcb_addr
= (u64
)arcb
;
98 uvcb_attest
->cont_token
= 0;
99 uvcb_attest
->user_data_len
= uvio_attest
->user_data_len
;
100 memcpy(uvcb_attest
->user_data
, uvio_attest
->user_data
, sizeof(uvcb_attest
->user_data
));
101 uvcb_attest
->meas_len
= uvio_attest
->meas_len
;
102 uvcb_attest
->meas_addr
= (u64
)meas
;
103 uvcb_attest
->add_data_len
= uvio_attest
->add_data_len
;
104 uvcb_attest
->add_data_addr
= (u64
)add_data
;
109 static int uvio_copy_attest_result_to_user(struct uv_cb_attest
*uvcb_attest
,
110 struct uvio_ioctl_cb
*uv_ioctl
,
111 u8
*measurement
, u8
*add_data
,
112 struct uvio_attest
*uvio_attest
)
114 struct uvio_attest __user
*user_uvio_attest
= (void __user
*)uv_ioctl
->argument_addr
;
115 u32 __user
*user_buf_add_len
= (u32 __user
*)&user_uvio_attest
->add_data_len
;
116 void __user
*user_buf_add
= (void __user
*)uvio_attest
->add_data_addr
;
117 void __user
*user_buf_meas
= (void __user
*)uvio_attest
->meas_addr
;
118 void __user
*user_buf_uid
= &user_uvio_attest
->config_uid
;
120 if (copy_to_user(user_buf_meas
, measurement
, uvio_attest
->meas_len
))
122 if (add_data
&& copy_to_user(user_buf_add
, add_data
, uvio_attest
->add_data_len
))
124 if (put_user(uvio_attest
->add_data_len
, user_buf_add_len
))
126 if (copy_to_user(user_buf_uid
, uvcb_attest
->config_uid
, sizeof(uvcb_attest
->config_uid
)))
131 static int get_uvio_attest(struct uvio_ioctl_cb
*uv_ioctl
, struct uvio_attest
*uvio_attest
)
133 u8 __user
*user_arg_buf
= (u8 __user
*)uv_ioctl
->argument_addr
;
135 if (copy_from_user(uvio_attest
, user_arg_buf
, sizeof(*uvio_attest
)))
138 if (uvio_attest
->arcb_len
> UVIO_ATT_ARCB_MAX_LEN
)
140 if (uvio_attest
->arcb_len
== 0)
142 if (uvio_attest
->meas_len
> UVIO_ATT_MEASUREMENT_MAX_LEN
)
144 if (uvio_attest
->meas_len
== 0)
146 if (uvio_attest
->add_data_len
> UVIO_ATT_ADDITIONAL_MAX_LEN
)
148 if (uvio_attest
->reserved136
)
154 * uvio_attestation() - Perform a Retrieve Attestation Measurement UVC.
156 * @uv_ioctl: ioctl control block
158 * uvio_attestation() does a Retrieve Attestation Measurement Ultravisor Call.
159 * It verifies that the given userspace addresses are valid and request sizes
160 * are sane. Every other check is made by the Ultravisor (UV) and won't result
161 * in a negative return value. It copies the input to kernelspace, builds the
162 * request, sends the UV-call, and copies the result to userspace.
164 * The Attestation Request has two input and two outputs.
165 * ARCB and User Data are inputs for the UV generated by userspace.
166 * Measurement and Additional Data are outputs for userspace generated by UV.
168 * The Attestation Request Control Block (ARCB) is a cryptographically verified
169 * and secured request to UV and User Data is some plaintext data which is
170 * going to be included in the Attestation Measurement calculation.
172 * Measurement is a cryptographic measurement of the callers properties,
173 * optional data configured by the ARCB and the user data. If specified by the
174 * ARCB, UV will add some Additional Data to the measurement calculation.
175 * This Additional Data is then returned as well.
177 * If the Retrieve Attestation Measurement UV facility is not present,
178 * UV will return invalid command rc. This won't be fenced in the driver
179 * and does not result in a negative return value.
181 * Context: might sleep
183 * Return: 0 on success or a negative error code on error
185 static int uvio_attestation(struct uvio_ioctl_cb
*uv_ioctl
)
187 struct uv_cb_attest
*uvcb_attest
= NULL
;
188 struct uvio_attest
*uvio_attest
= NULL
;
189 u8
*measurement
= NULL
;
195 if (uv_ioctl
->argument_len
!= sizeof(*uvio_attest
))
199 uvio_attest
= kzalloc(sizeof(*uvio_attest
), GFP_KERNEL
);
203 ret
= get_uvio_attest(uv_ioctl
, uvio_attest
);
208 arcb
= kvzalloc(uvio_attest
->arcb_len
, GFP_KERNEL
);
209 measurement
= kvzalloc(uvio_attest
->meas_len
, GFP_KERNEL
);
210 if (!arcb
|| !measurement
)
213 if (uvio_attest
->add_data_len
) {
214 add_data
= kvzalloc(uvio_attest
->add_data_len
, GFP_KERNEL
);
219 uvcb_attest
= kzalloc(sizeof(*uvcb_attest
), GFP_KERNEL
);
223 ret
= uvio_build_uvcb_attest(uvcb_attest
, arcb
, measurement
, add_data
, uvio_attest
);
227 uv_call_sched(0, (u64
)uvcb_attest
);
229 uv_ioctl
->uv_rc
= uvcb_attest
->header
.rc
;
230 uv_ioctl
->uv_rrc
= uvcb_attest
->header
.rrc
;
232 ret
= uvio_copy_attest_result_to_user(uvcb_attest
, uv_ioctl
, measurement
, add_data
,
244 * uvio_add_secret() - Perform an Add Secret UVC
246 * @uv_ioctl: ioctl control block
248 * uvio_add_secret() performs the Add Secret Ultravisor Call.
250 * The given userspace argument address and size are verified to be
251 * valid but every other check is made by the Ultravisor
252 * (UV). Therefore UV errors won't result in a negative return
253 * value. The request is then copied to kernelspace, the UV-call is
254 * performed and the results are copied back to userspace.
256 * The argument has to point to an Add Secret Request Control Block
257 * which is an encrypted and cryptographically verified request that
258 * inserts a protected guest's secrets into the Ultravisor for later
261 * If the Add Secret UV facility is not present, UV will return
262 * invalid command rc. This won't be fenced in the driver and does not
263 * result in a negative return value.
265 * Context: might sleep
267 * Return: 0 on success or a negative error code on error
269 static int uvio_add_secret(struct uvio_ioctl_cb
*uv_ioctl
)
271 void __user
*user_buf_arg
= (void __user
*)uv_ioctl
->argument_addr
;
272 struct uv_cb_guest_addr uvcb
= {
273 .header
.len
= sizeof(uvcb
),
274 .header
.cmd
= UVC_CMD_ADD_SECRET
,
279 if (uv_ioctl
->argument_len
> UVIO_ADD_SECRET_MAX_LEN
)
281 if (uv_ioctl
->argument_len
== 0)
284 asrcb
= kvzalloc(uv_ioctl
->argument_len
, GFP_KERNEL
);
289 if (copy_from_user(asrcb
, user_buf_arg
, uv_ioctl
->argument_len
))
293 uvcb
.addr
= (u64
)asrcb
;
294 uv_call_sched(0, (u64
)&uvcb
);
295 uv_ioctl
->uv_rc
= uvcb
.header
.rc
;
296 uv_ioctl
->uv_rrc
= uvcb
.header
.rrc
;
304 * Do the actual secret list creation. Calls the list secrets UVC until there
305 * is no more space in the user buffer, or the list ends.
307 static int uvio_get_list(void *zpage
, struct uvio_ioctl_cb
*uv_ioctl
)
309 const size_t data_off
= offsetof(struct uv_secret_list
, secrets
);
310 u8 __user
*user_buf
= (u8 __user
*)uv_ioctl
->argument_addr
;
311 struct uv_secret_list
*list
= zpage
;
312 u16 num_secrets_stored
= 0;
313 size_t user_off
= data_off
;
317 uv_list_secrets(list
, list
->next_secret_idx
, &uv_ioctl
->uv_rc
,
319 if (uv_ioctl
->uv_rc
!= UVC_RC_EXECUTED
&&
320 uv_ioctl
->uv_rc
!= UVC_RC_MORE_DATA
)
323 copy_len
= sizeof(list
->secrets
[0]) * list
->num_secr_stored
;
324 if (copy_to_user(user_buf
+ user_off
, list
->secrets
, copy_len
))
327 user_off
+= copy_len
;
328 num_secrets_stored
+= list
->num_secr_stored
;
329 } while (uv_ioctl
->uv_rc
== UVC_RC_MORE_DATA
&&
330 user_off
+ sizeof(*list
) <= uv_ioctl
->argument_len
);
332 list
->num_secr_stored
= num_secrets_stored
;
333 if (copy_to_user(user_buf
, list
, data_off
))
339 * uvio_list_secrets() - Perform a List Secret UVC
341 * @uv_ioctl: ioctl control block
343 * uvio_list_secrets() performs the List Secret Ultravisor Call. It verifies
344 * that the given userspace argument address is valid and its size is sane.
345 * Every other check is made by the Ultravisor (UV) and won't result in a
346 * negative return value. It builds the request, performs the UV-call, and
347 * copies the result to userspace.
349 * The argument specifies the location for the result of the UV-Call.
351 * Argument length must be a multiple of a page.
352 * The list secrets IOCTL will call the list UVC multiple times and fill
353 * the provided user-buffer with list elements until either the list ends or
354 * the buffer is full. The list header is merged over all list header from the
357 * If the List Secrets UV facility is not present, UV will return invalid
358 * command rc. This won't be fenced in the driver and does not result in a
359 * negative return value.
361 * Context: might sleep
363 * Return: 0 on success or a negative error code on error
365 static int uvio_list_secrets(struct uvio_ioctl_cb
*uv_ioctl
)
370 if (uv_ioctl
->argument_len
== 0 ||
371 uv_ioctl
->argument_len
% UVIO_LIST_SECRETS_LEN
!= 0)
374 zpage
= (void *)get_zeroed_page(GFP_KERNEL
);
378 rc
= uvio_get_list(zpage
, uv_ioctl
);
380 free_page((unsigned long)zpage
);
385 * uvio_lock_secrets() - Perform a Lock Secret Store UVC
387 * @ioctl: ioctl control block
389 * uvio_lock_secrets() performs the Lock Secret Store Ultravisor Call. It
390 * performs the UV-call and copies the return codes to the ioctl control block.
391 * After this call was dispatched successfully every following Add Secret UVC
392 * and Lock Secrets UVC will fail with return code 0x102.
394 * The argument address and size must be 0.
396 * If the Lock Secrets UV facility is not present, UV will return invalid
397 * command rc. This won't be fenced in the driver and does not result in a
398 * negative return value.
400 * Context: might sleep
402 * Return: 0 on success or a negative error code on error
404 static int uvio_lock_secrets(struct uvio_ioctl_cb
*ioctl
)
406 struct uv_cb_nodata uvcb
= {
407 .header
.len
= sizeof(uvcb
),
408 .header
.cmd
= UVC_CMD_LOCK_SECRETS
,
411 if (ioctl
->argument_addr
|| ioctl
->argument_len
)
414 uv_call(0, (u64
)&uvcb
);
415 ioctl
->uv_rc
= uvcb
.header
.rc
;
416 ioctl
->uv_rrc
= uvcb
.header
.rrc
;
422 * uvio_retr_secret() - Perform a retrieve secret UVC
424 * @uv_ioctl: ioctl control block.
426 * uvio_retr_secret() performs the Retrieve Secret Ultravisor Call.
427 * The first two bytes of the argument specify the index of the secret to be
428 * retrieved. The retrieved secret is copied into the argument buffer if there
430 * The argument length must be at least two bytes and at max 8192 bytes.
432 * Context: might sleep
434 * Return: 0 on success or a negative error code on error
436 static int uvio_retr_secret(struct uvio_ioctl_cb
*uv_ioctl
)
438 u16 __user
*user_index
= (u16 __user
*)uv_ioctl
->argument_addr
;
439 struct uv_cb_retr_secr uvcb
= {
440 .header
.len
= sizeof(uvcb
),
441 .header
.cmd
= UVC_CMD_RETR_SECRET
,
443 u32 buf_len
= uv_ioctl
->argument_len
;
447 if (buf_len
> UVIO_RETR_SECRET_MAX_LEN
|| buf_len
< sizeof(*user_index
))
450 buf
= kvzalloc(buf_len
, GFP_KERNEL
);
455 if (get_user(uvcb
.secret_idx
, user_index
))
458 uvcb
.buf_addr
= (u64
)buf
;
459 uvcb
.buf_size
= buf_len
;
460 uv_call_sched(0, (u64
)&uvcb
);
462 if (copy_to_user((__user
void *)uv_ioctl
->argument_addr
, buf
, buf_len
))
466 uv_ioctl
->uv_rc
= uvcb
.header
.rc
;
467 uv_ioctl
->uv_rrc
= uvcb
.header
.rrc
;
470 kvfree_sensitive(buf
, buf_len
);
474 static int uvio_copy_and_check_ioctl(struct uvio_ioctl_cb
*ioctl
, void __user
*argp
,
477 u8 nr
= _IOC_NR(cmd
);
479 if (_IOC_DIR(cmd
) != (_IOC_READ
| _IOC_WRITE
))
481 if (_IOC_TYPE(cmd
) != UVIO_TYPE_UVC
)
483 if (nr
>= UVIO_IOCTL_NUM_IOCTLS
)
485 if (_IOC_SIZE(cmd
) != sizeof(*ioctl
))
487 if (copy_from_user(ioctl
, argp
, sizeof(*ioctl
)))
489 if (ioctl
->flags
!= 0)
491 if (memchr_inv(ioctl
->reserved14
, 0, sizeof(ioctl
->reserved14
)))
498 * IOCTL entry point for the Ultravisor device.
500 static long uvio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
502 void __user
*argp
= (void __user
*)arg
;
503 struct uvio_ioctl_cb uv_ioctl
= { };
507 nr
= uvio_copy_and_check_ioctl(&uv_ioctl
, argp
, cmd
);
512 case UVIO_IOCTL_UVDEV_INFO_NR
:
513 ret
= uvio_uvdev_info(&uv_ioctl
);
515 case UVIO_IOCTL_ATT_NR
:
516 ret
= uvio_attestation(&uv_ioctl
);
518 case UVIO_IOCTL_ADD_SECRET_NR
:
519 ret
= uvio_add_secret(&uv_ioctl
);
521 case UVIO_IOCTL_LIST_SECRETS_NR
:
522 ret
= uvio_list_secrets(&uv_ioctl
);
524 case UVIO_IOCTL_LOCK_SECRETS_NR
:
525 ret
= uvio_lock_secrets(&uv_ioctl
);
527 case UVIO_IOCTL_RETR_SECRET_NR
:
528 ret
= uvio_retr_secret(&uv_ioctl
);
537 if (copy_to_user(argp
, &uv_ioctl
, sizeof(uv_ioctl
)))
543 static const struct file_operations uvio_dev_fops
= {
544 .owner
= THIS_MODULE
,
545 .unlocked_ioctl
= uvio_ioctl
,
548 static struct miscdevice uvio_dev_miscdev
= {
549 .minor
= MISC_DYNAMIC_MINOR
,
550 .name
= UVIO_DEVICE_NAME
,
551 .fops
= &uvio_dev_fops
,
554 static void __exit
uvio_dev_exit(void)
556 misc_deregister(&uvio_dev_miscdev
);
559 static int __init
uvio_dev_init(void)
561 set_supp_uv_cmds((unsigned long *)&uvdev_info
.supp_uv_cmds
);
562 return misc_register(&uvio_dev_miscdev
);
565 module_cpu_feature_match(S390_CPU_FEATURE_UV
, uvio_dev_init
);
566 module_exit(uvio_dev_exit
);
568 MODULE_AUTHOR("IBM Corporation");
569 MODULE_LICENSE("GPL");
570 MODULE_DESCRIPTION("Ultravisor UAPI driver");