14dafc08dSAmit Daniel Kachhap // SPDX-License-Identifier: GPL-2.0
24dafc08dSAmit Daniel Kachhap // Copyright (C) 2020 ARM Limited
34dafc08dSAmit Daniel Kachhap
44dafc08dSAmit Daniel Kachhap #define _GNU_SOURCE
54dafc08dSAmit Daniel Kachhap
6*0a775ccbSJoey Gouly #include <assert.h>
74dafc08dSAmit Daniel Kachhap #include <errno.h>
84dafc08dSAmit Daniel Kachhap #include <fcntl.h>
94dafc08dSAmit Daniel Kachhap #include <signal.h>
104dafc08dSAmit Daniel Kachhap #include <stdlib.h>
114dafc08dSAmit Daniel Kachhap #include <stdio.h>
124dafc08dSAmit Daniel Kachhap #include <string.h>
134dafc08dSAmit Daniel Kachhap #include <ucontext.h>
144dafc08dSAmit Daniel Kachhap #include <unistd.h>
15e8d3974fSJoey Gouly #include <sys/uio.h>
164dafc08dSAmit Daniel Kachhap #include <sys/mman.h>
174dafc08dSAmit Daniel Kachhap
184dafc08dSAmit Daniel Kachhap #include "kselftest.h"
194dafc08dSAmit Daniel Kachhap #include "mte_common_util.h"
204dafc08dSAmit Daniel Kachhap #include "mte_def.h"
214dafc08dSAmit Daniel Kachhap
224dafc08dSAmit Daniel Kachhap static size_t page_sz;
234dafc08dSAmit Daniel Kachhap
24*0a775ccbSJoey Gouly #define TEST_NAME_MAX 100
25*0a775ccbSJoey Gouly
26b9fc7001SJoey Gouly enum test_type {
27b9fc7001SJoey Gouly READ_TEST,
28e8d3974fSJoey Gouly WRITE_TEST,
29e8d3974fSJoey Gouly READV_TEST,
30e8d3974fSJoey Gouly WRITEV_TEST,
31b9fc7001SJoey Gouly LAST_TEST,
32b9fc7001SJoey Gouly };
33b9fc7001SJoey Gouly
check_usermem_access_fault(int mem_type,int mode,int mapping,int tag_offset,int tag_len,enum test_type test_type)34d53f8f8dSJoey Gouly static int check_usermem_access_fault(int mem_type, int mode, int mapping,
35b9fc7001SJoey Gouly int tag_offset, int tag_len,
36b9fc7001SJoey Gouly enum test_type test_type)
374dafc08dSAmit Daniel Kachhap {
384dafc08dSAmit Daniel Kachhap int fd, i, err;
394dafc08dSAmit Daniel Kachhap char val = 'A';
40b9fc7001SJoey Gouly ssize_t len, syscall_len;
414dafc08dSAmit Daniel Kachhap void *ptr, *ptr_next;
42682b064bSJoey Gouly int fileoff, ptroff, size;
43682b064bSJoey Gouly int sizes[] = {1, 2, 3, 8, 16, 32, 4096, page_sz};
444dafc08dSAmit Daniel Kachhap
45ff0b9abaSJoey Gouly err = KSFT_PASS;
464dafc08dSAmit Daniel Kachhap len = 2 * page_sz;
474dafc08dSAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
484dafc08dSAmit Daniel Kachhap fd = create_temp_file();
494dafc08dSAmit Daniel Kachhap if (fd == -1)
504dafc08dSAmit Daniel Kachhap return KSFT_FAIL;
514dafc08dSAmit Daniel Kachhap for (i = 0; i < len; i++)
5246cb11b1SAndre Przywara if (write(fd, &val, sizeof(val)) != sizeof(val))
5346cb11b1SAndre Przywara return KSFT_FAIL;
544dafc08dSAmit Daniel Kachhap lseek(fd, 0, 0);
554dafc08dSAmit Daniel Kachhap ptr = mte_allocate_memory(len, mem_type, mapping, true);
564dafc08dSAmit Daniel Kachhap if (check_allocated_memory(ptr, len, mem_type, true) != KSFT_PASS) {
574dafc08dSAmit Daniel Kachhap close(fd);
584dafc08dSAmit Daniel Kachhap return KSFT_FAIL;
594dafc08dSAmit Daniel Kachhap }
604dafc08dSAmit Daniel Kachhap mte_initialize_current_context(mode, (uintptr_t)ptr, len);
614dafc08dSAmit Daniel Kachhap /* Copy from file into buffer with valid tag */
62b9fc7001SJoey Gouly syscall_len = read(fd, ptr, len);
634dafc08dSAmit Daniel Kachhap mte_wait_after_trig();
64b9fc7001SJoey Gouly if (cur_mte_cxt.fault_valid || syscall_len < len)
654dafc08dSAmit Daniel Kachhap goto usermem_acc_err;
664dafc08dSAmit Daniel Kachhap /* Verify same pattern is read */
674dafc08dSAmit Daniel Kachhap for (i = 0; i < len; i++)
684dafc08dSAmit Daniel Kachhap if (*(char *)(ptr + i) != val)
694dafc08dSAmit Daniel Kachhap break;
704dafc08dSAmit Daniel Kachhap if (i < len)
714dafc08dSAmit Daniel Kachhap goto usermem_acc_err;
724dafc08dSAmit Daniel Kachhap
73d53f8f8dSJoey Gouly if (!tag_len)
74d53f8f8dSJoey Gouly tag_len = len - tag_offset;
75d53f8f8dSJoey Gouly /* Tag a part of memory with different value */
76d53f8f8dSJoey Gouly ptr_next = (void *)((unsigned long)ptr + tag_offset);
774dafc08dSAmit Daniel Kachhap ptr_next = mte_insert_new_tag(ptr_next);
78d53f8f8dSJoey Gouly mte_set_tag_address_range(ptr_next, tag_len);
794dafc08dSAmit Daniel Kachhap
80682b064bSJoey Gouly for (fileoff = 0; fileoff < 16; fileoff++) {
81682b064bSJoey Gouly for (ptroff = 0; ptroff < 16; ptroff++) {
82682b064bSJoey Gouly for (i = 0; i < ARRAY_SIZE(sizes); i++) {
83682b064bSJoey Gouly size = sizes[i];
844dafc08dSAmit Daniel Kachhap lseek(fd, 0, 0);
85b9fc7001SJoey Gouly
86b9fc7001SJoey Gouly /* perform file operation on buffer with invalid tag */
87b9fc7001SJoey Gouly switch (test_type) {
88b9fc7001SJoey Gouly case READ_TEST:
89b9fc7001SJoey Gouly syscall_len = read(fd, ptr + ptroff, size);
90b9fc7001SJoey Gouly break;
91e8d3974fSJoey Gouly case WRITE_TEST:
92e8d3974fSJoey Gouly syscall_len = write(fd, ptr + ptroff, size);
93e8d3974fSJoey Gouly break;
94e8d3974fSJoey Gouly case READV_TEST: {
95e8d3974fSJoey Gouly struct iovec iov[1];
96e8d3974fSJoey Gouly iov[0].iov_base = ptr + ptroff;
97e8d3974fSJoey Gouly iov[0].iov_len = size;
98e8d3974fSJoey Gouly syscall_len = readv(fd, iov, 1);
99e8d3974fSJoey Gouly break;
100e8d3974fSJoey Gouly }
101e8d3974fSJoey Gouly case WRITEV_TEST: {
102e8d3974fSJoey Gouly struct iovec iov[1];
103e8d3974fSJoey Gouly iov[0].iov_base = ptr + ptroff;
104e8d3974fSJoey Gouly iov[0].iov_len = size;
105e8d3974fSJoey Gouly syscall_len = writev(fd, iov, 1);
106e8d3974fSJoey Gouly break;
107e8d3974fSJoey Gouly }
108b9fc7001SJoey Gouly case LAST_TEST:
109b9fc7001SJoey Gouly goto usermem_acc_err;
110b9fc7001SJoey Gouly }
111b9fc7001SJoey Gouly
1124dafc08dSAmit Daniel Kachhap mte_wait_after_trig();
1134dafc08dSAmit Daniel Kachhap /*
1144dafc08dSAmit Daniel Kachhap * Accessing user memory in kernel with invalid tag should fail in sync
1154dafc08dSAmit Daniel Kachhap * mode without fault but may not fail in async mode as per the
1164dafc08dSAmit Daniel Kachhap * implemented MTE userspace support in Arm64 kernel.
1174dafc08dSAmit Daniel Kachhap */
118682b064bSJoey Gouly if (cur_mte_cxt.fault_valid) {
119ff0b9abaSJoey Gouly goto usermem_acc_err;
120682b064bSJoey Gouly }
121b9fc7001SJoey Gouly if (mode == MTE_SYNC_ERR && syscall_len < len) {
122ff0b9abaSJoey Gouly /* test passed */
123b9fc7001SJoey Gouly } else if (mode == MTE_ASYNC_ERR && syscall_len == size) {
124ff0b9abaSJoey Gouly /* test passed */
125ff0b9abaSJoey Gouly } else {
126ff0b9abaSJoey Gouly goto usermem_acc_err;
1274dafc08dSAmit Daniel Kachhap }
128682b064bSJoey Gouly }
129682b064bSJoey Gouly }
130682b064bSJoey Gouly }
131ff0b9abaSJoey Gouly
132ff0b9abaSJoey Gouly goto exit;
133ff0b9abaSJoey Gouly
1344dafc08dSAmit Daniel Kachhap usermem_acc_err:
135ff0b9abaSJoey Gouly err = KSFT_FAIL;
136ff0b9abaSJoey Gouly exit:
1374dafc08dSAmit Daniel Kachhap mte_free_memory((void *)ptr, len, mem_type, true);
1384dafc08dSAmit Daniel Kachhap close(fd);
1394dafc08dSAmit Daniel Kachhap return err;
1404dafc08dSAmit Daniel Kachhap }
1414dafc08dSAmit Daniel Kachhap
format_test_name(char * name,int name_len,int type,int sync,int map,int len,int offset)142*0a775ccbSJoey Gouly void format_test_name(char* name, int name_len, int type, int sync, int map, int len, int offset) {
143*0a775ccbSJoey Gouly const char* test_type;
144*0a775ccbSJoey Gouly const char* mte_type;
145*0a775ccbSJoey Gouly const char* map_type;
146*0a775ccbSJoey Gouly
147*0a775ccbSJoey Gouly switch (type) {
148*0a775ccbSJoey Gouly case READ_TEST:
149*0a775ccbSJoey Gouly test_type = "read";
150*0a775ccbSJoey Gouly break;
151*0a775ccbSJoey Gouly case WRITE_TEST:
152*0a775ccbSJoey Gouly test_type = "write";
153*0a775ccbSJoey Gouly break;
154*0a775ccbSJoey Gouly case READV_TEST:
155*0a775ccbSJoey Gouly test_type = "readv";
156*0a775ccbSJoey Gouly break;
157*0a775ccbSJoey Gouly case WRITEV_TEST:
158*0a775ccbSJoey Gouly test_type = "writev";
159*0a775ccbSJoey Gouly break;
160*0a775ccbSJoey Gouly default:
161*0a775ccbSJoey Gouly assert(0);
162*0a775ccbSJoey Gouly break;
163*0a775ccbSJoey Gouly }
164*0a775ccbSJoey Gouly
165*0a775ccbSJoey Gouly switch (sync) {
166*0a775ccbSJoey Gouly case MTE_SYNC_ERR:
167*0a775ccbSJoey Gouly mte_type = "MTE_SYNC_ERR";
168*0a775ccbSJoey Gouly break;
169*0a775ccbSJoey Gouly case MTE_ASYNC_ERR:
170*0a775ccbSJoey Gouly mte_type = "MTE_ASYNC_ERR";
171*0a775ccbSJoey Gouly break;
172*0a775ccbSJoey Gouly default:
173*0a775ccbSJoey Gouly assert(0);
174*0a775ccbSJoey Gouly break;
175*0a775ccbSJoey Gouly }
176*0a775ccbSJoey Gouly
177*0a775ccbSJoey Gouly switch (map) {
178*0a775ccbSJoey Gouly case MAP_SHARED:
179*0a775ccbSJoey Gouly map_type = "MAP_SHARED";
180*0a775ccbSJoey Gouly break;
181*0a775ccbSJoey Gouly case MAP_PRIVATE:
182*0a775ccbSJoey Gouly map_type = "MAP_PRIVATE";
183*0a775ccbSJoey Gouly break;
184*0a775ccbSJoey Gouly default:
185*0a775ccbSJoey Gouly assert(0);
186*0a775ccbSJoey Gouly break;
187*0a775ccbSJoey Gouly }
188*0a775ccbSJoey Gouly
189*0a775ccbSJoey Gouly snprintf(name, name_len,
190*0a775ccbSJoey Gouly "test type: %s, %s, %s, tag len: %d, tag offset: %d\n",
191*0a775ccbSJoey Gouly test_type, mte_type, map_type, len, offset);
192*0a775ccbSJoey Gouly }
193*0a775ccbSJoey Gouly
main(int argc,char * argv[])1944dafc08dSAmit Daniel Kachhap int main(int argc, char *argv[])
1954dafc08dSAmit Daniel Kachhap {
1964dafc08dSAmit Daniel Kachhap int err;
197*0a775ccbSJoey Gouly int t, s, m, l, o;
198*0a775ccbSJoey Gouly int mte_sync[] = {MTE_SYNC_ERR, MTE_ASYNC_ERR};
199*0a775ccbSJoey Gouly int maps[] = {MAP_SHARED, MAP_PRIVATE};
200*0a775ccbSJoey Gouly int tag_lens[] = {0, MT_GRANULE_SIZE};
201*0a775ccbSJoey Gouly int tag_offsets[] = {page_sz, MT_GRANULE_SIZE};
202*0a775ccbSJoey Gouly char test_name[TEST_NAME_MAX];
2034dafc08dSAmit Daniel Kachhap
2044dafc08dSAmit Daniel Kachhap page_sz = getpagesize();
2054dafc08dSAmit Daniel Kachhap if (!page_sz) {
2064dafc08dSAmit Daniel Kachhap ksft_print_msg("ERR: Unable to get page size\n");
2074dafc08dSAmit Daniel Kachhap return KSFT_FAIL;
2084dafc08dSAmit Daniel Kachhap }
2094dafc08dSAmit Daniel Kachhap err = mte_default_setup();
2104dafc08dSAmit Daniel Kachhap if (err)
2114dafc08dSAmit Daniel Kachhap return err;
212493b35dbSVincenzo Frascino
2134dafc08dSAmit Daniel Kachhap /* Register signal handlers */
2144dafc08dSAmit Daniel Kachhap mte_register_signal(SIGSEGV, mte_default_handler);
2154dafc08dSAmit Daniel Kachhap
216493b35dbSVincenzo Frascino /* Set test plan */
217*0a775ccbSJoey Gouly ksft_set_plan(64);
218493b35dbSVincenzo Frascino
219*0a775ccbSJoey Gouly for (t = 0; t < LAST_TEST; t++) {
220*0a775ccbSJoey Gouly for (s = 0; s < ARRAY_SIZE(mte_sync); s++) {
221*0a775ccbSJoey Gouly for (m = 0; m < ARRAY_SIZE(maps); m++) {
222*0a775ccbSJoey Gouly for (l = 0; l < ARRAY_SIZE(tag_lens); l++) {
223*0a775ccbSJoey Gouly for (o = 0; o < ARRAY_SIZE(tag_offsets); o++) {
224*0a775ccbSJoey Gouly int sync = mte_sync[s];
225*0a775ccbSJoey Gouly int map = maps[m];
226*0a775ccbSJoey Gouly int offset = tag_offsets[o];
227*0a775ccbSJoey Gouly int tag_len = tag_lens[l];
228*0a775ccbSJoey Gouly int res = check_usermem_access_fault(USE_MMAP, sync,
229*0a775ccbSJoey Gouly map, offset,
230*0a775ccbSJoey Gouly tag_len, t);
231*0a775ccbSJoey Gouly format_test_name(test_name, TEST_NAME_MAX,
232*0a775ccbSJoey Gouly t, sync, map, tag_len, offset);
233*0a775ccbSJoey Gouly evaluate_test(res, test_name);
234*0a775ccbSJoey Gouly }
235*0a775ccbSJoey Gouly }
236*0a775ccbSJoey Gouly }
237*0a775ccbSJoey Gouly }
238*0a775ccbSJoey Gouly }
2394dafc08dSAmit Daniel Kachhap
2404dafc08dSAmit Daniel Kachhap mte_restore_setup();
2414dafc08dSAmit Daniel Kachhap ksft_print_cnts();
2424dafc08dSAmit Daniel Kachhap return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL;
2434dafc08dSAmit Daniel Kachhap }
244