xref: /linux/tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*cbac9242SSteffen Eiden // SPDX-License-Identifier: GPL-2.0
2*cbac9242SSteffen Eiden /*
3*cbac9242SSteffen Eiden  *  selftest for the Ultravisor UAPI device
4*cbac9242SSteffen Eiden  *
5*cbac9242SSteffen Eiden  *  Copyright IBM Corp. 2022
6*cbac9242SSteffen Eiden  *  Author(s): Steffen Eiden <seiden@linux.ibm.com>
7*cbac9242SSteffen Eiden  */
8*cbac9242SSteffen Eiden 
9*cbac9242SSteffen Eiden #include <stdint.h>
10*cbac9242SSteffen Eiden #include <fcntl.h>
11*cbac9242SSteffen Eiden #include <errno.h>
12*cbac9242SSteffen Eiden #include <sys/ioctl.h>
13*cbac9242SSteffen Eiden #include <sys/mman.h>
14*cbac9242SSteffen Eiden 
15*cbac9242SSteffen Eiden #include <asm/uvdevice.h>
16*cbac9242SSteffen Eiden 
17*cbac9242SSteffen Eiden #include "../../../kselftest_harness.h"
18*cbac9242SSteffen Eiden 
19*cbac9242SSteffen Eiden #define UV_PATH  "/dev/uv"
20*cbac9242SSteffen Eiden #define BUFFER_SIZE 0x200
FIXTURE(uvio_fixture)21*cbac9242SSteffen Eiden FIXTURE(uvio_fixture) {
22*cbac9242SSteffen Eiden 	int uv_fd;
23*cbac9242SSteffen Eiden 	struct uvio_ioctl_cb uvio_ioctl;
24*cbac9242SSteffen Eiden 	uint8_t buffer[BUFFER_SIZE];
25*cbac9242SSteffen Eiden 	__u64 fault_page;
26*cbac9242SSteffen Eiden };
27*cbac9242SSteffen Eiden 
FIXTURE_VARIANT(uvio_fixture)28*cbac9242SSteffen Eiden FIXTURE_VARIANT(uvio_fixture) {
29*cbac9242SSteffen Eiden 	unsigned long ioctl_cmd;
30*cbac9242SSteffen Eiden 	uint32_t arg_size;
31*cbac9242SSteffen Eiden };
32*cbac9242SSteffen Eiden 
FIXTURE_VARIANT_ADD(uvio_fixture,att)33*cbac9242SSteffen Eiden FIXTURE_VARIANT_ADD(uvio_fixture, att) {
34*cbac9242SSteffen Eiden 	.ioctl_cmd = UVIO_IOCTL_ATT,
35*cbac9242SSteffen Eiden 	.arg_size = sizeof(struct uvio_attest),
36*cbac9242SSteffen Eiden };
37*cbac9242SSteffen Eiden 
FIXTURE_SETUP(uvio_fixture)38*cbac9242SSteffen Eiden FIXTURE_SETUP(uvio_fixture)
39*cbac9242SSteffen Eiden {
40*cbac9242SSteffen Eiden 	self->uv_fd = open(UV_PATH, O_ACCMODE);
41*cbac9242SSteffen Eiden 
42*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_addr = (__u64)self->buffer;
43*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_len = variant->arg_size;
44*cbac9242SSteffen Eiden 	self->fault_page =
45*cbac9242SSteffen Eiden 		(__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, MAP_ANONYMOUS, -1, 0);
46*cbac9242SSteffen Eiden }
47*cbac9242SSteffen Eiden 
FIXTURE_TEARDOWN(uvio_fixture)48*cbac9242SSteffen Eiden FIXTURE_TEARDOWN(uvio_fixture)
49*cbac9242SSteffen Eiden {
50*cbac9242SSteffen Eiden 	if (self->uv_fd)
51*cbac9242SSteffen Eiden 		close(self->uv_fd);
52*cbac9242SSteffen Eiden 	munmap((void *)self->fault_page, (size_t)getpagesize());
53*cbac9242SSteffen Eiden }
54*cbac9242SSteffen Eiden 
TEST_F(uvio_fixture,fault_ioctl_arg)55*cbac9242SSteffen Eiden TEST_F(uvio_fixture, fault_ioctl_arg)
56*cbac9242SSteffen Eiden {
57*cbac9242SSteffen Eiden 	int rc, errno_cache;
58*cbac9242SSteffen Eiden 
59*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, NULL);
60*cbac9242SSteffen Eiden 	errno_cache = errno;
61*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
62*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
63*cbac9242SSteffen Eiden 
64*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, self->fault_page);
65*cbac9242SSteffen Eiden 	errno_cache = errno;
66*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
67*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
68*cbac9242SSteffen Eiden }
69*cbac9242SSteffen Eiden 
TEST_F(uvio_fixture,fault_uvio_arg)70*cbac9242SSteffen Eiden TEST_F(uvio_fixture, fault_uvio_arg)
71*cbac9242SSteffen Eiden {
72*cbac9242SSteffen Eiden 	int rc, errno_cache;
73*cbac9242SSteffen Eiden 
74*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_addr = 0;
75*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
76*cbac9242SSteffen Eiden 	errno_cache = errno;
77*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
78*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
79*cbac9242SSteffen Eiden 
80*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_addr = self->fault_page;
81*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
82*cbac9242SSteffen Eiden 	errno_cache = errno;
83*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
84*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
85*cbac9242SSteffen Eiden }
86*cbac9242SSteffen Eiden 
87*cbac9242SSteffen Eiden /*
88*cbac9242SSteffen Eiden  * Test to verify that IOCTLs with invalid values in the ioctl_control block
89*cbac9242SSteffen Eiden  * are rejected.
90*cbac9242SSteffen Eiden  */
TEST_F(uvio_fixture,inval_ioctl_cb)91*cbac9242SSteffen Eiden TEST_F(uvio_fixture, inval_ioctl_cb)
92*cbac9242SSteffen Eiden {
93*cbac9242SSteffen Eiden 	int rc, errno_cache;
94*cbac9242SSteffen Eiden 
95*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_len = 0;
96*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
97*cbac9242SSteffen Eiden 	errno_cache = errno;
98*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
99*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
100*cbac9242SSteffen Eiden 
101*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_len = (uint32_t)-1;
102*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
103*cbac9242SSteffen Eiden 	errno_cache = errno;
104*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
105*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
106*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_len = variant->arg_size;
107*cbac9242SSteffen Eiden 
108*cbac9242SSteffen Eiden 	self->uvio_ioctl.flags = (uint32_t)-1;
109*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
110*cbac9242SSteffen Eiden 	errno_cache = errno;
111*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
112*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
113*cbac9242SSteffen Eiden 	self->uvio_ioctl.flags = 0;
114*cbac9242SSteffen Eiden 
115*cbac9242SSteffen Eiden 	memset(self->uvio_ioctl.reserved14, 0xff, sizeof(self->uvio_ioctl.reserved14));
116*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
117*cbac9242SSteffen Eiden 	errno_cache = errno;
118*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
119*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
120*cbac9242SSteffen Eiden 
121*cbac9242SSteffen Eiden 	memset(&self->uvio_ioctl, 0x11, sizeof(self->uvio_ioctl));
122*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, variant->ioctl_cmd, &self->uvio_ioctl);
123*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
124*cbac9242SSteffen Eiden }
125*cbac9242SSteffen Eiden 
TEST_F(uvio_fixture,inval_ioctl_cmd)126*cbac9242SSteffen Eiden TEST_F(uvio_fixture, inval_ioctl_cmd)
127*cbac9242SSteffen Eiden {
128*cbac9242SSteffen Eiden 	int rc, errno_cache;
129*cbac9242SSteffen Eiden 	uint8_t nr = _IOC_NR(variant->ioctl_cmd);
130*cbac9242SSteffen Eiden 	unsigned long cmds[] = {
131*cbac9242SSteffen Eiden 		_IOWR('a', nr, struct uvio_ioctl_cb),
132*cbac9242SSteffen Eiden 		_IOWR(UVIO_TYPE_UVC, nr, int),
133*cbac9242SSteffen Eiden 		_IO(UVIO_TYPE_UVC, nr),
134*cbac9242SSteffen Eiden 		_IOR(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
135*cbac9242SSteffen Eiden 		_IOW(UVIO_TYPE_UVC, nr, struct uvio_ioctl_cb),
136*cbac9242SSteffen Eiden 	};
137*cbac9242SSteffen Eiden 
138*cbac9242SSteffen Eiden 	for (size_t i = 0; i < ARRAY_SIZE(cmds); i++) {
139*cbac9242SSteffen Eiden 		rc = ioctl(self->uv_fd, cmds[i], &self->uvio_ioctl);
140*cbac9242SSteffen Eiden 		errno_cache = errno;
141*cbac9242SSteffen Eiden 		ASSERT_EQ(rc, -1);
142*cbac9242SSteffen Eiden 		ASSERT_EQ(errno_cache, ENOTTY);
143*cbac9242SSteffen Eiden 	}
144*cbac9242SSteffen Eiden }
145*cbac9242SSteffen Eiden 
146*cbac9242SSteffen Eiden struct test_attest_buffer {
147*cbac9242SSteffen Eiden 	uint8_t arcb[0x180];
148*cbac9242SSteffen Eiden 	uint8_t meas[64];
149*cbac9242SSteffen Eiden 	uint8_t add[32];
150*cbac9242SSteffen Eiden };
151*cbac9242SSteffen Eiden 
FIXTURE(attest_fixture)152*cbac9242SSteffen Eiden FIXTURE(attest_fixture) {
153*cbac9242SSteffen Eiden 	int uv_fd;
154*cbac9242SSteffen Eiden 	struct uvio_ioctl_cb uvio_ioctl;
155*cbac9242SSteffen Eiden 	struct uvio_attest uvio_attest;
156*cbac9242SSteffen Eiden 	struct test_attest_buffer attest_buffer;
157*cbac9242SSteffen Eiden 	__u64 fault_page;
158*cbac9242SSteffen Eiden };
159*cbac9242SSteffen Eiden 
FIXTURE_SETUP(attest_fixture)160*cbac9242SSteffen Eiden FIXTURE_SETUP(attest_fixture)
161*cbac9242SSteffen Eiden {
162*cbac9242SSteffen Eiden 	self->uv_fd = open(UV_PATH, O_ACCMODE);
163*cbac9242SSteffen Eiden 
164*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_addr = (__u64)&self->uvio_attest;
165*cbac9242SSteffen Eiden 	self->uvio_ioctl.argument_len = sizeof(self->uvio_attest);
166*cbac9242SSteffen Eiden 
167*cbac9242SSteffen Eiden 	self->uvio_attest.arcb_addr = (__u64)&self->attest_buffer.arcb;
168*cbac9242SSteffen Eiden 	self->uvio_attest.arcb_len = sizeof(self->attest_buffer.arcb);
169*cbac9242SSteffen Eiden 
170*cbac9242SSteffen Eiden 	self->uvio_attest.meas_addr = (__u64)&self->attest_buffer.meas;
171*cbac9242SSteffen Eiden 	self->uvio_attest.meas_len = sizeof(self->attest_buffer.meas);
172*cbac9242SSteffen Eiden 
173*cbac9242SSteffen Eiden 	self->uvio_attest.add_data_addr = (__u64)&self->attest_buffer.add;
174*cbac9242SSteffen Eiden 	self->uvio_attest.add_data_len = sizeof(self->attest_buffer.add);
175*cbac9242SSteffen Eiden 	self->fault_page =
176*cbac9242SSteffen Eiden 		(__u64)mmap(NULL, (size_t)getpagesize(), PROT_NONE, MAP_ANONYMOUS, -1, 0);
177*cbac9242SSteffen Eiden }
178*cbac9242SSteffen Eiden 
FIXTURE_TEARDOWN(attest_fixture)179*cbac9242SSteffen Eiden FIXTURE_TEARDOWN(attest_fixture)
180*cbac9242SSteffen Eiden {
181*cbac9242SSteffen Eiden 	if (self->uv_fd)
182*cbac9242SSteffen Eiden 		close(self->uv_fd);
183*cbac9242SSteffen Eiden 	munmap((void *)self->fault_page, (size_t)getpagesize());
184*cbac9242SSteffen Eiden }
185*cbac9242SSteffen Eiden 
att_inval_sizes_test(uint32_t * size,uint32_t max_size,bool test_zero,struct __test_metadata * _metadata,FIXTURE_DATA (attest_fixture)* self)186*cbac9242SSteffen Eiden static void att_inval_sizes_test(uint32_t *size, uint32_t max_size, bool test_zero,
187*cbac9242SSteffen Eiden 				 struct __test_metadata *_metadata,
188*cbac9242SSteffen Eiden 				 FIXTURE_DATA(attest_fixture) *self)
189*cbac9242SSteffen Eiden {
190*cbac9242SSteffen Eiden 	int rc, errno_cache;
191*cbac9242SSteffen Eiden 	uint32_t tmp = *size;
192*cbac9242SSteffen Eiden 
193*cbac9242SSteffen Eiden 	if (test_zero) {
194*cbac9242SSteffen Eiden 		*size = 0;
195*cbac9242SSteffen Eiden 		rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
196*cbac9242SSteffen Eiden 		errno_cache = errno;
197*cbac9242SSteffen Eiden 		ASSERT_EQ(rc, -1);
198*cbac9242SSteffen Eiden 		ASSERT_EQ(errno_cache, EINVAL);
199*cbac9242SSteffen Eiden 	}
200*cbac9242SSteffen Eiden 	*size = max_size + 1;
201*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
202*cbac9242SSteffen Eiden 	errno_cache = errno;
203*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
204*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
205*cbac9242SSteffen Eiden 	*size = tmp;
206*cbac9242SSteffen Eiden }
207*cbac9242SSteffen Eiden 
208*cbac9242SSteffen Eiden /*
209*cbac9242SSteffen Eiden  * Test to verify that attestation IOCTLs with invalid values in the UVIO
210*cbac9242SSteffen Eiden  * attestation control block are rejected.
211*cbac9242SSteffen Eiden  */
TEST_F(attest_fixture,att_inval_request)212*cbac9242SSteffen Eiden TEST_F(attest_fixture, att_inval_request)
213*cbac9242SSteffen Eiden {
214*cbac9242SSteffen Eiden 	int rc, errno_cache;
215*cbac9242SSteffen Eiden 
216*cbac9242SSteffen Eiden 	att_inval_sizes_test(&self->uvio_attest.add_data_len, UVIO_ATT_ADDITIONAL_MAX_LEN,
217*cbac9242SSteffen Eiden 			     false, _metadata, self);
218*cbac9242SSteffen Eiden 	att_inval_sizes_test(&self->uvio_attest.meas_len, UVIO_ATT_MEASUREMENT_MAX_LEN,
219*cbac9242SSteffen Eiden 			     true, _metadata, self);
220*cbac9242SSteffen Eiden 	att_inval_sizes_test(&self->uvio_attest.arcb_len, UVIO_ATT_ARCB_MAX_LEN,
221*cbac9242SSteffen Eiden 			     true, _metadata, self);
222*cbac9242SSteffen Eiden 
223*cbac9242SSteffen Eiden 	self->uvio_attest.reserved136 = (uint16_t)-1;
224*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
225*cbac9242SSteffen Eiden 	errno_cache = errno;
226*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
227*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EINVAL);
228*cbac9242SSteffen Eiden 
229*cbac9242SSteffen Eiden 	memset(&self->uvio_attest, 0x11, sizeof(self->uvio_attest));
230*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
231*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
232*cbac9242SSteffen Eiden }
233*cbac9242SSteffen Eiden 
att_inval_addr_test(__u64 * addr,struct __test_metadata * _metadata,FIXTURE_DATA (attest_fixture)* self)234*cbac9242SSteffen Eiden static void att_inval_addr_test(__u64 *addr, struct __test_metadata *_metadata,
235*cbac9242SSteffen Eiden 				FIXTURE_DATA(attest_fixture) *self)
236*cbac9242SSteffen Eiden {
237*cbac9242SSteffen Eiden 	int rc, errno_cache;
238*cbac9242SSteffen Eiden 	__u64 tmp = *addr;
239*cbac9242SSteffen Eiden 
240*cbac9242SSteffen Eiden 	*addr = 0;
241*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
242*cbac9242SSteffen Eiden 	errno_cache = errno;
243*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
244*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
245*cbac9242SSteffen Eiden 	*addr = self->fault_page;
246*cbac9242SSteffen Eiden 	rc = ioctl(self->uv_fd, UVIO_IOCTL_ATT, &self->uvio_ioctl);
247*cbac9242SSteffen Eiden 	errno_cache = errno;
248*cbac9242SSteffen Eiden 	ASSERT_EQ(rc, -1);
249*cbac9242SSteffen Eiden 	ASSERT_EQ(errno_cache, EFAULT);
250*cbac9242SSteffen Eiden 	*addr = tmp;
251*cbac9242SSteffen Eiden }
252*cbac9242SSteffen Eiden 
TEST_F(attest_fixture,att_inval_addr)253*cbac9242SSteffen Eiden TEST_F(attest_fixture, att_inval_addr)
254*cbac9242SSteffen Eiden {
255*cbac9242SSteffen Eiden 	att_inval_addr_test(&self->uvio_attest.arcb_addr, _metadata, self);
256*cbac9242SSteffen Eiden 	att_inval_addr_test(&self->uvio_attest.add_data_addr, _metadata, self);
257*cbac9242SSteffen Eiden 	att_inval_addr_test(&self->uvio_attest.meas_addr, _metadata, self);
258*cbac9242SSteffen Eiden }
259*cbac9242SSteffen Eiden 
main(int argc,char ** argv)260*cbac9242SSteffen Eiden int main(int argc, char **argv)
261*cbac9242SSteffen Eiden {
262*cbac9242SSteffen Eiden 	int fd = open(UV_PATH, O_ACCMODE);
263*cbac9242SSteffen Eiden 
264*cbac9242SSteffen Eiden 	if (fd < 0)
265*cbac9242SSteffen Eiden 		ksft_exit_skip("No uv-device or cannot access " UV_PATH  "\n"
266*cbac9242SSteffen Eiden 			       "Enable CONFIG_S390_UV_UAPI and check the access rights on "
267*cbac9242SSteffen Eiden 			       UV_PATH ".\n");
268*cbac9242SSteffen Eiden 	close(fd);
269*cbac9242SSteffen Eiden 	return test_harness_run(argc, argv);
270*cbac9242SSteffen Eiden }
271