Skip to content

Commit bf5add3

Browse files
swarupkotikalapudiakpm00
authored andcommitted
proc: test ProtectionKey in proc-empty-vm test
Check ProtectionKey field in /proc/*/smaps output, if system supports protection keys feature. [adobriyan@gmail.com: test support in the beginning of the program, use syscall, not glibc pkey_alloc(3) which may not compile] Link: https://lkml.kernel.org/r/ac05efa7-d2a0-48ad-b704-ffdd5450582e@p183 Signed-off-by: Swarup Laxman Kotiaklapudi <swarupkotikalapudi@gmail.com> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Reviewed-by: Swarup Laxman Kotikalapudi<swarupkotikalapudi@gmail.com> Tested-by: Swarup Laxman Kotikalapudi<swarupkotikalapudi@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 20e34aa commit bf5add3

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

tools/testing/selftests/proc/proc-empty-vm.c

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
* /proc/${pid}/smaps
2424
* /proc/${pid}/smaps_rollup
2525
*/
26+
#undef _GNU_SOURCE
27+
#define _GNU_SOURCE
28+
2629
#undef NDEBUG
2730
#include <assert.h>
2831
#include <errno.h>
@@ -34,6 +37,7 @@
3437
#include <sys/mman.h>
3538
#include <sys/ptrace.h>
3639
#include <sys/resource.h>
40+
#include <sys/syscall.h>
3741
#include <sys/types.h>
3842
#include <sys/wait.h>
3943
#include <unistd.h>
@@ -42,6 +46,43 @@
4246
#define TEST_VSYSCALL
4347
#endif
4448

49+
#if defined __amd64__
50+
#ifndef SYS_pkey_alloc
51+
#define SYS_pkey_alloc 330
52+
#endif
53+
#ifndef SYS_pkey_free
54+
#define SYS_pkey_free 331
55+
#endif
56+
#elif defined __i386__
57+
#ifndef SYS_pkey_alloc
58+
#define SYS_pkey_alloc 381
59+
#endif
60+
#ifndef SYS_pkey_free
61+
#define SYS_pkey_free 382
62+
#endif
63+
#else
64+
#error "SYS_pkey_alloc"
65+
#endif
66+
67+
static int g_protection_key_support;
68+
69+
static int protection_key_support(void)
70+
{
71+
long rv = syscall(SYS_pkey_alloc, 0, 0);
72+
if (rv > 0) {
73+
syscall(SYS_pkey_free, (int)rv);
74+
return 1;
75+
} else if (rv == -1 && errno == ENOSYS) {
76+
return 0;
77+
} else if (rv == -1 && errno == EINVAL) {
78+
// ospke=n
79+
return 0;
80+
} else {
81+
fprintf(stderr, "%s: error: rv %ld, errno %d\n", __func__, rv, errno);
82+
exit(EXIT_FAILURE);
83+
}
84+
}
85+
4586
/*
4687
* 0: vsyscall VMA doesn't exist vsyscall=none
4788
* 1: vsyscall VMA is --xp vsyscall=xonly
@@ -84,10 +125,6 @@ static const char proc_pid_smaps_vsyscall_1[] =
84125
"SwapPss: 0 kB\n"
85126
"Locked: 0 kB\n"
86127
"THPeligible: 0\n"
87-
/*
88-
* "ProtectionKey:" field is conditional. It is possible to check it as well,
89-
* but I don't have such machine.
90-
*/
91128
;
92129

93130
static const char proc_pid_smaps_vsyscall_2[] =
@@ -115,10 +152,6 @@ static const char proc_pid_smaps_vsyscall_2[] =
115152
"SwapPss: 0 kB\n"
116153
"Locked: 0 kB\n"
117154
"THPeligible: 0\n"
118-
/*
119-
* "ProtectionKey:" field is conditional. It is possible to check it as well,
120-
* but I'm too tired.
121-
*/
122155
;
123156

124157
static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___)
@@ -240,19 +273,27 @@ static int test_proc_pid_smaps(pid_t pid)
240273
}
241274
perror("open /proc/${pid}/smaps");
242275
return EXIT_FAILURE;
276+
}
277+
ssize_t rv = read(fd, buf, sizeof(buf));
278+
close(fd);
279+
280+
assert(0 <= rv);
281+
assert(rv <= sizeof(buf));
282+
283+
if (g_vsyscall == 0) {
284+
assert(rv == 0);
243285
} else {
244-
ssize_t rv = read(fd, buf, sizeof(buf));
245-
close(fd);
246-
if (g_vsyscall == 0) {
247-
assert(rv == 0);
248-
} else {
249-
size_t len = strlen(g_proc_pid_smaps_vsyscall);
250-
/* TODO "ProtectionKey:" */
251-
assert(rv > len);
252-
assert(memcmp(buf, g_proc_pid_smaps_vsyscall, len) == 0);
286+
size_t len = strlen(g_proc_pid_smaps_vsyscall);
287+
assert(rv > len);
288+
assert(memcmp(buf, g_proc_pid_smaps_vsyscall, len) == 0);
289+
290+
if (g_protection_key_support) {
291+
#define PROTECTION_KEY "ProtectionKey: 0\n"
292+
assert(memmem(buf, rv, PROTECTION_KEY, strlen(PROTECTION_KEY)));
253293
}
254-
return EXIT_SUCCESS;
255294
}
295+
296+
return EXIT_SUCCESS;
256297
}
257298

258299
static const char g_smaps_rollup[] =
@@ -419,6 +460,8 @@ int main(void)
419460
abort();
420461
}
421462

463+
g_protection_key_support = protection_key_support();
464+
422465
pid_t pid = fork();
423466
if (pid == -1) {
424467
perror("fork");

0 commit comments

Comments
 (0)