1 /*
2 * Check decoding of pkey_mprotect syscall.
3 *
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <sys/mman.h>
17
18 static const char *
19 sprintptr(kernel_ulong_t ptr)
20 {
21 static char buf[sizeof(ptr) * 2 + sizeof("0x")];
22
23 if (ptr)
24 snprintf(buf, sizeof(buf), "%#llx", (unsigned long long) ptr);
25 else
26 return "NULL";
27
28 return buf;
29 }
30
31 int
32 main(void)
33 {
34 static const kernel_ulong_t ptrs[] = {
35 0,
36 (kernel_ulong_t) 0xfacebeef00000000ULL,
37 (kernel_ulong_t) 0xbadc0dedda7a1057ULL,
38 };
39 static const kernel_ulong_t sizes[] = {
40 0,
41 (kernel_ulong_t) 0xfacebeef00000000ULL,
42 (kernel_ulong_t) 0xfedcba9876543210ULL,
43 (kernel_ulong_t) 0x123456789abcdef0ULL,
44 (kernel_ulong_t) 0xbadc0dedda7a1057ULL,
45 };
46 static const struct {
47 kernel_ulong_t val;
48 const char *str;
49 } prots[] = {
50 { ARG_STR(PROT_READ) },
51 /* For now, only 0x0300001f are used */
52 { (kernel_ulong_t) 0xdeadfeed00ca7500ULL,
53 sizeof(kernel_ulong_t) > sizeof(int) ?
54 "0xdeadfeed00ca7500 /* PROT_??? */" :
55 "0xca7500 /* PROT_??? */" },
56 { ARG_STR(PROT_READ|PROT_WRITE|0xface00) },
57 };
58 static const kernel_ulong_t pkeys[] = {
59 0,
60 -1LL,
61 (kernel_ulong_t) 0xface1e55,
62 (kernel_ulong_t) 0xbadc0ded00000001,
63 };
64
65 for (unsigned int i = 0;
66 i < ARRAY_SIZE(ptrs); ++i) {
67 for (unsigned int j = 0;
68 j < ARRAY_SIZE(sizes); ++j) {
69 for (unsigned int k = 0;
70 k < ARRAY_SIZE(prots); ++k) {
71 for (unsigned int l = 0;
72 l < ARRAY_SIZE(pkeys); ++l) {
73 long rc = syscall(__NR_pkey_mprotect,
74 ptrs[i], sizes[j],
75 prots[k].val, pkeys[l]);
76 printf("pkey_mprotect(%s, %llu, %s, %d)"
77 " = %s\n",
78 sprintptr(ptrs[i]),
79 (unsigned long long) sizes[j],
80 prots[k].str, (int) pkeys[l],
81 sprintrc(rc));
82 }
83 }
84 }
85 }
86
87 puts("+++ exited with 0 +++");
88
89 return 0;
90 }