Skip to content

Commit 3de9745

Browse files
weihongzhansendc
authored andcommitted
selftests/x86/lam: Add malloc and tag-bits test cases for linear-address masking
LAM is supported only in 64-bit mode and applies only addresses used for data accesses. In 64-bit mode, linear address have 64 bits. LAM is applied to 64-bit linear address and allow software to use high bits for metadata. LAM supports configurations that differ regarding which pointer bits are masked and can be used for metadata. LAM includes following mode: - LAM_U57, pointer bits in positions 62:57 are masked (LAM width 6), allows bits 62:57 of a user pointer to be used as metadata. There are some arch_prctls: ARCH_ENABLE_TAGGED_ADDR: enable LAM mode, mask high bits of a user pointer. ARCH_GET_UNTAG_MASK: get current untagged mask. ARCH_GET_MAX_TAG_BITS: the maximum tag bits user can request. zero if LAM is not supported. The LAM mode is for pre-process, a process has only one chance to set LAM mode. But there is no API to disable LAM mode. So all of test cases are run under child process. Functions of this test: MALLOC - LAM_U57 masks bits 57:62 of a user pointer. Process on user space can dereference such pointers. - Disable LAM, dereference a pointer with metadata above 48 bit or 57 bit lead to trigger SIGSEGV. TAG_BITS - Max tag bits of LAM_U57 is 6. Signed-off-by: Weihong Zhang <weihong.zhang@intel.com> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/all/20230312112612.31869-13-kirill.shutemov%40linux.intel.com
1 parent 23e5d9e commit 3de9745

2 files changed

Lines changed: 327 additions & 1 deletion

File tree

tools/testing/selftests/x86/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
1818
test_FCMOV test_FCOMI test_FISTTP \
1919
vdso_restorer
2020
TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
21-
corrupt_xstate_header amx
21+
corrupt_xstate_header amx lam
2222
# Some selftests require 32bit support enabled also on 64bit systems
2323
TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
2424

tools/testing/selftests/x86/lam.c

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <sys/syscall.h>
6+
#include <time.h>
7+
#include <signal.h>
8+
#include <setjmp.h>
9+
#include <sys/mman.h>
10+
#include <sys/wait.h>
11+
#include <inttypes.h>
12+
13+
#include "../kselftest.h"
14+
15+
#ifndef __x86_64__
16+
# error This test is 64-bit only
17+
#endif
18+
19+
/* LAM modes, these definitions were copied from kernel code */
20+
#define LAM_NONE 0
21+
#define LAM_U57_BITS 6
22+
23+
#define LAM_U57_MASK (0x3fULL << 57)
24+
/* arch prctl for LAM */
25+
#define ARCH_GET_UNTAG_MASK 0x4001
26+
#define ARCH_ENABLE_TAGGED_ADDR 0x4002
27+
#define ARCH_GET_MAX_TAG_BITS 0x4003
28+
29+
/* Specified test function bits */
30+
#define FUNC_MALLOC 0x1
31+
#define FUNC_BITS 0x2
32+
33+
#define TEST_MASK 0x3
34+
35+
#define MALLOC_LEN 32
36+
37+
struct testcases {
38+
unsigned int later;
39+
int expected; /* 2: SIGSEGV Error; 1: other errors */
40+
unsigned long lam;
41+
uint64_t addr;
42+
int (*test_func)(struct testcases *test);
43+
const char *msg;
44+
};
45+
46+
int tests_cnt;
47+
jmp_buf segv_env;
48+
49+
static void segv_handler(int sig)
50+
{
51+
ksft_print_msg("Get segmentation fault(%d).", sig);
52+
siglongjmp(segv_env, 1);
53+
}
54+
55+
static inline int cpu_has_lam(void)
56+
{
57+
unsigned int cpuinfo[4];
58+
59+
__cpuid_count(0x7, 1, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
60+
61+
return (cpuinfo[0] & (1 << 26));
62+
}
63+
64+
/*
65+
* Set tagged address and read back untag mask.
66+
* check if the untagged mask is expected.
67+
*
68+
* @return:
69+
* 0: Set LAM mode successfully
70+
* others: failed to set LAM
71+
*/
72+
static int set_lam(unsigned long lam)
73+
{
74+
int ret = 0;
75+
uint64_t ptr = 0;
76+
77+
if (lam != LAM_U57_BITS && lam != LAM_NONE)
78+
return -1;
79+
80+
/* Skip check return */
81+
syscall(SYS_arch_prctl, ARCH_ENABLE_TAGGED_ADDR, lam);
82+
83+
/* Get untagged mask */
84+
syscall(SYS_arch_prctl, ARCH_GET_UNTAG_MASK, &ptr);
85+
86+
/* Check mask returned is expected */
87+
if (lam == LAM_U57_BITS)
88+
ret = (ptr != ~(LAM_U57_MASK));
89+
else if (lam == LAM_NONE)
90+
ret = (ptr != -1ULL);
91+
92+
return ret;
93+
}
94+
95+
static unsigned long get_default_tag_bits(void)
96+
{
97+
pid_t pid;
98+
int lam = LAM_NONE;
99+
int ret = 0;
100+
101+
pid = fork();
102+
if (pid < 0) {
103+
perror("Fork failed.");
104+
} else if (pid == 0) {
105+
/* Set LAM mode in child process */
106+
if (set_lam(LAM_U57_BITS) == 0)
107+
lam = LAM_U57_BITS;
108+
else
109+
lam = LAM_NONE;
110+
exit(lam);
111+
} else {
112+
wait(&ret);
113+
lam = WEXITSTATUS(ret);
114+
}
115+
116+
return lam;
117+
}
118+
119+
/* According to LAM mode, set metadata in high bits */
120+
static uint64_t set_metadata(uint64_t src, unsigned long lam)
121+
{
122+
uint64_t metadata;
123+
124+
srand(time(NULL));
125+
126+
switch (lam) {
127+
case LAM_U57_BITS: /* Set metadata in bits 62:57 */
128+
/* Get a random non-zero value as metadata */
129+
metadata = (rand() % ((1UL << LAM_U57_BITS) - 1) + 1) << 57;
130+
metadata |= (src & ~(LAM_U57_MASK));
131+
break;
132+
default:
133+
metadata = src;
134+
break;
135+
}
136+
137+
return metadata;
138+
}
139+
140+
/*
141+
* Set metadata in user pointer, compare new pointer with original pointer.
142+
* both pointers should point to the same address.
143+
*
144+
* @return:
145+
* 0: value on the pointer with metadate and value on original are same
146+
* 1: not same.
147+
*/
148+
static int handle_lam_test(void *src, unsigned int lam)
149+
{
150+
char *ptr;
151+
152+
strcpy((char *)src, "USER POINTER");
153+
154+
ptr = (char *)set_metadata((uint64_t)src, lam);
155+
if (src == ptr)
156+
return 0;
157+
158+
/* Copy a string into the pointer with metadata */
159+
strcpy((char *)ptr, "METADATA POINTER");
160+
161+
return (!!strcmp((char *)src, (char *)ptr));
162+
}
163+
164+
165+
int handle_max_bits(struct testcases *test)
166+
{
167+
unsigned long exp_bits = get_default_tag_bits();
168+
unsigned long bits = 0;
169+
170+
if (exp_bits != LAM_NONE)
171+
exp_bits = LAM_U57_BITS;
172+
173+
/* Get LAM max tag bits */
174+
if (syscall(SYS_arch_prctl, ARCH_GET_MAX_TAG_BITS, &bits) == -1)
175+
return 1;
176+
177+
return (exp_bits != bits);
178+
}
179+
180+
/*
181+
* Test lam feature through dereference pointer get from malloc.
182+
* @return 0: Pass test. 1: Get failure during test 2: Get SIGSEGV
183+
*/
184+
static int handle_malloc(struct testcases *test)
185+
{
186+
char *ptr = NULL;
187+
int ret = 0;
188+
189+
if (test->later == 0 && test->lam != 0)
190+
if (set_lam(test->lam) == -1)
191+
return 1;
192+
193+
ptr = (char *)malloc(MALLOC_LEN);
194+
if (ptr == NULL) {
195+
perror("malloc() failure\n");
196+
return 1;
197+
}
198+
199+
/* Set signal handler */
200+
if (sigsetjmp(segv_env, 1) == 0) {
201+
signal(SIGSEGV, segv_handler);
202+
ret = handle_lam_test(ptr, test->lam);
203+
} else {
204+
ret = 2;
205+
}
206+
207+
if (test->later != 0 && test->lam != 0)
208+
if (set_lam(test->lam) == -1 && ret == 0)
209+
ret = 1;
210+
211+
free(ptr);
212+
213+
return ret;
214+
}
215+
216+
static int fork_test(struct testcases *test)
217+
{
218+
int ret, child_ret;
219+
pid_t pid;
220+
221+
pid = fork();
222+
if (pid < 0) {
223+
perror("Fork failed.");
224+
ret = 1;
225+
} else if (pid == 0) {
226+
ret = test->test_func(test);
227+
exit(ret);
228+
} else {
229+
wait(&child_ret);
230+
ret = WEXITSTATUS(child_ret);
231+
}
232+
233+
return ret;
234+
}
235+
236+
static void run_test(struct testcases *test, int count)
237+
{
238+
int i, ret = 0;
239+
240+
for (i = 0; i < count; i++) {
241+
struct testcases *t = test + i;
242+
243+
/* fork a process to run test case */
244+
ret = fork_test(t);
245+
if (ret != 0)
246+
ret = (t->expected == ret);
247+
else
248+
ret = !(t->expected);
249+
250+
tests_cnt++;
251+
ksft_test_result(ret, t->msg);
252+
}
253+
}
254+
255+
static struct testcases malloc_cases[] = {
256+
{
257+
.later = 0,
258+
.lam = LAM_U57_BITS,
259+
.test_func = handle_malloc,
260+
.msg = "MALLOC: LAM_U57. Dereferencing pointer with metadata\n",
261+
},
262+
{
263+
.later = 1,
264+
.expected = 2,
265+
.lam = LAM_U57_BITS,
266+
.test_func = handle_malloc,
267+
.msg = "MALLOC:[Negative] Disable LAM. Dereferencing pointer with metadata.\n",
268+
},
269+
};
270+
271+
272+
static struct testcases bits_cases[] = {
273+
{
274+
.test_func = handle_max_bits,
275+
.msg = "BITS: Check default tag bits\n",
276+
},
277+
};
278+
279+
static void cmd_help(void)
280+
{
281+
printf("usage: lam [-h] [-t test list]\n");
282+
printf("\t-t test list: run tests specified in the test list, default:0x%x\n", TEST_MASK);
283+
printf("\t\t0x1:malloc; 0x2:max_bits;\n");
284+
printf("\t-h: help\n");
285+
}
286+
287+
int main(int argc, char **argv)
288+
{
289+
int c = 0;
290+
unsigned int tests = TEST_MASK;
291+
292+
tests_cnt = 0;
293+
294+
if (!cpu_has_lam()) {
295+
ksft_print_msg("Unsupported LAM feature!\n");
296+
return -1;
297+
}
298+
299+
while ((c = getopt(argc, argv, "ht:")) != -1) {
300+
switch (c) {
301+
case 't':
302+
tests = strtoul(optarg, NULL, 16);
303+
if (!(tests & TEST_MASK)) {
304+
ksft_print_msg("Invalid argument!\n");
305+
return -1;
306+
}
307+
break;
308+
case 'h':
309+
cmd_help();
310+
return 0;
311+
default:
312+
ksft_print_msg("Invalid argument\n");
313+
return -1;
314+
}
315+
}
316+
317+
if (tests & FUNC_MALLOC)
318+
run_test(malloc_cases, ARRAY_SIZE(malloc_cases));
319+
320+
if (tests & FUNC_BITS)
321+
run_test(bits_cases, ARRAY_SIZE(bits_cases));
322+
323+
ksft_set_plan(tests_cnt);
324+
325+
return ksft_exit_pass();
326+
}

0 commit comments

Comments
 (0)